コード例 #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
ファイル: 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));
 }
コード例 #4
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));
 }
コード例 #5
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']);
 }
コード例 #6
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);
 }
コード例 #7
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']));
 }