summaryrefslogtreecommitdiffstats
path: root/library/Poolctrl/Validate/EventOverlapping.php
blob: fc1e0d5af5a9588bea1c6ce7ac5194e43e5f9aef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
require_once 'Zend/Validate/Abstract.php';

class Poolctrl_Validate_EventOverlapping extends Zend_Validate_Abstract
{
	const EVENT_OVERLAPPING = 'overlapping';

	protected $_messageTemplates = array(
	self::EVENT_OVERLAPPING => "This Event is overlapping an other Event",
	);

	protected $_start;
	protected $_eventMapper;
	protected $_event;
	protected $_poolID;

	public function __construct($option)
	{
		if ($option instanceof Zend_Config) {
			$option = $option->toArray();
		}

		if (is_array($option)) {
			$this->_eventMapper = new Application_Model_EventMapper();
			if (array_key_exists('start', $option)) {
				$_start = strtotime($option['start']);
				$this->_setStart($_start);
			} else {
				require_once 'Zend/Validate/Exception.php';
				throw new Zend_Validate_Exception("Missing option 'start'");
			}
			if (array_key_exists('poolID', $option)) {
				$_poolID = $option['poolID'];
				$this->_setPoolID($_poolID);
			} else {
				require_once 'Zend/Validate/Exception.php';
				throw new Zend_Validate_Exception("Missing option 'poolID'");
			}
			if (array_key_exists('eventID', $option)) {
				$_eventID = $option['eventID'];
				$this->_event = new Application_Model_Event();
				$this->_eventMapper->find($_eventID, $this->_event);
			}
		}
	}

	public function _getStart()
	{
		return $this->_start;
	}

	public function _setStart($_start)
	{
		$this->_start = $_start;
	}

	public function _getPoolID()
	{
		return $this->_poolID;
	}

	public function _setPoolID($_poolID)
	{
		$this->_poolID = $_poolID;
	}

	public function _getEvent()
	{
		return $this->_event;
	}

	public function _setEvent(Application_Model_Event $_event)
	{
		$this->_event = $_event;
	}

	public function _getEventMapper()
	{
		return $this->_eventMapper;
	}

	public function _setEventMapper(Application_Model_EventMapper $_eventMapper)
	{
		$this->_eventMapper = $_eventMapper;
	}

	public function isValid($value)
	{
		$this->_setValue(strtotime($value));
		if(is_object($this->_event)) {
			$overlappingEvents = $this->_eventMapper->getOverlappingEvents(date('Y-m-d H:i:s', $this->_start), date('Y-m-d H:i:s', $this->_value), $this->_poolID, $this->_event);
		} else {
			$overlappingEvents = $this->_eventMapper->getOverlappingEvents(date('Y-m-d H:i:s', $this->_start), date('Y-m-d H:i:s', $this->_value), $this->_poolID);
		}

		if (count($overlappingEvents) > 0) {
			$this->_error(self::EVENT_OVERLAPPING);
			return false;
		}
		return true;
	}
}