Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
CalendarUtil.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 
38  {
45  public static function makeCalendarItemByModel(RedBeanModel $model, SavedCalendar $savedCalendar)
46  {
47  $calendarItem = new CalendarItem();
48  $startAttribute = $savedCalendar->startAttributeName;
49  $endAttribute = $savedCalendar->endAttributeName;
50  Yii::app()->custom->setCalendarItemTitle($calendarItem, $model);
51  $calendarItem->setStartDateTime($model->$startAttribute);
52  if ($endAttribute != null)
53  {
54  $calendarItem->setEndDateTime($model->$endAttribute);
55  }
56  $calendarItem->setCalendarId($savedCalendar->id);
57  $calendarItem->setModelClass(get_class($model));
58  $calendarItem->setModelId($model->id);
59  $calendarItem->setColor($savedCalendar->color);
60  $calendarItem->setModuleClassName($savedCalendar->moduleClassName);
61  return $calendarItem;
62  }
63 
68  public static function getDateRangeType()
69  {
71  }
72 
78  public static function getStartDate($dateRangeType)
79  {
80  assert('is_string($dateRangeType)');
81  if($dateRangeType == SavedCalendar::DATERANGE_TYPE_MONTH)
82  {
83  return DateTimeUtil::getFirstDayOfAMonthDate();
84  }
85  if($dateRangeType == SavedCalendar::DATERANGE_TYPE_WEEK)
86  {
88  }
89  if($dateRangeType == SavedCalendar::DATERANGE_TYPE_DAY)
90  {
92  }
93  }
94 
100  public static function getEndDate($dateRangeType)
101  {
102  assert('is_string($dateRangeType)');
103  if($dateRangeType == SavedCalendar::DATERANGE_TYPE_MONTH)
104  {
105  $dateTime = new DateTime();
106  $dateTime->modify('first day of next month');
107  return Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateFormat(),
108  $dateTime->getTimestamp());
109  }
110  if($dateRangeType == SavedCalendar::DATERANGE_TYPE_WEEK)
111  {
112  $dateTime = new DateTime('Monday next week');
113  return Yii::app()->dateFormatter->format(DatabaseCompatibilityUtil::getDateFormat(),
114  $dateTime->getTimestamp());
115  }
116  if($dateRangeType == SavedCalendar::DATERANGE_TYPE_DAY)
117  {
119  }
120  }
121 
127  public static function getUserSavedCalendars(User $user)
128  {
129  $metadata = array();
130  $metadata['clauses'] = array(
131  1 => array(
132  'attributeName' => 'owner',
133  'relatedAttributeName' => 'id',
134  'operatorType' => 'equals',
135  'value' => $user->id,
136  )
137  );
138  $metadata['structure'] = '1';
139  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('SavedCalendar');
140  $where = RedBeanModelDataProvider::makeWhere('SavedCalendar', $metadata, $joinTablesAdapter);
141  return SavedCalendar::getSubset($joinTablesAdapter, null, null, $where);
142  }
143 
153  public static function processUserCalendarsAndMakeDataProviderForCombinedView($myCalendarIds = null,
154  $mySubscribedCalendarIds = null,
155  $dateRangeType = null,
156  $startDate = null,
157  $endDate = null)
158  {
159  $savedCalendarSubscriptions = SavedCalendarSubscriptions::makeByUser(Yii::app()->user->userModel,
160  $myCalendarIds,
161  $mySubscribedCalendarIds);
162  if($dateRangeType == null)
163  {
164  $dateRangeType = CalendarUtil::getDateRangeType();
165  ZurmoConfigurationUtil::setByUserAndModuleName(Yii::app()->user->userModel,
166  'CalendarsModule',
167  'myCalendarDateRangeType', $dateRangeType);
168  }
169  if($startDate == null)
170  {
171  $startDate = CalendarUtil::getStartDate($dateRangeType);
172  ZurmoConfigurationUtil::setByUserAndModuleName(Yii::app()->user->userModel,
173  'CalendarsModule',
174  'myCalendarStartDate', $startDate);
175  }
176  if($endDate == null)
177  {
178  $endDate = CalendarUtil::getEndDate($dateRangeType);
179  ZurmoConfigurationUtil::setByUserAndModuleName(Yii::app()->user->userModel,
180  'CalendarsModule',
181  'myCalendarEndDate', $endDate);
182  }
183  return CalendarItemsDataProviderFactory::getDataProviderByDateRangeType($savedCalendarSubscriptions,
184  $startDate, $endDate, $dateRangeType);
185  }
186 
192  public static function getFullCalendarItems(CalendarItemsDataProvider $dataProvider)
193  {
194  $fullCalendarItems = self::processDataProviderAndGetCalendarItems($dataProvider);
195  return self::processCalendarItemsAndAddMoreEventsIfRequired($fullCalendarItems);
196  }
197 
202  public static function processCalendarItemsAndAddMoreEventsIfRequired($fullCalendarItems)
203  {
204  $modifiedCalendarItems = array();
205  $dateToCalendarItemsCountData = array();
206  $moreEventsItemCreatedByDate = array();
207  foreach ($fullCalendarItems as $key => $fullCalItem)
208  {
209  $startDate = date('Y-m-d', strtotime($fullCalItem['start']));
210  //Check for the count of cal items on a given start date and if more than max add more events
211  if (isset($dateToCalendarItemsCountData[$startDate])
212  && (count($dateToCalendarItemsCountData[$startDate]) >= CalendarItemsDataProvider::MAXIMUM_CALENDAR_ITEMS_DISPLAYED_FOR_ANY_DATE))
213  {
214  if (in_array($startDate, $moreEventsItemCreatedByDate) === false)
215  {
216  $fullCalItem = self::createMoreEventsCalendarItem($fullCalItem, $key, $fullCalendarItems);
217  $moreEventsItemCreatedByDate[] = $startDate;
218  }
219  else
220  {
221  continue;
222  }
223  }
224  $fullCalItem['start'] = self::getFullCalendarFormattedDateTimeElement($fullCalItem['start']);
225  if (isset($fullCalItem['end']))
226  {
227  $fullCalItem['end'] = self::getFullCalendarFormattedDateTimeElement($fullCalItem['end']);
228  }
229  $dateToCalendarItemsCountData[$startDate][] = $fullCalItem;
230  $modifiedCalendarItems[] = $fullCalItem;
231  }
232  return $modifiedCalendarItems;
233  }
234 
240  public static function createMoreEventsCalendarItem($fullCalItem, $key, $fullCalendarItems)
241  {
242  $moreEventsCalItem = array();
243  $moreEventsCalItem['title'] = Zurmo::t('CalendarsModule', 'More Events..');
244  $moreEventsCalItem['start'] = $fullCalItem['start'];
245  $moreEventsCalItem['end'] = $fullCalItem['end'];
246  $moreEventsCalItem['color'] = '#cccccc';
247  $moreEventsCalItem['className'] = 'more-events';
248  return $moreEventsCalItem;
249  }
250 
257  public static function compareCalendarItemsByDateTime($firstDate, $secondDate)
258  {
259  assert('is_array($firstDate)');
260  assert('is_array($secondDate)');
261  $firstDateUnixTimestamp = strtotime($firstDate['start']);
262  $secondDateUnixTimestamp = strtotime($secondDate['start']);
263  return $firstDateUnixTimestamp - $secondDateUnixTimestamp;
264  }
265 
271  public static function getFullCalendarFormattedDateTimeElement($dateTime)
272  {
273  assert('is_string($dateTime)');
274  //The reason its put because timezone can vary from -12:00 to +12:00 max so
275  //if we offset the gmt date by timezoneoffset, on applying offset, correct results
276  //would come.
277  if (DateTimeUtil::isValidDbFormattedDate($dateTime))
278  {
280  }
281  $dateTimeObject = new DateTime();
282  $dateTimeObject->setTimestamp(strtotime($dateTime));
284  if ($offset < 0)
285  {
286  $offset = abs($offset);
287  $dateTimeObject->sub(new DateInterval('PT' . $offset . 'S'));
288  }
289  else
290  {
291  $dateTimeObject->add(new DateInterval('PT' . $offset . 'S'));
292  }
293  return $dateTimeObject->format('c');
294  }
295 
303  public static function getUsedCalendarColorsByUser(User $user, $modelClassName, $attributeName)
304  {
305  assert('is_string($modelClassName)');
306  assert('is_string($attributeName)');
307  $selectDistinct = false;
308  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter($modelClassName);
309  $selectQueryAdapter = new RedBeanModelSelectQueryAdapter($selectDistinct);
310  $selectQueryAdapter->addClause($modelClassName::getTableName(), 'color');
311  $metadata = array();
312  $metadata['clauses'] = array(
313  1 => array(
314  'attributeName' => $attributeName,
315  'relatedAttributeName' => 'id',
316  'operatorType' => 'equals',
317  'value' => $user->id,
318  )
319  );
320  $metadata['structure'] = '1';
321  $where = RedBeanModelDataProvider::makeWhere($modelClassName, $metadata, $joinTablesAdapter);
322  $sql = SQLQueryUtil::makeQuery($modelClassName::getTableName(), $selectQueryAdapter, $joinTablesAdapter, null, null, $where);
323  $records = ZurmoRedBean::getAll($sql);
324  $colors = array();
325  foreach ($records as $record)
326  {
327  if ($record['color'] != null && $record['color'] != '')
328  {
329  $colors[] = $record['color'];
330  }
331  }
332  return $colors;
333  }
334 
338  public static function getModalContainerId()
339  {
340  return ModalContainerView::ID;
341  }
342 
346  public static function resolveAjaxOptionsForModalView()
347  {
348  $title = Zurmo::t('Calendarsmodule', 'Shared Calendars');
349  return ModalView::getAjaxOptionsForModalLink($title, self::getModalContainerId(), 'auto', 600,
350  array('my'=>'center top+25','at'=>'center top+25'), $class = "''");
351  }
352 
359  public static function registerSharedCalendarModalScript($url, $selector)
360  {
361  assert('is_string($url)');
362  assert('is_string($selector)');
365  $ajaxOptions['beforeSend'] = new CJavaScriptExpression($ajaxOptions['beforeSend']);
366  return "$(document).on('click', '{$selector}', function()
367  {
368  $.ajax(
369  {
370  'type' : 'GET',
371  'url' : '{$url}',
372  'beforeSend' : {$ajaxOptions['beforeSend']},
373  'update' : '{$ajaxOptions['update']}',
374  'success': function(html){jQuery('#{$modalId}').html(html)}
375  });
376  }
377  );";
378  }
379 
385  public static function getUserSubscribedCalendars(User $user)
386  {
387  $searchAttributeData = array();
388  $searchAttributeData['clauses'] = array(
389  1 => array(
390  'attributeName' => 'user',
391  'relatedAttributeName' => 'id',
392  'operatorType' => 'equals',
393  'value' => intval($user->id),
394  )
395  );
396  $searchAttributeData['structure'] = '1';
397  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('SavedCalendarSubscription');
398  $where = RedBeanModelDataProvider::makeWhere('SavedCalendarSubscription', $searchAttributeData, $joinTablesAdapter);
399  $models = SavedCalendarSubscription::getSubset($joinTablesAdapter, null, null, $where, null);
400  return $models;
401  }
402 
411  public static function makeCalendarItemsList($data, $field, $itemClass, $type)
412  {
413  assert('is_array($data)');
414  assert('is_string($field)');
415  assert('is_string($itemClass)');
416  assert('is_string($type)');
417  $itemsContent = null;
418  foreach ($data as $calendarArray)
419  {
420  $isChecked = false;
421  if ($calendarArray[1] === true)
422  {
423  $isChecked = true;
424  }
425  $input = ZurmoHtml::checkBox($field,
426  $isChecked,
427  array('value' => $calendarArray[0]->id,
428  'class' => $itemClass));
429  $color = ZurmoHtml::tag('span', array('class' => 'cal-color', 'style' => 'background:' .
430  $calendarArray[0]->color), '');
431  if ($type == 'saved')
432  {
433  $label = $calendarArray[0]->name;
434  $options = self::getSavedCalendarOptions($calendarArray[0]->id);
435  $subscriptionData = null;
436  }
437  else
438  {
439  $savedCalendar = $calendarArray[0]->savedcalendar;
440  $label = $savedCalendar->name;
441  $options = self::getSharedCalendarOptions($calendarArray[0]->id);
442  $subscriptionData = CalendarUtil::getCalendarSubscriberData($calendarArray[0]->savedcalendar);
443  }
444  $label = ZurmoHtml::tag('strong', array('class' => 'cal-name'), $label);
445  $itemsContent .= ZurmoHtml::tag('li', array(), $input . $color . $label . $subscriptionData . $options);
446  }
447  return ZurmoHtml::tag('ul', array('class' => 'calendars-list'), $itemsContent);
448  }
449 
455  public static function getSharedCalendarOptions($savedCalendarSubscriptionId)
456  {
457  assert('is_int($savedCalendarSubscriptionId)');
458  $elementContent = ZurmoHtml::tag('li', array(),
459  ZurmoHtml::link(ZurmoHtml::tag('span', array(), Zurmo::t('Core', 'Unsubscribe')), '#',
460  array('data-value' => $savedCalendarSubscriptionId,
461  'class' => 'shared-cal-unsubscribe')));
462  $elementContent = ZurmoHtml::tag('ul', array(), $elementContent);
463  $content = ZurmoHtml::tag('li', array('class' => 'parent last'),
464  ZurmoHtml::link('<span></span>', 'javascript:void(0);') . $elementContent);
465  $content = ZurmoHtml::tag('ul', array('class' => 'options-menu edit-row-menu nav'), $content);
466  return $content;
467  }
468 
474  public static function getSavedCalendarOptions($calendarId)
475  {
476  assert('is_int($calendarId)');
477  $editUrl = Yii::app()->createUrl('calendars/default/edit', array('id' => $calendarId));
478  $editLinkContent = ZurmoHtml::tag('li', array(),
479  ZurmoHtml::link(ZurmoHtml::tag('span', array(), Zurmo::t('Core', 'Edit')), $editUrl,
480  array('data-value' => $calendarId,
481  'class' => 'my-cal-edit')));
482  $deleteLinkContent = ZurmoHtml::tag('li', array(),
483  ZurmoHtml::link(ZurmoHtml::tag('span', array(), Zurmo::t('Core', 'Delete')), '#',
484  array('data-value' => $calendarId,
485  'class' => 'my-cal-delete')));
486  $elementContent = ZurmoHtml::tag('ul', array(), $editLinkContent . $deleteLinkContent);
487  $content = ZurmoHtml::tag('li', array('class' => 'parent last'),
488  ZurmoHtml::link('<span></span>', 'javascript:void(0);') . $elementContent);
489  $content = ZurmoHtml::tag('ul', array('class' => 'options-menu edit-row-menu nav'), $content);
490  return $content;
491  }
492 
498  public static function getCalendarItemsDataProvider(User $user)
499  {
500  $mySavedCalendarIds = ZurmoConfigurationUtil::getByUserAndModuleName($user,
501  'CalendarsModule', 'myCalendarSelections');
502  $mySubscribedCalendarIds = ZurmoConfigurationUtil::getByUserAndModuleName($user,
503  'CalendarsModule', 'mySubscribedCalendarSelections');
504  $dateRangeType = ZurmoConfigurationUtil::getByUserAndModuleName($user,
505  'CalendarsModule', 'myCalendarDateRangeType');
507  'CalendarsModule', 'myCalendarStartDate');
509  'CalendarsModule', 'myCalendarEndDate');
511  $mySubscribedCalendarIds,
512  $dateRangeType,
513  $startDate,
514  $endDate);
515  }
516 
522  public static function getUsersSubscribedForCalendar(SavedCalendar $subscribedCalendar)
523  {
524  $searchAttributeData = array();
525  $users = array();
526  $searchAttributeData['clauses'] = array(
527  1 => array(
528  'attributeName' => 'savedcalendar',
529  'relatedAttributeName' => 'id',
530  'operatorType' => 'equals',
531  'value' => intval($subscribedCalendar->id),
532  )
533  );
534  $searchAttributeData['structure'] = '1';
535  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('SavedCalendarSubscription');
536  $where = RedBeanModelDataProvider::makeWhere('SavedCalendarSubscription', $searchAttributeData, $joinTablesAdapter);
537  $models = SavedCalendarSubscription::getSubset($joinTablesAdapter, null, null, $where, null);
538  foreach ($models as $model)
539  {
540  $users[] = $model->user;
541  }
542  return $users;
543  }
544 
550  public static function getCalendarSubscriberData(SavedCalendar $subscribedCalendar)
551  {
552  $users = CalendarUtil::getUsersSubscribedForCalendar($subscribedCalendar);
553  $content = null;
554  $alreadySubscribedUsers = array();
555  foreach ($users as $user)
556  {
557  //Take care of duplicates if any
558  if (!in_array($user->id, $alreadySubscribedUsers))
559  {
561  $alreadySubscribedUsers[] = $user->id;
562  }
563  }
564 
565  return $content;
566  }
567 
571  public static function registerSelectCalendarScript($startDate, $endDate)
572  {
573  //refer to http://stackoverflow.com/questions/9801095/jquery-fullcalendar-send-custom-parameter-and-refresh-calendar-with-json
574  $script = "$(document).on('click', '.mycalendar,.sharedcalendar',
575  function(){
576  $('#calendar').fullCalendar('refetchEvents');
577  }
578  );";
579  $cs = Yii::app()->getClientScript();
580  $cs->registerScript('mycalendarselectscript', $script);
581  }
582 
588  public static function getAlreadyUsedColorsByUser(User $user)
589  {
590  $savedCalUsedColors = CalendarUtil::getUsedCalendarColorsByUser($user, 'SavedCalendar', 'createdByUser');
591  $sharedCalUsedColors = CalendarUtil::getUsedCalendarColorsByUser($user, 'SavedCalendarSubscription', 'user');
592  return CMap::mergeArray($savedCalUsedColors, $sharedCalUsedColors);
593  }
594 
600  public static function setMyCalendarColor(SavedCalendar $savedCalendar, User $user)
601  {
602  if($savedCalendar->color == null)
603  {
604  $usedColors = CalendarUtil::getAlreadyUsedColorsByUser($user);
605  self::processAndSaveColor($savedCalendar, $usedColors);
606  }
607  }
608 
613  public static function setSharedCalendarColor(SavedCalendarSubscription $sharedCalendar)
614  {
615  if($sharedCalendar->color == null)
616  {
617  $usedColors = CalendarUtil::getAlreadyUsedColorsByUser(Yii::app()->user->userModel);
618  self::processAndSaveColor($sharedCalendar, $usedColors);
619  }
620  }
621 
627  public static function processAndSaveColor($calendar, $usedColors)
628  {
629  assert('$calendar instanceof SavedCalendar || $calendar instanceof SavedCalendarSubscription');
630  assert('is_array($usedColors)');
631  $availableColors = SavedCalendar::$colorsArray;
632  $filteredColors = array_diff($availableColors, $usedColors);
633  $color = array_shift($filteredColors);
634  $calendar->color = $color;
635  $calendar->save();
636  }
637 
643  public static function registerSavedCalendarDeleteScript($startDate, $endDate)
644  {
645  assert('is_string($startDate)');
646  assert('is_string($endDate)');
647  $url = Yii::app()->createUrl('calendars/default/delete');
648  $eventsUrl = Yii::app()->createUrl('calendars/default/getEvents');
650  $confirmTitle = Zurmo::t('Core', 'Are you sure you want to delete this {modelLabel}?',
651  array('{modelLabel}' => Zurmo::t('CalendarsModule', 'CalendarsModuleSingularLabel', $params)));
652  // Begin Not Coding Standard
653 
654  $script = "$(document).on('click', '.my-cal-delete', function()
655  {
656  if (!confirm('{$confirmTitle}'))
657  {
658  return false;
659  }
660  var calId = $(this).data('value');
661  $.ajax(
662  {
663  'type' : 'GET',
664  'url' : '{$url}' + '?id=' + calId,
665  beforeSend: function(xhr)
666  {
667  $('#my-calendars-list').html('');
668  $(this).makeLargeLoadingSpinner(true, '#my-calendars-list');
669  },
670  success : function(data)
671  {
672  $('#my-calendars-list').html(data);
673  $(this).makeLargeLoadingSpinner(false, '#my-calendars-list');
674  $('#calendar').fullCalendar('removeEventSource', getCalendarEvents('{$eventsUrl}', 'calendar'));
675  $('#calendar').fullCalendar('addEventSource', getCalendarEvents('{$eventsUrl}', 'calendar'));
676  }
677  });
678 
679  }
680  );";
681 
682  // End Not Coding Standard
683  $cs = Yii::app()->getClientScript();
684  if($cs->isScriptRegistered('calDeleteScript', ClientScript::POS_END) === false)
685  {
686  $cs->registerScript('calDeleteScript', $script, ClientScript::POS_END);
687  }
688  }
689 
695  public static function registerCalendarUnsubscriptionScript($startDate, $endDate)
696  {
697  assert('is_string($startDate)');
698  assert('is_string($endDate)');
699  $url = Yii::app()->createUrl('/calendars/default/unsubscribe');
700  $eventsUrl = Yii::app()->createUrl('calendars/default/getEvents');
701  // Begin Not Coding Standard
702 
703  $script = "$(document).on('click', '.shared-cal-unsubscribe', function(){
704  $.ajax(
705  {
706  type : 'GET',
707  url : '{$url}',
708  data : {'id' : $(this).data('value')},
709  beforeSend: function(xhr)
710  {
711  $('#shared-calendars-list').html('');
712  $(this).makeLargeLoadingSpinner(true, '#shared-calendars-list');
713  },
714  success : function(data)
715  {
716  $('#shared-calendars-list').html(data);
717  $(this).makeLargeLoadingSpinner(false, '#shared-calendars-list');
718  $('#calendar').fullCalendar('removeEventSource', getCalendarEvents('{$eventsUrl}', 'calendar'));
719  $('#calendar').fullCalendar('addEventSource', getCalendarEvents('{$eventsUrl}', 'calendar'));
720  }
721  }
722  );
723  })";
724 
725  // End Not Coding Standard
726  $cs = Yii::app()->getClientScript();
727  $cs->registerScript('calunsubscribescript', $script, ClientScript::POS_END);
728  }
729 
735  public static function getModelAttributesForSelectedModule($moduleClassName)
736  {
737  $modelClassName = $moduleClassName::getPrimaryModelName();
738  $adapter = new ModelAttributesAdapter(new $modelClassName(false));
739  $attributes = $adapter->getAttributes();
740  $selectedAttributes = array();
741  foreach ($attributes as $attribute => $value)
742  {
743  if ($value['elementType'] == 'DateTime' || $value['elementType'] == 'Date')
744  {
745  $selectedAttributes[$attribute] = $value['attributeLabel'];
746  }
747  }
748  return $selectedAttributes;
749  }
750 
757  public static function makeComponentFormAndPopulateReportFromData($componentFormsData, Report $report, $componentPrefix)
758  {
759  assert('is_string($componentPrefix)');
760  assert('is_array($componentFormsData)');
761  $moduleClassName = $report->getModuleClassName();
762  $addMethodName = 'add' . $componentPrefix;
763  $componentClassName = $componentPrefix . 'ForReportForm';
764  $rowKey = 0;
765  foreach ($componentFormsData as $componentFormData)
766  {
767  $component = new $componentClassName($moduleClassName,
768  $moduleClassName::getPrimaryModelName(),
769  $report->getType(),
770  $rowKey);
771  $component->setAttributes($componentFormData);
772  $report->{$addMethodName}($component);
773  $rowKey++;
774  }
775  }
776 
784  public static function saveCalendarWithSerializedData(Report $report, SavedCalendar $savedCalendar, $wizardFormPostData)
785  {
786  $filtersData = ArrayUtil::getArrayValue($wizardFormPostData, ComponentForReportForm::TYPE_FILTERS);
787  if ($filtersData != null)
788  {
789  $sanitizedFiltersData = DataToReportUtil::sanitizeFiltersData($report->getModuleClassName(),
790  $report->getType(),
791  $filtersData);
792  $unserializedData = array(ComponentForReportForm::TYPE_FILTERS => $sanitizedFiltersData,
793  'filtersStructure' => $report->getFiltersStructure());
794  $savedCalendar->serializedData = serialize($unserializedData);
795  }
796  if (!$savedCalendar->save())
797  {
798  throw new FailedToSaveModelException();
799  }
800  }
801 
809  public static function resolveReportBySavedCalendarPostData($type, $id = null, $postData)
810  {
811  assert('is_string($type)');
812  assert('is_array($postData)');
813  if ($id == null)
814  {
815  $report = new Report();
816  $report->setType($type);
817  }
818  else
819  {
820  $savedCalendar = SavedCalendar::getById(intval($id));
823  }
824  if (isset($postData['SavedCalendar']) && isset($postData['SavedCalendar']['moduleClassName']))
825  {
826  $report->setModuleClassName($postData['SavedCalendar']['moduleClassName']);
827  }
828  else
829  {
830  throw new NotSupportedException();
831  }
834  return $report;
835  }
836 
841  public static function getAvailableModulesForCalendar()
842  {
843  $moduleClassNames = array();
844  foreach (self::getCalendarModulesClassNamesCurrentUserHasAccessTo() as $moduleClassName)
845  {
846  $label = $moduleClassName::getModuleLabelByTypeAndLanguage('Plural');
847  $moduleClassNames[$moduleClassName] = $label;
848  }
849  return $moduleClassNames;
850  }
851 
856  {
857  $moduleClassNames = array();
858  $modules = Module::getModuleObjects();
859  foreach ($modules as $module)
860  {
861  if ($module::canShowOnCalendar())
862  {
863  if (RightsUtil::canUserAccessModule(get_class($module), Yii::app()->user->userModel))
864  {
865  $moduleClassNames[] = get_class($module);
866  }
867  }
868  }
869  return $moduleClassNames;
870  }
871 
881  public static function processAndGetDataProviderForEventsData($selectedMyCalendarIds = null,
882  $selectedSharedCalendarIds = null,
883  $startDate = null,
884  $endDate = null,
885  $dateRangeType = null,
886  $isSticky = true)
887  {
888  if ($isSticky)
889  {
890  ZurmoConfigurationUtil::setByUserAndModuleName(Yii::app()->user->userModel,
891  'CalendarsModule',
892  'myCalendarStartDate', $startDate);
893  ZurmoConfigurationUtil::setByUserAndModuleName(Yii::app()->user->userModel,
894  'CalendarsModule',
895  'myCalendarEndDate', $endDate);
896  ZurmoConfigurationUtil::setByUserAndModuleName(Yii::app()->user->userModel,
897  'CalendarsModule',
898  'myCalendarDateRangeType', $dateRangeType);
899  }
901  $selectedSharedCalendarIds,
902  $dateRangeType,
903  $startDate,
904  $endDate);
905  }
906 
911  public static function loadDefaultCalendars(User $user)
912  {
913  $name = Zurmo::t('CalendarsModule', 'My Meetings');
914  $mtgCalendar = self::populateSavedCalendar($user, $name, 'MeetingsModule', 'startDateTime', 'endDateTime');
915  $name = Zurmo::t('CalendarsModule', 'My Tasks');
916  $taskCalendar = self::populateSavedCalendar($user, $name, 'TasksModule', 'createdDateTime');
917  return array($mtgCalendar, $taskCalendar);
918  }
919 
929  public static function populateSavedCalendar(User $user,
930  $name,
931  $moduleClassName,
932  $startAttributeName,
933  $endAttributeName = '')
934  {
935  $savedCalendar = new SavedCalendar();
936  $savedCalendar->name = $name;
937  $savedCalendar->timeZone = $user->timeZone;
938  $savedCalendar->location = 'Chicago';
939  $savedCalendar->moduleClassName = $moduleClassName;
940  $savedCalendar->startAttributeName = $startAttributeName;
941  $savedCalendar->endAttributeName = $endAttributeName;
942  assert($savedCalendar->save()); // Not Coding Standard
943  CalendarUtil::setMyCalendarColor($savedCalendar, $user);
944  $filtersData = array('filtersStructure' => '1',
945  'Filters' => array(
946  array('attributeIndexOrDerivedType' => 'owner__User',
947  'structurePosition' => '1',
948  'operator' => 'equals',
949  'value' => $user->id,
950  'stringifiedModelForValue' => strval($user),
951  'availableAtRunTime' => '0')
952  )
953  );
954  CalendarUtil::populateFiltersDataInModel($savedCalendar, $filtersData);
955  assert($savedCalendar->save()); // Not Coding Standard
956  return $savedCalendar;
957  }
958 
965  public static function populateFiltersDataInModel(SavedCalendar $model, $data)
966  {
969  DataToReportUtil::resolveFilters($data, $report);
970  if (count($filtersData = ArrayUtil::getArrayValue($data, ComponentForReportForm::TYPE_FILTERS)) > 0)
971  {
972  $sanitizedFiltersData = DataToReportUtil::sanitizeFiltersData($report->getModuleClassName(),
973  $report->getType(),
974  $filtersData);
975  $unserializedData = array(ComponentForReportForm::TYPE_FILTERS => $sanitizedFiltersData,
976  'filtersStructure' => $report->getFiltersStructure());
977  $model->serializedData = serialize($unserializedData);
978  }
979  else
980  {
981  $model->serializedData = null;
982  }
983  }
984 
991  {
992  $calendarItems = $dataProvider->getData(true);
993  $fullCalendarItems = array();
994  for ($k = 0; $k < count($calendarItems); $k++)
995  {
996  $fullCalendarItem = array();
997  $calItem = $calendarItems[$k];
998  $fullCalendarItem['title'] = $calItem->getTitle();
999  $fullCalendarItem['start'] = $calItem->getStartDateTime();
1000  if (!DateTimeUtil::isDateTimeStringNull($calItem->getEndDateTime()))
1001  {
1002  $fullCalendarItem['end'] = $calItem->getEndDateTime();
1003  }
1004  else
1005  {
1006  $fullCalendarItem['end'] = '';
1007  }
1008  $fullCalendarItem['color'] = $calItem->getColor();
1009  $fullCalendarItem['modelClass'] = $calItem->getModelClass();
1010  $fullCalendarItem['modelId'] = $calItem->getModelId();
1011  $fullCalendarItem['calendarId'] = $calItem->getCalendarId();
1012  $fullCalendarItem['allDay'] = true;
1013  $fullCalendarItems[] = $fullCalendarItem;
1014  }
1015  if (count($fullCalendarItems) > 0)
1016  {
1017  ArrayUtil::sortArrayByElementField('compareCalendarItemsByDateTime', 'usort', $fullCalendarItems, 'CalendarUtil');
1018  }
1019  return $fullCalendarItems;
1020  }
1021 
1027  public static function populateDetailsUrlForCalendarItems($items)
1028  {
1029  assert('is_array($items)');
1030  $moduleClassNames = CalendarUtil::getAvailableModulesForCalendar();
1031  foreach ($items as $index => $item)
1032  {
1033  foreach ($moduleClassNames as $moduleClassName => $label)
1034  {
1035  if ($moduleClassName::getPrimaryModelName() == $item['modelClass'])
1036  {
1037  $moduleId = $moduleClassName::getDirectoryName();
1038  $item['detailsUrl'] = Yii::app()->createUrl($moduleId . '/default/details', array('id' => $item['modelId']));
1039  $items[$index] = $item;
1040  }
1041  }
1042  }
1043  return $items;
1044  }
1045  }
1046 ?>
static getTomorrowsDate()
static getModelAttributesForSelectedModule($moduleClassName)
static processUserCalendarsAndMakeDataProviderForCombinedView($myCalendarIds=null, $mySubscribedCalendarIds=null, $dateRangeType=null, $startDate=null, $endDate=null)
static resolveReportByWizardPostData(Report $report, $postData, $wizardFormClassName)
static checkBox($name, $checked=false, $htmlOptions=array())
Definition: ZurmoHtml.php:267
static sortArrayByElementField($compareFunction, $sortFunctionName, &$data, $className)
Definition: ArrayUtil.php:246
static processDataProviderAndGetCalendarItems(CalendarItemsDataProvider $dataProvider)
static renderSubscriberImageAndLinkContent(User $user, $imageSize=36, $class=null)
static getUserSubscribedCalendars(User $user)
static makeCalendarItemsList($data, $field, $itemClass, $type)
static getModalContainerId()
static getSavedCalendarOptions($calendarId)
static getTranslationParamsForAllModules()
Definition: LabelUtil.php:75
static setMyCalendarColor(SavedCalendar $savedCalendar, User $user)
static registerSelectCalendarScript($startDate, $endDate)
Definition: User.php:37
static setByUserAndModuleName($user, $moduleName, $key, $value, $cache=true)
getType()
Definition: Report.php:373
static getUserSavedCalendars(User $user)
static populateSavedCalendar(User $user, $name, $moduleClassName, $startAttributeName, $endAttributeName= '')
static getCalendarModulesClassNamesCurrentUserHasAccessTo()
static getAvailableModulesForCalendar()
static populateFiltersDataInModel(SavedCalendar $model, $data)
static getStartDate($dateRangeType)
static registerSavedCalendarDeleteScript($startDate, $endDate)
static resolveFiltersStructure($data, Report $report)
static convertDateToDateTimeByTimeZoneOffset($date)
static processAndGetDataProviderForEventsData($selectedMyCalendarIds=null, $selectedSharedCalendarIds=null, $startDate=null, $endDate=null, $dateRangeType=null, $isSticky=true)
static getEndDate($dateRangeType)
static getTodaysDate()
static getModuleObjects()
Definition: Module.php:51
static getDataProviderByDateRangeType($savedCalendarSubscriptions, $startDate, $endDate, $dateRangeType)
static compareCalendarItemsByDateTime($firstDate, $secondDate)
static sanitizeFiltersData($moduleClassName, $reportType, array $filtersData)
static getArrayValue($array, $element, $defaultValue=null)
Definition: ArrayUtil.php:45
static processCalendarItemsAndAddMoreEventsIfRequired($fullCalendarItems)
static makeWhere($modelClassName, array $metadata, &$joinTablesAdapter)
static saveCalendarWithSerializedData(Report $report, SavedCalendar $savedCalendar, $wizardFormPostData)
static getCalendarSubscriberData(SavedCalendar $subscribedCalendar)
static makeReportBySavedCalendar(SavedCalendar $savedCalendar)
static makeCalendarItemByModel(RedBeanModel $model, SavedCalendar $savedCalendar)
static resolveReportBySavedCalendarPostData($type, $id=null, $postData)
getFiltersStructure()
Definition: Report.php:331
static createMoreEventsCalendarItem($fullCalItem, $key, $fullCalendarItems)
static populateDetailsUrlForCalendarItems($items)
static getFirstDayOfAWeek($stringTime=null)
static getFullCalendarFormattedDateTimeElement($dateTime)
static loadDefaultCalendars(User $user)
static getSubset(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $offset=null, $count=null, $where=null, $orderBy=null, $modelClassName=null, $selectDistinct=false)
static canUserAccessModule($moduleClassName, $user)
Definition: RightsUtil.php:103
static getByUserAndModuleName($user, $moduleName, $key, $cache=true)
static makeComponentFormAndPopulateReportFromData($componentFormsData, Report $report, $componentPrefix)
static getUsedCalendarColorsByUser(User $user, $modelClassName, $attributeName)
getModuleClassName()
Definition: Report.php:288
static makeByUser(User $user, $selectedCalendarIds=null, $subscribedCalendarIds=null)
static registerCalendarUnsubscriptionScript($startDate, $endDate)
static getSharedCalendarOptions($savedCalendarSubscriptionId)
const DATERANGE_TYPE_MONTH
static getDateRangeType()
static getById($id, $modelClassName=null)
static resolveFilters($data, Report $report, $shouldRemoveOnlyRuntimeFilters=false)
static resolveAjaxOptionsForModalView()
static registerSharedCalendarModalScript($url, $selector)
static getFullCalendarItems(CalendarItemsDataProvider $dataProvider)
static setSharedCalendarColor(SavedCalendarSubscription $sharedCalendar)
static getUsersSubscribedForCalendar(SavedCalendar $subscribedCalendar)
static getAlreadyUsedColorsByUser(User $user)
static processAndSaveColor($calendar, $usedColors)
static getCalendarItemsDataProvider(User $user)
static resolveAccessCanCurrentUserWriteModel(RedBeanModel $model, $fromAjax=false)
Generated on Sun Aug 9 2020 07:10:29
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.