/** * 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; }
/** * doExecute * * @return void */ public function doExecute() { $faker = \Faker\Factory::create(); $password = new \Windwalker\Crypt\Password(); $pass = $password->create('1234'); $userMapper = new DataMapper('users'); foreach (range(1, 100) as $row) { $data['username'] = $row == 1 ? 'asika' : $faker->userName; $data['fullname'] = $row == 1 ? 'Simon Asika' : $faker->name; $data['email'] = $faker->email; $data['password'] = $pass; $data['description'] = $faker->text(); $data['image'] = 'http://cl.ly/Yapj/397512_301002056764411_3688152576777782947_n.jpg'; $data['website'] = $faker->url; $data['state'] = $faker->numberBetween(-1, 1); $data['timezone'] = $faker->timezone; $data['language'] = $faker->languageCode; $data['activation'] = md5(uniqid()); $data['register_date'] = $faker->date; $data['last_visit_date'] = $faker->date; $data['params'] = ''; $userMapper->createOne($data); } $this->command->out('User Seeder executed.')->out(); }
/** * 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; }
/** * doExecute * * @return void */ public function doExecute() { $faker = \Faker\Factory::create(); $blogMapper = new DataMapper('blogs'); $authorMapper = new DataMapper('authors'); $users = (new DataMapper('users'))->findAll(); foreach ($blogMapper->findAll() as $blog) { // Is user $randUsers = $faker->randomElements(iterator_to_array($users), rand(1, 3)); $hasOwner = false; foreach ($randUsers as $user) { $data = array(); $data['user'] = $user->id; $data['blog'] = $blog->id; $data['admin'] = $hasOwner ? rand(0, 1) : 1; $data['owner'] = $hasOwner ? 0 : 1; $authorMapper->createOne($data); $hasOwner = true; } // Is Author foreach (range(1, rand(1, 3)) as $row) { $data = array(); $data['blog'] = $blog->id; $data['name'] = $faker->name; $data['description'] = $faker->text(); $data['image'] = 'http://cl.ly/Yapj/397512_301002056764411_3688152576777782947_n.jpg'; $data['website'] = $faker->url; $authorMapper->createOne($data); } } $this->command->out('Author Seeder executed.')->out(); }
/** * 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; }
/** * 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; }
/** * doExecute * * @return void */ public function doExecute() { $faker = \Faker\Factory::create(); $blogMapper = new DataMapper('blogs'); $categoryMapper = new DataMapper('categories'); $authorMapper = new DataMapper('authors'); $postMapper = new DataMapper('posts'); $psMappingMapper = new DataMapper('category_post_maps'); $content = file_get_contents(__DIR__ . '/fixtures/testcase.md'); list($introtext, $fulltext) = explode('<!-- {READMORE} -->', $content); $metadesc = $faker->text(); foreach ($blogMapper->findAll() as $blog) { $authors = iterator_to_array($authorMapper->find(['blog' => $blog->id])); $categories = iterator_to_array($categoryMapper->find(['blog' => $blog->id])); // Post foreach (range(1, 30) as $row) { $data = array(); $data['blog'] = $blog->id; $data['type'] = 'post'; $data['title'] = $faker->sentence(rand(3, 8)); $data['alias'] = \Windwalker\Filter\OutputFilter::stringURLSafe($data['title']); $data['introtext'] = $introtext; $data['fulltext'] = $fulltext; $data['metadesc'] = $metadesc; $data['state'] = 1; $data['created'] = $faker->dateTime->format('Y-m-d H:i:s'); $data['modified'] = $faker->dateTime->format('Y-m-d H:i:s'); $data['publish_up'] = ''; $data['publish_down'] = ''; $data['author'] = $faker->randomElement($authors)->id; $data = $postMapper->createOne($data); // Categories foreach ($faker->randomElements($categories, rand(1, 3)) as $category) { $map['category'] = $category->id; $map['post'] = $data['id']; $psMappingMapper->createOne($map); } } // Static foreach (range(1, 3) as $row) { $data = array(); $data['blog'] = $blog->id; $data['type'] = 'static'; $data['title'] = $faker->sentence(rand(3, 8)); $data['alias'] = \Windwalker\Filter\OutputFilter::stringURLSafe($data['title']); $data['introtext'] = $introtext; $data['fulltext'] = $fulltext; $data['metadesc'] = $metadesc; $data['state'] = 1; $data['created'] = $faker->dateTime->format('Y-m-d H:i:s'); $data['modified'] = $faker->dateTime->format('Y-m-d H:i:s'); $data['publish_up'] = ''; $data['publish_down'] = ''; $data['author'] = $faker->randomElement($authors)->id; $data = $postMapper->createOne($data); } } $this->command->out('Post Seeder executed.')->out(); }
/** * doExecute * * @return void */ public function doExecute() { $faker = \Faker\Factory::create(); $password = new Password(); $userMapper = new DataMapper('users'); foreach (range(1, 10) as $i) { $data = array('username' => $faker->userName, 'email' => $faker->email, 'password' => $password->create('1234')); $userMapper->createOne($data); } }
/** * 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.'); }
/** * doExecute * * @return void */ public function doExecute() { $faker = \Faker\Factory::create(); $blogMapper = new DataMapper('blogs'); $categoryMapper = new DataMapper('categories'); foreach ($blogMapper->findAll() as $blog) { foreach (range(1, rand(3, 6)) as $row) { $data = array(); $data['blog'] = $blog->id; $data['title'] = $faker->sentence(rand(3, 8)); $data['alias'] = \Windwalker\Filter\OutputFilter::stringURLSafe($data['title']); $data['image'] = 'http://unsplash.it/300/200'; $data['state'] = 1; $data['ordering'] = $row; $categoryMapper->createOne($data); } } $this->command->out('Category Seeder executed.')->out(); }
/** * Execute the controller. * * @return mixed Return executed result. * * @throws \LogicException * @throws \RuntimeException */ public function execute() { $query = $this->input->getString('query'); $query = trim($query); if (!$query) { return; } $queries = explode(' ', $query); $mapper = new DataMapper('users'); $conditions = []; $q = Ioc::getDatabase()->getQuery(true); foreach ($queries as $query) { if (!trim($query)) { continue; } $query = $q->quote('%' . $query . '%'); $conditions[] = 'username LIKE ' . $query; $conditions[] = 'fullname LIKE ' . $query; $conditions[] = 'email LIKE ' . $query; } $conditions = new QueryElement('()', $conditions, ' OR '); $users = $mapper->find([(string) $conditions], 'username', 0, 10); $suggestions = []; $tmpl = <<<VALUE <div> \t<img width="48" height="48" class="find-author-avatar pull-left" src="%s" alt=""/> \t<div class="find-author-name">%s <small>%s</small></div> \t<small>%s</small> \t<div class="clearfix"></div> </div> VALUE; foreach ($users as $user) { $suggestions[] = ['value' => sprintf($tmpl, UserHelper::getAvatar($user->id), $user->fullname, $user->username, $user->email), 'data' => $user->username]; } $response = new Response(); $response->setBody(json_encode(['suggestions' => $suggestions])); $response->setMimeType('text/json'); $response->respond(); exit; return true; }
/** * doExecute * * @return void */ public function doExecute() { $faker = \Faker\Factory::create(); $blogMapper = new DataMapper('blogs'); $users = (new DataMapper('users'))->findAll(); foreach (range(1, 600) as $row) { $data['owner'] = $users[$faker->numberBetween(1, count($users) - 1)]->id; $data['title'] = $faker->sentence(4); $data['alias'] = \Windwalker\Filter\OutputFilter::stringURLSafe($data['title']); $data['sub_title'] = $faker->sentence(8); $data['description'] = $faker->text(); $data['image'] = 'https://unsplash.it/200/300'; $data['state'] = 1; $data['timezone'] = $faker->timezone; $data['disqus'] = 'asukademy'; $data['webmaster'] = md5(1234); $data['analytics'] = 'UA-xxxx-xx-xx'; $data['params'] = ''; $blogMapper->createOne($data); } $this->command->out('Blog Seeder executed.')->out(); }
/** * 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; }
/** * Delete records by where conditions. * * @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%"' * * @return boolean Will be always true. */ public function delete($conditions) { $this->observers->update('onBeforeDelete', array(&$conditions)); $result = parent::delete($conditions); $this->observers->update('onAfterDelete', array(&$result)); return $result; }
/** * @covers Windwalker\DataMapper\AbstractDataMapper::updateAll * @todo Implement testUpdateAll(). */ public function testFlush() { $mapper = new DataMapper('ww_content_tags'); $dataset = new DataSet(); $dataset[] = new Data(array('content_id' => 4, 'tag_id' => 1)); $dataset[] = new Data(array('content_id' => 4, 'tag_id' => 2)); $dataset[] = new Data(array('content_id' => 4, 'tag_id' => 4)); $dataset[] = new Data(array('content_id' => 4, 'tag_id' => 5)); $mapper->flush($dataset, array('content_id' => 4)); $tagMaps = $mapper->find(array('content_id' => 4)); $this->assertEquals(array(1, 2, 4, 5), ArrayHelper::getColumn((array) $tagMaps, 'tag_id'), 'Flush data wrong.'); }
/** * Method to test getDb(). * * @return void * * @covers Windwalker\DataMapper\DataMapper::getDb */ public function testGetAndSetDb() { $this->assertInstanceOf('Windwalker\\DataMapper\\Adapter\\WindwalkerAdapter', $this->instance->getDb()); }
/** * 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; }
/** * doDelete * * @param array $conditions * * @throws \Exception * @return mixed */ protected function doDelete(array $conditions) { $dataset = $this->find($conditions); $this->db->transactionStart(true); try { // Loop each data. foreach ($dataset as &$data) { // Loop the relation mapper. foreach ($this->relations as $field => $relation) { // Prepare sub conditions $subConditions = array(); // Find relation data to this field. foreach ($relation['relations'] as $left => $right) { $subConditions[$right] = $data->{$left}; } $relation['table']->delete($subConditions); } parent::doDelete($conditions); } } catch (\Exception $e) { $this->db->transactionRollback(true); throw $e; } $this->db->transactionCommit(true); 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)); }