1 <?php
2 /**
3 * @package FrameworkOnFramework
4 * @subpackage model
5 * @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE.txt
7 */
8 // Protect from unauthorized access
9 defined('FOF_INCLUDED') or die;
10
11 /**
12 * FrameworkOnFramework model behavior class
13 *
14 * @package FrameworkOnFramework
15 * @since 2.1
16 */
17 class FOFModelFieldDate extends FOFModelFieldText
18 {
19 /**
20 * Returns the default search method for this field.
21 *
22 * @return string
23 */
24 public function getDefaultSearchMethod()
25 {
26 return 'exact';
27 }
28
29 /**
30 * Perform a between limits match. When $include is true
31 * the condition tested is:
32 * $from <= VALUE <= $to
33 * When $include is false the condition tested is:
34 * $from < VALUE < $to
35 *
36 * @param mixed $from The lowest value to compare to
37 * @param mixed $to The higherst value to compare to
38 * @param boolean $include Should we include the boundaries in the search?
39 *
40 * @return string The SQL where clause for this search
41 */
42 public function between($from, $to, $include = true)
43 {
44 if ($this->isEmpty($from) || $this->isEmpty($to))
45 {
46 return '';
47 }
48
49 $extra = '';
50
51 if ($include)
52 {
53 $extra = '=';
54 }
55
56 $sql = '((' . $this->getFieldName() . ' >' . $extra . ' "' . $from . '") AND ';
57 $sql .= '(' . $this->getFieldName() . ' <' . $extra . ' "' . $to . '"))';
58
59 return $sql;
60 }
61
62 /**
63 * Perform an outside limits match. When $include is true
64 * the condition tested is:
65 * (VALUE <= $from) || (VALUE >= $to)
66 * When $include is false the condition tested is:
67 * (VALUE < $from) || (VALUE > $to)
68 *
69 * @param mixed $from The lowest value of the excluded range
70 * @param mixed $to The higherst value of the excluded range
71 * @param boolean $include Should we include the boundaries in the search?
72 *
73 * @return string The SQL where clause for this search
74 */
75 public function outside($from, $to, $include = false)
76 {
77 if ($this->isEmpty($from) || $this->isEmpty($to))
78 {
79 return '';
80 }
81
82 $extra = '';
83
84 if ($include)
85 {
86 $extra = '=';
87 }
88
89 $sql = '((' . $this->getFieldName() . ' <' . $extra . ' "' . $from . '") OR ';
90 $sql .= '(' . $this->getFieldName() . ' >' . $extra . ' "' . $to . '"))';
91
92 return $sql;
93 }
94
95 /**
96 * Interval date search
97 *
98 * @param string $value The value to search
99 * @param string|array|object $interval The interval. Can be (+1 MONTH or array('value' => 1, 'unit' => 'MONTH', 'sign' => '+'))
100 * @param boolean $include If the borders should be included
101 *
102 * @return string the sql string
103 */
104 public function interval($value, $interval, $include = true)
105 {
106 if ($this->isEmpty($value) || $this->isEmpty($interval))
107 {
108 return '';
109 }
110
111 $interval = $this->getInterval($interval);
112
113 if ($interval['sign'] == '+')
114 {
115 $function = 'DATE_ADD';
116 }
117 else
118 {
119 $function = 'DATE_SUB';
120 }
121
122 $extra = '';
123
124 if ($include)
125 {
126 $extra = '=';
127 }
128
129 $sql = '(' . $this->getFieldName() . ' >' . $extra . ' ' . $function;
130 $sql .= '(' . $this->getFieldName() . ', INTERVAL ' . $interval['value'] . ' ' . $interval['unit'] . '))';
131
132 return $sql;
133 }
134
135 /**
136 * Perform a between limits match. When $include is true
137 * the condition tested is:
138 * $from <= VALUE <= $to
139 * When $include is false the condition tested is:
140 * $from < VALUE < $to
141 *
142 * @param mixed $from The lowest value to compare to
143 * @param mixed $to The higherst value to compare to
144 * @param boolean $include Should we include the boundaries in the search?
145 *
146 * @return string The SQL where clause for this search
147 */
148 public function range($from, $to, $include = true)
149 {
150 if ($this->isEmpty($from) && $this->isEmpty($to))
151 {
152 return '';
153 }
154
155 $extra = '';
156
157 if ($include)
158 {
159 $extra = '=';
160 }
161
162 if ($from)
163 $sql[] = '(' . $this->getFieldName() . ' >' . $extra . ' "' . $from . '")';
164 if ($to)
165 $sql[] = '(' . $this->getFieldName() . ' <' . $extra . ' "' . $to . '")';
166
167 $sql = '(' . implode(' AND ', $sql) . ')';
168
169 return $sql;
170 }
171
172 /**
173 * Parses an interval –which may be given as a string, array or object– into
174 * a standardised hash array that can then be used bu the interval() method.
175 *
176 * @param string|array|object $interval The interval expression to parse
177 *
178 * @return array The parsed, hash array form of the interval
179 */
180 protected function getInterval($interval)
181 {
182 if (is_string($interval))
183 {
184 if (strlen($interval) > 2)
185 {
186 $interval = explode(" ", $interval);
187 $sign = ($interval[0] == '-') ? '-' : '+';
188 $value = (int) substr($interval[0], 1);
189
190 $interval = array(
191 'unit' => $interval[1],
192 'value' => $value,
193 'sign' => $sign
194 );
195 }
196 else
197 {
198 $interval = array(
199 'unit' => 'MONTH',
200 'value' => 1,
201 'sign' => '+'
202 );
203 }
204 }
205 else
206 {
207 $interval = (array) $interval;
208 }
209
210 return $interval;
211 }
212 }
213