/**
  * Initialize admin page theme
  */
 public function init()
 {
     if (ArticleSetting::getInfo('permission') == 1) {
         $arrThemes = Utility::getCurrentTemplate('public');
         Yii::app()->theme = $arrThemes['folder'];
         $this->layout = $arrThemes['layout'];
     } else {
         $this->redirect(Yii::app()->createUrl('site/login'));
     }
 }
 /**
  * After save attributes
  */
 protected function afterSave()
 {
     parent::afterSave();
     $article_path = "public/article/" . $this->article_id;
     if ($this->isNewRecord && in_array($this->article_type, array(1, 3))) {
         // Add article directory
         if (!file_exists($article_path)) {
             @mkdir($article_path, 0777, true);
             // Add file in article directory (index.php)
             $newFile = $article_path . '/index.php';
             $FileHandle = fopen($newFile, 'w');
         }
     }
     if ($this->article_type == 1) {
         if ($this->isNewRecord || !$this->isNewRecord && ArticleSetting::getInfo('media_limit') == 1) {
             $this->media = CUploadedFile::getInstance($this, 'media');
             if ($this->media instanceof CUploadedFile) {
                 $fileName = time() . '_' . $this->article_id . '_' . Utility::getUrlTitle($this->title) . '.' . strtolower($this->media->extensionName);
                 if ($this->media->saveAs($article_path . '/' . $fileName)) {
                     if ($this->isNewRecord || !$this->isNewRecord && $this->media_id == 0) {
                         $images = new ArticleMedia();
                         $images->article_id = $this->article_id;
                         $images->cover = 1;
                         $images->media = $fileName;
                         $images->save();
                     } else {
                         if ($this->old_media != '' && file_exists($article_path . '/' . $this->old_media)) {
                             rename($article_path . '/' . $this->old_media, 'public/article/verwijderen/' . $this->article_id . '_' . $this->old_media);
                         }
                         $images = ArticleMedia::model()->findByPk($this->media_id);
                         $images->media = $fileName;
                         $images->update();
                     }
                 }
             }
         }
     } else {
         if ($this->article_type == 2) {
             if ($this->isNewRecord) {
                 $video = new ArticleMedia();
                 $video->article_id = $this->article_id;
                 $video->cover = 1;
                 $video->media = $this->video;
                 $video->save();
             } else {
                 if ($this->media_id == 0) {
                     $video = new ArticleMedia();
                     $video->article_id = $this->article_id;
                     $video->cover = 1;
                     $video->media = $this->video;
                     if ($video->save()) {
                         $data = Articles::model()->findByPk($this->article_id);
                         $data->media_id = $video->media_id;
                         $data->update();
                     }
                 } else {
                     $video = ArticleMedia::model()->findByPk($this->media_id);
                     $video->media = $this->video;
                     $video->update();
                 }
             }
         }
     }
     $this->file = CUploadedFile::getInstance($this, 'file');
     if ($this->file instanceof CUploadedFile) {
         $fileName = time() . '_' . $this->article_id . '_' . Utility::getUrlTitle($this->title) . '.' . strtolower($this->file->extensionName);
         if ($this->file->saveAs($article_path . '/' . $fileName)) {
             if (!$this->isNewRecord && $this->media_file != '' && file_exists($article_path . '/' . $this->old_file)) {
                 rename($article_path . '/' . $this->old_file, 'public/article/verwijderen/' . $this->article_id . '_' . $this->old_file);
             }
             $article = Articles::model()->findByPk($this->article_id);
             $article->media_file = $fileName;
             $article->update();
         }
     }
     // Add Keyword
     if (!$this->isNewRecord) {
         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 ArticleTag();
             $tag->article_id = $this->article_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();
         }
     }
     // Reset headline
     if (ArticleSetting::getInfo('headline') == 1) {
         if ($this->headline == 1) {
             self::model()->updateAll(array('headline' => 0), array('condition' => 'article_id != :id AND cat_id = :cat', 'params' => array(':id' => $this->article_id, ':cat' => $this->cat_id)));
         }
     } else {
     }
 }
 /**
  * After save attributes
  */
 protected function afterSave()
 {
     parent::afterSave();
     //set flex cover in article
     //if($this->cover == 1 || count(self::getPhoto($this->article_id)) == 1) {
     if ($this->cover == 1) {
         $cover = Articles::model()->findByPk($this->article_id);
         $cover->media_id = $this->media_id;
         $cover->update();
     }
     if ($this->article->article_type == 1) {
         $setting = ArticleSetting::getInfo('media_limit, media_resize, media_resize_size', 'many');
         //create thumb image
         if ($setting->media_resize == 1) {
             Yii::import('ext.phpthumb.PhpThumbFactory');
             $article_path = "public/article/" . $this->article_id;
             $articleImg = PhpThumbFactory::create($article_path . '/' . $this->media, array('jpegQuality' => 90, 'correctPermissions' => true));
             $resizeSize = explode(',', $setting->media_resize_size);
             if ($resizeSize[1] == 0) {
                 $articleImg->resize($resizeSize[0]);
             } else {
                 $articleImg->adaptiveResize($resizeSize[0], $resizeSize[1]);
             }
             $articleImg->save($article_path . '/' . $this->media);
         }
         //delete other media (if media_limit = 1)
         if ($setting->media_limit == 1) {
             self::model()->deleteAll(array('condition' => 'article_id = :id AND cover = :cover', 'params' => array(':id' => $this->article_id, ':cover' => 0)));
         }
     }
 }