コード例 #1
0
ファイル: SearchController.php プロジェクト: Andyyang1981/pi
 /**
  * Searching articles by title. 
  */
 public function simpleAction()
 {
     $order = 'time_publish DESC';
     $page = $this->params('p', 1);
     $module = $this->getModule();
     $config = Pi::config('', $module);
     $limit = intval($config['page_limit_all']) ?: 40;
     $offset = $limit * ($page - 1);
     // Build where
     $where = array();
     $keyword = $this->params('keyword', '');
     if ($keyword) {
         $where['subject like ?'] = sprintf('%%%s%%', $keyword);
     }
     // Retrieve data
     $articleResultset = Entity::getAvailableArticlePage($where, $page, $limit, null, $order, $module);
     // Total count
     $where = array_merge($where, array('time_publish <= ?' => time(), 'status' => Article::FIELD_STATUS_PUBLISHED, 'active' => 1));
     $modelArticle = $this->getModel('article');
     $totalCount = $modelArticle->getSearchRowsCount($where);
     // Paginator
     $paginator = Paginator::factory($totalCount, array('limit' => $limit, 'page' => $page, 'url_options' => array('page_param' => 'p', 'params' => array('module' => $module, 'controller' => 'search', 'action' => 'simple', 'keyword' => $keyword))));
     // Prepare search form
     $form = new SimpleSearchForm();
     $form->setData($this->params()->fromQuery());
     $this->view()->assign(array('title' => __('Search result of '), 'articles' => $articleResultset, 'keyword' => $keyword, 'p' => $page, 'paginator' => $paginator, 'count' => $totalCount, 'form' => $form));
 }
コード例 #2
0
ファイル: IndexController.php プロジェクト: Andyyang1981/pi
 /**
  * Default action to generate RSS info
  */
 public function indexAction()
 {
     $page = $this->params('page', 1);
     $page = $page > 0 ? $page : 1;
     $limit = $this->params('limit', 100);
     $limit = $limit > 500 ? 500 : $limit;
     $timestamp = time();
     $sitename = Pi::config('sitename');
     $feed = $this->getDataModel(array('title' => sprintf(__('All Articles of %s'), $sitename), 'description' => sprintf(__('All Articles of %s'), $sitename), 'copyright' => $sitename, 'date_created' => $timestamp, 'entries' => array()));
     $columns = array('id', 'subject', 'time_publish', 'category', 'content');
     $data = Entity::getAvailableArticlePage(null, $page, $limit, $columns, null, $this->getModule());
     foreach ($data as $row) {
         $entry = array('title' => $row['subject'], 'date_modified' => (int) $row['time_publish'], 'channel' => $row['channel_title'], 'category' => $row['category_title'] ?: '&nbsp;', 'link' => Pi::url($row['url'], true), 'description' => $row['content'] ?: '&nbsp;');
         $feed->entry = $entry;
     }
     return $feed;
 }
コード例 #3
0
ファイル: StatsController.php プロジェクト: Andyyang1981/pi
 /**
  * Default page 
  */
 public function indexAction()
 {
     $topVisitsEver = Entity::getTotalVisits(10);
     $topVisits7 = Entity::getVisitsRecently(7, 10);
     $topVisits30 = Entity::getVisitsRecently(30, 10);
     $totalEver = Stats::getTotalRecently();
     $total7 = Stats::getTotalRecently(7);
     $total30 = Stats::getTotalRecently(30);
     $totalEverByCategory = Stats::getTotalRecentlyByCategory();
     $total7ByCategory = Stats::getTotalRecentlyByCategory(7);
     $total30ByCategory = Stats::getTotalRecentlyByCategory(30);
     $topSubmittersEver = Stats::getSubmittersRecently(null, 10);
     $topSubmitters7 = Stats::getSubmittersRecently(7, 10);
     $topSubmitters30 = Stats::getSubmittersRecently(30, 10);
     if ($this->config('enable_tag') && Pi::service('tag')->active()) {
         $topTags = Pi::service('tag')->top(10, $this->getModule(), null);
         $this->view()->assign('topTags', $topTags);
     }
     $this->view()->assign(array('title' => _a('Statistic'), 'topVisitsEver' => $topVisitsEver, 'topVisits7' => $topVisits7, 'topVisits30' => $topVisits30, 'totalEver' => $totalEver, 'total7' => $total7, 'total30' => $total30, 'totalEverByCategory' => $totalEverByCategory, 'total7ByCategory' => $total7ByCategory, 'total30ByCategory' => $total30ByCategory, 'topSubmittersEver' => $topSubmittersEver, 'topSubmitters7' => $topSubmitters7, 'topSubmitters30' => $topSubmitters30));
 }
