Example #1
0
 protected function afterSave()
 {
     if (Yii::app()->user->hasState('files')) {
         $userImages = Yii::app()->user->getState('files');
         $path = Yii::app()->getBasePath() . "/../images/catalog/{$this->id}/";
         //Create the folder and give permissions if it doesnt exists
         if (!is_dir($path)) {
             mkdir($path);
             chmod($path, 0777);
         }
         if (!is_dir($path . "thumb/")) {
             mkdir($path . "thumb/");
             chmod($path . "thumb/", 0777);
         }
         //Now lets create the corresponding models and move the files
         foreach ($userImages as $image) {
             if (is_file($image["path"])) {
                 if (rename($image["path"], $path . $image["filename"]) && rename($image["thumb"], $path . "thumb/" . $image["filename"])) {
                     chmod($path . $image["filename"], 0777);
                     chmod($path . 'thumb/' . $image["filename"], 0777);
                     $img = new ArticlesPictures();
                     $img->size = $image["size"];
                     $img->mime = $image["mime"];
                     $img->name = $image["name"];
                     $img->thumb = "/images/catalog/{$this->id}/thumb/" . $image["filename"];
                     $img->picture = $image["filename"];
                     $img->source = "/images/catalog/{$this->id}/" . $image["filename"];
                     $img->article_id = $this->id;
                     if (!$img->save()) {
                         //Its always good to log something
                         Yii::log("Could not save Image:\n" . CVarDumper::dumpAsString($img->getErrors()), CLogger::LEVEL_ERROR);
                         //this exception will rollback the transaction
                         throw new Exception('Could not save Image');
                     }
                 }
             } else {
                 //You can also throw an execption here to rollback the transaction
                 Yii::log($image["path"] . " is not a file", CLogger::LEVEL_WARNING);
             }
         }
         //Clear the user's session
         Yii::app()->user->setState('files', null);
     }
     parent::afterSave();
 }
Example #2
0
 /**
  * Returns the static model of the specified AR class.
  * Please note that you should have this exact method in all your CActiveRecord descendants!
  * @param string $className active record class name.
  * @return Blog the static model class
  */
 public static function model($className = __CLASS__)
 {
     return parent::model($className);
 }