Example #1
0
 public function run()
 {
     $faker = Faker::create();
     $log = new Stream('php://stdout');
     $log->info('Start ' . __CLASS__);
     /** @var Phalcon\Db\AdapterInterface $database */
     $database = $this->getDI()->get('db');
     $database->begin();
     for ($i = 0; $i <= self::TAGS_TOTAL; $i++) {
         $title = $faker->company;
         $description = $faker->text;
         $tags = new Tags();
         $tags->name = $title;
         $tags->slug = \Phalcon\Tag::friendlyTitle($title);
         $tags->numberPosts = 0;
         $tags->noBounty = 'N';
         $tags->noDigest = 'N';
         $tags->description = $description;
         if (!$tags->save()) {
             var_dump($tags->getMessages());
             $database->rollback();
             die;
         }
         $log->info('tags: ' . $tags->getName());
     }
 }
Example #2
0
 /**
  * @param $tag is string
  * @param $object is model Posts
  * @return bool
  */
 public function saveTagsInPosts($tag, $object)
 {
     $tagsId = [];
     $postId = $object->getId();
     $getTagsId = $this->getTagsId($tag);
     if (!is_array($getTagsId)) {
         return false;
     }
     $postsTags = PostsTags::find(['postsId = ?0', 'bind' => [$postId]]);
     foreach ($postsTags as $value) {
         $tagsId[] = $value->tagsId;
     }
     //Deleted tags
     $rows2 = array_diff($tagsId, $getTagsId);
     $this->deletedTag($postId, $rows2);
     $rows = array_diff($getTagsId, $tagsId);
     foreach ($rows as $tagId) {
         $postsTags = new PostsTags();
         $postsTags->setTagsId($tagId);
         $postsTags->setPostsId($postId);
         if (!$postsTags->save()) {
             return false;
         }
         //Update the total of posts related to a tags
         if ($object->getOperationMade() == \Phalcon\Mvc\Model::OP_CREATE) {
             $tags = Tags::findFirstById($tagId);
             $number = $tags->getNumberposts();
             $tags->setNumberPosts($number + 1);
             $tags->save();
         }
     }
     return true;
 }
Example #3
0
 public function renderTags()
 {
     $element = "<select id ='tags' name='tags[]' class ='form-control' multiple='multiple'>";
     $tags = Tags::find();
     foreach ($tags as $key => $tag) {
         $selected = '';
         if (is_array($this->tagsId)) {
             if (in_array($tag->id, $this->tagsId)) {
                 $selected = 'selected';
             }
         }
         $element .= "<option value='{$tag->id}' {$selected}> {$tag->name}";
         $element .= "</option>";
     }
     $element .= "</select>";
     return $element;
 }
 /**
  * Retrieve a list of Posts for a specific tags id.
  *
  * @param int    $id       The Tags ID
  * @param string $slugTags
  *
  * @return array list of posts
  */
 public function postByTagAction($id, $slugName)
 {
     $join = ['type' => 'join', 'model' => 'Phanbook\\Models\\PostsTags', 'on' => 'pt.postsId = p.id', 'alias' => 'pt'];
     /**@Todo later for security*/
     $where = 'p.deleted = 0 AND pt.tagsId = ' . $id;
     list($itemBuilder, $totalBuilder) = $this->prepareQueries($join, $where, $this->perPage);
     //$itemBuilder->andWhere($conditions);
     $page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
     $totalPosts = $totalBuilder->getQuery()->setUniqueRow(true)->execute();
     $totalPages = ceil($totalPosts->count / $this->perPage);
     if ($page > 1) {
         $itemBuilder->offset((int) $page);
     }
     //@todo refacttor
     $this->view->setVars(['tab' => 'tags', 'type' => Posts::POST_ALL, 'posts' => $itemBuilder->getQuery()->execute(), 'totalPages' => $totalPages, 'currentPage' => $page, 'slugName' => $slugName, 'tags' => Tags::find()]);
     $this->tag->setTitle(t('These posts fillter by tags'));
     return $this->view->pick('post');
 }
 public function tagSuggestAction()
 {
     $this->view->disable();
     $this->setJsonResponse();
     $q = $this->request->getQuery('q', 'string');
     if ($q) {
         $tags = Tags::query()->Where('name LIKE "%' . $q . '%"')->execute();
         $params = ['tags' => $tags->toArray()];
         if ($this->request->isAjax()) {
             $this->view->getRender('partials', 'tags-suggestions', $params, function ($view) {
                 $view->setRenderLevel(View::LEVEL_ACTION_VIEW);
             });
             echo $this->view->getContent();
             return 1;
         }
         //echo json_encode($data);
     }
 }