コード例 #4
0
ファイル: TagController.php プロジェクト: Andyyang1981/pi
 /**
  * Process article list related with tag
  * 
  * @return ViewModel 
  */
 public function listAction()
 {
     $tag = $this->params('tag', '');
     $page = $this->params('p', 1);
     $page = $page > 0 ? $page : 1;
     $where = $articleIds = $articles = array();
     if (empty($tag)) {
         return $this->jumpTo404(__('Cannot find this page'));
     }
     $module = $this->getModule();
     $config = Pi::config('', $module);
     $limit = $config['page_limit_all'] ?: 40;
     $offset = ($page - 1) * $limit;
     // Total count
     $totalCount = (int) Pi::service('tag')->getCount($tag, $module);
     // Get article ids
     $articleTags = Pi::service('tag')->getList($tag, $module, '', $limit, $offset);
     foreach ($articleTags as $row) {
         $articleIds[] = $row['item'];
     }
     if ($articleIds) {
         $where['id'] = $articleIds;
         $articles = array_flip($articleIds);
         $columns = array('id', 'subject', 'time_publish', 'category');
         $resultsetArticle = Entity::getAvailableArticlePage($where, 1, $limit, $columns, '', $module);
         foreach ($resultsetArticle as $key => $val) {
             $articles[$key] = $val;
         }
         $articles = array_filter($articles, function ($var) {
             return is_array($var);
         });
     }
     // Pagination
     $paginator = Paginator::factory($totalCount, array('limit' => $limit, 'page' => $page, 'url_options' => array('page_param' => 'p', 'params' => array('tag' => $tag))));
     $this->view()->assign(array('title' => __('Articles on Tag '), 'articles' => $articles, 'paginator' => $paginator, 'p' => $page, 'tag' => $tag, 'config' => $config, 'count' => $totalCount));
     $this->view()->viewModel()->getRoot()->setVariables(array('breadCrumbs' => true, 'Tag' => $tag));
 }
