Example #1
0
 /**
  * @param array $items
  * @param array $options
  * @param array $extend_data
  * @return array
  */
 public function prepareView($items, $options = array(), $extend_data = array())
 {
     $extend_options = array_merge($this->extend_options, $options);
     $extend_data = array_merge($this->extend_data, (array) $extend_data);
     foreach ($items as &$item) {
         blogHelper::extendIcon($item);
         if (!isset($extend_options['link']) || $extend_options['link']) {
             $item['link'] = blogBlog::getUrl($item, true);
         }
         if (!empty($extend_options['escape'])) {
             $item['name'] = htmlspecialchars($item['name'], ENT_QUOTES, 'utf-8');
             $item['link'] = htmlspecialchars($item['link'], ENT_QUOTES, 'utf-8');
         }
         unset($item);
     }
     if (isset($options['new']) && $options['new']) {
         $post_model = new blogPostModel();
         $blog_activity = blogActivity::getInstance();
         $posts_update = $post_model->getAddedPostCount(blogActivity::getUserActivity(), array_keys($items), true);
         if ($posts_update) {
             foreach ($posts_update as $blog_id => $new) {
                 if (isset($items[$blog_id])) {
                     $items[$blog_id]['new_post'] = 0;
                     $post_ids = explode(':', $new);
                     foreach ($post_ids as $post_id) {
                         if ($blog_activity->isNew("b.{$blog_id}", $post_id, isset($options['expire']) ? $options['expire'] : null)) {
                             ++$items[$blog_id]['new_post'];
                         }
                     }
                     if (!$items[$blog_id]['new_post']) {
                         unset($items[$blog_id]['new_post']);
                     }
                 }
             }
         }
     }
     /**
      * Prepare blog data
      * Extend each blog item via plugins data
      * @event prepare_blogs_frontend
      * @event prepare_blogs_backend
      * @param array $items
      * @param int $items[]['id']
      * @return void
      */
     wa()->event('prepare_blogs_' . wa()->getEnv(), $items);
     return $items;
 }
Example #2
0
 public static function extendPostState(&$posts, $mode = false)
 {
     $user = wa()->getUser();
     $timezone = $user->getTimezone();
     $contact_id = $user->getId();
     $current_datetime = waDateTime::date("Y-m-d", null, $timezone);
     $activity_datetime = blogActivity::getUserActivity();
     $blog_activity = null;
     if ('view' === $mode) {
         $blog_activity = blogActivity::getInstance();
         $viewed_ids = array();
     }
     foreach ($posts as &$post) {
         if ($post['datetime']) {
             if (in_array($post['status'], array(blogPostModel::STATUS_DEADLINE))) {
                 $datetime = waDateTime::date("Y-m-d", $post['datetime'], $timezone);
                 if ($datetime <= $current_datetime) {
                     $post['overdue'] = true;
                 }
             } elseif (in_array($post['status'], array(blogPostModel::STATUS_PUBLISHED))) {
                 if ($activity_datetime && $post['datetime'] > $activity_datetime && (!$contact_id || $contact_id != $post['contact_id'])) {
                     if ('view' === $mode) {
                         $post['new'] = $blog_activity->isNew("b.{$post['blog_id']}", $post['id']);
                         if ($post['new'] == blogActivity::STATE_NEW) {
                             if (!isset($viewed_ids[$post['blog_id']])) {
                                 $viewed_ids[$post['blog_id']] = array();
                             }
                             $viewed_ids[$post['blog_id']][] = $post['id'];
                         } elseif (!$post['new']) {
                             unset($post['new']);
                         }
                     } else {
                         $post['new'] = true;
                     }
                 }
             }
         }
         unset($post);
     }
     if ($blog_activity && !empty($viewed_ids)) {
         foreach ($viewed_ids as $blog_id => $post_ids) {
             $blog_activity->set("b.{$blog_id}", $post_ids);
         }
     }
 }
 public function getCount($blog_id, $post_id = null, $datetime = null, $expire = null, $post_contact_id = null, $status = self::STATUS_PUBLISHED)
 {
     $where = array();
     $join = array();
     if ($datetime) {
         $where[] = "{$this->table}.datetime > '{$this->escape($datetime)}'";
         if ($contact_id = wa()->getUser()->getId()) {
             $where[] = "{$this->table}.contact_id != " . intval($contact_id);
         }
     }
     if ($post_contact_id = max(0, intval($post_contact_id))) {
         $post_model = new blogPostModel();
         $post_table = $post_model->getTableName();
         $post_table_id = $post_model->getTableId();
         $join[] = " INNER JOIN {$post_table} ON {$post_table}.{$post_table_id} = {$this->table}.post_id";
         $where[] = "{$post_table}.contact_id = {$post_contact_id}";
     }
     if ($status) {
         $where[] = $this->getWhereByField('status', $status, true);
     }
     if ($post_id) {
         $where[] = $this->getWhereByField('post_id', $post_id, true);
     }
     if ($blog_id !== null) {
         $where[] = $this->getWhereByField('blog_id', $blog_id, true);
     }
     if ($datetime) {
         $count_by_post = $post_id && is_array($post_id);
         if ($count_by_post) {
             $count = array_fill_keys($post_id, 0);
         } else {
             $count = 0;
         }
         $sql = "SELECT {$this->table}.{$this->id} AS {$this->id}, post_id FROM {$this->table} " . implode('', $join) . " WHERE (" . implode(') AND (', $where) . ")";
         if ($comments = $this->query($sql)->fetchAll($this->id, true)) {
             $blog_activity = blogActivity::getInstance();
             foreach ($comments as $id => $comment_post_id) {
                 if ($blog_activity->isNew("c.{$comment_post_id}", $id, $expire)) {
                     if ($count_by_post) {
                         ++$count[$comment_post_id];
                     } else {
                         ++$count;
                     }
                 }
             }
             $viewed_comments = array();
         }
     } elseif ($post_id && is_array($post_id)) {
         $sql = "SELECT post_id, COUNT(*) FROM {$this->table} " . implode('', $join) . " WHERE (" . implode(') AND (', $where) . ") GROUP BY post_id";
         $count = $this->query($sql)->fetchAll('post_id', true);
     } else {
         $sql = "SELECT COUNT(*) FROM {$this->table} " . implode('', $join) . " WHERE (" . implode(') AND (', $where) . ")";
         $count = $this->query($sql)->fetchField();
     }
     return $count;
 }