1<?php
2/**
3 * Copyright (C) 2008-2011 e107 Inc (e107.org), Licensed under GNU GPL (http://www.gnu.org/licenses/gpl.txt)
4 * $Id$
5 *
6 * News by month menu
7 */
8
9if (!defined('e107_INIT')) { exit; }
10
11$cString = 'nq_news_months_menu_'.md5(serialize($parm).USERCLASS_LIST.e_LANGUAGE);
12$cached = e107::getCache()->retrieve($cString);
13
14if(!empty($parm))
15{
16	if(is_string($parm))
17	{
18		parse_str($parm, $parms);
19	}
20	else
21	{
22		$parms = $parm;
23	}
24
25}
26
27
28if(false === $cached)
29{
30	if(!function_exists('newsFormatDate'))
31	{
32		function newsFormatDate($year, $month, $day = "") {
33			$date = $year;
34			$date .= (strlen($month) < 2)?"0".$month:
35			$month;
36			$date .= (strlen($day) < 2 && $day != "")?"0".$day:
37			$day;
38			return $date;
39		}
40	}
41
42	if(!isset($parms['showarchive']))
43	{
44		$parms['showarchive'] = 0;
45	}
46
47
48//	e107::lan('blogcalendar_menu', e_LANGUAGE); // FIXME decide on language file structure (#743)
49	e107::includeLan(e_PLUGIN.'blogcalendar_menu/languages/'.e_LANGUAGE.'.php');
50
51	$tp = e107::getParser();
52	$sql = e107::getDb();
53
54	$marray  = e107::getDate()->terms('month');
55
56
57	//$parms['year'] = "2011 0";
58	if(vartrue($parms['year']))
59	{
60		$date = $parms['year'];
61		list($cur_year, $cur_month) = explode(" ", date($date));
62		$start = mktime(0, 0, 0, 1, 1, $cur_year);
63		$end = mktime(23, 59, 59, 12, 31, $cur_year);
64	}
65	else
66	{
67		$date = "Y n";
68		list($cur_year, $cur_month) = explode(" ", date($date));
69		$start = mktime(0, 0, 0, 1, 1, $cur_year);
70		$end = time();
71	}
72
73	$req_year = $cur_year;
74	if(e_PAGE == 'news.php' && strstr(e_QUERY, "month"))
75	{
76		$tmp = explode('.', e_QUERY);
77		$item = $tmp[1];
78		$req_month = intval(substr($item, 4, 2));
79		$req = 'month';
80	}
81	else
82	{
83		$req_month = $cur_month;
84	}
85
86	$xmonth_cnt = array();
87	$month_links = array();
88
89	e107::getDebug()->logTime('News months menu');
90	if(!$sql->select("news", "news_id, news_datestamp", "news_class IN (".USERCLASS_LIST.") AND (FIND_IN_SET('0', news_render_type) OR FIND_IN_SET(1, news_render_type)) AND news_datestamp > ".intval($start)." AND news_datestamp < ".intval($end)." ORDER BY news_datestamp DESC"))
91	{
92		e107::getCache()->set($cString, '');
93		return '';
94	}
95	while ($news = $sql->fetch())
96	{
97		$xmonth = date("n", $news['news_datestamp']);
98		if ((!isset($month_links[$xmonth]) || !$month_links[$xmonth]))
99		{
100			$xmonth_cnt[$xmonth] = 0;
101			$month_links[$xmonth] = e107::getUrl()->create('news/list/month', 'id='.newsFormatDate($req_year, $xmonth));
102		}
103		$xmonth_cnt[$xmonth]++;
104	}
105
106
107	e107::getDebug()->log($month_links);
108
109	// go over the link array and create the option fields
110	$menu_text = array();
111	$template = e107::getTemplate('news', 'news_menu', 'months', true, true);
112	$bullet = defined('BULLET') ? THEME_ABS.'images/'.BULLET : THEME_ABS.'images/bullet2.gif';
113	$vars = new e_vars(array('bullet' => $bullet));
114	foreach($month_links as $index => $val)
115	{
116		$vars->addData(array(
117			'active' => $index == $req_month ? " active" : '',
118			'url' => $val,
119			'month' => $marray[$index],
120			'count' => $xmonth_cnt[$index],
121		));
122		$menu_text[] = $tp->simpleParse($template['item'], $vars);
123	}
124	$cached = $template['start'].implode(varset($template['separator'],''), $menu_text).$template['end'];
125
126	$ns->setContent('text', $cached);
127
128	if($cached)
129	{
130		if(!$parms['showarchive'])
131		{
132			if(isset($template['footer']))
133			{
134				$footer = $tp->replaceConstants($template['footer'],'abs');
135				$footer = $tp->parseTemplate($footer,true);
136				$ns->setUniqueId('news-months-menu')->setContent('footer', $footer);
137			}
138			else
139			{
140				$footer = '<div class="e-menu-link news-menu-archive"><a class="btn btn-default btn-secondary btn-sm" href="'.e_PLUGIN_ABS.'blogcalendar_menu/archive.php">'.BLOGCAL_L2.'</a></div>';
141				$cached .= $footer;
142			}
143
144		}
145
146		$cached = $ns->tablerender(BLOGCAL_L1." ".$req_year, $cached, 'news_months_menu', true);
147		$ns->setUniqueId(null);
148
149
150	}
151	e107::getCache()->set($cString, $cached);
152}
153
154echo $cached;