コード例 #5
0
ファイル: Draft.php プロジェクト: Andyyang1981/pi
 /**
  * Get draft article details.
  * 
  * @param int  $id  Draft article ID
  * @return array 
  */
 public static function getDraft($id)
 {
     $result = array();
     $module = Pi::service('module')->current();
     $config = Pi::config('', $module);
     $row = Pi::model('draft', $module)->findRow($id, 'id', false);
     if (empty($row->id)) {
         return array();
     }
     $subject = $subtitle = $content = '';
     if ($row->markup) {
         $subject = Pi::service('markup')->render($row->subject, 'html', $row->markup);
         $subtitle = Pi::service('markup')->render($row->subtitle, 'html', $row->markup);
     } else {
         $subject = Pi::service('markup')->render($row->subject, 'html');
         $subtitle = Pi::service('markup')->render($row->subtitle, 'html');
     }
     $content = Compiled::compiled($row->markup, $row->content, 'html');
     $result = array('title' => $subject, 'content' => self::breakPage($content), 'slug' => $row->slug, 'seo' => array('title' => $row->seo_title, 'keywords' => $row->seo_keywords, 'description' => $row->seo_description), 'subtitle' => $subtitle, 'source' => $row->source, 'pages' => $row->pages, 'time_publish' => $row->time_publish, 'author' => array(), 'attachment' => array(), 'tag' => $row->tag, 'related' => array(), 'category' => $row->category);
     // Get author
     if ($row->author) {
         $author = Pi::model('author', $module)->find($row->author);
         if ($author) {
             $result['author'] = $author->toArray();
             if (empty($result['author']['photo'])) {
                 $result['author']['photo'] = Pi::service('asset')->getModuleAsset($config['default_author_photo'], $module);
             }
         }
     }
     // Get attachments
     $resultsetDraftAsset = Pi::model('asset_draft', $module)->select(array('draft' => $id, 'type' => 'attachment'));
     $mediaIds = array(0);
     foreach ($resultsetDraftAsset as $asset) {
         $mediaIds[$asset->media] = $asset->media;
     }
     $modelMedia = Pi::model('media', $module);
     $rowMedia = $modelMedia->select(array('id' => $mediaIds));
     foreach ($rowMedia as $media) {
         $result['attachment'][] = array('original_name' => $media->title, 'extension' => $media->type, 'size' => $media->size, 'url' => Pi::service('url')->assemble('admin', array('module' => $module, 'controller' => 'media', 'action' => 'download', 'name' => $media->id)));
     }
     // Get related articles
     $relatedIds = $related = array();
     $relatedIds = $row->related;
     if ($relatedIds) {
         $related = array_flip($relatedIds);
         $where = array('id' => $relatedIds);
         $columns = array('id', 'subject');
         $resultsetRelated = Entity::getArticlePage($where, 1, null, $columns, null, $module);
         foreach ($resultsetRelated as $key => $val) {
             if (array_key_exists($key, $related)) {
                 $related[$key] = $val;
             }
         }
         $result['related'] = array_filter($related, function ($var) {
             return is_array($var);
         });
     }
     if (empty($row->seo_keywords) && $config['seo_keywords']) {
         if ($config['seo_keywords'] == Article::FIELD_SEO_KEYWORDS_TAG) {
             $result['seo']['keywords'] = implode(' ', $result['tag']);
         } else {
             if ($config['seo_keywords'] == Article::FIELD_SEO_KEYWORDS_CATEGORY) {
                 $rowCategory = Pi::model('category', $module)->find($row->category);
                 $result['seo']['keywords'] = $rowCategory->title;
             }
         }
     }
     if (empty($row->seo_description) && $config['seo_description']) {
         if ($config['seo_description'] == Article::FIELD_SEO_DESCRIPTION_SUMMARY) {
             $result['seo']['description'] = $row->summary;
         }
     }
     return $result;
 }
