Example #1
0
 public function destroy()
 {
     $tag = $this->tag;
     $delete = ArticleTag::transaction(function () use($tag) {
         return $tag->destroy();
     });
     if (!$delete) {
         return redirect_message(array('admin', $this->get_class()), array('_flash_message' => '刪除失敗!'));
     }
     $this->_clean_cell();
     return redirect_message(array('admin', $this->get_class()), array('_flash_message' => '刪除成功!'));
 }
Example #2
0
 public function update()
 {
     if (!$this->has_post()) {
         return redirect_message(array('admin', $this->get_class(), $this->article->id, 'edit'), array('_flash_message' => '非 POST 方法,錯誤的頁面請求。'));
     }
     $posts = OAInput::post();
     $posts['content'] = OAInput::post('content', false);
     $cover = OAInput::file('cover');
     if (!((string) $this->article->cover || $cover)) {
         return redirect_message(array('admin', $this->get_class(), $this->article->id, 'edit'), array('_flash_message' => '請選擇圖片(gif、jpg、png)檔案!', 'posts' => $posts));
     }
     if ($msg = $this->_validation_posts($posts)) {
         return redirect_message(array('admin', $this->get_class(), $this->article->id, 'edit'), array('_flash_message' => $msg, 'posts' => $posts));
     }
     if ($columns = array_intersect_key($posts, $this->article->table()->columns)) {
         foreach ($columns as $column => $value) {
             $this->article->{$column} = $value;
         }
     }
     $article = $this->article;
     $update = ArticleTag::transaction(function () use($article, $cover) {
         if (!$article->save()) {
             return false;
         }
         if ($cover && !$article->cover->put($cover)) {
             return false;
         }
         return true;
     });
     if (!$update) {
         return redirect_message(array('admin', $this->get_class(), $this->article->id, 'edit'), array('_flash_message' => '更新失敗!', 'posts' => $posts));
     }
     $ori_ids = column_array($article->mappings, 'article_tag_id');
     if (($del_ids = array_diff($ori_ids, $posts['tag_ids'])) && ($mappings = ArticleTagMapping::find('all', array('select' => 'id, article_tag_id', 'conditions' => array('article_id = ? AND article_tag_id IN (?)', $article->id, $del_ids))))) {
         foreach ($mappings as $mapping) {
             ArticleTagMapping::transaction(function () use($mapping) {
                 return $mapping->destroy();
             });
         }
     }
     if (($add_ids = array_diff($posts['tag_ids'], $ori_ids)) && ($tags = ArticleTag::find('all', array('select' => 'id', 'conditions' => array('id IN (?)', $add_ids))))) {
         foreach ($tags as $tag) {
             ArticleTagMapping::transaction(function () use($tag, $article) {
                 return verifyCreateOrm(ArticleTagMapping::create(Array_intersect_key(array('article_tag_id' => $tag->id, 'article_id' => $article->id), ArticleTagMapping::table()->columns)));
             });
         }
     }
     if ($article->sources) {
         foreach ($article->sources as $source) {
             ArticleSource::transaction(function () use($source) {
                 return $source->destroy();
             });
         }
     }
     if ($posts['sources']) {
         foreach ($posts['sources'] as $i => $source) {
             ArticleSource::transaction(function () use($i, $source, $article) {
                 return verifyCreateOrm(ArticleSource::create(array_intersect_key(array_merge($source, array('article_id' => $article->id, 'sort' => $i)), ArticleSource::table()->columns)));
             });
         }
     }
     $this->_clean_cell($article);
     return redirect_message(($url = Session::getData('admin_articles_index_url')) ? $url : array('admin', $this->get_class()), array('_flash_message' => '更新成功!'));
 }