Example #6
0
 public function tagSuggestAction()
 {
     $this->view->disable();
     $this->setJsonResponse();
     $q = $this->request->getQuery('q', 'string');
     if ($q) {
         $tags = Tags::query()->Where('name LIKE "%' . $q . '%"')->execute();
         $params = ['tags' => $tags->toArray()];
         if ($this->request->isAjax()) {
             /**
              * Hierarchical Rendering
              * @link https://docs.phalconphp.com/en/latest/reference/views.html#stand-alone-component
              */
             echo $this->view->getRender('partials', 'tags-suggestions', $params, function ($view) {
                 $view->setRenderLevel(View::LEVEL_ACTION_VIEW);
             });
             //echo $this->view->getContent();
             return 1;
         }
     }
 }
 public function run()
 {
     $faker = Faker::create();
     $log = new Stream('php://stdout');
     $log->info('Start ' . __CLASS__);
     /** @var Phalcon\Db\AdapterInterface $database */
     $database = $this->getDI()->get('db');
     $database->begin();
     $postsId = Posts::find(['columns' => 'id'])->toArray();
     $tagsId = Tags::find(['columns' => 'id'])->toArray();
     for ($i = 0; $i <= self::POSTS_TAGS_TOTAL; $i++) {
         $postRandId = array_rand($postsId);
         $tagsRandId = array_rand($tagsId);
         $postTag = new PostsTags();
         $postTag->postsId = $postsId[$postRandId]['id'];
         $postTag->tagsId = $tagsId[$tagsRandId]['id'];
         if (!$postTag->save()) {
             var_dump($tags->getMessages());
             $database->rollback();
             die;
         }
         $log->info('Posts<->Tags ' . $postTag->getPostsId() . '<->' . $postTag->getTagsId());
     }
 }
Example #8
0
 /**
  * @todo Update the total of posts related to a tags
  *
  * @return bool
  */
 public function saveTagsInPosts($_tag, $object)
 {
     $tags = $this->isTags($_tag);
     if (is_array($tags)) {
         //
         $postsTags = PostsTags::find(['postsId = ?0', 'bind' => [$object->getId()]]);
         if (isset($postsTags)) {
             $postsTags->delete();
         }
         foreach ($tags as $tagsId) {
             $postsTags = new PostsTags();
             $postsTags->setTagsId($tagsId);
             $postsTags->setPostsId($object->getId());
             if (!$postsTags->save()) {
                 return false;
             }
             //Update the total of posts related to a tags
             if ($object->getOperationMade() == \Phalcon\Mvc\Model::OP_CREATE) {
                 $tag = Tags::findFirstById($tagsId);
                 $number = $tag->getNumberposts();
                 $tag->setNumberPosts($number + 1);
                 if (!$tag->save()) {
                     return false;
                 }
             }
         }
         return true;
     }
     return false;
 }
Example #9
0
 public function deleteAction($id)
 {
     if (!($object = Tags::findFirstById($id))) {
         $this->flashSession->error(t('Tag doesn\'t exist.'));
         return $this->response->redirect('tag');
     }
     if (!$object->delete()) {
         return $this->response->redirect('tag');
     }
     $this->flashSession->success(t('Data was successfully deleted'));
     return $this->response->redirect($this->router->getControllerName());
 }
