class_DateBE.inc.php 13.1 KB
<?php
/**
 * @file	DateBE.inc.php
 * @category freeSN
 * @mailto	code [at] netz.coop
 * @version 0.4.200901
 * @link http://netz.coop
 * 
 *  @copyright Copyright by netz.coop e.G. 2015
 *  
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

/**
 *	Termin
 *
 *	@version 		0.1.071229
 *	@author 		dev [at] mensch [dot] coop
 */
class DateBE extends MultimediaText  {
	protected $Start;
	protected $End;

	public static $Cycle_kind_array = array(
		1 => 'cycle_weekly',
		2 => 'cycle_evenweek',
		3 => 'cycle_oddweek',
		4 => 'cycle_first',
		5 => 'cycle_second',
		6 => 'cycle_third',
		7 => 'cycle_fourth',
		8 => 'cycle_last',
		9 => 'cycle_date',
		);
	public static $Cycle_day_array = array(
			1 => 'Mo',
			2 => 'Tu',
			3 => 'We',
			4 => 'Th',
			5 => 'Fr',
			6 => 'Sa',
			7 => 'Su'

	);
	/**
	 * Stelle 1:
	 * 1 - wöchentlich
	 * 2 - jede grade Woche
	 * 3 - jede ungrade Woche
	 * 4 - jeden 1. Wochentag
	 * 5 - jeden 2. Wochentag
	 * 6 - jeden 3. Wochentag
	 * 7 - jeden 4. Wochentag
	 * 8 - jeden letzten Wochentag
	 * 9 - Datum im Monat
	 *
	 * Stelle 2
	 * 1 - Mo
	 * 2 - Di
	 * 3 - Mi
	 * 4 - Do
	 * 5 - Fr
	 * 6 - Sa
	 * 7 - So
	 *
	 * 11 - 1.
	 * 12 - 2.
	 * 13 - 3.
	 * 14 - 3.
	 * .
	 * .
	 * .
	 * .
	 * .
	 */
	protected $Cycle;
	protected $Cycle_kind;
	protected $Cycle_day;


	protected $TmpReadPrivate;
	protected $TmpWritePrivate;
	protected $PartOfProfileID;
	protected $LocationID;


