<?php
    
class attr_datetime extends attr {
    function getDBType() {
    	return "int(12)";
    }
    
    function render_input() {
		if ($this->getAttrSetting('mand'))
	    		$defval = date('Y-m-d\TH:i:00');
	    	  else 
		    	$defval = '';
		
		$cv = $this->getCurVal();
		    	
	    $tpl = '<input type="datetime-local" class="form-control" name="'.$this->attr_name.'"  id="'.$this->attr_name.'" value="'.($cv?date('Y-m-d\TH:i:s', $cv):$defval).'">';
		return $tpl;
    }
    
    function lPost() {
    	$time  = strtotime($_POST[$this->attr_name]);
    	if ( $time == false ) {
    			$time = $_POST[$this->attr_name];
    	}
       	$this->obj->set($this->attr_name, $time);
    }

    function render_text( $crop = false ) {
    	return date('d.m.Y H:i:s',$this->getCurVal());
    }


	function getFilter( $sql_where = '' ) {

		$fname = $this->obj->obj_name . '_' . $this->attr_name;

		    if (!isset($_GET[$fname.'_year'])) $_GET[$fname.'_year'] = 0;
		    if (!isset($_GET[$fname.'_month'])) $_GET[$fname.'_month'] = 0;
		    if (!isset($_GET[$fname.'_week'])) $_GET[$fname.'_week'] = 0;
		    if (!isset($_GET[$fname.'_sday'])) $_GET[$fname.'_sday'] = 0;

		    $y = intval( $_GET[$fname.'_year'] );
		    $m = intval( $_GET[$fname.'_month'] );
		    $w = intval( $_GET[$fname.'_week'] );
		    $d = intval( $_GET[$fname.'_sday'] );
					
					$ret = '';
				    // Display
					 // By date 
					  //  year
					    $nofirst = true ;

					    if ($y=='0')
						    $html.="<a href='" . _getcurl(array( 'curpage', $fname.'_year')) . "&".$fname.'_year'."=0' ><b>Все</b></a>";
						else
						    $html.="<a href='" . _getcurl(array( 'curpage', $fname.'_year')) . "&".$fname.'_year'."=0' >Все</a>";

					    for ($year=2010;$year<=date('Y')+2;$year++) {
					    if ( $nofirst ) {
					    $html .= ' | ';
					    }
					    $nofirst = true;
					    $html.="<a href='" . _getcurl(array( 'curpage', $fname.'_year')) . "&".$fname.'_year'."=$year' >";
					    if ($y==$year)
				 		    $html.= "<b>$year</b>";
						else		
					 	    $html.= "$year";
					    $html.="</a>";    
					    }
					   $html.= "<br>";
					  // month
					    if ($m=='0')
						    $html.="<a href='" . _getcurl(array( 'curpage', $fname.'_month')) . "&".$fname.'_month'."=0' ><b>Все</b></a>";
						else
						    $html.="<a href='" . _getcurl(array( 'curpage', $fname.'_month')) . "&".$fname.'_month'."=0' >Все</a>";
					    for ($month=1;$month<=12;$month++) {
						    $html .= ' | ';
						    $html.="<a href='" . _getcurl(array( 'curpage', $fname.'_month')) . "&".$fname.'_month'."=$month' >";
						    if ($m==$month)
				 			    $html.= "<b>"._getMonthName($month)."</b>";
							else		
						 	    $html.= _getMonthName($month);
						    $html.="</a>";    
					    }
					   $html.= "<br>";
					 // week
					 if ( $sql_where != 'noweek' ) {
					    if ($w=='0')
						    $html.="<a href='" . _getcurl(array( 'curpage', $fname.'_week')) . "&".$fname.'_week'."=0' ><b>Все</b></a>";
						else
						    $html.="<a href='" . _getcurl(array( 'curpage', $fname.'_week')) . "&".$fname.'_week'."=0' >Все</a>";

					    $weeks = $this->getSeven(mktime(0,0,0, $m, 1, $y));
				            
					    foreach ($weeks as $k=>$week) {
						    if ($k == 0) continue;
						    $html .= ' | ';
						    $html .="<a href='" . _getcurl(array( 'curpage', $fname.'_week')) . "&".$fname.'_week'."=".($k)."' >";
						    if ($w==$k)
				 			    $html .= "<b>".date('d', $weeks[($k-1)]).'-'.date('d', $week)."</b>";
							else		
				 			    $html .= date('d', $weeks[($k-1)]).'-'.date('d', $week);
						    $html.="</a>";    
					    }
					    
					   $html .= "<br>";	    
					 // day
					 if ( $sql_where != 'nodays' ) {

					    if ($d=='0')
						    $html.= "<a href='" . _getcurl(array( 'curpage', $fname.'_sday')) . "&".$fname.'_sday'."=0' ><b>Все</b></a>";
						else
						    $html.= "<a href='" . _getcurl(array( 'curpage', $fname.'_sday')) . "&".$fname.'_sday'."=0' >Все</a>";

					    for ($i=1; $i<=date("t", mktime(0,0,0,$m,1,$y)); $i++) {
						    if ($i == 0) continue;
						    $html .= ' | ';
						    $html .= "<a href='" . _getcurl(array( 'curpage', $fname.'_sday')) . "&".$fname.'_sday'."=".($i)."' >";
						    if ($d==$i)
				 			    $html.= "<b>".$i."</b>";
							else		
				 			    $html.= $i;
						    $html.="</a>";    
						}	    
					   $html .= "<br>";	    
					   $html.= "<a href='" . 	_getcurl(
					   					array($fname.'_year',
					   						  $fname.'_month',
					   						  $fname.'_week',
					   					 	  $fname.'_sday',
					   						  'curpage')
					   					 )."&".$fname.'_year'."=".date('Y')."&".$fname.'_month'."=".date('m')."&".$fname.'_week'."=0&".$fname.'_sday'."=".date('d')."'>";
					   if ( ( $d == date('d') ) && ($y == date('Y') ) && ($m == date('m') ) ) {
						   $html .="<b>Сегодня</b>";
					   } else {
						   $html .="Сегодня";
					   }
					   $html .="</a>";
					 }
					}
				    // SQL
				    $range = $this->getTimeRangeMonth($y, $m);

				    if ( $w ) {
				    $ret .= ' AND 
						 ( '. ($this->obj->obj_name?($this->obj->obj_name. '.'):'') . $this->attr_name .' >= ' . $weeks[($w-1)] . '
						  AND 
						  '.  ($this->obj->obj_name?($this->obj->obj_name. '.'):'') . $this->attr_name .' <= ' . $weeks[$w] . ' )';
				    }elseif ( $d ) {
				    $ret .= ' AND  
						 ( '. ($this->obj->obj_name?($this->obj->obj_name. '.'):'')  . $this->attr_name .' >= ' . mktime(0,0,0,$m, $d, $y) . '
						  AND 
						  '. ($this->obj->obj_name?($this->obj->obj_name. '.'):'')  . $this->attr_name .' <= ' .  mktime(23,59,59,$m, $d, $y) . ' )';

				    } else {
				    $ret .= ' AND  
						 ( '. ($this->obj->obj_name?($this->obj->obj_name. '.'):'') . $this->attr_name .' >= ' . $range['first'] . '
						  AND 
						  '. ($this->obj->obj_name?($this->obj->obj_name. '.'):'') . $this->attr_name .' <= ' . $range['last'] . ' )';
				    }
				    $r['sql'] = $ret;
					$r['html'] = $html;

		return $r;
	}
    
  
   function getTimeRangeMonth($year, $month) {
	  if ($year && $month) {
		  $ret['first'] = mktime(0,0,0,$month,1,$year);
		  $ret['last'] = mktime(23,59,59,$month,date("t", $ret['first']),$year);
	   }
	  else {
	    if ($year) {
		  $ret['first'] = mktime(0,0,0,1,1,$year);
		  $ret['last'] = mktime(23,59,59,12,31,$year);
	    }
	    else {
		  $ret['first'] = 0;
		  $ret['last'] = 999999999999;
	    }
	  }
	 
	  return $ret;
	 }

	 function getSeven ($date) { // С шагом в 7 дней
	   $day = date('d', $date);
	   $first_day = mktime(0,0,0,date('m', $date),1,date('Y', $date));
	   $last_day = mktime(23,59,59,date('m', $date),date('t', $date),date('Y', $date));
	   $ret[] = $first_day;
	   $tmp = $first_day;
	   while ( $tmp < $last_day ) {
	    $tmp =  mktime(0,0,0,date('m', $tmp),date('d', $tmp)+7,date('Y', $tmp)); 
	    if ($tmp > $last_day)
	       $ret[] = $last_day;
	      else
	       $ret[] = $tmp;
	   }
	   return $ret;
	 }
  
}

?>