Пример #1
0
 /**
  * @return \Phalcon\Http\ResponseInterface
  */
 public function saveAction()
 {
     $this->view->disable();
     //  Is not $_POST
     if (!$this->request->isPost()) {
         return $this->indexRedirect();
     }
     $id = $this->request->getPost('id');
     if (!empty($id)) {
         $object = Posts::findFirstById($id);
     } else {
         $object = new Posts();
     }
     $object->setSlug(Slug::generate($this->request->getPost('title')));
     $object->setUsersId($this->auth->getAuth()['id']);
     $object->setType(Posts::POST_PAGE);
     $form = new PostsForm($object);
     $form->bind($_POST, $object);
     //  Form isn't valid
     if (!$form->isValid($this->request->getPost())) {
         $this->saveLoger($form->getMessages());
         // Redirect to edit form if we have an ID in page, otherwise redirect to add a new item page
         return $this->response->redirect($this->getPathController() . (!is_null($id) ? '/edit/' . $id : '/new'));
     } else {
         if (!$object->save()) {
             $this->saveLoger($object->getMessages());
             return $this->dispatcher->forward(['controller' => $this->getPathController(), 'action' => 'new']);
         } else {
             $this->flashSession->success(t('Data was successfully saved'));
             return $this->indexRedirect();
         }
     }
 }
Пример #2
0
 /**
  * Subscribe to a post to receive e-mail notifications
  *
  * @return mixed
  */
 public function indexAction()
 {
     $this->view->disable();
     $this->setJsonResponse();
     if (!$this->request->isPost()) {
         return false;
     }
     //Find the post by Id
     $post = Posts::findFirstById($this->request->getPost('objectId'));
     if (!$post) {
         $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'The Post does not exist'];
         return $this->jsonMessages;
     }
     /**
      * Sometime We need to get object User login, so I do check user like below
      * By the way, you can checking session
      *
      * {code} Users::findFirstById($this->auth->getAuth()['id'] {/code}
      */
     $userId = $this->auth->getAuth()['id'];
     if (!$userId) {
         $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'You must log in first to subscribe post'];
         return $this->jsonMessages;
     }
     $subscription = PostsSubscribers::findFirst(['postsId = ?0 AND usersId = ?1', 'bind' => [$post->getId(), $userId]]);
     if (!$subscription) {
         $subscription = new PostsSubscribers();
         $subscription->setPostsId($post->getId());
         $subscription->setUsersId($userId);
         if (!$subscription->save()) {
             foreach ($subscription->getMessages() as $message) {
                 $this->logger->error('Subscribe save false ' . $message . __LINE__ . 'and' . __CLASS__);
             }
             return false;
         }
         $this->jsonMessages['messages'][] = ['type' => 'info', 'content' => 'You have just subscribe post', 'flag' => 1];
         return $this->jsonMessages;
     } else {
         //unsubsribe posts
         if (!$subscription->delete()) {
             foreach ($subscription->getMessages() as $message) {
                 $this->logger->error('Unsubscribe delete false ' . $message . __LINE__ . 'and' . __CLASS__);
             }
             return false;
         }
         $this->jsonMessages['messages'][] = ['type' => 'info', 'content' => 'You have just unsubscribe post'];
         return $this->jsonMessages;
     }
 }
Пример #3
0
 /**
  * Displays a post and its comments
  *
  * @param $id
  * @param $slug
  *
  * @return \Phalcon\Http\ResponseInterface
  */
 public function viewAction($id, $slug)
 {
     $id = (int) $id;
     $userId = $this->auth->getAuth()['id'];
     if (!($object = Posts::findFirstById($id))) {
         $this->flashSession->error(t('Posts doesn\'t exist.'));
         return $this->indexRedirect();
     }
     if ($object->getDeleted()) {
         $this->flashSession->error('The Post is deleted');
         return $this->indexRedirect();
     }
     $ipAddress = $this->request->getClientAddress();
     $parameters = ['postsId = ?0 AND ipaddress = ?1', 'bind' => [$id, $ipAddress]];
     $viewed = PostsViews::count($parameters);
     //A view is stored by ipaddress
     if (!$viewed) {
         //Increase the number of views in the post
         $object->setNumberViews($object->getNumberViews() + 1);
         if ($object->getUsersId() != $userId) {
             $object->user->increaseKarma(Karma::VISIT_ON_MY_POST);
             if ($userId > 0) {
                 $user = Users::findFirstById($userId);
                 if ($user) {
                     if ($user->getModerator() == 'Y') {
                         $user->increaseKarma(Karma::MODERATE_VISIT_POST);
                     } else {
                         $user->increaseKarma(Karma::VISIT_POST);
                     }
                     //send log to server
                     if (!$user->save()) {
                         $this->saveLoger($user->getMessages());
                     }
                 }
             }
         }
         $postView = new PostsViews();
         $postView->setPostsId($id);
         $postView->setIpaddress($ipAddress);
         if (!$postView->save()) {
             $this->saveLoger($postView->getMessages());
         }
     }
     $this->view->setVars(['post' => $object, 'form' => new ReplyForm(), 'votes' => $object->getVotes($id, Vote::OBJECT_POSTS), 'postsReply' => $object->getPostsWithVotes($id), 'commentForm' => new CommentForm(), 'userPosts' => $object->user]);
     $this->tag->setTitle($this->escaper->escapeHtml($object->getTitle()));
     return $this->view->pickCustom('single');
 }
