diff --git a/_extensions/s2_blog/_include/Lib.php b/_extensions/s2_blog/_include/Lib.php index aaa03e18..3902523a 100644 --- a/_extensions/s2_blog/_include/Lib.php +++ b/_extensions/s2_blog/_include/Lib.php @@ -17,10 +17,10 @@ class Lib // '1' -> '01' public static function extend_number ($n) { - return strlen($n) == 1 ? '0'.$n : $n; + return \strlen($n) === 1 ? '0'.$n : $n; } - public static function calendar ($year, $month, $day, $url = '', $day_flags = null) + public static function calendar($year, $month, $day, $url = '', $dayUrls = null) { /** @var DbLayer $s2_db */ $s2_db = \Container::get(DbLayer::class); @@ -32,29 +32,30 @@ public static function calendar ($year, $month, $day, $url = '', $day_flags = nu $end_time = mktime(0, 0, 0, $month + 1, 1, $year); // Dealing with week days - $n = date('w', $start_time); - if (Lang::get('Sunday starts week', 's2_blog') != '1') - { - $n -= 1; - if ($n == -1) $n = 6; - } + $n = (int)date('w', $start_time); + if (Lang::get('Sunday starts week', 's2_blog') !== '1') { + --$n; + if ($n === -1) { + $n = 6; + } + } // How many days have the month? $day_count = (int) date('j', mktime(0, 0, 0, $month + 1, 0, $year)); // day = 0 // Flags for the days when posts have been written - if ($day_flags === null) - { - $day_flags = []; - $query = array( - 'SELECT' => 'create_time', + if ($dayUrls === null) { + $dayUrls = []; + $query = array( + 'SELECT' => 'create_time, url', 'FROM' => 's2_blog_posts', 'WHERE' => 'create_time < '.$end_time.' AND create_time >= '.$start_time.' AND published = 1' ); ($hook = s2_hook('fn_s2_blog_calendar_pre_get_days_qr')) ? eval($hook) : null; $result = $s2_db->buildAndQuery($query); - while ($row = $s2_db->fetchRow($result)) - $day_flags[1 + intval(($row[0] - $start_time) / 86400)] = 1; + while ($row = $s2_db->fetchRow($result)) { + $dayUrls[1 + (int)(($row[0] - $start_time) / 86400)][] = $row[1]; + } } // Header @@ -62,8 +63,9 @@ public static function calendar ($year, $month, $day, $url = '', $day_flags = nu if ($day == '-1') { // One of 12 year tables - if ($start_time < time()) - $month_name = ''.$month_name.''; + if ($start_time < time()) { + $month_name = '' . $month_name . ''; + } $header = ''.$month_name.''; } else @@ -92,8 +94,16 @@ public static function calendar ($year, $month, $day, $url = '', $day_flags = nu for ($i = 1; $i <= $day_count; $i++) { $n++; - // Are there posts? - $b = isset($day_flags[$i]) ? ''.$i.'' : $i; + if (!isset($dayUrls[$i])) { + // No posts + $b = $i; + } elseif (\count($dayUrls[$i]) !== 1) { + // Several posts, link to the day page + $b = '' . $i . ''; + } else { + // One post, link to it + $b = '' . $i . ''; + } $classes = array(); if ($i == $day) // Current day diff --git a/_extensions/s2_blog/_include/Page/Year.php b/_extensions/s2_blog/_include/Page/Year.php index 187f4573..e9b83d3a 100644 --- a/_extensions/s2_blog/_include/Page/Year.php +++ b/_extensions/s2_blog/_include/Page/Year.php @@ -65,20 +65,22 @@ private function year_posts ($year) } $query = array( - 'SELECT' => 'create_time', + 'SELECT' => 'create_time, url', 'FROM' => 's2_blog_posts', 'WHERE' => 'create_time < '.$end_time.' AND create_time >= '.$start_time.' AND published = 1' ); ($hook = s2_hook('fn_s2_blog_year_posts_pre_get_days_qr')) ? eval($hook) : null; $result = $s2_db->buildAndQuery($query); - $day_flags = array_fill(1, 12, array()); - while ($row = $s2_db->fetchRow($result)) - $day_flags[(int) date('m', $row[0])][(int) date('j', $row[0])] = 1; + $dayUrlsArray = array_fill(1, 12, []); + while ($row = $s2_db->fetchRow($result)) { + $dayUrlsArray[(int)date('m', $row[0])][(int)date('j', $row[0])][] = $row[1]; + } - $content = array(); - for ($i = 1; $i <= 12; $i++) - $content[] = Lib::calendar($year, Lib::extend_number($i), '-1', '', $day_flags[$i]); + $content = []; + for ($i = 1; $i <= 12; $i++) { + $content[] = Lib::calendar($year, Lib::extend_number($i), '-1', '', $dayUrlsArray[$i]); + } $page['text'] = $this->renderPartial('year', array( 'content' => $content