Frontend\Modules\Blog\Engine\Model::getAllForDateRange PHP Method

getAllForDateRange() public static method

Get all items between a start and end-date
public static getAllForDateRange ( integer $start, integer $end, integer $limit = 10, integer $offset ) : array
$start integer The start date as a UNIX-timestamp.
$end integer The end date as a UNIX-timestamp.
$limit integer The number of items to get.
$offset integer The offset.
return array
    public static function getAllForDateRange($start, $end, $limit = 10, $offset = 0)
    {
        $start = (int) $start;
        $end = (int) $end;
        $limit = (int) $limit;
        $offset = (int) $offset;
        // get the items
        $items = (array) FrontendModel::getContainer()->get('database')->getRecords('SELECT i.id, i.revision_id, i.language, i.title, i.introduction, i.text, i.num_comments AS comments_count,
             c.title AS category_title, m2.url AS category_url, i.image,
             UNIX_TIMESTAMP(i.publish_on) AS publish_on, i.user_id, i.allow_comments,
             m.url
             FROM blog_posts AS i
             INNER JOIN blog_categories AS c ON i.category_id = c.id
             INNER JOIN meta AS m ON i.meta_id = m.id
             INNER JOIN meta AS m2 ON c.meta_id = m2.id
             WHERE i.status = ? AND i.language = ? AND i.hidden = ? AND i.publish_on BETWEEN ? AND ?
             ORDER BY i.publish_on DESC
             LIMIT ?, ?', array('active', LANGUAGE, 'N', FrontendModel::getUTCDate('Y-m-d H:i', $start), FrontendModel::getUTCDate('Y-m-d H:i', $end), $offset, $limit), 'id');
        // no results?
        if (empty($items)) {
            return array();
        }
        // init var
        $link = FrontendNavigation::getURLForBlock('Blog', 'Detail');
        $folders = FrontendModel::getThumbnailFolders(FRONTEND_FILES_PATH . '/Blog/Images', true);
        // loop
        foreach ($items as $key => $row) {
            // URLs
            $items[$key]['full_url'] = $link . '/' . $row['url'];
            // comments
            if ($row['comments_count'] > 0) {
                $items[$key]['comments'] = true;
            }
            if ($row['comments_count'] > 1) {
                $items[$key]['comments_multiple'] = true;
            }
            // allow comments as boolean
            $items[$key]['allow_comments'] = $row['allow_comments'] == 'Y';
            // reset allow comments
            if (!FrontendModel::get('fork.settings')->get('Blog', 'allow_comments')) {
                $items[$key]['allow_comments'] = false;
            }
            // image?
            if (isset($row['image'])) {
                foreach ($folders as $folder) {
                    $items[$key]['image_' . $folder['dirname']] = $folder['url'] . '/' . $folder['dirname'] . '/' . $row['image'];
                }
            }
        }
        // get all tags
        $tags = FrontendTagsModel::getForMultipleItems('Blog', array_keys($items));
        // loop tags and add to correct item
        foreach ($tags as $postId => $data) {
            $items[$postId]['tags'] = $data;
        }
        // return
        return $items;
    }

Usage Example

Example #1
0
 /**
  * Load the data, don't forget to validate the incoming data
  */
 private function getData()
 {
     // get parameters
     $this->year = $this->URL->getParameter(1);
     $this->month = $this->URL->getParameter(2);
     // redirect /2010/6 to /2010/06 to avoid duplicate content
     if ($this->month !== null && mb_strlen($this->month) != 2) {
         $queryString = isset($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '';
         $this->redirect(FrontendNavigation::getURLForBlock('Blog', 'Archive') . '/' . $this->year . '/' . str_pad($this->month, 2, '0', STR_PAD_LEFT) . $queryString, 301);
     }
     if (mb_strlen($this->year) != 4) {
         $this->redirect(FrontendNavigation::getURL(404));
     }
     // redefine
     $this->year = (int) $this->year;
     if ($this->month !== null) {
         $this->month = (int) $this->month;
     }
     // validate parameters
     if ($this->year == 0 || $this->month === 0) {
         $this->redirect(FrontendNavigation::getURL(404));
     }
     // requested page
     $requestedPage = $this->URL->getParameter('page', 'int', 1);
     // rebuild url
     $url = $this->year;
     // build timestamp
     if ($this->month !== null) {
         $this->startDate = gmmktime(00, 00, 00, $this->month, 01, $this->year);
         $this->endDate = gmmktime(23, 59, 59, $this->month, gmdate('t', $this->startDate), $this->year);
         $url .= '/' . str_pad($this->month, 2, '0', STR_PAD_LEFT);
     } else {
         // year
         $this->startDate = gmmktime(00, 00, 00, 01, 01, $this->year);
         $this->endDate = gmmktime(23, 59, 59, 12, 31, $this->year);
     }
     // set URL and limit
     $this->pagination['url'] = FrontendNavigation::getURLForBlock('Blog', 'Archive') . '/' . $url;
     $this->pagination['limit'] = $this->get('fork.settings')->get('Blog', 'overview_num_items', 10);
     // populate count fields in pagination
     $this->pagination['num_items'] = FrontendBlogModel::getAllForDateRangeCount($this->startDate, $this->endDate);
     $this->pagination['num_pages'] = (int) ceil($this->pagination['num_items'] / $this->pagination['limit']);
     // redirect if the request page doesn't exists
     if ($requestedPage > $this->pagination['num_pages'] || $requestedPage < 1) {
         $this->redirect(FrontendNavigation::getURL(404));
     }
     // populate calculated fields in pagination
     $this->pagination['requested_page'] = $requestedPage;
     $this->pagination['offset'] = $this->pagination['requested_page'] * $this->pagination['limit'] - $this->pagination['limit'];
     // get articles
     $this->items = FrontendBlogModel::getAllForDateRange($this->startDate, $this->endDate, $this->pagination['limit'], $this->pagination['offset']);
 }