Exemplo n.º 1
0
 public function actionUpdate($id)
 {
     $this->layout = '@app/views/layouts/admin';
     $modelSlider = $this->findSlider($id);
     $post = Yii::$app->request->post();
     if ($modelSlider->load($post) && $modelSlider->save()) {
         $transaction = Yii::$app->db->beginTransaction();
         try {
             $i = 0;
             foreach ($post['SliderItem'] as $item) {
                 $err = $_FILES['slide-' . $i]['error'];
                 if ($item['id'] > 0 || $err == 0 || $item['title'] !== '' || $item['body'] !== '') {
                     if ($item['id'] > 0) {
                         $sliderItem = SliderItem::findOne($item['id']);
                     } else {
                         $sliderItem = new SliderItem();
                     }
                     $sliderItem->title = $item['title'];
                     $sliderItem->body = $item['body'];
                     $sliderItem->slider_id = $modelSlider->id;
                     $sliderItem->sort_order = $i;
                     if ($_FILES['slide-' . $i]['error'] == 0) {
                         if ($sliderItem->fid > 0) {
                             File::fileUnlink(['fid' => $sliderItem->fid]);
                         }
                         $file = UploadedFile::getInstanceByName('slide-' . $i);
                         $saveFile = new File();
                         $sliderItem->fid = $saveFile->uploadFile($file, 'SliderItem', $sliderItem->id, 'slider');
                     }
                     $sliderItem->save();
                 }
                 $i++;
             }
             $transaction->commit();
         } catch (\Exception $e) {
             $transaction->rollBack();
         }
         return $this->redirect(['/slider/node/view', 'slug' => $modelSlider->name]);
     } else {
         $items = $modelSlider->items;
         if (!count($items)) {
             $items[0] = new SliderItem();
         }
         return $this->render('update', ['modelSlider' => $modelSlider, 'modelSliderItem' => $items]);
     }
 }
Exemplo n.º 2
0
 public function afterDelete()
 {
     File::fileUnlink(['fid' => $this->fid]);
     parent::afterDelete();
     // TODO: Change the autogenerated stub
 }