示例#1
0
 public function update()
 {
     if (!$this->has_post()) {
         return redirect_message(array('admin', $this->get_class(), $this->user->id, 'edit'), array('_flash_message' => '非 POST 方法,錯誤的頁面請求。'));
     }
     $posts = OAInput::post();
     if ($msg = $this->_validation_posts($posts)) {
         return redirect_message(array('admin', $this->get_class(), $this->user->id, 'edit'), array('_flash_message' => $msg, 'posts' => $posts));
     }
     if ($columns = array_intersect_key($posts, $this->user->table()->columns)) {
         foreach ($columns as $column => $value) {
             $this->user->{$column} = $value;
         }
     }
     $user = $this->user;
     $update = user::transaction(function () use($user) {
         return $user->save();
     });
     if (!$update) {
         return redirect_message(array('admin', $this->get_class(), $this->user->id, 'edit'), array('_flash_message' => '更新失敗!', 'posts' => $posts));
     }
     $ori_keys = column_array($user->roles, 'name');
     if (($del_keys = array_diff($ori_keys, $posts['roles'])) && ($roles = UserRole::find('all', array('select' => 'id', 'conditions' => array('user_id = ? AND name IN (?)', $user->id, $del_keys))))) {
         foreach ($roles as $role) {
             UserRole::transaction(function () use($role) {
                 return $role->destroy();
             });
         }
     }
     if ($add_keys = array_diff($posts['roles'], $ori_keys)) {
         foreach ($add_keys as $add_key) {
             UserRole::transaction(function () use($add_key, $user) {
                 return verifyCreateOrm(UserRole::create(Array_intersect_key(array('name' => $add_key, 'user_id' => $user->id), UserRole::table()->columns)));
             });
         }
     }
     return redirect_message(array('admin', $this->get_class()), array('_flash_message' => '更新成功!'));
 }
示例#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' => '更新成功!'));
 }