<?PHP
/*
=====================================================
 DataLife Engine v9.7 - by DLE Farsi Group 
-----------------------------------------------------
 http://datalifeengine.ir/
-----------------------------------------------------
 Copyright (c) 2006,2012 DLE Farsi Group
=====================================================
*/

if(!defined('DATALIFEENGINE')){
	die("Hacking attempt!");
}

if ($config['allow_calendar'] == "yes" AND $mod == "calendar") {
	$tpl->result['calendar'] = dle_cache("calendar" . $year . $month . $day, $config['skin']);
	
	if( ! $tpl->result['calendar'] ){
		
		$events = array();
		
		$thisdate = date("Y-m-d H:i:s", $_TIME);
		$jmonth = jdate("m", $_TIME);
		$jyear = jdate("Y" , $_TIME);	
		$this_startm = date("Y-m-d H:i:s", jmaketime(0, 0, 0, $jmonth, 1, $jyear));
		$this_endm = date("Y-m-d H:i:s", jmaketime(0, 0, 0, ($jmonth+1), 1, $jyear));
		$startm = date("Y-m-d H:i:s", jmaketime(0, 0, 0, $month, 1, $year));
		$endm = date("Y-m-d H:i:s", jmaketime(0, 0, 0, ($month+1), 1, $year));
		
		if ($year != '' and $month != ''){		
			if (($year == $jyear AND $month > $jmonth) OR ($year > $jyear)) {
				$sql = "";
			} else {
				$sql = "SELECT date FROM " . PREFIX . "_post WHERE date BETWEEN '$startm' AND '$endm' AND approve = '1'";
			}
			$jmonth = $month; 
			$jyear  = $year;
		} else{
			$sql = "SELECT date FROM " . PREFIX . "_post WHERE date BETWEEN '$this_startm' AND '$this_endm' AND approve = '1'";
		}

		if ($sql != "" ) {
			$db->query($sql);
			while($row = $db->get_row()){
				$datetime = strtotime($row['date']) + ($config['date_adjust'] * 60);
				$tday = jdate("d", $datetime);
				$tmonth = jdate("m", $datetime);
				$tyear = jdate("Y", $datetime);
				$events[$tday] = jmaketime(0,0,0,$tmonth,$tday,$tyear);
			}
			$db->free();
		}
		
		$jmonth = intval($jmonth);
		$jyear  = intval($jyear);
		$pre_month = $jmonth-1;
		$next_month = $jmonth+1;	
		$pre_year = $jyear-1;
		$next_year = $jyear+1;
		$prev_of_month = jmaketime(0, 0, 0, ($pre_month == 0 ? "12" : $pre_month), 1, ($pre_month == 0 ? $pre_year : $jyear));
		$next_of_month = jmaketime(0, 0, 0, ($next_month == 13 ? "1" : $next_month), 1, ($next_month == 13 ? $next_year : $jyear));
		$prev_of_year = jmaketime(0, 0, 0, $jmonth, 1, $pre_year);
		$next_of_year = jmaketime(0, 0, 0, $jmonth, 1, $next_year);
		$first_of_month = jmaketime(0, 0, 0, $jmonth, 1, $jyear);
		$maxdays = jdate('t', $first_of_month); //29-31
		$cal_day = 1;
		$weekday = date('w', ($first_of_month + 86400)); //0-6
		
		if ($config['allow_alt_url'] == "yes") {
			$date_link['prev'] = '<a class="monthlink" onclick="doCalendar('.jdate("'m','Y'", $prev_of_month) . '); return false;" href="' . $config['http_home_url'].($pre_month == 0 ? $jyear-1 : $jyear) . '/'.($pre_month == 0 ? "12" : $pre_month) . '/" title="' . $lang['prev_moth'] . '">&laquo;</a>&nbsp;&nbsp;';
			$date_link['next'] = '&nbsp;&nbsp;<a class="monthlink" onclick="doCalendar('.jdate("'m','Y'", $next_of_month) . '); return false;" href="' . $config['http_home_url'] . ($next_month == 13 ? $jyear+1 : $jyear) . '/'.($next_month == 13 ? "1" : $next_month) . '/" title="' . $lang['next_moth'] . '">&raquo;</a>';
			$date_link['prev_y'] = '<a class="monthlink" onclick="doCalendar('.jdate("'m','Y'", $prev_of_year) . '); return false;" href="' . $config['http_home_url']. $pre_year . '/' . $jmonth . '/" title="' . $lang['prev_year'] . '">&lt;</a>&nbsp;&nbsp;&nbsp;&nbsp;';
			$date_link['next_y'] = '&nbsp;&nbsp;&nbsp;&nbsp;<a class="monthlink" onclick="doCalendar('.jdate("'m','Y'", $next_of_year) . '); return false;" href="' . $config['http_home_url']. $next_year . '/' . $jmonth . '/" title="' . $lang['next_year'] . '">&gt;</a>';
		} else {
			$date_link['prev'] = '<a class="monthlink" onclick="doCalendar('.jdate("'m','Y'", $prev_of_month) . '); return false;" href="' . $PHP_SELF . '?year='.($pre_month == 0 ? $jyear-1 : $jyear) . '&month='.($pre_month == 0 ? "12" : $pre_month) . '" title="' . $lang['prev_moth'] . '">&laquo;</a>&nbsp;&nbsp;';
			$date_link['next'] = '&nbsp;&nbsp;<a class="monthlink" onclick="doCalendar('.jdate("'m','Y'", $next_of_month) . '); return false;" href="' . $PHP_SELF . '?year='.($next_month == 13 ? $jyear+1 : $jyear) . '&month='.($next_month == 13 ? "1" : $next_month) . '" title="' . $lang['next_moth'] . '">&raquo;</a>';
			$date_link['prev_y'] = '<a class="monthlink" onclick="doCalendar('.jdate("'m','Y'", $prev_of_year) . '); return false;" href="' . $PHP_SELF . '?year=' . $pre_year . '&month=' . $jmonth . '" title="' . $lang['prev_year'] . '">&lt;</a>&nbsp;&nbsp;&nbsp;&nbsp;';
			$date_link['next_y'] = '&nbsp;&nbsp;&nbsp;&nbsp;<a class="monthlink" onclick="doCalendar('.jdate("'m','Y'", $next_of_year) . '); return false;" href="' . $PHP_SELF . '?year=' . $next_year . '&month=' . $jmonth . '" title="' . $lang['next_year'] . '">&gt;</a>';
		}
		
		$buffer  = '<div id="calendar-layer"><table id="calendar" cellpadding="3" class="calendar" width="100%"><caption><strong>' . $date_link['prev_y'] . $date_link['prev'] . $lang['date']['month'][$jmonth] . ' ' . $jyear . $date_link['next'] . $date_link['next_y'] . '</strong></caption><thead><tr align="center">';
		
		for ($it=0; $it<=5; $it++)
			$buffer .= '<th>' . $lang['date']['shortweek'][$it] . '</th>';
		$buffer .= '<th class="weekday">' . $lang['date']['shortweek'][6] . '</th></tr></thead>';
		
		$buffer .= ($weekday > 0 && $weekday <= 6) ? '<tr align="center"><td colspan="' . $weekday . '">&nbsp;</td>' : '<tr align="center">';
		
		list( $todayjyear, $todayjmonth, $todayjday ) = gregorian_to_jalali(date("Y"), date("m"), date("d"));
		$days = 1;

		while($maxdays >= $cal_day) {
			if ($weekday == 7) {
				$buffer .= '</tr><tr align="center">';
				$weekday = 0;
			}
			
			if ($days < 10) $days = "0" . $days;

			if (isset($events[$days])){

				$date['title'] = langdate('d F Y', $events[$days]);			

				if ($weekday == '6'){

					$class = $todayjday == $days && ($jmonth == $todayjmonth) && ($jyear == $todayjyear) ? 'day-current' : 'day-active';
				
					if ($config['allow_alt_url'] == "yes")
						$buffer .= '<td class="' . $class . '"><a class="day-active" href="' . $config['http_home_url'] . ''.jdate("Y/m/d", $events[$days]) . '/" title="' . $lang['cal_post'] . ' ' . $date['title'] . '">' . $cal_day . '</a></td>';
					else
						$buffer .= '<td class="' . $class . '"><a class="day-active" href="' . $PHP_SELF . '?year='.jdate("Y", $events[$days]) . '&month='.jdate("m", $events[$days]) . '&day='.jdate("d", $events[$days]) . '" title="' . $lang['cal_post'] . ' ' . $date['title'] . '">' . $cal_day . '</a></td>';

				} else {
					$class = $todayjday == $days && ($jmonth == $todayjmonth) && ($jyear == $todayjyear) ? 'day-current' : 'day-active-v';
					
					if ($config['allow_alt_url'] == "yes")
						$buffer .= '<td class="' . $class . '"><a class="day-active-v" href="' . $config['http_home_url'] . ''.jdate("Y/m/d", $events[$days]) . '/" title="' . $lang['cal_post'] . ' ' . $date[title] . '">' . $cal_day . '</a></td>';
					else
						$buffer .= '<td class="' . $class . '"><a class="day-active-v" href="' . $PHP_SELF . '?year='.jdate("Y", $events[$days]) . '&month='.jdate("m", $events[$days]) . '&day='.jdate("d", $events[$days]) . '" title="' . $lang['cal_post'] . ' ' . $date[title] . '">' . $cal_day . '</a></td>';

				}
			} else {
				if ($todayjday == $days && ($jmonth == $todayjmonth) && ($jyear == $todayjyear)) 
				$buffer .= '<td class="day-current">' . $cal_day . '</td>';
				elseif ($weekday == "6") $buffer .= '<td class="weekday">' . $cal_day . '</td>';
				else $buffer .= '<td class="day">' . $cal_day . '</td>';
			}

			$cal_day++;
			$weekday++;
			$days++;
		}

		if ($weekday != 7){$buffer .= '<td colspan="' . (7 - $weekday) . '">&nbsp;</td>';}

		$tpl->result['calendar'] = $buffer . '</tr></table></div>';
	}
	
	create_cache("calendar" . $year . $month . $day, $tpl->result['calendar'], $config['skin']);
	
	echo $tpl->result['calendar'];
}

