/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionAjaxAdd($id) { $articlePhoto = CUploadedFile::getInstanceByName('namaFile'); $article_path = "public/article/" . $id; $fileName = time() . '_' . $id . '_' . Utility::getUrlTitle(Articles::getInfo($id, 'title')) . '.' . strtolower($articlePhoto->extensionName); if ($articlePhoto->saveAs($article_path . '/' . $fileName)) { $model = new ArticleMedia(); $model->article_id = $id; $model->media = $fileName; if ($model->save()) { $url = Yii::app()->controller->createUrl('ajaxmanage', array('id' => $model->article_id, 'replace' => 'true')); echo CJSON::encode(array('id' => 'media-render', 'get' => $url)); } } }
/** * 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 { } }