Ejemplo n.º 1
0
 /**
  * Likes controller for route /like_post/{like}
  *
  * @param  int   @post_id  The post to be edited.
  */
 public function like_post($post_id)
 {
     // If unknown user or bot, cannot like.
     if ($this->user->data['user_id'] == ANONYMOUS || $this->user->data['is_bot']) {
         return;
     }
     // Add language variables for response.
     $this->user->add_lang_ext('nuleaf/likes', 'likes');
     // Grab forum id for permission.
     $sql = 'SELECT forum_id
 FROM ' . POSTS_TABLE . '
 WHERE post_id = ' . $post_id;
     $result = $this->db->sql_query_limit($sql, 1);
     $forum_id = $this->db->sql_fetchrow($result)['forum_id'];
     $this->db->sql_freeresult($result);
     // Does the user have permission to like posts in this forum?
     if ($this->auth->acl_get('!f_like', $forum_id)) {
         $json_response = new json_response();
         $json_response->send(array('error' => $this->user->lang('LIKE_NOT_AUTHORIZED')));
         return;
     }
     if ($this->request->is_ajax()) {
         $liked = $this->likes_manager->is_liked($post_id);
         if ($liked) {
             // If post is already liked, unlike it.
             $likes_count = $this->likes_manager->unlike($post_id);
         } else {
             // Else like the post.
             $likes_count = $this->likes_manager->like($post_id);
         }
         // Since the post has now been liked/unliked, $liked is reversed.
         $json_response = new json_response();
         $json_response->send(array('likes_count' => $likes_count, 'liked' => !$liked, 'LIKE_POST' => $this->user->lang('LIKE_POST'), 'UNLIKE_POST' => $this->user->lang('UNLIKE_POST'), 'LIKE_BUTTON' => $this->user->lang('LIKE_BUTTON'), 'UNLIKE_BUTTON' => $this->user->lang('UNLIKE_BUTTON')));
     }
 }
Ejemplo n.º 2
0
 /**
  * Event: core.viewtopic_modify_post_row.
  * 
  * Adds varibles needed for the likes extension to be displayed on each post.
  */
 public function add_likes_to_post($event)
 {
     // Add language files to postrow display loop.
     $this->user->add_lang_ext('nuleaf/likes', 'likes');
     $post_row = $event['post_row'];
     // Modify post row data to include variables needed for the likes to be displayed on post.
     $post_id = $post_row['POST_ID'];
     $post_row['LIKES_COUNT'] = $this->likes_manager->get_likes_count($post_id);
     $post_row['LIKED'] = $this->likes_manager->is_liked($post_id);
     $post_row['U_LIKES'] = $this->helper->route('nuleaf_likes_controller', array('post_id' => $post_id));
     $event['post_row'] = $post_row;
     // Include necessary functions and css.
     if ($this->config['likes_on']) {
         $this->template->assign_vars(array('LIKES_INCLUDEJS' => true, 'LIKES_INCLUDECSS' => true, 'LIKES_ON' => true));
     }
 }