<?php
/*********************************************************************************
 * The contents of this file are subject to the SugarCRM Public License Version
 * 1.1.3 ("License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.sugarcrm.com/SPL
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * All copies of the Covered Code must include on each user interface screen:
 *    (i) the "Powered by SugarCRM" logo and
 *    (ii) the SugarCRM copyright notice
 * in the same form as they appear in the distribution.  See full license for
 * requirements.
 *
 * The Original Code is: SugarCRM Open Source
 * The Initial Developer of the Original Code is SugarCRM, Inc.
 * Portions created by SugarCRM are Copyright (C) 2004-2005 SugarCRM, Inc.;
 * All Rights Reserved.
 * Contributor(s): ______________________________________.
 ********************************************************************************/
$global_time_date_obj =  new TimeDate();


class SugarWidgetFieldDateTime extends SugarWidgetReportField
{


// joey 8/14/05: commented out the other returns because to_db_date was making all dates 2001-01-01

 function queryFilterOn(&$layout_def)
 {
	global $global_time_date_obj;
	return "LEFT(".$this->_get_column_select($layout_def).",10)='".PearDatabase::quote($layout_def['input_name0'])."'\n";
 }

 function queryFilterBefore(&$layout_def)
 {
	global $global_time_date_obj;
	return $this->_get_column_select($layout_def)."<'".PearDatabase::quote($layout_def['input_name0']." 00:00:00")."'\n";
 }

 function queryFilterAfter(&$layout_def)
 {
	global $global_time_date_obj;
	return $this->_get_column_select($layout_def).">'".PearDatabase::quote($layout_def['input_name0']." 23:59:59")."'\n";
 }

 function queryFilterBetween_Dates(&$layout_def)
 {
	global $global_time_date_obj;
	return "(".$this->_get_column_select($layout_def).">'".PearDatabase::quote($layout_def['input_name0']." 00:00:00")."' AND \n".  $this->_get_column_select($layout_def)."<'".PearDatabase::quote($layout_def['input_name1']. " 23:59:59")."')\n";
 }

 function queryFilterNot_Equals_str (&$layout_def)
 {
	global $global_time_date_obj;
	return "ISNULL(".$this->_get_column_select($layout_def).") OR \n".
	"LEFT(".$this->_get_column_select($layout_def).",10)!='".PearDatabase::quote($layout_def['input_name0'])."'\n";
 }

 function queryFilterTP_yesterday(&$layout_def)
 {
	return "LEFT(".$this->_get_column_select($layout_def).",10)  = LEFT(( current_date - interval '1' day),10) ";
 }

 function queryFilterTP_today(&$layout_def)
 {
	return "LEFT(".$this->_get_column_select($layout_def).",10) =  LEFT(current_date,10) ";
 }

 function queryFilterTP_tomorrow(&$layout_def)
 {
	return "LEFT(".$this->_get_column_select($layout_def).",10) =  LEFT((current_date + interval '1' day),10)";
 }

 function queryFilterTP_last_week(&$layout_def)
 {
//	return "TRUNC(".$this->_get_column_select($layout_def).",'W') =  (current_date + interval '1' week)";
 }

 function queryFilterTP_next_week(&$layout_def)
 {
 }
	
 function queryFilterTP_last_7_days(&$layout_def)
 {
	return "LEFT(".$this->_get_column_select($layout_def).",10) BETWEEN LEFT((current_date - interval '7' day),10) AND LEFT(current_date,10)";
 }

 function queryFilterTP_next_7_days(&$layout_def)
 {
	return "LEFT(".$this->_get_column_select($layout_def).",10) BETWEEN LEFT(current_date) AND LEFT((current_date + interval '7' day),10)";
 }

 function queryFilterTP_last_month(&$layout_def)
 {
	return "LEFT(".$this->_get_column_select($layout_def).",7) = LEFT( (current_date  - interval '1' month),7)";
 }

 function queryFilterTP_this_month(&$layout_def)
 {
	return "LEFT(".$this->_get_column_select($layout_def).",7) = LEFT( current_date,7)";
 }

 function queryFilterTP_next_month(&$layout_def)
 {
	return "LEFT(".$this->_get_column_select($layout_def).",7) = LEFT( (current_date  + interval '1' month),7)";
 }

 function queryFilterTP_last_30_days(&$layout_def)
 {
	return "LEFT(".$this->_get_column_select($layout_def).",10) BETWEEN LEFT((current_date - interval '1' month),10) AND LEFT(current_date,10) AND LEFT((current_date - interval '1' month),10)";
 }

 function queryFilterTP_next_30_days(&$layout_def)
 {
	return "LEFT(".$this->_get_column_select($layout_def).",10) BETWEEN current_date AND (current_date + interval '1' month)";
 }