コード例 #6
0
ファイル: DraftController.php プロジェクト: Andyyang1981/pi
 /**
  * Edit a draft
  * 
  * @return ViewModel 
  */
 public function editAction()
 {
     // Denied user viewing if no front-end management permission assigned
     if (!$this->config('enable_front_edit') && 'front' == $this->section) {
         return $this->jumpTo404();
     }
     $id = $this->params('id', 0);
     $module = $this->getModule();
     $options = Setup::getFormConfig();
     $elements = $options['elements'];
     if (!$id) {
         return;
     }
     $draftModel = $this->getModel('draft');
     $row = $draftModel->findRow($id, 'id', false);
     // Generate user permissions
     $status = '';
     switch ((int) $row->status) {
         case DraftModel::FIELD_STATUS_DRAFT:
             $status = 'draft';
             break;
         case DraftModel::FIELD_STATUS_PENDING:
             $status = 'pending';
             break;
         case DraftModel::FIELD_STATUS_REJECTED:
             $status = 'rejected';
             break;
     }
     if ($row->article) {
         $status = 'publish';
     }
     $isMine = $row->uid == Pi::user()->getId();
     $rules = Rule::getPermission($isMine);
     if (!(isset($rules[$row->category][$status . '-edit']) and $rules[$row->category][$status . '-edit'])) {
         return $this->jumpToDenied();
     }
     $categories = array();
     $approve = array();
     $delete = array();
     foreach ($rules as $key => $rule) {
         if (isset($rule[$status . '-edit']) and $rule[$status . '-edit']) {
             $categories[$key] = true;
         }
         // Getting approving and deleting permission for draft article
         if (isset($rule['approve']) and $rule['approve']) {
             $approve[] = $key;
         }
         if (isset($rule['approve-delete']) and $rule['approve-delete']) {
             $delete[] = $key;
         }
     }
     $currentDelete = (isset($rules[$row->category][$status . '-delete']) and $rules[$row->category][$status . '-delete']) ? true : false;
     $currentApprove = (isset($rules[$row->category]['approve']) and $rules[$row->category]['approve']) ? true : false;
     if (empty($row)) {
         return;
     }
     // prepare data
     $data = (array) $row;
     $data['category'] = $data['category'] ?: $this->config('default_category');
     $data['related'] = $data['related'] ? implode(self::TAG_DELIMITER, $data['related']) : '';
     $data['time_publish'] = $data['time_publish'] ? _date($data['time_publish']) : '';
     $data['time_update'] = $data['time_update'] ? _date($data['time_update']) : '';
     $featureImage = $data['image'] ? Pi::url($data['image']) : '';
     $featureThumb = $data['image'] ? Pi::url(Media::getThumbFromOriginal($data['image'])) : '';
     $form = $this->getDraftForm('edit', $options);
     $allCategory = $form->get('category')->getValueOptions();
     $form->get('category')->setValueOptions(array_intersect_key($allCategory, $categories));
     $form->setData($data);
     // Get author info
     if (in_array('author', $elements) and $data['author']) {
         $author = $this->getModel('author')->find($data['author']);
         if ($author) {
             $this->view()->assign('author', array('id' => $author->id, 'name' => $author->name));
         }
     }
     // Get submitter info
     $columns = array('id', 'name');
     if ($data['uid']) {
         $user = Pi::user()->get($data['uid'], $columns);
         if ($user) {
             $this->view()->assign('user', array('id' => $user['id'], 'name' => $user['name']));
         }
     }
     // Get update user info
     if ($data['user_update']) {
         $userUpdate = Pi::user()->get($data['user_update'], $columns);
         if ($userUpdate) {
             $this->view()->assign('userUpdate', array('id' => $userUpdate['id'], 'name' => $userUpdate['name']));
         }
     }
     // Get related articles
     if (in_array('related', $elements)) {
         $related = $relatedIds = array();
         if (!empty($row->related)) {
             $relatedIds = array_flip($row->related);
             $related = Entity::getArticlePage(array('id' => $row->related), 1);
             foreach ($related as $item) {
                 if (array_key_exists($item['id'], $relatedIds)) {
                     $relatedIds[$item['id']] = $item;
                 }
             }
             $related = array_filter($relatedIds, function ($var) {
                 return is_array($var);
             });
         }
     }
     // Get assets
     $attachments = $images = array();
     $resultsetDraftAsset = $this->getModel('asset_draft')->select(array('draft' => $id))->toArray();
     // Getting media ID
     $mediaIds = array(0);
     foreach ($resultsetDraftAsset as $asset) {
         $mediaIds[] = $asset['media'];
     }
     // Getting media details
     $resultsetMedia = $this->getModel('media')->select(array('id' => $mediaIds));
     $medias = array();
     foreach ($resultsetMedia as $media) {
         $medias[$media->id] = $media->toArray();
     }
     // Getting assets
     foreach ($resultsetDraftAsset as $asset) {
         $media = $medias[$asset['media']];
         if ('attachment' == $asset['type']) {
             $attachments[] = array('id' => $asset['id'], 'title' => $media['title'], 'size' => $media['size'], 'deleteUrl' => $this->url('', array('controller' => 'draft', 'action' => 'remove-asset', 'id' => $asset['id'])), 'downloadUrl' => $this->url('admin', array('controller' => 'media', 'action' => 'download', 'id' => $media['id'])));
         } else {
             $imageSize = getimagesize(Pi::path($media['url']));
             $images[] = array('id' => $asset['id'], 'title' => $media['title'], 'size' => $media['size'], 'w' => $imageSize['0'], 'h' => $imageSize['1'], 'downloadUrl' => $this->url('', array('controller' => 'media', 'action' => 'download', 'id' => $media['id'])), 'preview_url' => Pi::url($media['url']), 'thumb_url' => Pi::url(Media::getThumbFromOriginal($media['url'])));
         }
     }
     $this->setModuleConfig();
     $this->view()->assign(array('title' => __('Edit Article'), 'form' => $form, 'draft' => (array) $row, 'related' => $related, 'attachments' => $attachments, 'images' => $images, 'featureImage' => $featureImage, 'featureThumb' => $featureThumb, 'config' => Pi::config('', $module), 'from' => $this->params('from', ''), 'elements' => $elements, 'status' => $row->article ? Article::FIELD_STATUS_PUBLISHED : $row->status, 'rules' => $rules, 'approve' => $approve, 'delete' => $delete, 'currentDelete' => $currentDelete, 'currentApprove' => $currentApprove));
     $this->view()->setTemplate('draft-edit', $module, 'front');
 }