Example #10
0
 /**
  * Default it will get all posts
  *
  */
 public function indexAction()
 {
     /* @var \Phalcon\Mvc\Model\Query\BuilderInterface $itemBuilder */
     /* @var \Phalcon\Mvc\Model\Query\BuilderInterface $totalBuilder */
     $tab = $this->request->getQuery('tab');
     $page = isset($_GET['page']) ? (int) $_GET['page'] : $this->numberPage;
     $perPage = isset($_GET['perPage']) ? (int) $_GET['perPage'] : $this->perPage;
     if ($tab == "answers") {
         $join = ['type' => 'join', 'model' => 'Phanbook\\Models\\PostsReply', 'on' => 'r.postsId = p.id', 'alias' => 'r'];
         list($itemBuilder, $totalBuilder) = ModelBase::prepareQueriesPosts($join, false, $perPage);
         $itemBuilder->groupBy(array('p.id'));
     } else {
         list($itemBuilder, $totalBuilder) = ModelBase::prepareQueriesPosts('', false, $perPage);
     }
     $userId = $this->auth->getAuth();
     /*
      * Create the conditions according to the parameter order
      */
     $params = null;
     switch ($tab) {
         case 'hot':
             $this->tag->setTitle('Hot Questions');
             $itemBuilder->orderBy('p.modifiedAt DESC');
             break;
         case 'unanswered':
             $this->tag->setTitle('Unanswered Questions');
             $unansweredConditions = 'p.numberReply = 0 AND p.acceptedAnswer <> "Y"';
             $itemBuilder->where($unansweredConditions);
             $totalBuilder->where($unansweredConditions);
             break;
         case 'week':
             $this->tag->setTitle('Hot Questions This Week');
             $lastWeek = new \DateTime();
             $lastWeek->modify('-1 week');
             $params = array($lastWeek->getTimestamp());
             $weekConditions = 'p.createdAt >= ?0';
             $itemBuilder->where($weekConditions);
             $totalBuilder->where($weekConditions);
             break;
         case 'month':
             $this->tag->setTitle('Hot Questions This Month');
             $lastMonths = new \DateTime();
             $lastMonths->modify('-6 month');
             $params = array($lastMonths->getTimestamp());
             $monthConditions = 'p.createdAt >= ?0';
             $itemBuilder->where($monthConditions);
             $totalBuilder->where($monthConditions);
             break;
         case 'questions':
             $this->tag->setTitle('Questions');
             $questionConditions = 'p.type = "questions"';
             $itemBuilder->where($questionConditions);
             $totalBuilder->where($questionConditions);
             break;
         case 'blog':
             $this->tag->setTitle('Blogs');
             $blogConditions = 'p.type = "blog"';
             $itemBuilder->where($blogConditions);
             $totalBuilder->where($blogConditions);
             break;
         case 'hackernews':
             $this->tag->setTitle('Hacker News');
             $tipConditions = 'p.type = "hackernews"';
             $itemBuilder->where($tipConditions);
             $totalBuilder->where($tipConditions);
             break;
         default:
             $this->tag->setTitle($this->config->application->tagline);
     }
     $type = Posts::POST_PAGE;
     $conditions = "p.deleted = 0 AND p.type != '{$type}'";
     $itemBuilder->andWhere($conditions);
     $totalBuilder->andWhere($conditions);
     //order like tabs sort
     if (!$tab) {
         $tab = 'hot';
     }
     $totalPosts = $totalBuilder->getQuery()->setUniqueRow(true)->execute($params);
     $totalPages = ceil($totalPosts->count / $perPage);
     $offset = ($page - 1) * $perPage + 1;
     if ($page > 1) {
         $itemBuilder->offset($offset);
     }
     $this->view->setVars(['tab' => $tab, 'type' => Posts::POST_ALL, 'posts' => $itemBuilder->getQuery()->execute($params), 'totalPages' => $totalPages, 'currentPage' => $page, 'tags' => Tags::find()]);
     return $this->view->pick('post');
 }
Example #11
0
 public function initialize()
 {
     $this->view->setVars(['tab' => $this->currentOrder, 'tags' => Tags::find(), 'hotPosts' => Posts::getHotPosts(5), 'totalPost' => Posts::totalPost(), 'highestKarma' => Users::highestKarma(), 'totalReply' => PostsReply::totalReply()]);
     if (isset($this->config->perPage)) {
         $this->perPage = $this->config->perPage;
     }
 }
Example #12
0
 /**
  * Returns those tags that don't have bounties
  *
  * @return array
  */
 public function getFullNoBountyTags()
 {
     if (!$this->fullNoBountyTags) {
         $this->fullNoBountyTags = Tags::find('noBounty = "Y"')->toArray();
     }
     return $this->fullNoBountyTags;
 }