Example #1
0
 /**
  * This is invoked after the record is deleted.
  */
 protected function afterDelete()
 {
     PhotoRelation::model()->deleteAll('photo_id=:photo_id', array(':photo_id' => $this->id));
     // Delete original file
     if ($this->sourceSaved) {
         $path = $this->path();
         if (strpos($path, '/') === 0) {
             $path = substr($path, 1);
         }
         unlink($path);
     }
     // Delete all sizes files
     foreach ($this->sizes as $prefix => $size) {
         $path = $this->path($prefix);
         if (strpos($path, '/') === 0) {
             $path = substr($path, 1);
         }
         if (file_exists($path)) {
             unlink($path);
         }
     }
 }
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $model = $this->loadModel($id);
     $newRelation = new ProductRelation();
     $ProductValue = $model->value;
     $newPhoto = new Photo();
     // ajax validator
     $this->performAjaxValidation($model, $ProductValue, $newPhoto);
     $catalogs = Catalog::getTree();
     $cat_list = array();
     foreach ($model->catalogs as $item) {
         $cat_list[$item->id] = $item;
     }
     if (isset($_POST['Product'])) {
         $model->attributes = $_POST['Product'];
         $ProductValue->attributes = $_POST['ProductValue'];
         $newPhoto->attributes = $_POST['Photo'];
         $newRelation->attributes = $_POST['ProductRelation'];
         if ($model->validate() && $ProductValue->validate() && $newPhoto->validate()) {
             // Update photo title (if set)
             /*
             				if ($model->photo) {
             					if ($model->photo->title!=$model->name) {
             						$model->photo->title = $model->name;
             						$model->photo->save();
             					}
             				}//*/
             // Upload new photo
             $newPhoto->filename = CUploadedFile::getInstance($newPhoto, 'filename');
             if ($newPhoto->filename) {
                 $newPhoto->setDefault();
                 $newPhoto->title = $model->name;
                 if ($newPhoto->save()) {
                     $newPhoto->fullSave();
                     if (!$model->photo_id) {
                         $model->photo_id = $newPhoto->id;
                     }
                 }
             }
             // Save product model
             if ($model->save()) {
                 $new_attr = $ProductValue->attributes;
                 foreach ($new_attr as $name => $fieldarr) {
                     if (is_array($fieldarr)) {
                         $new_attr[$name] = implode(', ', $fieldarr);
                     }
                 }
                 $ProductValue->attributes = $new_attr;
                 // Save product value
                 $ProductValue->save();
                 // Add relation to new photo
                 if ($newPhoto->id) {
                     $photoRel = new PhotoRelation();
                     $photoRel->item_id = $model->id;
                     $photoRel->photo_id = $newPhoto->id;
                     $photoRel->save();
                 }
             }
             // Catalog relation
             foreach ($_POST['Product']['catalogs'] as $i => $item) {
                 if ($item) {
                     if (!isset($cat_list[$i])) {
                         $catRel = new CatalogRelation();
                         $catRel->cid = $i;
                         $catRel->pr_id = $model->id;
                         $catRel->save();
                     }
                 } else {
                     if (isset($cat_list[$i])) {
                         CatalogRelation::model()->find(array('condition' => 'cid=:cid AND pr_id=:pr_id', 'params' => array(':cid' => $i, ':pr_id' => $model->id)))->delete();
                     }
                 }
             }
             if ($newRelation->pr_id) {
                 $relations = explode(',', $newRelation->pr_id);
                 foreach ($relations as $rel) {
                     $pr_id = (int) trim($rel);
                     if ($pr_id) {
                         $newRelation = new ProductRelation();
                         $newRelation->pr_id = $pr_id;
                         $newRelation->pid = $model->id;
                         $newRelation->save();
                     }
                 }
             }
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('update', array('model' => $model, 'ProductValue' => $ProductValue, 'newPhoto' => $newPhoto, 'catalogs' => $catalogs, 'cat_list' => $cat_list, 'newRelation' => $newRelation));
 }