コード例 #7
0
ファイル: TopicController.php プロジェクト: Andyyang1981/pi
 /**
  * list articles of a topic for users to view
  */
 public function listAction()
 {
     $topic = $this->params('topic', '');
     if (empty($topic)) {
         return $this->jumpTo404(__('Invalid topic ID!'));
     }
     if (is_numeric($topic)) {
         $row = $this->getModel('topic')->find($topic);
     } else {
         $row = $this->getModel('topic')->find($topic, 'slug');
     }
     $title = $row->title;
     // Return 503 code if topic is not active
     if (!$row->active) {
         return $this->jumpToException(__('The topic requested is not active'), 503);
     }
     $this->view()->assign('topic', $row->toArray());
     $topicId = $row->id;
     $page = $this->params('p', 1);
     $page = $page > 0 ? $page : 1;
     $module = $this->getModule();
     $config = Pi::config('', $module);
     $limit = (int) $config['page_limit_all'];
     // Getting relations
     $modelRelation = $this->getModel('article_topic');
     $rowRelation = $modelRelation->select(array('topic' => $topicId));
     $articleIds = array(0);
     $lastAdded = 0;
     foreach ($rowRelation as $row) {
         $articleIds[] = $row['article'];
         if ($row['time'] > $lastAdded) {
             $lastAdded = $row['time'];
         }
     }
     $where = array('id' => $articleIds);
     // Get articles
     $resultsetArticle = Entity::getAvailableArticlePage($where, $page, $limit);
     // Total count
     $where = array_merge($where, array('time_publish <= ?' => time(), 'status' => Article::FIELD_STATUS_PUBLISHED, 'active' => 1));
     $modelArticle = $this->getModel('article');
     $totalCount = $modelArticle->getSearchRowsCount($where);
     // Pagination
     $paginator = Paginator::factory($totalCount, array('limit' => $limit, 'page' => $page, 'url_options' => array('page_param' => 'p', 'params' => array('topic' => $topic, 'list' => 'all'))));
     $this->view()->assign(array('title' => empty($topic) ? __('All') : $title, 'articles' => $resultsetArticle, 'paginator' => $paginator, 'lastAdded' => $lastAdded, 'count' => $totalCount));
 }
コード例 #8
0
ファイル: Block.php プロジェクト: Andyyang1981/pi
 /**
  * List custom articles and with a slideshow besides article list
  * 
  * @param array   $options
  * @param string  $module
  * @return boolean 
  */
 public static function recommendedSlideshow($options = array(), $module = null)
 {
     if (!$module) {
         return false;
     }
     // Getting custom article list
     $columns = array('subject', 'summary', 'time_publish', 'image');
     $ids = explode(',', $options['articles']);
     foreach ($ids as &$id) {
         $id = trim($id);
     }
     $where = array('id' => $ids);
     $articles = Entity::getAvailableArticlePage($where, 1, 10, $columns, null, $module);
     $config = Pi::config('', $module);
     $image = $config['default_feature_thumb'];
     $image = Pi::service('asset')->getModuleAsset($image, $module);
     foreach ($articles as &$article) {
         $article['subject'] = mb_substr($article['subject'], 0, $options['max_subject_length'], 'UTF-8');
         $article['summary'] = mb_substr($article['summary'], 0, $options['max_summary_length'], 'UTF-8');
         $article['image'] = $article['image'] ? Media::getThumbFromOriginal(Pi::url($article['image'])) : $image;
     }
     // Getting image link url
     $urlRows = explode('\\n', $options['image-link']);
     $imageLinks = array();
     foreach ($urlRows as $row) {
         list($id, $url) = explode(':', trim($row), 2);
         $imageLinks[trim($id)] = trim($url);
     }
     // Fetching image ID
     $images = explode(',', $options['images']);
     $imageIds = array();
     foreach ($images as $key => &$image) {
         $image = trim($image);
         if (is_numeric($image)) {
             $imageIds[] = $image;
         } else {
             $url = $image ?: 'image/default-recommended.png';
             $image = array('url' => Pi::service('asset')->getModuleAsset($url, $module), 'link' => $imageLinks[$key + 1], 'title' => _b('This is default recommended image'), 'description' => _b('You should to add your own images and its title and description!'));
         }
     }
     if (!empty($imageIds)) {
         $images = array();
         $rowset = Pi::model('media', $module)->select(array('id' => $imageIds));
         foreach ($rowset as $row) {
             $id = $row['id'];
             $link = isset($imageLinks[$id]) ? $imageLinks[$id] : '';
             $images[] = array('url' => Pi::url($row['url']), 'link' => $link, 'title' => $row['title'], 'description' => $row['description']);
         }
     }
     return array('articles' => $articles, 'target' => $options['target'], 'style' => $options['block-style'], 'elements' => (array) $options['element'], 'height' => $options['image-height'], 'images' => $images, 'config' => Pi::config('', $module), 'rows' => $options['description_rows']);
 }
