/** * Execute the controller. * * @throws \Exception * @return mixed Return executed result. */ public function execute() { $id = $this->input->get('id'); $user = User::get(); $blog = Blog::get(); $type = $this->input->get('type'); $route = $type == 'static' ? 'statics' : 'posts'; try { if (!$id) { throw new ValidFailException('Where is your post ID?'); } if (!Author::isAdmin()) { throw new ValidFailException('Access deny'); } $postMapper = new DataMapper('posts'); $post = $postMapper->findOne($id); if ($post->blog != $blog->id) { throw new ValidFailException('You cannot change post of other blog.'); } $post['state'] = $this->input->get('state', 1); $postMapper->updateOne($post); } catch (ValidFailException $e) { $this->setRedirect(Router::buildHttp('admin:' . $route), $e->getMessage(), 'danger'); return false; } catch (\Exception $e) { if (WINDWALKER_DEBUG) { throw $e; } $this->setRedirect(Router::buildHttp('admin:' . $route), 'Fail', 'danger'); return false; } $this->setRedirect(Router::buildHttp('admin:' . $route), 'Success', 'success'); return true; }
/** * Method to test updateOne(). * * @return void * * @covers Windwalker\DataMapper\AbstractDataMapper::updateOne */ public function testUpdateOne() { // Update from array $data = array('id' => 10, 'params' => '{}'); $updateData = $this->instance->updateOne($data); $this->assertEquals('{}', $this->loadToData('SELECT * FROM ww_flower WHERE id = 10 LIMIT 1')->params); $this->assertInstanceOf('Windwalker\\Data\\Data', $updateData, 'Return not Data object.'); // Update from Data $data = new Data(array('id' => 11, 'params' => '{}')); $updateData = $this->instance->updateOne($data); $this->assertEquals('{}', $this->loadToData('SELECT * FROM ww_flower WHERE id = 11 LIMIT 1')->params); $this->assertInstanceOf('Windwalker\\Data\\Data', $updateData, 'Return not Data object.'); // TODO: Test Update Nulls }
/** * permission * * @param string $permission * * @throws ValidFailException * @return boolean */ protected function permission($permission) { $authorMapper = new DataMapper('authors'); $id = $this->input->get('id'); $author = $authorMapper->findOne($id); if ($author->blog != Blog::get()->id) { throw new ValidFailException('You cannot change permission of author which in other blog.'); } if ($author->owner) { throw new ValidFailException('You cannot change permission of blog owner'); } $author['admin'] = $permission == Author::ADMIN ? 1 : 0; $authorMapper->updateOne($author, 'id'); $this->setRedirect(Router::buildHttp('admin:authors'), 'Save success', 'success'); return true; }
/** * Same as update(), just update one row. * * @param mixed $data The data we want to update. * @param array $condFields The where condition tell us record exists or not, if not set, * will use primary key instead. * @param bool $updateNulls Update empty fields or not. * * @return mixed|Data */ public function updateOne($data, $condFields = null, $updateNulls = false) { $this->observers->update('onBeforeUpdateOne', array(&$data, &$condFields, &$updateNulls)); $data = parent::updateOne($data, $condFields, $updateNulls); $this->observers->update('onAfterUpdateOne', array(&$data)); return $data; }