Пример #4
0
 /**
  * Finds related posts
  */
 public function showRelatedAction()
 {
     $this->view->disable();
     if ($this->request->isAjax()) {
         //$this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
         $post = Posts::findFirstById($this->request->getPost('id'));
         if ($post) {
             $posts = Posts::postRelated($post);
             if (count($posts) > 0) {
                 $params = ['posts' => $posts];
                 echo $this->view->getRender('partials', 'list-posts', $params, function ($view) {
                     $view->setRenderLevel(View::LEVEL_ACTION_VIEW);
                 });
             }
         }
     }
 }
Пример #5
0
 /**
  * Finds related posts
  */
 public function showRelatedAction()
 {
     if ($this->request->isAjax()) {
         $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
         $post = Posts::findFirstById($this->request->getPost('id'));
         if ($post) {
             $indexer = new Indexer();
             $posts = $indexer->search(['title' => $post->getTitle(), 'content' => $post->getContent()], 5, true);
             if (count($posts) == 0) {
                 $posts = $indexer->search(['title' => $post->title], 5, true);
             }
             $this->view->object = $posts;
         } else {
             $this->view->object = array();
         }
         return 1;
     }
 }
Пример #6
0
 public function saveAction()
 {
     if (!$this->request->isPost()) {
         $this->flashSession->error(t('Hack attempt!!!'));
         return $this->currentRedirect();
     }
     $id = $this->request->getPost('id');
     if (!($object = Posts::findFirstById($id))) {
         $this->flashSession->error(t('The post not exist'));
         return $this->currentRedirect();
     }
     $object->setSticked($this->request->getPost('sticked'));
     if (!$object->save()) {
         error_log('Save false ' . __LINE__ . ' and ' . __CLASS__);
         return false;
     }
     $this->flashSession->success(t('Data was successfully saved'));
     return $this->response->redirect($this->router->getControllerName());
 }
Пример #7
0
 /**
  * Search documents in ElasticSearch by the specified criteria
  *
  * @param array   $fields
  * @param int     $limit
  * @param boolean $returnPosts
  */
 public function search(array $fields, $limit = 10, $returnPosts = false)
 {
     try {
         $client = new Client();
         $searchParams['index'] = $this->index;
         $searchParams['type'] = $this->type;
         $searchParams['body']['fields'] = array('id', 'karma');
         if (count($fields) == 1) {
             $searchParams['body']['query']['match'] = $fields;
         } else {
             $terms = array();
             foreach ($fields as $field => $value) {
                 $terms[] = array('term' => array($field => $value));
             }
             $searchParams['body']['query']['bool']['must'] = $terms;
         }
         $searchParams['body']['from'] = 0;
         $searchParams['body']['size'] = $limit;
         $queryResponse = $client->search($searchParams);
         $results = [];
         if (is_array($queryResponse['hits'])) {
             $d = 0.5;
             foreach ($queryResponse['hits']['hits'] as $hit) {
                 $post = Posts::findFirstById($hit['fields']['id'][0]);
                 if ($post) {
                     if ($hit['fields']['karma'][0] > 0 && ($post->getNumberReply() > 0 || $post->getAcceptedAnswer() == 'Y')) {
                         $score = $hit['_score'] * 250 + $hit['fields']['karma'][0] + $d;
                         if (!$returnPosts) {
                             $results[$score] = array('slug' => 'posts/' . $post->getId() . '/' . $post->getSlug(), 'title' => $post->getTitle(), 'created' => $post->getHumanCreatedAt());
                         } else {
                             $results[$score] = $post;
                         }
                         $d += 0.05;
                     }
                 }
             }
         }
         krsort($results);
         return array_values($results);
     } catch (\Exception $e) {
         return array();
     }
 }
Пример #8
0
 /**
  * Finds related posts
  */
 public function showRelatedAction()
 {
     $this->view->disable();
     if ($this->request->isAjax()) {
         #$this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
         $post = Posts::findFirstById($this->request->getPost('id'));
         if ($post) {
             $indexer = new Indexer();
             $posts = $indexer->search(['title' => $post->getTitle(), 'content' => $post->getContent()], 5, true);
             if (count($posts) == 0) {
                 $posts = $indexer->search(['title' => $post->title], 5, true);
             }
             if (count($posts) > 0) {
                 $params = ['posts' => $posts];
                 echo $this->view->getRender('partials', 'list-posts', $params, function ($view) {
                     $view->setRenderLevel(View::LEVEL_ACTION_VIEW);
                 });
             }
         }
     }
 }