コード例 #9
0
ファイル: TopicController.php プロジェクト: Andyyang1981/pi
 /**
  * List all articles for pulling
  */
 public function pullAction()
 {
     // Fetch topic details
     $topic = $this->params('topic', '');
     if (empty($topic)) {
         return $this->jumpTo404(_a('Invalid topic ID!'));
     }
     if (is_numeric($topic)) {
         $rowTopic = $this->getModel('topic')->find($topic);
     } else {
         $rowTopic = $this->getModel('topic')->find($topic, 'slug');
     }
     $where = array();
     $page = $this->params('p', 1);
     $limit = $this->params('limit', 20);
     $data = $ids = array();
     $module = $this->getModule();
     $modelArticle = $this->getModel('article');
     $categoryModel = $this->getModel('category');
     $modelRelation = $this->getModel('article_topic');
     // Get topic articles
     $rowRelation = $modelRelation->select(array('topic' => $rowTopic->id));
     $topicArticles = array();
     foreach ($rowRelation as $row) {
         $topicArticles[] = $row['article'];
     }
     // Get category
     $category = $this->params('category', 0);
     if ($category > 1) {
         $categoryIds = $categoryModel->getDescendantIds($category);
         if ($categoryIds) {
             $where['category'] = $categoryIds;
         }
     }
     // Get topic
     $model = $this->getModel('topic');
     $topics = $model->getList();
     // Build where
     $where['status'] = Article::FIELD_STATUS_PUBLISHED;
     $where['active'] = 1;
     $keyword = $this->params('keyword', '');
     if (!empty($keyword)) {
         $where['subject like ?'] = sprintf('%%%s%%', $keyword);
     }
     // Retrieve data
     $data = Entity::getArticlePage($where, $page, $limit);
     // Getting article topic
     $articleIds = array_keys($data);
     if (empty($articleIds)) {
         $articleIds = array(0);
     }
     $rowRelation = $this->getModel('article_topic')->select(array('article' => $articleIds));
     $relation = array();
     foreach ($rowRelation as $row) {
         if (isset($relation[$row['article']])) {
             $relation[$row['article']] .= ',' . $topics[$row['topic']];
         } else {
             $relation[$row['article']] = $topics[$row['topic']];
         }
     }
     // Total count
     $select = $modelArticle->select()->columns(array('total' => new Expression('count(id)')))->where($where);
     $resulsetCount = $modelArticle->selectWith($select);
     $totalCount = (int) $resulsetCount->current()->total;
     // Paginator
     $paginator = Paginator::factory($totalCount, array('limit' => $limit, 'page' => $page, 'url_options' => array('page_param' => 'p', 'params' => array_filter(array('module' => $module, 'controller' => 'topic', 'action' => 'pull', 'topic' => $topic, 'category' => $category, 'keyword' => $keyword)))));
     // Prepare search form
     $form = new SimpleSearchForm();
     $form->setData($this->params()->fromQuery());
     $count = $this->getModel('article_topic')->count(array('topic' => $rowTopic->id));
     $this->view()->assign(array('title' => _a('All Articles'), 'data' => $data, 'form' => $form, 'paginator' => $paginator, 'category' => $category, 'categories' => Pi::api('api', $module)->getCategoryList(), 'action' => 'pull', 'topics' => $topics, 'relation' => $relation, 'topic' => $rowTopic->toArray(), 'pulled' => $topicArticles, 'count' => $count));
 }
