/** * Execute the controller. * * @throws \Exception * @return mixed Return executed result. */ public function execute() { $id = $this->input->get('id'); $authorMapper = new DataMapper('authors'); $return = $this->input->getBase64('return'); $return = $return ? base64_decode($return) : Router::buildHttp('admin:authors'); try { if (!$id) { throw new \Exception('Delete fail'); } $author = $authorMapper->findOne($id); $blog = Blog::get(); $user = User::get(); if ($author->owner) { throw new ValidFailException('You cannot delete owner.'); } if ($user->id != $author->user && $blog->id != $author->blog) { throw new ValidFailException('You cannot delete authors of other blog.'); } $authorMapper->delete(['id' => $id]); } catch (ValidFailException $e) { $this->setRedirect($return, $e->getMessage(), 'danger'); return false; } catch (\Exception $e) { if (WINDWALKER_DEBUG) { throw $e; } $this->setRedirect($return, 'Delete fail', 'danger'); return false; } $this->setRedirect($return, 'Remove Author success', 'success'); return true; }
/** * @covers Windwalker\DataMapper\AbstractDataMapper::findOne * @todo Implement testFindOne(). */ public function testFindOne() { // Test find by primary key $data = $this->object->findOne(3); $this->assertInstanceOf('Windwalker\\Data\\Data', $data, 'Return not Data object.'); $this->assertEquals($data->title, 'Article Categories Module', 'Title not matched'); // Test find by other key $data = $this->object->findOne(array('catid' => 64, 'created_by' => 256)); $this->assertEquals($data->title, 'Latest Articles Module', 'Title not matched'); }
/** * 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; }
/** * save * * @param Data $data * * @return mixed|Data */ public function save(Data $data) { $data->title = trim($data->title); $data->alias = $data->alias ?: OutputFilter::stringURLUnicodeSlug($data->title); if (!$data->alias) { $data->alias = OutputFilter::stringURLSafe((new Date())->toISO8601()); } $mapper = new DataMapper('posts'); // Increment alias $conditions = ['alias' => $data->alias, 'blog' => $data->blog]; $conditions[] = 'id != ' . ($data->id ?: -1); while ($mapper->findOne($conditions)->notNull()) { $conditions['alias'] = $data->alias = String::increment($data->alias, String::INCREMENT_STYLE_DASH); } $data = $mapper->saveOne($data); // Save Categories $categories = $data->category ?: []; $dataSet = []; foreach ($categories as $k => $cat) { $dataSet[$k]['category'] = $cat; $dataSet[$k]['post'] = $data->id; } (new DataMapper('category_post_maps'))->flush($dataSet, ['post' => $data->id]); return $data; }
/** * onBeforeRouting * * @param Event $event * * @return void * * @throws \Exception */ public function onBeforeRouting(Event $event) { $app = Ioc::getApplication(); $uri = $app->initUri(); $uri = new Uri(strtolower($uri->full)); $host = $uri->getHost(); $host = explode('.', $host); array_pop($host); array_pop($host); $alias = implode('.', $host); $alias = trim(str_replace('.', '', $alias)); // If is main domain but logged in, go to admin if (!$alias && UserHelper::isLogin()) { $app->set('client', 'admin'); return; } // Has subdomain, means it is users' blog if ($alias) { $blogMapper = new DataMapper('blogs'); $blog = $blogMapper->findOne(['alias' => $alias]); if ($blog->isNull()) { throw new \Exception('Blog not found', 404); } Ioc::getContainer('front')->set('current.blog', $blog); $app->set('client', 'front'); return; } // Main domain, got to site $app->set('client', 'site'); }
/** * Execute the controller. * * @throws \Exception * @return mixed Return executed result. */ public function execute() { $id = $this->input->get('id'); $user = User::get(); $blog = Blog::get(); try { if (!$id) { throw new ValidFailException('No 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 delete post of other blog.'); } $postMapper->delete(['id' => $id]); } catch (ValidFailException $e) { $this->setRedirect(Router::buildHttp('admin:posts'), $e->getMessage(), 'danger'); return false; } catch (\Exception $e) { if (WINDWALKER_DEBUG) { throw $e; } $this->setRedirect(Router::buildHttp('admin:posts'), 'Delete fail', 'danger'); return false; } $this->setRedirect(Router::buildHttp('admin:posts'), 'Delete success', 'success'); return true; }
/** * Execute the controller. * * @throws \Exception * @return mixed Return executed result. */ public function execute() { $id = $this->input->get('id'); $blogMapper = new DataMapper('blogs'); $authorMapper = new DataMapper('authors'); $catMapper = new DataMapper('categories'); $postMapper = new DataMapper('posts'); try { if (!$id) { throw new \Exception('Delete fail'); } $author = $authorMapper->findOne(['blog' => $id, 'user' => User::get()->id]); if (!$author->owner) { throw new ValidFailException('Only owner can remove blog.'); } $blogMapper->delete(['id' => $id]); $authorMapper->delete(['blog' => $id]); $catMapper->delete(['blog' => $id]); $postMapper->delete(['blog' => $id]); } catch (ValidFailException $e) { $this->setRedirect(Router::buildHttp('admin:blogs'), $e->getMessage(), 'danger'); return false; } catch (\Exception $e) { if (WINDWALKER_DEBUG) { throw $e; } $this->setRedirect(Router::buildHttp('admin:blogs'), 'Delete fail', 'danger'); return false; } $this->setRedirect(Router::buildHttp('admin:blogs'), 'Delete Blog success', 'success'); return true; }
/** * Method to test getDataClass(). * * @return void * * @covers Windwalker\DataMapper\AbstractDataMapper::getDataClass */ public function testGetAndSetDataClass() { $this->instance->setDataClass('stdClass'); $this->assertEquals('stdClass', $this->instance->getDataClass()); // If we use DataSet, all stdClass will be auto convert to Data $this->assertInstanceOf('Windwalker\\Data\\Data', $this->instance->findOne()); $this->instance->setDatasetClass('ArrayObject'); $this->assertInstanceOf('stdClass', $this->instance->findOne()); }
/** * Get category object. * * @param integer $pk Category id. * * @return \Windwalker\Data\Data */ public function getCategory($pk = null) { if (!empty($this->category)) { return $this->category; } $input = $this->getContainer()->get('input'); $pk = $pk ?: $this->state->get('category.id', $input->get('id')); $mapper = new DataMapper('#__categories'); $data = $mapper->findOne($pk); $data->params = new Registry($data->params); return $data; }
/** * @covers Windwalker\DataMapper\AbstractDataMapper::findOne * @todo Implement testFindOne(). */ public function testFindOne() { $data = $this->object->findOne(array('id' => array(5, 6))); $compareContent = $this->loadToData(<<<SQL SELECT * FROM ww_content WHERE id IN(5, 6) LIMIT 1 SQL ); $cMapper = new DataMapper('ww_content2'); $compareContent->b = $cMapper->findOne(array('content_id' => $compareContent->id)); $this->assertEquals($compareContent, $data, 'Record not matches.'); }
/** * Execute the controller. * * @throws \Exception * @return mixed Return executed result. */ public function execute() { $id = $this->input->get('id'); $blog = Blog::get(); try { $catMapper = new DataMapper('categories'); $category = $catMapper->findOne($id); if ($category->blog != $blog->id) { throw new ValidFailException('You cannot delete category of other blog.'); } $catMapper->delete(['id' => $id]); } catch (ValidFailException $e) { $this->setRedirect(Router::buildHttp('admin:categories'), $e->getMessage(), 'error'); return false; } catch (\Exception $e) { if (WINDWALKER_DEBUG) { throw $e; } $this->setRedirect(Router::buildHttp('admin:categories'), 'Delete fail', 'error'); return false; } $this->setRedirect(Router::buildHttp('admin:categories'), 'Delete success', 'success'); return true; }
/** * 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; }
/** * getTitle * * @return Data */ protected function getTitle() { $table = $this->table ?: $this->get('table', $this->view); $value = $this->getValue(); $keyField = $this->get('key_field', $this->keyField); $titleField = $this->get('title_field', $this->titleField); $dataMapper = new DataMapper($table); $data = $dataMapper->findOne(array($keyField => $value)); return $data->{$titleField}; }
/** * initUser * * @return void */ protected function initUser() { $mapper = new DataMapper('#__users'); $users = $mapper->findOne(); if ($users->notNull()) { return; } $helper = new TableHelper('#__users'); $helper->initRow(mt_rand(50, 150)); }
/** * createBlogAlias * * @param string $username * * @return string */ protected function createBlogAlias($username) { $username = strtolower($username); $alias = OutputFilter::stringURLSafe($username); $blogMapper = new DataMapper('blogs'); if ($blogMapper->findOne(['alias' => $alias])->notNull()) { $alias = $alias . '-blog'; } while ($blogMapper->findOne(['alias' => $alias])->notNull()) { $alias = 'u' . rand(100000, 9999999999.0); } return $alias; }
/** * Find one record and return a data. * * Same as `$mapper->find($conditions, 'id', 0, 1);` * * @param mixed $conditions Where conditions, you can use array or Compare object. * Example: * - `array('id' => 5)` => id = 5 * - `new GteCompare('id', 20)` => 'id >= 20' * - `new Compare('id', '%Flower%', 'LIKE')` => 'id LIKE "%Flower%"' * @param mixed $order Order sort, can ba string, array or object. * Example: * - `id ASC` => ORDER BY id ASC * - `array('catid DESC', 'id')` => ORDER BY catid DESC, id * * @return mixed|Data Found row data. */ public function findOne($conditions = array(), $order = null) { $this->observers->update('onBeforeFindOne', array($conditions, $order)); $data = parent::findOne($conditions, $order); $this->observers->update('onAfterFindOne', array(&$data)); return $data; }