Пример #9
0
 /**
  * Method for voting a task
  *
  * @return mixed
  */
 public function voteAction()
 {
     $this->view->disable();
     if (!$this->request->isPost()) {
         return $this->response->redirect($this->router->getControllerName());
     }
     $way = 'positive';
     if ($this->request->getPost('way') == 'negative') {
         $way = 'negative';
     }
     $objectId = $this->request->getPost('objectId');
     $object = $this->request->getPost('object');
     $vote = Vote::vote($objectId, $object, $way);
     $user = Users::findFirstById($this->auth->getAuth()['id']);
     $this->setJsonResponse();
     if (!$user) {
         $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'You need to login first'];
         return $this->jsonMessages;
     }
     if ($object == Vote::OBJECT_POSTS) {
         if (!($post = Posts::findFirstById($objectId))) {
             $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'Post does not exist'];
             return $this->jsonMessages;
         }
         $this->setPointPost($way, $user, $post);
         //Adding notification when you have receive vote on the post, and not for now for post replies
         if ($user->getId() != $post->getUsersId()) {
             $this->setActivityNotifications($user, $post);
         }
     }
     if ($object == Vote::OBJECT_POSTS_REPLY) {
         if (!($postReply = PostsReply::findFirstById($objectId))) {
             $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'Post reply does not exist'];
             return $this->jsonMessages;
         }
         //Set karam Voting someone else's post (positive or negative) on posts reply
         $this->setPointReply($way, $user, $postReply);
     }
     if (!$vote) {
         $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => 'Vote have a problem :)'];
         return $this->jsonMessages;
     }
     if ($user->getVote() <= 0) {
         $this->jsonMessages['messages'][] = ['type' => 'error', 'content' => t('You don\'t have enough votes available :)')];
         return $this->jsonMessages;
     }
     //checking the user have already voted this post yet
     if (is_array($vote)) {
         $this->jsonMessages['messages'][] = $vote;
         return $this->jsonMessages;
     }
     if ($this->request->isAjax()) {
         $vote = (new Vote())->getVotes($objectId, $object);
         return ['data' => $vote['positive'] - $vote['negative']];
     }
     echo 0;
     return 0;
 }
Пример #10
0
 /**
  * The answer a question
  * @return mixed
  */
 public function answerAction()
 {
     $this->view->disable();
     $auth = $this->auth->getAuth();
     if (!$auth) {
         $this->flashSession->error(t('You must be logged in first to post answer'));
         return $this->currentRedirect();
     }
     if ($this->request->isPost()) {
         $postId = $this->request->getPost('id');
         $content = $this->request->getPost('content', 'trim');
         if (str_word_count($content) < 10) {
             $this->flashSession->error(t('Body must be at least 15 word'));
             return $this->currentRedirect();
         }
         $post = Posts::findFirstById($postId);
         $user = Users::findFirstById($auth['id']);
         //Only update the number of replies if the user that commented isn't the same that posted
         if ($user->getId() != $post->getUsersId()) {
             $post->setNumberReply($post->getNumberReply() + 1);
             $post->user->increaseKarma(Karma::SOMEONE_REPLIED_TO_MY_POST);
             $user->increaseKarma(Karma::REPLY_ON_SOMEONE_ELSE_POST);
             if (!$post->save() || !$user->save()) {
                 error_log('Save fail answerAction. I am on here ' . __LINE__);
                 return false;
             }
         }
         $object = new PostsReply();
         $object->setPostsId($postId);
         $object->setContent($content);
         $object->setUsersId($auth['id']);
         if (!$object->save()) {
             foreach ($object->getMessages() as $message) {
                 $this->flashSession->error($message);
             }
             return $this->currentRedirect();
         }
         $this->flashSession->success(t('Data was successfully saved'));
         return $this->currentRedirect();
     }
 }
Пример #11
0
 /**
  * @return \Phalcon\Http\ResponseInterface
  */
 public function saveAction()
 {
     $this->view->disable();
     //  Is not $_POST
     if (!$this->request->isPost()) {
         return $this->indexRedirect();
     }
     $id = $this->request->getPost('id');
     $auth = $this->auth->getAuth();
     $tags = $this->request->getPost('tags', 'string', null);
     if (!empty($id)) {
         $object = Posts::findFirstById($id);
     } else {
         $object = new Posts();
         //@todo
     }
     $object->setSlug(Slug::generate($this->request->getPost('title')));
     $object->setUsersId($auth['id']);
     $form = new PostsForm($object);
     $form->bind($_POST, $object);
     //  Form isn't valid
     if (!$form->isValid($this->request->getPost())) {
         $this->saveLoger($form->getMessages());
         // Redirect to edit form if we have an ID in page, otherwise redirect to add a new item page
         return $this->response->redirect($this->getPathController() . (!is_null($id) ? '/edit/' . $id : '/new'));
     } else {
         $object->setStatus(Posts::PUBLISH_STATUS);
         //Save post to draft
         if ($this->request->getPost('saveDraft')) {
             $object->setStatus(Posts::DRAFT_STATUS);
         }
         if (!$object->save()) {
             $this->saveLoger($object->getMessages());
             return $this->dispatcher->forward(['controller' => $this->getPathController(), 'action' => 'new']);
         } else {
             $this->phanbook->tag()->saveTagsInPosts($tags, $object);
             $this->flashSession->success(t('Data was successfully saved'));
             return $this->indexRedirect();
         }
     }
 }