1: <?php
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: Yii::import('application.components.Ical');
38:
39: 40: 41: 42: 43: 44:
45: class CalendarController extends x2base {
46:
47: private $_currentUser;
48:
49: public $modelClass = 'X2Calendar';
50: public $calendarUsers = null;
51: public $groupCalendars = null;
52: public $sharedCalendars = null;
53: public $googleCalendars = null;
54: public $calendarFilter = null;
55:
56: public function accessRules(){
57: return array(
58: array(
59: 'allow',
60: 'actions' => array('getItems'),
61: 'users' => array('*'),
62: ),
63: array(
64: 'allow',
65: 'actions' => array(
66: 'index',
67: 'jsonFeed',
68: 'jsonFeedGroup',
69: 'jsonFeedShared',
70: 'jsonFeedGoogle',
71: 'myCalendarPermissions',
72: 'create',
73: 'update',
74: 'list',
75: 'delete',
76: 'createEvent',
77: 'view',
78: 'viewAction',
79: 'editAction',
80: 'viewGoogleEvent',
81: 'editGoogleEvent',
82: 'moveAction',
83: 'moveGoogleEvent',
84: 'resizeAction',
85: 'resizeGoogleEvent',
86: 'saveAction',
87: 'saveGoogleEvent',
88: 'deleteGoogleEvent',
89: 'completeAction',
90: 'uncompleteAction',
91: 'deleteAction',
92: 'saveCheckedCalendar',
93: 'saveCheckedCalendarFilter',
94: 'syncActionsToGoogleCalendar',
95: 'toggleUserCalendarsVisible',
96: 'togglePortletVisible',
97:
98: ),
99: 'users' => array('@'),
100: ),
101: array(
102: 'allow',
103: 'actions' => array('admin', 'userCalendarPermissions'),
104: 'users' => array('admin'),
105: ),
106: array(
107: 'deny',
108: 'users' => array('*'),
109: ),
110: );
111: }
112:
113: 114: 115:
116: public function actionIndex(){
117:
118:
119: $this->initCheckedCalendars();
120: $this->render('calendar', array());
121: }
122:
123: 124: 125:
126: public function actionAdmin(){
127: $this->initCheckedCalendars();
128: $this->render('calendar');
129: }
130:
131: public function actionView($id){
132: if($id == 0)
133: $this->redirect(array('index'));
134: else{
135: $model = X2Calendar::model()->findByPk($id);
136: parent::view($model, 'calendar');
137: }
138: }
139:
140:
141:
142: 143: 144:
145: public function actionMyCalendarPermissions(){
146:
147: if(isset($_POST['save-button'])){
148:
149:
150: X2CalendarPermissions::model()->deleteAllByAttributes(array('user_id' => Yii::app()->user->id));
151:
152: $viewPermission = array();
153: $editPermission = array();
154:
155:
156: if(isset($_POST['view-permission'])){
157: $viewPermission = $_POST['view-permission'];
158: }
159: if(isset($_POST['edit-permission'])){
160: $editPermission = $_POST['edit-permission'];
161: }
162:
163: $users = User::model()->findAll(array('select' => 'id'));
164: foreach($users as $user){
165: $view = in_array($user->id, $viewPermission);
166: $edit = in_array($user->id, $editPermission);
167:
168: $permission = new X2CalendarPermissions;
169: $permission->user_id = Yii::app()->user->id;
170: $permission->other_user_id = $user->id;
171: $permission->view = $view;
172: $permission->edit = $edit;
173: $permission->save();
174: }
175: }
176:
177: $this->render('myCalendarPermissions');
178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206:
207: }
208:
209: 210: 211:
212: public function actionUserCalendarPermissions(){
213: if(isset($_POST['user-id'])){
214: $id = $_POST['user-id'];
215:
216:
217: X2CalendarPermissions::model()->deleteAllByAttributes(array('user_id' => $id));
218:
219: $viewPermission = array();
220: $editPermission = array();
221:
222:
223: if(isset($_POST['view-permission'])){
224: $viewPermission = $_POST['view-permission'];
225: }
226: if(isset($_POST['edit-permission'])){
227: $editPermission = $_POST['edit-permission'];
228: }
229:
230: $users = User::model()->findAll(array('select' => 'id'));
231: foreach($users as $user){
232: $view = in_array($user->id, $viewPermission);
233: $edit = in_array($user->id, $editPermission);
234:
235: $permission = new X2CalendarPermissions;
236: $permission->user_id = $id;
237: $permission->other_user_id = $user->id;
238: $permission->view = $view;
239: $permission->edit = $edit;
240: $permission->save();
241: }
242: }
243: if(isset($_GET['id'])){
244: $this->render('userCalendarPermissions', array('id' => $_GET['id']));
245: }else{
246: $this->render('userCalendarPermissions');
247: }
248: }
249:
250:
251: public function actionQuickView ($id) {
252: echo Yii::t('app', 'Quick view not supported');
253: }
254:
255: 256: 257:
258: public function actionCreate(){
259:
260: $model = new X2Calendar;
261:
262: if(isset($_POST['X2Calendar'])){
263:
264:
265: foreach(array_keys($model->attributes) as $field){
266: if(isset($_POST['X2Calendar'][$field])){
267: $model->$field = $_POST['X2Calendar'][$field];
268: $fieldData = Fields::model()->findByAttributes(array('modelName' => 'Calendar', 'fieldName' => $field));
269: if(isset($fieldData) && $fieldData->type == 'assignment' && $fieldData->linkType == 'multiple'){
270: $model->$field = Fields::parseUsers($model->$field);
271: }elseif(isset($fieldData) && $fieldData->type == 'date'){
272: $model->$field = strtotime($model->$field);
273: }
274: }
275: }
276:
277: if($model->googleCalendar && isset($_SESSION['token'])){
278: $token = json_decode($_SESSION['token'], true);
279: $model->googleRefreshToken = $token['refresh_token'];
280: $model->googleAccessToken = $_SESSION['token'];
281: }
282:
283: $model->createdBy = Yii::app()->user->name;
284: $model->updatedBy = Yii::app()->user->name;
285: $model->createDate = time();
286: $model->lastUpdated = time();
287:
288: $model->save();
289: $this->redirect(array('index'));
290: }
291:
292: $admin = Yii::app()->settings;
293: $googleIntegration = $admin->googleIntegration;
294:
295:
296:
297: $credentials = Yii::app()->settings->getGoogleIntegrationCredentials ();
298: if($googleIntegration && $credentials){
299: require_once 'protected/integration/Google/google-api-php-client/src/Google/autoload.php';
300:
301: $client = new Google_Client();
302: $client->setApplicationName("Google Calendar Integration");
303:
304:
305:
306: $client->setClientId($credentials['clientId']);
307: $client->setClientSecret($credentials['clientSecret']);
308: $client->setRedirectUri((@$_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$this->createUrl(''));
309:
310: $client->setAccessType('offline');
311: $googleCalendar = new Google_Service_Calendar($client);
312:
313: if(isset($_GET['unlinkGoogleCalendar'])){
314: unset($_SESSION['token']);
315: }
316:
317:
318: if(isset($_GET['code'])){
319: $client->authenticate($_GET['code']);
320: $_SESSION['token'] = $client->getAccessToken();
321: header('Location: '.(@$_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
322: }
323:
324: if(isset($_SESSION['token'])){
325: $client->setAccessToken($_SESSION['token']);
326: $calList = $googleCalendar->calendarList->listCalendarList();
327: $googleCalendarList = array();
328: foreach($calList['items'] as $cal)
329: $googleCalendarList[$cal['id']] = $cal['summary'];
330: }else{
331: $googleCalendarList = null;
332: }
333: }else{
334: $client = null;
335: $googleCalendarList = null;
336: }
337:
338: $this->render('create', array('model' => $model,
339: 'googleIntegration' => $googleIntegration,
340: 'client' => $client,
341: 'googleCalendarList' => $googleCalendarList,
342: )
343: );
344: }
345:
346: 347: 348:
349: public function actionUpdate($id){
350: $model = $this->loadModel($id);
351:
352: if(isset($_POST['X2Calendar'])){
353:
354:
355: if(!isset($_POST['X2Calendar']['viewPermission']))
356: $model->viewPermission = '';
357: if(!isset($_POST['X2Calendar']['editPermission']))
358: $model->editPermission = '';
359:
360:
361: foreach(array_keys($model->attributes) as $field){
362: if(isset($_POST['X2Calendar'][$field])){
363: $model->$field = $_POST['X2Calendar'][$field];
364: $fieldData = Fields::model()->findByAttributes(array('modelName' => 'Calendar', 'fieldName' => $field));
365: if($fieldData->type == 'assignment' && $fieldData->linkType == 'multiple'){
366: $model->$field = Fields::parseUsers($model->$field);
367: }elseif($fieldData->type == 'date'){
368: $model->$field = strtotime($model->$field);
369: }
370: }
371: }
372:
373: $model->updatedBy = Yii::app()->user->name;
374: $model->lastUpdated = time();
375:
376: $model->save();
377: $this->redirect(array('view', 'id' => $model->id));
378: }
379:
380: $admin = Yii::app()->settings;
381: $googleIntegration = $admin->googleIntegration;
382:
383: $this->render('update', array('model' => $model, 'googleIntegration' => $googleIntegration));
384: }
385:
386: public function actionList(){
387: $model = new X2Calendar('search');
388: $this->render('index', array('model' => $model));
389: }
390:
391: 392: 393:
394: public function actionDelete($id){
395: $model = $this->loadModel($id);
396: $model->delete();
397: $this->redirect(array('list'));
398: }
399:
400: 401: 402: 403: 404:
405: private function constructFilterClause(array $filter){
406: $clause = "";
407: $clause .= "((complete != \"Yes\") ";
408: if(in_array('contacts', $filter))
409: $clause .= "OR (associationType = \"contacts\") ";
410: if(in_array('accounts', $filter))
411: $clause .= "OR (associationType = \"accounts\") ";
412: if(in_array('opportunities', $filter))
413: $clause .= "OR (associationType = \"opportunities\") ";
414: if(in_array('quotes', $filter))
415: $clause .= "OR (associationType = \"quotes\") ";
416: if(in_array('products', $filter))
417: $clause .= "OR (associationType = \"product\") ";
418: if(in_array('media', $filter))
419: $clause .= "OR (associationType = \"media\") ";
420: if(in_array('completed', $filter))
421: $clause .= "OR (complete = \"Yes\") ";
422: $clause .= ")";
423: return $clause;
424: }
425:
426: 427: 428: 429:
430:
431: 432: 433:
434:
435: 436: 437: 438: 439:
440: public function actionJsonFeed($user, $start, $end){
441: echo CJSON::encode($this->feedUser($user, $start, $end));
442:
443: }
444:
445: public function actionJsonFeedGroup($groupId, $start, $end){
446: echo CJSON::encode($this->feedGroup($groupId, $start, $end));
447: }
448:
449: public function actionJsonFeedGoogle($calendarId){
450: echo CJSON::encode($this->feedGoogle($calendarId));
451: }
452:
453: public function formatActionToEvent($action, $id){
454: if( !($action->visibility >= 1 ||
455: $action->assignedTo == Yii::app()->user->name ||
456: Yii::app()->params->isAdmin) )
457: return false;
458:
459:
460: $linked = !empty($action->associationType) &&
461: strtolower($action->associationType) != 'none' &&
462: class_exists(X2Model::getModelName($action->associationType));
463: if ($linked) {
464: $associatedModel = X2Model::getMOdelOfTypeWithId (
465: X2Model::getModelName($action->associationType), $action->associationId);
466: if ($associatedModel) {
467: $associationUrl = $associatedModel->getUrl ();
468: } else {
469: $associationUrl = '';
470: }
471: }
472:
473: $title = $action->shortActionText;
474:
475:
476: $title = preg_replace('/<b>/', '', $title);
477: $title = preg_replace('/<\/b>/', '', $title);
478: $title = preg_replace('/\n\n/', "\n", $title);
479: $title = preg_replace('/<!--EndSig-->/', '', $title);
480: $title = preg_replace('/<!--BeginOpenedEmail-->/', '', $title);
481: $title = preg_replace('/<!--BeginSignature-->/', '', $title);
482:
483: if(in_array($action->type, array(
484: 'email', 'emailFrom', 'email_quote', 'email_invoice', 'emailOpened',
485: 'emailOpened_quote', 'emailOpened_invoice'))){
486: $title = 'Email: '.$title;
487: }
488:
489: $event = array(
490: 'title' => $title,
491: 'description' => $title,
492: 'start' => date('Y-m-d H:i', $action->dueDate),
493: 'id' => $action->id,
494: 'complete' => $action->complete,
495: 'calendarAssignment' => $id,
496: 'allDay' => false,
497: );
498:
499: if($action->allDay)
500: $event['allDay'] = $action->allDay;
501:
502: if($action->color) {
503: $event['color'] = $action->color;
504: } else {
505: $event['color'] = '#6389de';
506:
507:
508: }
509:
510: static $brightnesses = array ();
511: if (!isset ($brightnesses[$event['color']])) {
512: $brightnesses[$event['color']] = X2Color::getColorBrightness ($event['color']);
513: }
514: if ($brightnesses[$event['color']] < 115) {
515: $event['textColor'] = 'white';
516: }
517:
518: if($action->type == 'event'){
519: if($action->completeDate)
520: $event['end'] = date('Y-m-d H:i', $action->completeDate);
521:
522: $event['type'] = 'event';
523: $event['associationType'] = $action->associationType;
524: }
525:
526: $event['linked'] = $linked;
527: if($linked){
528: $event['associationType'] = $action->associationType;
529: $event['associationUrl'] = $associationUrl;
530: $event['associationName'] = $action->associationName;
531: }
532:
533: $editable = X2CalendarPermissions::getEditableUserCalendarNames();
534:
535: $userEditable = !is_int($id) && isset($editable[$id]);
536:
537: $event['editable'] = $userEditable &&
538: Yii::app()->user->checkAccess('ActionsUpdate',array('X2Model'=>$action));
539:
540: return $event;
541:
542: }
543:
544: 545: 546: 547: 548:
549:
550: public function feedAll($start=null, $end=null){
551:
552:
553: if(!isset($start))
554: $start = strtotime("-1 month");
555: $end = strtotime("+1 month");
556:
557:
558: $this->calendarUsers = X2CalendarPermissions::getViewableUserCalendarNames();
559: $this->groupCalendars = X2Calendar::getViewableGroupCalendarNames();
560: $this->calendarFilter = X2Calendar::getCalendarFilters();
561:
562: $user = User::model()->findByPk(Yii::app()->user->getId());
563: $showCalendars = json_decode($user->showCalendars, true);
564:
565:
566: if(!isset($showCalendars['groupCalendars'])){
567: $showCalendars['groupCalendars'] = array();
568: $user->showCalendars = json_encode($showCalendars);
569: $user->update();
570: }
571:
572:
573: $events = array();
574: foreach($showCalendars['userCalendars'] as $cal){
575: $events = array_merge($events, $this->feedUser( $cal, $start, $end));
576: }
577:
578: foreach($showCalendars['groupCalendars'] as $cal){
579: $events = array_merge($events, $this->feedGroup( $cal, $start, $end));
580: }
581:
582: return $events;
583: }
584:
585: 586: 587: 588: 589: 590: 591:
592: public function feedUser($user, $start, $end){
593:
594:
595:
596:
597:
598: $actions = $this->calendarActions($user,$start,$end);
599:
600: $events = array();
601: foreach($actions as $action){
602: $event = $this->formatActionToEvent($action, $user);
603:
604: if($event)
605: $events[] = $event;
606: }
607:
608: return $events;
609: }
610:
611: 612: 613: 614: 615: 616: 617:
618: public function feedGroup($groupId, $start, $end){
619: return $this->feedUser($groupId, $start, $end);
620: }
621:
622: 623: 624:
625:
626:
627:
628: public function feedGoogle($calendarId){
629: $calendar = X2Calendar::model()->findByPk($calendarId);
630: $events = array();
631: if($calendar->googleCalendarId){
632: $googleCalendar = $calendar->getGoogleCalendar();
633: $googleEvents = $googleCalendar->events->listEvents($calendar->googleCalendarId);
634: foreach($googleEvents['items'] as $googleEvent){
635: $description = $googleEvent['summary'];
636: if(isset($googleEvent['start']['dateTime'])){
637: $allDay = false;
638: $start = $googleEvent['start']['dateTime'];
639: if(isset($googleEvent['end']['dateTime']))
640: $end = $googleEvent['end']['dateTime'];
641: } else{
642: $allDay = true;
643: $start = $googleEvent['start']['date'];
644: if(isset($googleEvent['end']['date'])){
645: $end = date("Y-m-d", strtotime($googleEvent['end']['date']) - 86400);
646: }
647: }
648: $title = mb_substr($description, 0, 30, 'UTF-8');
649: if(isset($googleEvent['colorId'])){
650: $colorTable = array(
651: 10 => 'Green',
652: 11 => 'Red',
653: 6 => 'Orange',
654: 8 => 'Black',
655: );
656: if(isset($colorTable[$googleEvent['colorId']]))
657: $color = $colorTable[$googleEvent['colorId']];
658: }
659: $events[] = array(
660: 'title' => $title,
661: 'id' => $googleEvent['id'],
662: 'description' => $description,
663: 'start' => $start,
664: 'allDay' => $allDay,
665: );
666: end($events);
667: $last = key($events);
668: if(isset($end))
669: $events[$last]['end'] = $end;
670: if(isset($color)){
671: $events[$last]['color'] = $color;
672: unset($color);
673: }
674: }
675: }
676:
677: return $events;
678: }
679:
680: 681: 682: 683:
684: public function actionEditAction(){
685: if(isset($_POST['ActionId'])){
686: $id = $_POST['ActionId'];
687: $model = Actions::model()->findByPk($id);
688: $isEvent = json_decode($_POST['IsEvent']);
689:
690: Yii::app()->clientScript->scriptMap['*.js'] = false;
691: Yii::app()->clientScript->scriptMap['*.css'] = false;
692: $this->renderPartial('editAction', array('model' => $model, 'isEvent' => $isEvent), false, true);
693: }
694: }
695:
696: 697: 698: 699:
700: public function actionViewAction(){
701: if(isset($_POST['ActionId'])){
702: $id = $_POST['ActionId'];
703: $model = Actions::model()->findByPk($id);
704: $isEvent = json_decode($_POST['IsEvent']);
705:
706: Yii::app()->clientScript->scriptMap['*.js'] = false;
707: Yii::app()->clientScript->scriptMap['*.css'] = false;
708: $this->renderPartial(
709: 'viewAction',
710: array(
711: 'model' => $model,
712: 'isEvent' => $isEvent
713: ), false, true);
714: }
715: }
716:
717: public function actionViewGoogleEvent(){
718: if(isset($_POST['EventId']) && isset($_POST['CalendarId'])){
719: $eventId = $_POST['EventId'];
720: $calendarId = $_POST['CalendarId'];
721: $calendar = X2Calendar::model()->findByPk($calendarId);
722: $googleCalendar = $calendar->getGoogleCalendar();
723: $googleEvent = $googleCalendar->events->get($calendar->googleCalendarId, $eventId);
724: $model = new Actions;
725: $model->actionDescription = $googleEvent['summary'];
726: if(isset($googleEvent['start']['dateTime'])){
727: $model->allDay = false;
728: $model->dueDate = strtotime($googleEvent['start']['dateTime']);
729: }else{
730: $model->allDay = true;
731: $model->dueDate = strtotime($googleEvent['start']['date']);
732: }
733: if(isset($googleEvent['end']['dateTime']))
734: $model->completeDate = strtotime($googleEvent['end']['dateTime']);
735: else
736: $model->completeDate = strtotime($googleEvent['end']['date']);
737:
738: if(isset($googleEvent['colorId'])){
739: $colorTable = array(
740: 10 => 'Green',
741: 11 => 'Red',
742: 6 => 'Orange',
743: 8 => 'Black',
744: );
745: if(isset($colorTable[$googleEvent['colorId']]))
746: $model->color = $colorTable[$googleEvent['colorId']];
747: }
748: Yii::app()->clientScript->scriptMap['*.js'] = false;
749: Yii::app()->clientScript->scriptMap['*.css'] = false;
750: $this->renderPartial('viewGoogleEvent', array('model' => $model, 'eventId' => $eventId), false, true);
751: }
752: }
753:
754: public function actionEditGoogleEvent(){
755: if(isset($_POST['EventId']) && isset($_POST['CalendarId'])){
756: $eventId = $_POST['EventId'];
757: $calendarId = $_POST['CalendarId'];
758: $calendar = X2Calendar::model()->findByPk($calendarId);
759:
760: $googleCalendar = $calendar->getGoogleCalendar();
761: $googleEvent = $googleCalendar->events->get($calendar->googleCalendarId, $eventId);
762: $model = new Actions;
763: $model->actionDescription = $googleEvent['summary'];
764: if(isset($googleEvent['start']['dateTime'])){
765: $model->allDay = false;
766: $model->dueDate = strtotime($googleEvent['start']['dateTime']);
767: }else{
768: $model->allDay = true;
769: $model->dueDate = strtotime($googleEvent['start']['date']);
770: }
771: if(isset($googleEvent['end']['dateTime']))
772: $model->completeDate = strtotime($googleEvent['end']['dateTime']);
773: else
774: $model->completeDate = strtotime($googleEvent['end']['date']) - 86400;
775:
776: if(isset($googleEvent['colorId'])){
777: $colorTable = array(
778: 10 => 'Green',
779: 11 => 'Red',
780: 6 => 'Orange',
781: 8 => 'Black',
782: );
783: if(isset($colorTable[$googleEvent['colorId']]))
784: $model->color = $colorTable[$googleEvent['colorId']];
785: }
786: Yii::app()->clientScript->scriptMap['*.js'] = false;
787: Yii::app()->clientScript->scriptMap['*.css'] = false;
788: $this->renderPartial('editGoogleEvent', array('model' => $model, 'eventId' => $eventId), false, true);
789: }
790: }
791:
792:
793:
794: public function actionMoveAction(){
795: if(isset($_POST['id'])){
796: $id = $_POST['id'];
797: $dayDelta = $_POST['dayChange'];
798: $minuteDelta = $_POST['minuteChange'];
799: $allDay = $_POST['isAllDay'];
800:
801: $action = Actions::model()->findByPk($id);
802: $action->allDay = (($allDay == 'true' || $allDay == 1) ? 1 : 0);
803: $action->dueDate += ($dayDelta * 86400) + ($minuteDelta * 60);
804: if($action->completeDate)
805: $action->completeDate += ($dayDelta * 86400) + ($minuteDelta * 60);
806:
807: $profile = Profile::model()->findByAttributes(array('username' => $action->assignedTo));
808: if(isset($profile))
809: $profile->updateGoogleCalendarEvent($action);
810:
811: if($action->save()){
812: $event = X2Model::model('Events')->findByAttributes(array('associationType' => 'Actions', 'associationId' => $action->id));
813: if(isset($event)){
814: $event->timestamp = $action->dueDate;
815: $event->save();
816: }
817: }
818: }
819: }
820:
821:
822: public function actionMoveGoogleEvent($calendarId){
823: if(isset($_POST['EventId'])){
824: $eventId = $_POST['EventId'];
825: $dayDelta = $_POST['dayChange'];
826: $minuteDelta = $_POST['minuteChange'];
827: $allDay = json_decode($_POST['isAllDay']);
828: $calendar = X2Calendar::model()->findByPk($calendarId);
829: $googleCalendar = $calendar->getGoogleCalendar();
830: $googleEvent = $googleCalendar->events->get($calendar->googleCalendarId, $eventId);
831:
832: if(isset($googleEvent['start']['dateTime'])){
833: $start = strtotime($googleEvent['start']['dateTime']);
834: if($allDay){
835: unset($googleEvent['start']['dateTime']);
836: $googleEvent['start']['date'] = date('Y-m-d', $start);
837: }else{
838: $start += ($dayDelta * 86400) + ($minuteDelta * 60);
839: $googleEvent['start']['dateTime'] = date('c', $start);
840: }
841: }else{
842: $start = strtotime($googleEvent['start']['date']);
843: if($allDay){
844: $start += ($dayDelta * 86400) + ($minuteDelta * 60);
845: $googleEvent['start']['date'] = date('Y-m-d', $start);
846: }else{
847: unset($googleEvent['start']['date']);
848: $start += ($dayDelta * 86400) + ($minuteDelta * 60);
849: $googleEvent['start']['dateTime'] = date('c', $start);
850: }
851: }
852: if(isset($googleEvent['end']['dateTime'])){
853: $end = strtotime($googleEvent['end']['dateTime']);
854: if($allDay){
855: unset($googleEvent['end']['dateTime']);
856: $end = strtotime($googleEvent['start']['date']) + 86400;
857: $googleEvent['end']['date'] = date('Y-m-d', $end);
858: }else{
859: $end += ($dayDelta * 86400) + ($minuteDelta * 60);
860: $googleEvent['end']['dateTime'] = date('c', $end);
861: }
862: }else if(isset($googleEvent['end']['date'])){
863: $end = strtotime($googleEvent['end']['date']);
864: if($allDay){
865: $end += ($dayDelta * 86400) + ($minuteDelta * 60);
866: $googleEvent['end']['date'] = date('Y-m-d', $end);
867: }else{
868: unset($googleEvent['end']['date']);
869: $end = strtotime($googleEvent['start']['dateTime']) + 7200;
870: $googleEvent['end']['dateTime'] = date('c', $end);
871: }
872: }
873:
874: $googleEvent = new Event($googleEvent);
875: $googleCalendar->events->update($calendar->googleCalendarId, $eventId, $googleEvent);
876: }
877: }
878:
879:
880:
881: public function actionResizeAction(){
882: if(isset($_POST['id'])){
883: $id = $_POST['id'];
884: $dayDelta = $_POST['dayChange'];
885: $minuteDelta = $_POST['minuteChange'];
886:
887: $action = Actions::model()->findByPk($id);
888: if($action->completeDate)
889: $action->completeDate += ($dayDelta * 86400) + ($minuteDelta * 60);
890: else if($action->type == 'event')
891: $action->completeDate = $action->dueDate + ($dayDelta * 86400) + ($minuteDelta * 60);
892:
893: $profile = Profile::model()->findByAttributes(array('username' => $action->assignedTo));
894: if(isset($profile))
895: $profile->updateGoogleCalendarEvent($action);
896:
897: $action->save();
898: }
899: }
900:
901:
902: public function actionResizeGoogleEvent($calendarId){
903: if(isset($_POST['EventId'])){
904: $eventId = $_POST['EventId'];
905: $dayDelta = $_POST['dayChange'];
906: $minuteDelta = $_POST['minuteChange'];
907: $calendar = X2Calendar::model()->findByPk($calendarId);
908: $googleCalendar = $calendar->getGoogleCalendar();
909: $googleEvent = $googleCalendar->events->get($calendar->googleCalendarId, $eventId);
910:
911: if(isset($googleEvent['end']['dateTime'])){
912: $end = strtotime($googleEvent['end']['dateTime']);
913: $end += ($dayDelta * 86400) + ($minuteDelta * 60);
914: $googleEvent['end']['dateTime'] = date('c', $end);
915: }else if(isset($googleEvent['end']['date'])){
916: $end = strtotime($googleEvent['end']['date']);
917: $end += ($dayDelta * 86400) + ($minuteDelta * 60);
918: $googleEvent['end']['date'] = date('Y-m-d', $end);
919: }
920:
921: $googleEvent = new Event($googleEvent);
922: $googleCalendar->events->update($calendar->googleCalendarId, $eventId, $googleEvent);
923: }
924: }
925:
926:
927: public function actionSaveGoogleEvent($calendarId){
928: if(isset($_POST['EventId'])){
929: $eventId = $_POST['EventId'];
930: $calendar = X2Calendar::model()->findByPk($calendarId);
931: $googleCalendar = $calendar->getGoogleCalendar();
932: $googleEvent = $googleCalendar->events->get($calendar->googleCalendarId, $eventId);
933:
934: $model = new Actions;
935: foreach($model->attributes as $field => $value){
936: if(isset($_POST['Actions'][$field])){
937: $model->$field = $_POST['Actions'][$field];
938: }
939: }
940:
941: if($model->allDay){
942: $googleEvent['start']['date'] = date('Y-m-d', Formatter::parseDateTime($model->dueDate));
943: if($model->completeDate)
944: $googleEvent['end']['date'] = date('Y-m-d', Formatter::parseDateTime($model->completeDate) + 86400);
945: if(isset($googleEvent['start']['dateTime']))
946: unset($googleEvent['start']['dateTime']);
947: if(isset($googleEvent['end']['dateTime']))
948: unset($googleEvent['end']['dateTime']);
949: } else{
950: $googleEvent['start']['dateTime'] = date('c', Formatter::parseDateTime($model->dueDate));
951: if($model->completeDate)
952: $googleEvent['end']['dateTime'] = date('c', Formatter::parseDateTime($model->completeDate));
953: if(isset($googleEvent['start']['date']))
954: unset($googleEvent['start']['date']);
955: if(isset($googleEvent['end']['date']))
956: unset($googleEvent['end']['date']);
957: }
958:
959: if($model->color && $model->color != '#3366CC'){
960: $colorTable = array(
961: 10 => 'Green',
962: 11 => 'Red',
963: 6 => 'Orange',
964: 8 => 'Black',
965: );
966: if(($key = array_search($model->color, $colorTable)) != false)
967: $googleEvent['colorId'] = $key;
968: }
969:
970: $googleEvent = new Event($googleEvent);
971: $googleEvent->setSummary($_POST['Actions']['actionDescription']);
972: $googleCalendar->events->update($calendar->googleCalendarId, $eventId, $googleEvent);
973:
974: }
975:
976:
977: }
978:
979:
980: public function actionDeleteGoogleEvent($calendarId){
981: if(isset($_POST['EventId'])){
982: $eventId = $_POST['EventId'];
983: $calendar = X2Calendar::model()->findByPk($calendarId);
984: $googleCalendar = $calendar->getGoogleCalendar();
985: $googleCalendar->events->delete($calendar->googleCalendarId, $eventId);
986: }
987: }
988:
989:
990: public function actionCompleteAction(){
991: if(isset($_POST['id'])){
992: $id = $_POST['id'];
993:
994: $action = Actions::model()->findByPk($id);
995: $action->complete = "Yes";
996: $action->completedBy = Yii::app()->user->getName();
997: $action->completeDate = time();
998: $action->update();
999: }
1000: }
1001:
1002:
1003: public function actionUncompleteAction(){
1004: if(isset($_POST['id'])){
1005: $id = $_POST['id'];
1006:
1007: $action = Actions::model()->findByPk($id);
1008: $action->complete = "No";
1009: $action->completedBy = null;
1010: $action->completeDate = null;
1011: $action->update();
1012: }
1013: }
1014:
1015:
1016: public function actionDeleteAction(){
1017: if(isset($_POST['id'])){
1018: $id = $_POST['id'];
1019:
1020: $action = Actions::model()->findByPk($id);
1021:
1022: $profile = Profile::model()->findByAttributes(array('username' => $action->assignedTo));
1023: if(isset($profile))
1024: $profile->deleteGoogleCalendarEvent($action);
1025: X2Model::model('Events')->deleteAllByAttributes(array('associationType' => 'Actions', 'type' => 'calendar_event', 'associationId' => $action->id));
1026: $action->delete();
1027: }
1028: }
1029:
1030:
1031:
1032: public function initCheckedCalendars(){
1033: $user = User::model()->findByPk(Yii::app()->user->getId());
1034:
1035: if($user->showCalendars == null)
1036: $user->initCheckedCalendars();
1037: }
1038:
1039:
1040: public function actionSaveCheckedCalendar(){
1041: if(isset($_POST['Calendar'])){
1042: $calendar = $_POST['Calendar'];
1043: $checked = $_POST['Checked'];
1044: $type = $_POST['Type'];
1045: $calendarType = $type.'Calendars';
1046:
1047:
1048: $user = User::model()->findByPk(Yii::app()->user->getId());
1049: $showCalendars = json_decode($user->showCalendars, true);
1050:
1051: if($checked)
1052: if(!in_array($calendar, $showCalendars[$calendarType]))
1053: $showCalendars[$calendarType][] = $calendar;
1054: else
1055: if(($key = array_search($calendar, $showCalendars[$calendarType])) !== false)
1056: unset($showCalendars[$calendarType][$key]);
1057:
1058: print_r($showCalendars);
1059: $user->showCalendars = CJSON::encode($showCalendars);
1060: $user->update();
1061: }
1062: }
1063:
1064:
1065: public function actionSaveCheckedCalendarFilter(){
1066: if(isset($_POST['Filter'])){
1067: $filterName = $_POST['Filter'];
1068: $checked = $_POST['Checked'];
1069: $user = User::model()->findByPk(Yii::app()->user->id);
1070: $calendarFilter = explode(',', $user->calendarFilter);
1071:
1072: if($checked)
1073: if(!in_array($filterName, $calendarFilter))
1074: $calendarFilter[] = $filterName;
1075: else
1076: if(($key = array_search($filterName, $calendarFilter)) !== false)
1077: unset($calendarFilter[$key]);
1078:
1079: $user->calendarFilter = implode(',', $calendarFilter);
1080: $user->update();
1081: }
1082: }
1083:
1084: 1085: 1086:
1087:
1088: public function actionSyncActionsToGoogleCalendar(){
1089: $errors = array();
1090: $model = Yii::app()->params->profile;
1091: $client = null;
1092: if(isset($_POST['Profile'])){
1093: foreach(array_keys($model->attributes) as $field){
1094: if(isset($_POST['Profile'][$field])){
1095: $model->$field = $_POST['Profile'][$field];
1096: }
1097: }
1098:
1099: if($model->syncGoogleCalendarId && isset($_SESSION['token'])){
1100: $token = json_decode($_SESSION['token'], true);
1101:
1102:
1103: $model->syncGoogleCalendarAccessToken = $_SESSION['token'];
1104: }
1105:
1106: $model->update();
1107:
1108: }
1109:
1110: if(isset($_SESSION['calendarForceRefresh']) && $_SESSION['calendarForceRefresh']){
1111: unset($_SESSION['calendarForceRefresh']);
1112: Yii::app()->user->setFlash(
1113: 'error',
1114: 'Your Refresh Token was invalid and needed to be refreshed. The last action you ' .
1115: 'attempted to Sync with Google did not successfully synchronize.');
1116: }
1117: $admin = Yii::app()->settings;
1118: $googleIntegration = $admin->googleIntegration;
1119:
1120: 1121:
1122: if($googleIntegration){
1123:
1124:
1125:
1126:
1127:
1128:
1129: $auth = new GoogleAuthenticator();
1130:
1131: 1132:
1133: $syncGoogleCalendarName = null;
1134: try{
1135: if(isset($_GET['unlinkGoogleCalendar'])){
1136:
1137:
1138: unset($_SESSION['token']);
1139: $model->syncGoogleCalendarId = null;
1140:
1141:
1142: $model->syncGoogleCalendarAccessToken = null;
1143: $model->update();
1144: $googleCalendarList = null;
1145: if($auth->getAccessToken()){
1146: $googleCalendar = $auth->getCalendarService();
1147: try{
1148: $calList = $googleCalendar->calendarList->listCalendarList();
1149: $googleCalendarList = array();
1150: foreach($calList['items'] as $cal){
1151: $googleCalendarList[$cal['id']] = $cal['summary'];
1152: }
1153: }catch(Google_Service_Exception $e){
1154: if($e->getCode() == '403'){
1155: $errors[] = $e->getMessage();
1156: Yii::app()->user->setFlash('error', $e->getMessage());
1157: $googleCalendarList = null;
1158:
1159: }elseif($e->getCode() == '401'){
1160: $errors[] = 'Invalid user credentials provided. Please try again.';
1161: Yii::app()->user->setFlash(
1162: 'error',
1163: 'Invalid user credentials. Please ensure your account is ' .
1164: 'able to use this service or delete the access permissions ' .
1165: 'and try again.');
1166: $googleCalendarList = null;
1167: $auth->flushCredentials();
1168: }
1169: }
1170: }else{
1171: $googleCalendarList = null;
1172: }
1173: }else{
1174: if($auth->getAccessToken()){
1175: $googleCalendar = $auth->getCalendarService();
1176: try{
1177: $calList = $googleCalendar->calendarList->listCalendarList();
1178: $googleCalendarList = array();
1179: foreach($calList['items'] as $cal){
1180: $googleCalendarList[$cal['id']] = $cal['summary'];
1181: }
1182:
1183:
1184: }catch(Google_Service_Exception $e){
1185: if($e->getCode() == '403'){
1186: $errors[] = 'Google Calendar API access has not been configured.';
1187: Yii::app()->user->setFlash(
1188: 'error',
1189: 'Google Calendar API access has not been configured.');
1190: $googleCalendarList = null;
1191:
1192: }elseif($e->getCode() == '401'){
1193: $errors[] = 'Invalid user credentials provided. Please try again.';
1194: Yii::app()->user->setFlash(
1195: 'error',
1196: 'Invalid user credentials. Please ensure your account is ' .
1197: 'able to use this service or delete the access permissions ' .
1198: 'and try again.');
1199: $googleCalendarList = null;
1200: $auth->flushCredentials();
1201: }
1202: }
1203: }else{
1204: $googleCalendarList = null;
1205: }
1206: }
1207: }catch(Google_Auth_Exception $e){
1208: $auth->flushCredentials();
1209: $auth->setErrors($e->getMessage());
1210: $client = null;
1211: $googleCalendarList = null;
1212: $syncGoogleCalendarName = null;
1213: }
1214: }else{
1215: $client = null;
1216: $googleCalendarList = null;
1217: $syncGoogleCalendarName = null;
1218: }
1219: $syncGoogleCalendarId = Yii::app()->params->profile->syncGoogleCalendarId;
1220:
1221: $this->render('syncActionsToGoogleCalendar', array(
1222: 'errors' => $errors,
1223: 'auth' => isset($auth) ? $auth : null,
1224: 'model' => $model,
1225: 'googleIntegration' => $googleIntegration,
1226: 'client' => $client,
1227: 'googleCalendarList' => $googleCalendarList,
1228: 'syncGoogleCalendarName' => $syncGoogleCalendarId,
1229: ));
1230: }
1231:
1232: public function actionToggleUserCalendarsVisible(){
1233: echo Yii::app()->params->profile->userCalendarsVisible;
1234: }
1235:
1236: public function actionTogglePortletVisible($portlet){
1237: $parameterName = $portlet."Visible";
1238: if(isset(Yii::app()->params->profile->$parameterName)){
1239: $visible = Yii::app()->params->profile->$parameterName;
1240: $visible = !$visible;
1241: Yii::app()->params->profile->$parameterName = $visible;
1242: Yii::app()->params->profile->update();
1243: echo $visible;
1244: }else{
1245: echo 1;
1246: }
1247: }
1248:
1249: 1250: 1251: 1252: 1253:
1254: public function loadModel($id){
1255: $model = X2Calendar::model()->findByPk((int) $id);
1256: if($model === null)
1257: throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.'));
1258: return $model;
1259: }
1260:
1261: 1262: 1263: 1264: 1265: 1266: 1267: 1268: 1269: 1270: 1271:
1272: public function calendarActions($calendarUser, $start, $end){
1273: $filter = explode(',', $this->currentUser->calendarFilter);
1274: $staticAction = Actions::model();
1275:
1276: $criteria = $staticAction->getAccessCriteria();
1277:
1278: $criteria->addCondition('`assignedTo` REGEXP BINARY :unameRegex');
1279: $permissionsBehavior = Yii::app()->params->modelPermissions;
1280: $criteria->params[':unameRegex'] = $permissionsBehavior::getUserNameRegex($calendarUser);
1281:
1282: $criteria->addCondition(self::constructFilterClause($filter));
1283: $criteria->addCondition("`type` IS NULL OR `type`='' OR `type`='event'");
1284: $criteria->addCondition('(`dueDate` >= :start1 AND `dueDate` <= :end1) '
1285: .'OR (`completeDate` >= :start2 AND `completeDate` <= :end2)');
1286: $criteria->params = array_merge($criteria->params, array(
1287: ':start1' => $start,
1288: ':start2' => $start,
1289: ':end1' => $end,
1290: ':end2' => $end
1291: ));
1292: return Actions::model()->findAllWithoutActionText($criteria);
1293: }
1294:
1295: 1296: 1297: 1298:
1299: public function getCurrentUser() {
1300: if(!isset($this->_currentUser)) {
1301: $this->_currentUser = User::model()->findByPk(Yii::app()->user->id);
1302: }
1303: return $this->_currentUser;
1304: }
1305:
1306: 1307: 1308: 1309: 1310: 1311:
1312: public function ($selectOptions = array(), $model = null, $menuParams = null) {
1313: $Calendar = Modules::displayName();
1314: $Actions = Modules::displayName(true, "Actions");
1315: $User = Modules::displayName(false, "Users");
1316:
1317: 1318: 1319: 1320: 1321: 1322:
1323:
1324: $menuItems = array(
1325: array(
1326: 'name'=>'index',
1327: 'label'=>Yii::t('calendar', '{calendar}', array('{calendar}'=>$Calendar)),
1328: 'url'=>array('index')
1329: ),
1330: array(
1331: 'name'=>'myPermissions',
1332: 'label'=>Yii::t('calendar', 'My {calendar} Permissions', array(
1333: '{calendar}'=>$Calendar,
1334: )),
1335: 'url'=>array('myCalendarPermissions')
1336: ),
1337: array(
1338: 'name'=>'userPermissions',
1339: 'label'=>Yii::t('calendar', '{user} {calendar} Permissions', array(
1340: '{calendar}'=>$Calendar,
1341: '{user}'=>$User,
1342: )),
1343: 'url'=>array('userCalendarPermissions'),
1344: ),
1345: array(
1346: 'name'=>'sync',
1347: 'label'=>Yii::t('calendar', 'Sync My {actions} To Google Calendar', array(
1348: '{actions}' => $Actions,
1349: )),
1350: 'url'=>array('syncActionsToGoogleCalendar'),
1351: ),
1352: );
1353:
1354: $this->prepareMenu($menuItems, $selectOptions);
1355: $this->actionMenu = $this->formatMenu($menuItems, $menuParams);
1356: }
1357: }
1358: