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

getArchiveNumbers() public static method

Get the statistics for the archive
public static getArchiveNumbers ( ) : array
return array
    public static function getArchiveNumbers()
    {
        // grab stats
        $numbers = FrontendModel::getContainer()->get('database')->getPairs('SELECT DATE_FORMAT(i.publish_on, "%Y%m") AS month, COUNT(i.id)
             FROM blog_posts AS i
             INNER JOIN meta AS m ON i.meta_id = m.id
             WHERE i.status = ? AND i.language = ? AND i.hidden = ? AND i.publish_on <= ?
             GROUP BY month', array('active', LANGUAGE, 'N', FrontendModel::getUTCDate('Y-m-d H:i')));
        // init vars
        $stats = array();
        $link = FrontendNavigation::getURLForBlock('Blog', 'Archive');
        $firstYear = (int) date('Y');
        $lastYear = 0;
        // loop the numbers
        foreach ($numbers as $key => $count) {
            // init vars
            $year = mb_substr($key, 0, 4);
            $month = mb_substr($key, 4, 2);
            // reset
            if ($year < $firstYear) {
                $firstYear = $year;
            }
            if ($year > $lastYear) {
                $lastYear = $year;
            }
            // generate timestamp
            $timestamp = gmmktime(00, 00, 00, $month, 01, $year);
            // initialize if needed
            if (!isset($stats[$year])) {
                $stats[$year] = array('url' => $link . '/' . $year, 'label' => $year, 'total' => 0, 'months' => null);
            }
            // increment the total
            $stats[$year]['total'] += (int) $count;
            $stats[$year]['months'][$key] = array('url' => $link . '/' . $year . '/' . $month, 'label' => $timestamp, 'total' => $count);
        }
        // loop years
        for ($i = $firstYear; $i <= $lastYear; ++$i) {
            // year missing
            if (!isset($stats[$i])) {
                $stats[$i] = array('url' => null, 'label' => $i, 'total' => 0, 'months' => null);
            }
        }
        // sort
        krsort($stats);
        // reset stats
        foreach ($stats as &$row) {
            // remove url for empty years
            if ($row['total'] == 0) {
                $row['url'] = null;
            }
            // any months?
            if (!empty($row['months'])) {
                // sort months
                ksort($row['months']);
            }
        }
        // return
        return $stats;
    }

Usage Example

Example #1
0
 /**
  * Parse
  */
 private function parse()
 {
     // we will cache this widget for 24 hours
     $this->tpl->cache(FRONTEND_LANGUAGE . '_blogWidgetArchiveCache', 24 * 60 * 60);
     // if the widget isn't cached, assign the variables
     if (!$this->tpl->isCached(FRONTEND_LANGUAGE . '_blogWidgetArchiveCache')) {
         // get the numbers
         $this->tpl->assign('widgetBlogArchive', FrontendBlogModel::getArchiveNumbers());
     }
 }
All Usage Examples Of Frontend\Modules\Blog\Engine\Model::getArchiveNumbers