// Show Archive
if ($config['allow_archives'] == "yes" AND $mod == "archives") {

	$tpl->result['archives'] = dle_cache("archives", $config['skin']);

	if ( ! $tpl->result['archives'] ) {
  
		$added_time = time() + ($config['date_adjust'] * 60);
		$db->query("SELECT MIN(date), date FROM " . PREFIX . "_post GROUP BY date");
		$row = $db->get_row();
		$firstposty = jdate("Y", strtotime($row['date']) + $added_time);
		$firstpostm = jdate("m", strtotime($row['date']) + $added_time);
		
		$jal_month = jdate("m", $_TIME)+1;
		$jal_year = jdate("Y", $_TIME);
		if ($jal_month > 12) {
			$jal_month = 1;
			$jal_year++;
		}  
		
		$tpl->load_template("archives.tpl");
		
		while( jmaketime( 0, 0, 0, $jal_month, 1, $jal_year ) >= -abs( jmaketime( 0, 0, 0, $firstpostm, 1, $firstposty ) ) ){
			$jal_nextmonth = $jal_month - 1;
			$jal_nextyear = $jal_year;
			if ($jal_nextmonth < 1) {
				$jal_nextmonth = 12;
				$jal_nextyear--;
			}
					
			$thisdate = jmaketime(0,0,0,$jal_nextmonth,1,$jal_nextyear);
			$endm = date("Y-m-d H:i:s", jmaketime(0,0,0,$jal_month,1,$jal_year));
			$startm = date("Y-m-d H:i:s", $thisdate);
		   
			$db->query("SELECT id FROM " . PREFIX . "_post WHERE approve = '1' AND date >= '$startm' AND date < '$endm'");
			$count = $db->num_rows();
			
			if ($count > 0) {
				$arch_month = jdate("M", $thisdate);
				$arch_year = jdate("Y", $thisdate);
				$arch_month_url = jdate("m", $thisdate);

				if ($config['allow_alt_url'] == "yes")
					$tpl->set( "{link}", $config['http_home_url'] . $arch_year. '/' . $arch_month_url . '/' );
				else
					$tpl->set( "{link}", $PHP_SELF . '?year=' . $arch_year . '&month=' . $arch_month_url );
				
				$tpl->set( "{title}", $arch_month . ' ' . $arch_year );
				$tpl->set( "{count}", $count );
				
				$tpl->compile("archives");
			}
			
			$jal_month = $jal_nextmonth;
			$jal_year = $jal_nextyear;	
			
		}
		
		$tpl->clear();
		$db->free();
		
		create_cache ("archives", $tpl->result['archives'], $config['skin']);
	}

	echo $tpl->result['archives'];
}

?>