	public function setTmpReadPrivate($Private){
		D::li('setTmpReadPrivate muss das denn sein oder kann sowas nicht raus');
		$this->TmpReadPrivate = $Private;
	}
	public function setTmpWritePrivate($Private){
		D::li('setTmpWritePrivate muss das denn sein oder kann sowas nicht raus');
		$this->TmpWritePrivate = $Private;
	}
	/**
	 * function return an array with the BasisElement cycle timestamps of this date for a period of time
	 * if date has no cycle in the defined period of time, an empty array will be returned
	 * if date isn't a cycledate, the date start- and endtimestamp of this date will be returned
	 *
	 * @param int param_start timestamp
	 * @param int param_end timestamp
	 *
	 * @return array array[]=>(ID,start,end)
	 */
	public function getCycleDateArray($param_start, $param_end){

		$return_array = array();

		if($this->Cycle_kind && $this->Cycle_day){

			if($param_end > $param_start && $this->Start < $param_end){

				if($param_start > $this->Start){
					$var_startStamp = $param_start;
				}
				else{
					$var_startStamp = $this->Start;
				}

				$var_startCyleHour = $var_endCyleHour = date('H',$this->Start);
				$var_startCyleMinute = $var_endCyleMinute = date('i',$this->Start);
				$var_endDayDiff = 0;

				if($this->Start != $this->End){
					$var_endCyleHour = date('H',$this->End);
					$var_endCyleMinute = date('i',$this->End);

					if(date('N', $this->Start) != date('N',$this->End)){
						$var_endDayDiff = date('N',$this->End) - date('N', $this->Start);
					}
				}

				$var_startCalMonth = date('m', $var_startStamp);
				$var_startCalYear = date('Y', $var_startStamp);
				$var_startDay = date('d',$var_startStamp);


				if($this->Cycle_kind < 9){

					if(date('N', $var_startStamp) > $this->Cycle_day){
						$var_startDay +=	7 + $this->Cycle_day - date('N', $var_startStamp);
					}
					else if(date('N', $var_startStamp) < $this->Cycle_day){
						$var_startDay +=	$this->Cycle_day - date('N', $var_startStamp);
					}
					$var_startStamp = mktime($var_startCyleHour, $var_startCyleMinute, 0, $var_startCalMonth, $var_startDay, $var_startCalYear);
					$var_endStamp = mktime($var_endCyleHour, $var_endCyleMinute, 0, $var_startCalMonth, $var_startDay+$var_endDayDiff, $var_startCalYear);

					if($this->Cycle_kind <4){
						$var_plusDay = 7;
						if($this->Cycle_kind == 2){
							if(date('W',$var_startStamp) % 2 != 0){
								$var_startDay+=$var_plusDay;
								$var_startStamp = mktime($var_startCyleHour, $var_startCyleMinute, 0, $var_startCalMonth, $var_startDay, $var_startCalYear);
								$var_endStamp = mktime($var_endCyleHour, $var_endCyleMinute, 0, $var_startCalMonth, $var_startDay+$var_endDayDiff, $var_startCalYear);
							}
							$var_plusDay = 14;
						}
						if($this->Cycle_kind == 3){
							if(date('W',$var_startStamp) % 2 == 0){
								$var_startDay+=$var_plusDay;
								$var_startStamp = mktime($var_startCyleHour, $var_startCyleMinute, 0, $var_startCalMonth, $var_startDay, $var_startCalYear);
								$var_endStamp = mktime($var_endCyleHour, $var_endCyleMinute, 0, $var_startCalMonth, $var_startDay+$var_endDayDiff, $var_startCalYear);
							}
							$var_plusDay = 14;
						}

						while($param_end >= $var_startStamp){
							$return_array[] = array($this->ID,$var_startStamp, $var_endStamp);
							$var_startDay +=$var_plusDay;
							$var_startStamp = mktime($var_startCyleHour, $var_startCyleMinute, 0, $var_startCalMonth, $var_startDay, $var_startCalYear);
							$var_endStamp = mktime($var_endCyleHour, $var_endCyleMinute, 0, $var_startCalMonth, $var_startDay+$var_endDayDiff, $var_startCalYear);
						}
						return $return_array;
					}
					else if($this->Cycle_kind < 8){
						$var_index = ($this->Cycle_kind - 3)*7;
						while($param_end >= $var_startStamp){
						//	if(0.5 < date('j',$var_startStamp) / $var_index && date('j',$var_startStamp) / $var_index <= 1){
							if($var_index - date('j',$var_startStamp) < 7 && $var_index - date('j',$var_startStamp) >= 0){
								$return_array[] = array($this->ID,$var_startStamp, $var_endStamp);
							}
							$var_startDay +=7;
							$var_startStamp = mktime($var_startCyleHour, $var_startCyleMinute, 0, $var_startCalMonth, $var_startDay, $var_startCalYear);
							$var_endStamp = mktime($var_endCyleHour, $var_endCyleMinute, 0, $var_startCalMonth, $var_startDay+$var_endDayDiff, $var_startCalYear);
						}
						return $return_array;
					}
					else if($this->Cycle_kind == 8){
						while(true){
							$var_startDay +=7;
							$var_NextstartStamp = mktime($var_startCyleHour, $var_startCyleMinute, 0, $var_startCalMonth, $var_startDay, $var_startCalYear);
							if(date('m',$var_NextstartStamp) != date('m',$var_startStamp)){
								$return_array[] = array($this->ID,$var_startStamp, $var_endStamp);
							}
							if($var_NextstartStamp > $param_end){
								return $return_array;
							}
							$var_startStamp = $var_NextstartStamp;
							$var_endStamp = mktime($var_endCyleHour, $var_endCyleMinute, 0, $var_startCalMonth, $var_startDay+$var_endDayDiff, $var_startCalYear);
						}
					}
					else{
						return $return_array;
					}

				}
				else if ($this->Cycle_kind == 9 && 32 > $this->Cycle_day && $this->Cycle_day >10){
					$var_day = $this->Cycle_day -10;
					if($var_startDay > $var_day){
						$var_startCalMonth +=1;
					}
					$var_startStamp = mktime($var_startCyleHour, $var_startCyleMinute, 0, $var_startCalMonth, $var_day, $var_startCalYear);
					$var_endStamp = mktime($var_endCyleHour, $var_endCyleMinute, 0, $var_startCalMonth, $var_day+$var_endDayDiff, $var_startCalYear);

					while($param_end >= $var_startStamp){
						$return_array[]=array($this->ID,$var_startStamp,$var_endStamp);
						$var_startCalMonth += 1;
						$var_startStamp = mktime($var_startCyleHour, $var_startCyleMinute, 0, $var_startCalMonth, $var_day, $var_startCalYear);
						$var_endStamp = mktime($var_endCyleHour, $var_endCyleMinute, 0, $var_startCalMonth, $var_day+$var_endDayDiff, $var_startCalYear);
					}
					return $return_array;
				}
			}
			else{
				return $return_array;
			}
		}
		else{
			$return_array[] = array($this->ID,$this->Start,$this->End);
			return $return_array;
		}
	}

