/**
  * After save attributes
  */
 protected function afterSave()
 {
     parent::afterSave();
     if ($this->isNewRecord) {
         // Add album directory
         $album_path = "public/album/" . $this->album_id;
         if (!file_exists($album_path)) {
             @mkdir($album_path, 0777, true);
             // Add file in album directory (index.php)
             $newFile = $album_path . '/index.php';
             $FileHandle = fopen($newFile, 'w');
         }
         $this->media = CUploadedFile::getInstance($this, 'media');
         if ($this->media instanceof CUploadedFile) {
             $fileName = time() . '_' . $this->album_id . '_' . Utility::getUrlTitle($this->title) . '.' . strtolower($this->media->extensionName);
             if ($this->media->saveAs($album_path . '/' . $fileName)) {
                 $images = new AlbumPhoto();
                 $images->album_id = $this->album_id;
                 $images->cover = '1';
                 $images->media = $fileName;
                 $images->save();
             }
         }
     } else {
         // Add Tags
         if ($this->keyword != '') {
             $model = OmmuTags::model()->find(array('select' => 'tag_id, body', 'condition' => 'publish = 1 AND body = :body', 'params' => array(':body' => $this->keyword)));
             $tag = new AlbumTag();
             $tag->album_id = $this->album_id;
             if ($model != null) {
                 $tag->tag_id = $model->tag_id;
             } else {
                 $data = new OmmuTags();
                 $data->body = $this->keyword;
                 if ($data->save()) {
                     $tag->tag_id = $data->tag_id;
                 }
             }
             $tag->save();
         }
     }
     if (AlbumSetting::getInfo('headline') == 1) {
         if ($this->headline == 1) {
             self::model()->updateAll(array('headline' => 0), array('condition' => 'album_id != :id AND cat_id = :cat', 'params' => array(':id' => $this->album_id, ':cat' => $this->cat_id)));
         }
     }
 }
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer the ID of the model to be loaded
  */
 public function loadModel($id)
 {
     $model = AlbumTag::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, Yii::t('phrase', 'The requested page does not exist.'));
     }
     return $model;
 }
 /**
  * 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 actionEdit($id)
 {
     $model = $this->loadModel($id);
     $setting = AlbumSetting::model()->findByPk(1, array('select' => 'photo_limit, meta_keyword'));
     $tag = AlbumTag::model()->findAll(array('condition' => 'album_id = :id', 'params' => array(':id' => $model->album_id)));
     // Uncomment the following line if AJAX validation is needed
     $this->performAjaxValidation($model);
     if (isset($_POST['Albums'])) {
         $model->attributes = $_POST['Albums'];
         $jsonError = CActiveForm::validate($model);
         if (strlen($jsonError) > 2) {
             $errors = $model->getErrors();
             $summary['msg'] = "<div class='errorSummary'><strong>" . Yii::t('phrase', 'Please fix the following input errors:') . "</strong>";
             $summary['msg'] .= "<ul>";
             foreach ($errors as $key => $value) {
                 $summary['msg'] .= "<li>{$value[0]}</li>";
             }
             $summary['msg'] .= "</ul></div>";
             $message = json_decode($jsonError, true);
             $merge = array_merge_recursive($summary, $message);
             $encode = json_encode($merge);
             echo $encode;
         } else {
             if (isset($_GET['enablesave']) && $_GET['enablesave'] == 1) {
                 if ($model->save()) {
                     echo CJSON::encode(array('type' => 0, 'msg' => '<div class="errorSummary success"><strong>' . Yii::t('phrase', 'Album success updated.') . '</strong></div>'));
                 } else {
                     print_r($model->getErrors());
                 }
             }
         }
         Yii::app()->end();
     } else {
         $this->pageTitle = Yii::t('phrase', 'Update Album') . ': ' . $model->title;
         $this->pageDescription = '';
         $this->pageMeta = '';
         $this->render('admin_edit', array('model' => $model, 'setting' => $setting, 'tag' => $tag));
     }
 }