 function queryFilterTP_last_quarter(&$layout_def)
 {
	//return "LEFT(".$this->_get_column_select($layout_def).",10) BETWEEN (current_date + interval '1' month) AND current_date";
 }

 function queryFilterTP_this_quarter(&$layout_def)
 {
 }

 function queryFilterTP_last_year(&$layout_def)
 {
	return "LEFT(".$this->_get_column_select($layout_def).",4) = EXTRACT(YEAR FROM ( current_date  - interval '1' year))";
 }

 function queryFilterTP_this_year(&$layout_def)
 {
	return "LEFT(".$this->_get_column_select($layout_def).",4) = EXTRACT(YEAR FROM ( current_date ))";
 }

 function queryFilterTP_next_year(&$layout_def)
 {
	return "LEFT(".$this->_get_column_select($layout_def).",4) = EXTRACT(YEAR FROM ( current_date  + interval '1' year))";
 }

 function &queryGroupBy($layout_def)
 {
   // i guess qualifier and column_function are the same..
   if (! empty($layout_def['qualifier']))
   {
	$func_name = 'queryGroupBy'.$layout_def['qualifier'];
//print_r($layout_def);
//print $func_name;
	if ( method_exists($this,$func_name))
	{
        	return $this->$func_name($layout_def)." \n";
	}
   }
   return parent::queryGroupBy($layout_def)." \n";
 }

 function &queryOrderBy($layout_def)
 {
   // i guess qualifier and column_function are the same..
   if (! empty($layout_def['qualifier']))
   {
	$func_name = 'queryGroupBy'.$layout_def['qualifier'];
	if ( method_exists($this,$func_name))
	{
        	return $this->$func_name($layout_def)." \n";
	}
   }
   return parent::queryOrderBy($layout_def)." \n";
 }

        function  displayList($layout_def)
        {
		global $global_time_date_obj;
   		// i guess qualifier and column_function are the same..
   		if (! empty($layout_def['column_function']))
		 {
			$func_name = 'displayList'.$layout_def['column_function'];
			if ( method_exists($this,$func_name))
			{
       			 	return $this->$func_name($layout_def)." \n";
			}
   		}
              	$content = $this->displayListPlain($layout_def);
		return $global_time_date_obj->to_display_date_time($content);
        }


 function querySelect(&$layout_def)
 {
   // i guess qualifier and column_function are the same..
   if (! empty($layout_def['column_function']))
   {
	$func_name = 'querySelect'.$layout_def['column_function'];
	if ( method_exists($this,$func_name))
	{
        	return $this->$func_name($layout_def)." \n";
	}
   }
   return parent::querySelect($layout_def)." \n";
 }
 function &displayListyear(&$layout_def)
 {
	global $app_list_strings;
  	preg_match('/(\d{4})/',$this->displayListPlain($layout_def),$match);
	return $match[1];

 }

 function &displayListmonth(&$layout_def)
 {
	global $app_list_strings;
  	preg_match('/(\d{4})-(\d\d)/',$this->displayListPlain($layout_def),$match);
	$match[2] = preg_replace('/^0/','',$match[2]);
	return $app_list_strings['dom_cal_month_long'][$match[2]]." {$match[1]}";

 }
 function &querySelectmonth(&$layout_def)
 {
	return "LEFT( ". $this->_get_column_select($layout_def).",7 ) ".$this->_get_column_alias($layout_def) ." \n";
 }

 function &queryGroupByMonth($layout_def)
 {
	return "LEFT(".$this->_get_column_select($layout_def).", 7) \n";
 }

 function &querySelectyear(&$layout_def)
 {
	return "LEFT( ". $this->_get_column_select($layout_def).",4 ) ".$this->_get_column_alias($layout_def) ." \n";
 }

 function &queryGroupByYear($layout_def)
 {
	return "LEFT(".$this->_get_column_select($layout_def).", 4) \n";
 }

 function &querySelectquarter(&$layout_def)
 {
	return "CONCAT(LEFT(".$this->_get_column_select($layout_def).", 4), '-', QUARTER(".$this->_get_column_select($layout_def).") )".$this->_get_column_alias($layout_def)."\n";
 }
 function &displayListquarter(&$layout_def)
 {
  preg_match('/(\d{4})-(\d)/',$this->displayListPlain($layout_def),$match);
  return "Q". $match[2]." ". $match[1];

 }
 function &queryGroupByQuarter($layout_def)
 {
	return "CONCAT(LEFT(".$this->_get_column_select($layout_def).", 4), '-', QUARTER(".$this->_get_column_select($layout_def).") )\n";
 }



}

?>
