예제 #1
0
 /**
  * Gets all news articles titlelink.
  *
  * NOTE: Since this function only returns titlelinks for use with the object model it does not exclude articles that are password protected via a category
  *
  *
  * @param int $articles_per_page The number of articles to get
  * @param string $published "published" for an published articles,
  * 													"unpublished" for an unpublised articles,
  * 													"published-unpublished" for published articles only from an unpublished category,
  * 													"sticky" for sticky articles (published or not!) for admin page use only,
  * 													"all" for all articles
  * @param boolean $ignorepagination Since also used for the news loop this function automatically paginates the results if the "page" GET variable is set. To avoid this behaviour if using it directly to get articles set this TRUE (default FALSE)
  * @param string $sortorder "date" (default), "title", "id, "popular", "mostrated", "toprated", "random"
  * 													This parameter is not used for date archives
  * @param bool $sortdirection TRUE for descending, FALSE for ascending. Note: This parameter is not used for date archives
  * @param bool $sticky set to true to place "sticky" articles at the front of the list.
  * @return array
  */
 function getArticles($articles_per_page = 0, $published = NULL, $ignorepagination = false, $sortorder = NULL, $sortdirection = NULL, $sticky = NULL, $category = NULL)
 {
     global $_zp_current_category, $_zp_post_date, $_zp_newsCache;
     if (empty($published)) {
         if (zp_loggedin() || $category && $category->isMyItem(ZENPAGE_NEWS_RIGHTS)) {
             $published = "all";
         } else {
             $published = "published";
         }
     }
     if ($category) {
         $sortObj = $category;
     } else {
         $sortObj = $this;
     }
     if (is_null($sticky)) {
         $sticky = $sortObj->getSortSticky();
     }
     if (is_null($sortdirection)) {
         $sortdirection = $sortObj->getSortDirection('news');
     }
     if (is_null($sortorder)) {
         $sortorder = $sortObj->getSortType('news');
     }
     $newsCacheIndex = "{$sortorder}-{$sortdirection}-{$published}-" . (bool) $sticky;
     if ($category) {
         $newsCacheIndex .= '-' . $category->getTitlelink();
     }
     if (isset($_zp_newsCache[$newsCacheIndex])) {
         $result = $_zp_newsCache[$newsCacheIndex];
     } else {
         $show = $currentcategory = false;
         if ($category) {
             if (is_object($_zp_current_category)) {
                 $currentcategory = $_zp_current_category->getTitlelink();
             }
             $showConjunction = ' AND ';
             // new code to get nested cats
             $catid = $category->getID();
             $subcats = $category->getSubCategories();
             if ($subcats) {
                 $cat = " (cat.cat_id = '" . $catid . "'";
                 foreach ($subcats as $subcat) {
                     $subcatobj = new ZenpageCategory($subcat);
                     $cat .= "OR cat.cat_id = '" . $subcatobj->getID() . "' ";
                 }
                 $cat .= ") AND cat.news_id = news.id ";
             } else {
                 $cat = " cat.cat_id = '" . $catid . "' AND cat.news_id = news.id ";
             }
         } else {
             $showConjunction = ' WHERE ';
         }
         if ($sticky) {
             $sticky = 'sticky DESC,';
         }
         if ($sortdirection) {
             $dir = " DESC";
         } else {
             $dir = " ASC";
         }
         // sortorder and sortdirection (only used for all news articles and categories naturally)
         switch ($sortorder) {
             case "date":
             default:
                 $sort1 = "date" . $dir;
                 break;
             case 'lastchange':
                 $sort1 = 'lastchange' . $dir;
                 break;
             case "id":
                 $sort1 = "id" . $dir;
                 break;
             case "title":
                 $sort1 = "title" . $dir;
                 break;
             case "popular":
                 $sort1 = 'hitcounter' . $dir;
                 break;
             case "mostrated":
                 $sort1 = 'total_votes' . $dir;
                 break;
             case "toprated":
                 $sort1 = '(total_value/total_votes) DESC, total_value';
                 break;
             case "random":
                 $sort1 = 'RAND()';
                 break;
         }
         /** get all articles * */
         switch ($published) {
             case "published":
                 $show = "{$showConjunction} `show` = 1 AND date <= '" . date('Y-m-d H:i:s') . "'";
                 $getUnpublished = false;
                 break;
             case "published-unpublished":
                 $show = "{$showConjunction} `show` = 1 AND date <= '" . date('Y-m-d H:i:s') . "'";
                 $getUnpublished = true;
                 break;
             case "unpublished":
                 $show = "{$showConjunction} `show` = 0 AND date <= '" . date('Y-m-d H:i:s') . "'";
                 $getUnpublished = true;
                 break;
             case 'sticky':
                 $show = "{$showConjunction} `sticky` <> 0";
                 $getUnpublished = true;
                 break;
             case "all":
                 $getUnpublished = true;
                 $show = false;
                 break;
         }
         $order = " ORDER BY {$sticky}";
         if (in_context(ZP_ZENPAGE_NEWS_DATE)) {
             $datesearch = '';
             switch ($published) {
                 case "published":
                     $datesearch = "date LIKE '{$_zp_post_date}%' ";
                     break;
                 case "unpublished":
                     $datesearch = "date LIKE '{$_zp_post_date}%' ";
                     break;
                 case "all":
                     $datesearch = "date LIKE '{$_zp_post_date}%' ";
                     break;
             }
             if ($datesearch) {
                 if ($show) {
                     $datesearch = ' AND ' . $datesearch;
                 } else {
                     $datesearch = ' WHERE ' . $datesearch;
                 }
             }
             $order .= " date DESC";
         } else {
             $datesearch = "";
             if ($category) {
                 $order .= ' news.';
             } else {
                 $order .= ' ';
             }
             $order .= $sort1;
         }
         if ($category) {
             $sql = "SELECT DISTINCT news.date, news.title, news.titlelink, news.sticky FROM " . prefix('news') . " as news, " . prefix('news2cat') . " as cat WHERE" . $cat . $show . $order;
         } else {
             $sql = "SELECT date, title, titlelink, sticky FROM " . prefix('news') . $show . $datesearch . " " . $order;
         }
         $resource = query($sql);
         $result = array();
         if ($resource) {
             while ($item = db_fetch_assoc($resource)) {
                 $article = new ZenpageNews($item['titlelink']);
                 if ($getUnpublished || $article->isMyItem(ZENPAGE_NEWS_RIGHTS) || $currentcategory && $article->inNewsCategory($currentcategory) || $article->categoryIsVisible()) {
                     $result[] = $item;
                 }
             }
             db_free_result($resource);
             if ($sortorder == 'title') {
                 // multi-lingual field!
                 $result = sortByMultilingual($result, 'title', $sortdirection);
                 if ($sticky) {
                     $stickyItems = array();
                     foreach ($result as $key => $element) {
                         if ($element['sticky']) {
                             array_unshift($stickyItems, $element);
                             unset($result[$key]);
                         }
                     }
                     $stickyItems = sortMultiArray($stickyItems, 'sticky', true);
                     $result = array_merge($stickyItems, $result);
                 }
             }
         }
         $_zp_newsCache[$newsCacheIndex] = $result;
     }
     if ($articles_per_page) {
         if ($ignorepagination) {
             $offset = 0;
         } else {
             $offset = self::getOffset($articles_per_page);
         }
         $result = array_slice($result, $offset, $articles_per_page);
     }
     return $result;
 }
 /**
  * Gets news articles titlelinks this category is attached to
  *
  * NOTE: Since this function only returns titlelinks for use with the object model it does not exclude articles that are password protected via a category
  *
  *
  * @param int $articles_per_page The number of articles to get
  * @param string $published "published" for published articles
  *													"published-unpublished" for published articles only from an unpublished category,
  * 													"unpublished" for unpublished articles,
  * 													"sticky" for sticky articles (published or not!) for Admin page use only,
  * 													"all" for all articles
  * @param boolean $ignorepagination Since also used for the news loop this function automatically paginates the results if the "page" GET variable is set. To avoid this behaviour if using it directly to get articles set this TRUE (default FALSE)
  * @param string $sortorder "date" for sorting by date (default)
  * 													"title" for sorting by title
  * 													This parameter is not used for date archives
  * @param string $sortdirection "desc" (default) for descending sort order
  * 													    "asc" for ascending sort order
  * 											        This parameter is not used for date archives
  * @param bool $sticky set to true to place "sticky" articles at the front of the list.
  * @return array
  */
 function getArticles($articles_per_page = '', $published = NULL, $ignorepagination = false, $sortorder = "date", $sortdirection = "desc", $sticky = true)
 {
     global $_zp_current_category, $_zp_post_date, $_zp_zenpage;
     $_zp_zenpage->processExpired('news');
     if (empty($published)) {
         $published = "published";
         if (zp_loggedin(ZENPAGE_NEWS_RIGHTS | VIEW_NEWS_RIGHTS)) {
             $published = "all";
         }
     }
     $show = "";
     // new code to get nested cats
     $catid = $this->getID();
     $subcats = $this->getSubCategories();
     if ($subcats) {
         $cat = " (cat.cat_id = '" . $catid . "'";
         foreach ($subcats as $subcat) {
             $subcatobj = new ZenpageCategory($subcat);
             $cat .= "OR cat.cat_id = '" . $subcatobj->getID() . "' ";
         }
         $cat .= ") AND cat.news_id = news.id ";
     } else {
         $cat = " cat.cat_id = '" . $catid . "' AND cat.news_id = news.id ";
     }
     if (in_context(ZP_ZENPAGE_NEWS_DATE)) {
         $postdate = $_zp_post_date;
     } else {
         $postdate = NULL;
     }
     if ($sticky) {
         $sticky = 'sticky DESC,';
     }
     // sortorder and sortdirection
     switch ($sortorder) {
         case "date":
         default:
             $sort1 = "date";
             break;
         case "title":
             $sort1 = "title";
             break;
     }
     switch ($sortdirection) {
         case "desc":
         default:
             $dir = "DESC";
             break;
         case "asc":
             $dir = "ASC";
             $sticky = false;
             //makes no sense
             break;
     }
     /*** get articles by category ***/
     switch ($published) {
         case "published":
             $show = " AND `show` = 1 AND date <= '" . date('Y-m-d H:i:s') . "'";
             $getUnpublished = false;
             break;
         case "published-unpublished":
             $show = " AND `show` = 1 AND date <= '" . date('Y-m-d H:i:s') . "'";
             $getUnpublished = true;
             break;
         case "unpublished":
             $show = " AND `show` = 0 AND date <= '" . date('Y-m-d H:i:s') . "'";
             $getUnpublished = true;
             break;
         case 'sticky':
             $show = ' AND `sticky` <> 0';
             $getUnpublished = true;
             break;
         case "all":
             $getUnpublished = true;
             $show = "";
             break;
     }
     $order = " ORDER BY " . $sticky . "news.{$sort1} {$dir}";
     $sql = "SELECT DISTINCT news.titlelink FROM " . prefix('news') . " as news, " . prefix('news2cat') . " as cat WHERE" . $cat . $show . $order;
     $resource = $result = query($sql);
     if ($resource) {
         if ($ignorepagination) {
             $offset = 0;
         } else {
             $offset = $_zp_zenpage->getOffset($articles_per_page);
         }
         if (is_object($_zp_current_category)) {
             $currentcategory = $_zp_current_category->getTitlelink();
         } else {
             $currentcategory = false;
         }
         $result = array();
         while ($item = db_fetch_assoc($resource)) {
             $article = new ZenpageNews($item['titlelink']);
             if ($getUnpublished || $currentcategory && $article->inNewsCategory($currentcategory) || $article->categoryIsVisible()) {
                 $offset--;
                 if ($offset < 0) {
                     $result[] = $item;
                     if ($articles_per_page && count($result) >= $articles_per_page) {
                         break;
                     }
                 }
             }
         }
     }
     return $result;
 }