コード例 #10
0
ファイル: Topic.php プロジェクト: Andyyang1981/pi
 /**
  * Get top visits in period of topic articles
  * 
  * @param int     $days
  * @param int     $limit
  * @param int     $category
  * @param int     $topic
  * @param string  $module
  * @return array 
  */
 public static function getVisitsRecently($days, $limit = null, $category = null, $topic = null, $module = null)
 {
     $module = $module ?: Pi::service('module')->current();
     $dateTo = time();
     $dateFrom = $dateTo - 24 * 3600 * $days;
     $where = array('active' => 1, 'status' => Article::FIELD_STATUS_PUBLISHED);
     $modelVisit = Pi::model('visit', $module);
     $tableVisit = $modelVisit->getTable();
     $where[$tableVisit . '.time >= ?'] = $dateFrom;
     $where[$tableVisit . '.time < ?'] = $dateTo;
     $modelCategory = Pi::model('category', $module);
     if ($category && $category > 1) {
         $categoryIds = $modelCategory->getDescendantIds($category);
         if ($categoryIds) {
             $where['category'] = $categoryIds;
         }
     }
     if ($topic) {
         $where['r.topic'] = $topic;
     }
     $modelArticle = Pi::model('article', $module);
     $modelRelation = Pi::model('article_topic', $module);
     $tableArticle = $modelArticle->getTable();
     $tableRelation = $modelRelation->getTable();
     $select = $modelVisit->select()->columns(array('article', 'total' => new Expression('count(*)')))->join(array('a' => $tableArticle), sprintf('%s.article = a.id', $tableVisit))->join(array('r' => $tableRelation), 'a.id = r.article')->where($where)->offset(0)->group(array(sprintf('%s.article', $tableVisit)))->limit($limit)->order('total DESC');
     $rowset = $modelVisit->selectWith($select);
     $articleIds = array(0);
     foreach ($rowset as $row) {
         $articleIds[] = $row['article'];
     }
     $where = array('id' => $articleIds);
     return Entity::getAvailableArticlePage($where, 1, $limit, null, null, $module);
 }
コード例 #11
0
 /**
  * List articles of a category
  */
 public function listAction()
 {
     $modelCategory = $this->getModel('category');
     $category = $this->params('category', '');
     $categoryId = is_numeric($category) ? (int) $category : $modelCategory->slugToId($category);
     $page = $this->params('p', 1);
     $page = $page > 0 ? $page : 1;
     $module = $this->getModule();
     $config = Pi::config('', $module);
     $limit = (int) $config['page_limit_all'] ?: 40;
     $where = array();
     $route = Pi::api('api', $module)->getRouteName();
     // Get category nav
     $rowset = Pi::model('category', $module)->enumerate(null, null);
     $rowset = array_shift($rowset);
     $navs = $this->canonizeCategory($rowset['child'], $route);
     $allNav['all'] = array('label' => __('All'), 'route' => $route, 'controller' => 'list', 'params' => array('category' => 'all'));
     $navs = $allNav + $navs;
     // Get all categories
     $categories = array('all' => array('id' => 0, 'title' => __('All articles'), 'image' => '', 'url' => Pi::service('url')->assemble(Pi::api('api', $module)->getRouteName($module), array('controller' => 'list', 'action' => 'all', 'list' => 'all'))));
     $rowset = Pi::model('category', $module)->enumerate(null, null, true);
     foreach ($rowset as $row) {
         if ('root' == $row['name']) {
             continue;
         }
         $url = Pi::service('url')->assemble('', array('controller' => 'category', 'action' => 'list', 'category' => $row['id']));
         $categories[$row['id']] = array('id' => $row['id'], 'title' => $row['title'], 'image' => $row['image'], 'url' => $url);
     }
     $categoryIds = $modelCategory->getDescendantIds($categoryId);
     if (empty($categoryIds)) {
         return $this->jumpTo404(__('Invalid category id'));
     }
     $where['category'] = $categoryIds;
     $categoryInfo = $categories[$categoryId];
     // Get subcategories article count
     $modelArticle = $this->getModel('article');
     $select = $modelArticle->select()->where(array('category' => $categoryIds, 'active' => 1, 'time_publish < ?' => time()))->columns(array('category', 'count' => new Expression('count(*)')))->group(array('category'));
     $resultCount = $modelArticle->selectWith($select);
     $counts = array();
     foreach ($resultCount as $row) {
         $counts[$row['category']] = $row['count'];
     }
     // Get articles
     $columns = array('id', 'subject', 'time_publish', 'category', 'summary', 'author', 'image');
     $resultsetArticle = Entity::getAvailableArticlePage($where, $page, $limit, $columns, null, $module);
     $articleCategoryIds = $authorIds = array();
     foreach ($resultsetArticle as $row) {
         $authorIds[] = $row['author'];
         $articleCategoryIds[] = $row['category'];
     }
     // Get author
     $authors = array();
     if (!empty($authorIds)) {
         $rowAuthor = $this->getModel('author')->select(array('id' => $authorIds));
         foreach ($rowAuthor as $row) {
             $authors[$row->id] = $row->toArray();
         }
     }
     // Total count
     $where = array_merge($where, array('time_publish <= ?' => time(), 'status' => Article::FIELD_STATUS_PUBLISHED, 'active' => 1));
     $modelArticle = $this->getModel('article');
     $totalCount = $modelArticle->getSearchRowsCount($where);
     // Pagination
     $paginator = Paginator::factory($totalCount, array('limit' => $limit, 'page' => $page, 'url_options' => array('page_param' => 'p', 'params' => array('category' => $category))));
     $module = $this->getModule();
     $config = Pi::config('', $module);
     $this->view()->assign(array('title' => __('Article List in Category'), 'articles' => $resultsetArticle, 'paginator' => $paginator, 'categories' => $categories, 'categoryInfo' => $categoryInfo, 'category' => $category, 'p' => $page, 'config' => $config, 'counts' => $counts, 'categoryId' => array_shift($categoryIds), 'subCategoryId' => $categoryIds, 'route' => $route, 'elements' => $config['list_item'], 'authors' => $authors, 'length' => $config['list_summary_length'], 'navs' => $this->config('enable_list_nav') ? $navs : ''));
     $this->view()->viewModel()->getRoot()->setVariables(array('breadCrumbs' => true, 'Tag' => $categoryInfo['title']));
 }
