Кастомный метод который выводит года и месяца в которых есть новости (пример
http://test.thecoder.ru/novosti/)
public function archive_news(){
// GET - параметр year
$get_year = (int) getRequest('year');
// page_id Ленты новостей
$parent_id = 6;
// Ищем самую позднею новость
$news_last = new selector('pages');
$news_last->types('hierarchy-type')->name('news', 'item');
$news_last->where('hierarchy')->page($parent_id)->childs(1);
$news_last->order('publish_time')->desc();
$news_last->limit(0, 1);
$news_last_element = $news_last->first;
// Самый последний Год
$last_year = $news_last_element->publish_time->getFormattedDate('Y');
// Ищем самую раннею новость
$news_first = new selector('pages');
$news_first->types('hierarchy-type')->name('news', 'item');
$news_first->where('hierarchy')->page($parent_id)->childs(1);
$news_first->order('publish_time')->asc();
$news_first->limit(0, 1);
$news_first_element = $news_first->first;
// Самый первый Год, может совпадать с последним
$first_year = $news_first_element->publish_time->getFormattedDate('Y');
//Итоговый массив
$block_arr = array();
$arr_year = array();
$lines_arr = array();
for($i = $last_year; $i >= $first_year; $i--){
$year_id = (int) $i;
$line_arr_year = array();
$line_arr_year['node:text'] = $year_id;
$line_arr_year['attribute:link'] = '?year=' . $i;
if($get_year == $year_id) $line_arr_year['attribute:active'] = 1;
$lines_arr[] = $line_arr_year;
}
$arr_year['nodes:year'] = $lines_arr;
$block_arr['years'] = $arr_year;
// GET - параметр month
$get_month = (int) getRequest('month');
//составление sql запроса выборки всех новостей за указанный год в GET-параметре year
if($get_year){
$date1 = mktime(0, 0, 0, 1, 1, $get_year);
$date2 = mktime(23, 59, 59, 12, 31, $get_year);
$news = new selector('pages');
$news->types('hierarchy-type')->name('news', 'item');
$news->where('hierarchy')->page($parent_id)->childs(1);
$news->where('publish_time')->between($date1, $date2);
$news->order('publish_time')->asc();
$sql = $news->query();
// подменяем данные в sql запросе, чтобы нам вернулись только id месяцев
$sql = str_replace("SQL_CALC_FOUND_ROWS h.id as id, h.rel as pid", "FROM_UNIXTIME(oc_120.int_val, '%c')", $sql);
$result = l_mysql_query($sql);
$months = array();
while ($row = mysql_fetch_row($result)) {
list($month_id) = $row;
if(!in_array($month_id, $months)) $months[] = intval($month_id);
}
$months = array_unique($months);
$arr_month = array();
$lines = array();
$months_ru = array(1 => 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноября', 'Декабрь');
foreach($months as $item){
$line_arr = array();
$line_arr['node:text'] = $months_ru[$item];
$line_arr['attribute:id'] = $item;
$line_arr['attribute:link'] = '?year=' . $get_year . '&month=' . $item;
if($get_month == $item) $line_arr['attribute:active'] = 1;
$lines[] = $line_arr;
}
$arr_month['nodes:month'] = $lines;
$block_arr['months'] = $arr_month;
}
// $block_arr - итоговый php-массив, который будет преобразован в XML
return $block_arr;
}
};
Вызов кастома <xsl:apply-templates select="document('udata://custom/archive_news/')/udata" />
Шаблон:
<xsl:template match="udata[@method = 'archive_news']">
<h3>Архив</h3>
<dl class="archive">
<dt><ul>
<xsl:apply-templates select="//year" />
</ul>
</dt>
<dd>
<ul>
<xsl:apply-templates select="//month" />
</ul>
</dd>
</dl>
</xsl:template>
<xsl:template match="year|month">
<li>
<xsl:if test="@active = 1">
<xsl:attribute name="class">active</xsl:attribute>
</xsl:if>
<a href="{@link}"><xsl:value-of select="." /></a>
</li>
</xsl:template>