	/**
	 *	Funktion füllt Objekt mit den übergebenen Array
	 *
	 *	@param	Array $param_Data
	 *
	 *	Funktionen die diese Funktion aufrufen
	 */
	public function setData(Account &$param_HostAccount, iStorable &$param_iStorable, array $param_Data) {
		if($param_Data!=null) {

			//D::show($param_Data,'$param_Data');
			if(array_key_exists('Start_hour', $param_Data) && array_key_exists('End_hour', $param_Data)) {
				$param_Data['Start'] =	mktime($param_Data['Start_hour'], $param_Data['Start_minute'], 0, $param_Data['Start_month'], $param_Data['Start_day'], $param_Data['Start_year']);
				$param_Data['End'] = 	mktime($param_Data['End_hour'], $param_Data['End_minute'], 0, $param_Data['End_month'], $param_Data['End_day'], $param_Data['End_year']);
				$this->Start = $param_Data['Start'];
				$this->End = $param_Data['End'];
			} else if (!array_key_exists('Start', $param_Data)) {
				return Error::newError("Error","date start is required","");
			}
			if($param_Data['Start']>$param_Data['End']){
				$param_Data['End'] = $param_Data['Start'];
			}


			//Cycle Date
			if(array_key_exists('Cycle', $param_Data) && is_array($param_Data['Cycle'])	&& array_key_exists('Cycle', $param_Data['Cycle']) && $param_Data['Cycle']['Cycle'] == 1
			&& array_key_exists('Cycle_kind', $param_Data['Cycle']) && $param_Data['Cycle']['Cycle_kind'] && $param_Data['Cycle']['Cycle_kind']<10
			&& array_key_exists('Cycle_day', $param_Data['Cycle']) && $param_Data['Cycle']['Cycle_day'] && $param_Data['Cycle']['Cycle_day']<100){
				$this->Cycle_kind = $param_Data['Cycle']['Cycle_kind'];
				$this->Cycle_day = $param_Data['Cycle']['Cycle_day'];
				$param_Data['Cycle'] = $param_Data['Cycle']['Cycle_kind'].$param_Data['Cycle']['Cycle_day'];
			}
			else if ($param_Data['Cycle'] && $param_Data['Cycle'] !=0 && $param_Data['Cycle']< 1000){
				$this->Cycle = $param_Data['Cycle'];
				$this->Cycle_kind = substr($this->Cycle,0,1);
				$this->Cycle_day = substr($this->Cycle,1);

			}
			else{
				unset($param_Data['Cycle']);
				$this->Cycle = null;
			}
			parent::setData($param_HostAccount, $param_iStorable, $param_Data);
			if(array_key_exists("Profile", $param_Data) && $param_Data["Profile"]) {
				$this->PartOfProfileID = $param_Data["Profile"];
			}


			return true;
		} else {
			D::h1("else param_Data==null ==>> Warum wird setData aufgerufen ");
			return false;
		}
	}
	public function getObjDOMElement(Account &$param_HostAccount, iStorable &$param_iStorable, DOMDocument $param_DOMDocument, $param_KindOf=null, array $param_DOMElemente=null) {
		if(!is_array($param_DOMElemente)) {
			$param_DOMElemente = array();
		}

		$DOMElement = parent::getObjDOMElement($param_HostAccount, $param_iStorable, $param_DOMDocument, $param_KindOf, $param_DOMElemente);


		if($param_KindOf==="form" ) {
			foreach($DOMElement->getElementsByTagName('Element') as $key => $value){
				if($value->hasAttribute('objvar') && $value->getAttribute('objvar')=='Cycle'){
					$DOMElementCycleKind = $param_DOMDocument->createElement('Cycle_kind');
					if($this->Cycle_kind){
						$DOMElementCycleKind->setAttribute('value',$this->Cycle_kind);
					}
					foreach(self::$Cycle_kind_array as $key_kind => $value_kind){
						$DOMElementKind = $param_DOMDocument->createElement('kind',$value_kind);
						$DOMElementKind->setAttribute('value',$key_kind);
						if($this->Cycle_kind && $this->Cycle_kind == $key_kind){
							$DOMElementKind->setAttribute('selected','selected');
						}
						$DOMElementCycleKind->appendChild($DOMElementKind);
					}
					$DOMElementCycleDay = $param_DOMDocument->createElement('Cycle_day');
					if($this->Cycle_day){
						$DOMElementCycleDay->setAttribute('value',$this->Cycle_day);
					}
					foreach(self::$Cycle_day_array as $key_day => $value_day){
						$DOMElementDay = $param_DOMDocument->createElement('day',$value_day);
						$DOMElementDay->setAttribute('value',$key_day);
						if($this->Cycle_day && $this->Cycle_kind == $key_day){
							$DOMElementKind->setAttribute('selected','selected');
						}
						$DOMElementCycleDay->appendChild($DOMElementDay);
					}
					for($i = 11; $i < 42;$i++){
						$DOMElementDay = $param_DOMDocument->createElement('day',($i-10).'.');
						$DOMElementDay->setAttribute('value',$i);
						if($this->Cycle_day && $this->Cycle_day == $i){
							$DOMElementDay->setAttribute('selected','selected');
						}
						$DOMElementCycleDay->appendChild($DOMElementDay);
					}
					$value->setAttribute('value',$this->Cycle);
					$value->appendChild($DOMElementCycleKind);
					$value->appendChild($DOMElementCycleDay);
					break;
				}
			}
		}
		else{
			foreach($DOMElement->getElementsByTagName('Element') as $key => $value){
				if($value->hasAttribute('objvar') && $value->getAttribute('objvar')=='Cycle'){
					if(array_key_exists($this->Cycle_kind,self::$Cycle_kind_array))
						$value->setAttribute('kind',self::$Cycle_kind_array[$this->Cycle_kind]);

					if($this->Cycle_day > 10)
						$value->setAttribute('day',($this->Cycle_day - 10).'.');
					else if(array_key_exists($this->Cycle_day,self::$Cycle_day_array))
						$value->setAttribute('day',self::$Cycle_day_array[$this->Cycle_day]);

					break;
				}
			}
		}
		return $DOMElement;
	}
}
?>