コード例 #12
0
ファイル: ArticleController.php プロジェクト: Andyyang1981/pi
 /**
  * Get article by title via AJAX.
  * 
  * @return ViewModel 
  */
 public function getFuzzyArticleAction()
 {
     Pi::service('log')->mute();
     $articles = array();
     $pageCount = $total = 0;
     $module = $this->getModule();
     $where = array('status' => Article::FIELD_STATUS_PUBLISHED);
     $keyword = $this->params('keyword', '');
     $type = $this->params('type', 'title');
     $limit = $this->params('limit', 10);
     $limit = $limit > 100 ? 100 : $limit;
     $page = $this->params('page', 1);
     $exclude = $this->params('exclude', 0);
     $offset = $limit * ($page - 1);
     $articleModel = $this->getModel('article');
     if (strcasecmp('tag', $type) == 0) {
         if ($keyword) {
             $total = Pi::service('tag')->getCount($keyword, $module);
             $pageCount = ceil($total / $limit);
             // Get article ids
             $articleIds = Pi::service('tag')->getList($keyword, $module, '', $limit, $offset);
             if ($articleIds) {
                 $where['id'] = $articleIds;
                 $articles = array_flip($articleIds);
                 // Get articles
                 $resultsetArticle = Entity::getArticlePage($where, 1, $limit, null, null, $module);
                 foreach ($resultsetArticle as $key => $val) {
                     $articles[$key] = $val;
                 }
                 $articles = array_filter($articles, function ($var) {
                     return is_array($var);
                 });
             }
         }
     } else {
         // Get resultset
         if ($keyword) {
             $where['subject like ?'] = sprintf('%%%s%%', $keyword);
         }
         $articles = Entity::getArticlePage($where, $page, $limit);
         // Get total
         $total = $articleModel->getSearchRowsCount($where);
         $pageCount = ceil($total / $limit);
     }
     foreach ($articles as $key => &$article) {
         if ($exclude && $exclude == $key) {
             unset($articles[$key]);
         }
         $article['time_publish_text'] = date('Y-m-d', $article['time_publish']);
     }
     echo json_encode(array('status' => true, 'message' => __('OK'), 'data' => array_values($articles), 'paginator' => array('currentPage' => $page, 'pageCount' => $pageCount, 'keyword' => $keyword, 'type' => $type, 'limit' => $limit, 'totalCount' => $total)));
     exit;
 }