All Data Structures Functions Variables Pages
MixedDateTypesSearchFormAttributeMappingRules.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
41  {
42  const TYPE_YESTERDAY = 'Yesterday';
43 
44  const TYPE_TODAY = 'Today';
45 
46  const TYPE_TOMORROW = 'Tomorrow';
47 
48  const TYPE_BEFORE = 'Before';
49 
50  const TYPE_AFTER = 'After';
51 
52  const TYPE_ON = 'On';
53 
54  const TYPE_BETWEEN = 'Between';
55 
56  const TYPE_NEXT_7_DAYS = 'Next 7 Days';
57 
58  const TYPE_LAST_7_DAYS = 'Last 7 Days';
59 
60  const TYPE_LAST_30_DAYS = 'Last 30 Days';
61 
62  const TYPE_THIS_MONTH = 'This Month';
63 
64  const TYPE_LAST_MONTH = 'Last Month';
65 
66  const TYPE_NEXT_MONTH = 'Next Month';
67 
68  const TYPE_BEFORE_TODAY = 'Before Today';
69 
70  const TYPE_IS_TIME_FOR = 'Is Time For';
71 
72  const TYPE_IS_EMPTY = 'Is Empty';
73 
74  const TYPE_IS_NOT_EMPTY = 'Is Not Empty';
75 
76  const TYPE_WAS_ON = 'Was On';
77 
78  const TYPE_BECOMES_ON = 'Becomes On';
79 
80  const TYPE_CHANGES = 'Changes';
81 
82  const TYPE_DOES_NOT_CHANGE = 'Does Not Change';
83 
92  public static function resolveValueDataIntoUsableValue($value)
93  {
94  if (isset($value['type']) && $value['type'] != null)
95  {
96  $validValueTypes = static::getValidValueTypes();
97  if (!in_array($value['type'], $validValueTypes))
98  {
99  throw new NotSupportedException();
100  }
101  if ($value['type'] == self::TYPE_TODAY || $value['type'] == self::TYPE_BEFORE_TODAY)
102  {
105  new DateTime(null, new DateTimeZone(Yii::app()->timeZoneHelper->getForCurrentUser())));
106  }
107  elseif ($value['type'] == self::TYPE_TOMORROW)
108  {
111  new DateTime(null, new DateTimeZone(Yii::app()->timeZoneHelper->getForCurrentUser())));
112  }
113  elseif ($value['type'] == self::TYPE_YESTERDAY)
114  {
117  new DateTime(null, new DateTimeZone(Yii::app()->timeZoneHelper->getForCurrentUser())));
118  }
119  elseif ($value['type'] == self::TYPE_BEFORE || $value['type'] == self::TYPE_AFTER ||
120  $value['type'] == self::TYPE_ON)
121  {
122  if ($value["firstDate"] == null)
123  {
124  return null;
125  }
126  return $value['firstDate'];
127  }
128  else
129  {
130  throw new NotSupportedException();
131  }
132  }
133  return null;
134  }
135 
141  {
142  if ($value['type'] != self::TYPE_BETWEEN)
143  {
144  throw new NotSupportedException();
145  }
146  if ($value["firstDate"] == null)
147  {
148  return null;
149  }
150  return $value['firstDate'];
151  }
152 
158  {
159  if ($value['type'] != self::TYPE_BETWEEN)
160  {
161  throw new NotSupportedException();
162  }
163  if ($value["secondDate"] == null)
164  {
165  return null;
166  }
167  return $value['secondDate'];
168  }
169 
173  public static function getValidValueTypes()
174  {
175  return array( self::TYPE_YESTERDAY,
176  self::TYPE_TODAY,
177  self::TYPE_TOMORROW,
178  self::TYPE_BEFORE,
179  self::TYPE_AFTER,
180  self::TYPE_ON,
181  self::TYPE_BETWEEN,
182  self::TYPE_NEXT_7_DAYS,
183  self::TYPE_LAST_7_DAYS,
184  self::TYPE_LAST_30_DAYS,
185  self::TYPE_THIS_MONTH,
186  self::TYPE_LAST_MONTH,
187  self::TYPE_NEXT_MONTH,
188  self::TYPE_BEFORE_TODAY,
189  self::TYPE_IS_TIME_FOR,
190  self::TYPE_IS_EMPTY,
191  self::TYPE_IS_NOT_EMPTY,
192  self::TYPE_WAS_ON,
193  self::TYPE_BECOMES_ON,
194  self::TYPE_CHANGES,
195  self::TYPE_DOES_NOT_CHANGE,
196  );
197  }
198 
202  public static function getValueTypesAndLabels()
203  {
204  return array(self::TYPE_YESTERDAY => Zurmo::t('Core', 'Yesterday'),
205  self::TYPE_TODAY => Zurmo::t('Core', 'Today'),
206  self::TYPE_TOMORROW => Zurmo::t('Core', 'Tomorrow'),
207  self::TYPE_BEFORE => Zurmo::t('Core', 'Before'),
208  self::TYPE_AFTER => Zurmo::t('Core', 'After'),
209  self::TYPE_ON => Zurmo::t('Core', 'On{date}', array('{date}' => null)),
210  self::TYPE_BETWEEN => Zurmo::t('Core', 'Between'),
211  self::TYPE_NEXT_7_DAYS => Zurmo::t('Core', 'Next 7 Days'),
212  self::TYPE_LAST_7_DAYS => Zurmo::t('Core', 'Last 7 Days'),
213  self::TYPE_LAST_30_DAYS => Zurmo::t('Core', 'Last 30 Days'),
214  self::TYPE_THIS_MONTH => Zurmo::t('Core', 'This Month'),
215  self::TYPE_LAST_MONTH => Zurmo::t('Core', 'Last Month'),
216  self::TYPE_NEXT_MONTH => Zurmo::t('Core', 'Next Month'),
217  self::TYPE_BEFORE_TODAY => Zurmo::t('Core', 'Before Today'),
218  );
219  }
220 
224  public static function getTimeBasedValueTypesAndLabels()
225  {
226  return array( self::TYPE_BEFORE => Zurmo::t('Core', 'Before'),
227  self::TYPE_AFTER => Zurmo::t('Core', 'After'),
228  self::TYPE_ON => Zurmo::t('Core', 'On{date}', array('{date}' => null)),
229  self::TYPE_BETWEEN => Zurmo::t('Core', 'Between'),
230  self::TYPE_IS_EMPTY => Zurmo::t('Core', 'Is Empty'),
231  self::TYPE_IS_NOT_EMPTY => Zurmo::t('Core', 'Is Not Empty'),
232  );
233  }
234 
238  public static function getTimeOnlyValueTypesAndLabels()
239  {
240  return array(self::TYPE_IS_TIME_FOR => Zurmo::t('Core', 'Is'));
241  }
242 
246  public static function getValueTypesRequiringFirstDateInput()
247  {
248  return array(self::TYPE_BEFORE, self::TYPE_AFTER, self::TYPE_ON, self::TYPE_BETWEEN, self::TYPE_WAS_ON,
249  self::TYPE_BECOMES_ON);
250  }
251 
255  public static function getValueTypesRequiringSecondDateInput()
256  {
257  return array(self::TYPE_BETWEEN);
258  }
259 
263  public static function getValueTypesWhereValueIsRequired()
264  {
265  return array( self::TYPE_BEFORE, self::TYPE_AFTER, self::TYPE_ON, self::TYPE_BETWEEN, self::TYPE_WAS_ON,
266  self::TYPE_BECOMES_ON);
267  }
268 
273  {
274  return array(self::TYPE_BETWEEN);
275  }
276 
283  public static function resolveAttributesAndRelations($attributeName, & $attributeAndRelations, $value)
284  {
285  assert('is_string($attributeName)');
286  assert('$attributeAndRelations == "resolveEntireMappingByRules"');
287  assert('empty($value) || $value == null || is_array($value)');
288  $delimiter = FormModelUtil::DELIMITER;
289  $parts = explode($delimiter, $attributeName);
290  if (count($parts) != 2)
291  {
292  throw new NotSupportedException();
293  }
294  list($realAttributeName, $type) = $parts;
295  if (isset($value['type']) && $value['type'] != null)
296  {
297  if ($value['type'] == self::TYPE_YESTERDAY ||
298  $value['type'] == self::TYPE_TODAY ||
299  $value['type'] == self::TYPE_TOMORROW)
300  {
301  $attributeAndRelations = array(array($realAttributeName, null, 'equals', 'resolveValueByRules'));
302  }
303  elseif ($value['type'] == self::TYPE_AFTER)
304  {
305  $attributeAndRelations = array(array($realAttributeName, null, 'greaterThanOrEqualTo', 'resolveValueByRules'));
306  }
307  elseif ($value['type'] == self::TYPE_BEFORE)
308  {
309  $attributeAndRelations = array(array($realAttributeName, null, 'lessThanOrEqualTo', 'resolveValueByRules'));
310  }
311  elseif ($value['type'] == self::TYPE_ON)
312  {
313  $attributeAndRelations = array(array($realAttributeName, null, 'equals', 'resolveValueByRules'));
314  }
315  elseif ($value['type'] == self::TYPE_BETWEEN)
316  {
317  $firstDateValue = static::resolveValueDataForBetweenIntoUsableFirstDateValue($value);
318  $secondDateValue = static::resolveValueDataForBetweenIntoUsableSecondDateValue($value);
319  $attributeAndRelations = array(array($realAttributeName, null, 'greaterThanOrEqualTo', $firstDateValue, true),
320  array($realAttributeName, null, 'lessThanOrEqualTo', $secondDateValue, true));
321  }
322  elseif ($value['type'] == self::TYPE_NEXT_7_DAYS)
323  {
324  $today = static::calculateNewDateByDaysFromNow(0);
325  $todayPlusSevenDays = static::calculateNewDateByDaysFromNow(7);
326  $attributeAndRelations = array(array($realAttributeName, null, 'greaterThanOrEqualTo', $today, true),
327  array($realAttributeName, null, 'lessThanOrEqualTo', $todayPlusSevenDays, true));
328  }
329  elseif ($value['type'] == self::TYPE_LAST_7_DAYS)
330  {
331  $today = static::calculateNewDateByDaysFromNow(0);
332  $todayMinusSevenDays = static::calculateNewDateByDaysFromNow(-7);
333  $attributeAndRelations = array(array($realAttributeName, null, 'greaterThanOrEqualTo', $todayMinusSevenDays, true),
334  array($realAttributeName, null, 'lessThanOrEqualTo', $today, true));
335  }
336  elseif ($value['type'] == self::TYPE_LAST_30_DAYS)
337  {
338  $today = static::calculateNewDateByDaysFromNow(0);
339  $todayMinusThirtyDays = static::calculateNewDateByDaysFromNow(-30);
340  $attributeAndRelations = array(array($realAttributeName, null, 'greaterThanOrEqualTo', $todayMinusThirtyDays, true),
341  array($realAttributeName, null, 'lessThanOrEqualTo', $today, true));
342  }
343  elseif ($value['type'] == self::TYPE_THIS_MONTH)
344  {
345  $firstDateValue = DateTimeUtil::getFirstDayOfAMonthDate();
346  $secondDateValue = DateTimeUtil::getLastDayOfAMonthDate();
347  $attributeAndRelations = array(array($realAttributeName, null, 'greaterThanOrEqualTo', $firstDateValue, true),
348  array($realAttributeName, null, 'lessThanOrEqualTo', $secondDateValue, true));
349  }
350  elseif ($value['type'] == self::TYPE_LAST_MONTH)
351  {
352  $firstDateValue = DateTimeUtil::getFirstDayOfLastMonthDate();
353  $secondDateValue = DateTimeUtil::getLastDayOfLastMonthDate();
354  $attributeAndRelations = array(array($realAttributeName, null, 'greaterThanOrEqualTo', $firstDateValue, true),
355  array($realAttributeName, null, 'lessThanOrEqualTo', $secondDateValue, true));
356  }
357  elseif ($value['type'] == self::TYPE_NEXT_MONTH)
358  {
359  $firstDateValue = DateTimeUtil::getFirstDayOfNextMonthDate();
360  $secondDateValue = DateTimeUtil::getLastDayOfNextMonthDate();
361  $attributeAndRelations = array(array($realAttributeName, null, 'greaterThanOrEqualTo', $firstDateValue, true),
362  array($realAttributeName, null, 'lessThanOrEqualTo', $secondDateValue, true));
363  }
364  elseif ($value['type'] == self::TYPE_BEFORE_TODAY)
365  {
366  $attributeAndRelations = array(array($realAttributeName, null, 'lessThan', 'resolveValueByRules'));
367  }
368  else
369  {
370  throw new NotSupportedException();
371  }
372  }
373  else
374  {
375  $attributeAndRelations = array(array($realAttributeName, null, null, 'resolveValueByRules'));
376  }
377  }
378 
384  public static function calculateNewDateByDaysFromNow($daysFromNow)
385  {
386  assert('is_int($daysFromNow)');
388  new DateTime(null, new DateTimeZone(Yii::app()->timeZoneHelper->getForCurrentUser())));
389  }
390  }
391 ?>
static resolveAttributesAndRelations($attributeName, &$attributeAndRelations, $value)
static calculateNewByDaysFromNow($daysFromNow, DateTime $dateTime)
static calculateNew($calculation, DateTime $dateTime)
Generated on Fri Jun 5 2020 07:10:27