コード例 #1
0
ファイル: IndexController.php プロジェクト: hoalangoc/ftf
 public function createAction()
 {
     if (!$this->_helper->requireUser->isValid()) {
         return;
     }
     if (!$this->_helper->requireAuth()->setAuthParams('event', null, 'create')->isValid()) {
         return;
     }
     // Render
     $this->_helper->content->setEnabled();
     // Get navigation
     $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('ynevent_main');
     $viewer = Engine_Api::_()->user()->getViewer();
     $parent_type = $this->_getParam('parent_type');
     $parent_id = $this->_getParam('parent_id', $this->_getParam('subject_id'));
     if ($parent_type == 'group' && Engine_Api::_()->hasItemType('group')) {
         $this->view->group = $group = Engine_Api::_()->getItem('group', $parent_id);
         if (!$this->_helper->requireAuth()->setAuthParams($group, null, 'event')->isValid()) {
             return;
         }
     } else {
         $parent_type = 'user';
         $parent_id = $viewer->getIdentity();
     }
     // Create form
     $this->view->parent_type = $parent_type;
     $this->view->gEndDate = Engine_Api::_()->getApi('settings', 'core')->getSetting("ynevent.day", "");
     $this->view->form = $form = new Ynevent_Form_Create(array('parent_type' => $parent_type, 'parent_id' => $parent_id));
     // Not post/invalid
     if (!$this->getRequest()->isPost()) {
         return;
     }
     $localeObject = Zend_Registry::get('Locale');
     $dateLocaleString = $localeObject->getTranslation('short', 'Date', $localeObject);
     $dateLocaleString = preg_replace('~\'[^\']+\'~', '', $dateLocaleString);
     $dateLocaleString = strtolower($dateLocaleString);
     $dateLocaleString = preg_replace(array('/y+/i', '/m+/i', '/d+/i'), array('Y', 'm', 'd'), $dateLocaleString);
     $dateLocaleString = str_replace('  ', '/', $dateLocaleString);
     $dateLocaleString = str_replace('.', '/', $dateLocaleString);
     $dateLocaleString = str_replace('-', '/', $dateLocaleString);
     $date_default = trim(date($dateLocaleString));
     $language = $localeObject->getLanguage();
     if ($language == 'ar') {
         $date_default = date('d/m/Y');
     }
     $_post = $this->getRequest()->getPost();
     $_post['repeatstartdate']['hour'] = 1;
     $_post['repeatstartdate']['minute'] = 10;
     $_post['repeatstartdate']['ampm'] = 'AM';
     $_post['repeatenddate']['hour'] = 1;
     $_post['repeatenddate']['minute'] = 20;
     $_post['repeatenddate']['ampm'] = 'AM';
     $_post['repeatstarttime']['date'] = $date_default;
     $_post['repeatendtime']['date'] = $date_default;
     if (!$_post['repeat_type']) {
         $_post['repeatstartdate']['date'] = $date_default;
         $_post['repeatenddate']['date'] = $date_default;
         $_post['repeatstarttime']['hour'] = 1;
         $_post['repeatstarttime']['minute'] = 10;
         $_post['repeatstarttime']['ampm'] = 'AM';
         $_post['repeatendtime']['hour'] = 1;
         $_post['repeatendtime']['minute'] = 20;
         $_post['repeatendtime']['ampm'] = 'AM';
         $_post['spec_start_date']['date'] = $date_default;
         $_post['spec_end_date']['date'] = $date_default;
         $_post['spec_start_date']['hour'] = 1;
         $_post['spec_start_date']['minute'] = 10;
         $_post['spec_start_date']['ampm'] = 'AM';
         $_post['spec_end_date']['hour'] = 1;
         $_post['spec_end_date']['minute'] = 20;
         $_post['spec_end_date']['ampm'] = 'AM';
     } else {
         $_post['starttime']['date'] = $date_default;
         $_post['endtime']['date'] = $date_default;
         $_post['starttime']['hour'] = 1;
         $_post['starttime']['minute'] = 10;
         $_post['starttime']['ampm'] = 'AM';
         $_post['endtime']['hour'] = 1;
         $_post['endtime']['minute'] = 20;
         $_post['endtime']['ampm'] = 'AM';
         if ($_post['repeat_frequency'] == 99) {
             $_post['repeatstartdate']['date'] = $date_default;
             $_post['repeatenddate']['date'] = $date_default;
             $_post['repeatstarttime']['hour'] = 1;
             $_post['repeatstarttime']['minute'] = 10;
             $_post['repeatstarttime']['ampm'] = 'AM';
             $_post['repeatendtime']['hour'] = 1;
             $_post['repeatendtime']['minute'] = 20;
             $_post['repeatendtime']['ampm'] = 'AM';
             if ($_post['input_start_specifys']) {
                 $_post['spec_start_date']['date'] = $date_default;
                 $_post['spec_end_date']['date'] = $date_default;
                 $_post['spec_start_date']['hour'] = 1;
                 $_post['spec_start_date']['minute'] = 10;
                 $_post['spec_start_date']['ampm'] = 'AM';
                 $_post['spec_end_date']['hour'] = 1;
                 $_post['spec_end_date']['minute'] = 20;
                 $_post['spec_end_date']['ampm'] = 'AM';
             }
         } else {
             $_post['spec_start_date']['date'] = $date_default;
             $_post['spec_end_date']['date'] = $date_default;
             $_post['spec_start_date']['hour'] = 1;
             $_post['spec_start_date']['minute'] = 10;
             $_post['spec_start_date']['ampm'] = 'AM';
             $_post['spec_end_date']['hour'] = 1;
             $_post['spec_end_date']['minute'] = 20;
             $_post['spec_end_date']['ampm'] = 'AM';
         }
     }
     // Location
     $provincesAssoc = array();
     $country_id = $_post['country_id'];
     if ($country_id) {
         $provincesAssoc = Engine_Api::_()->getDbTable('locations', 'user')->getLocationsAssoc($country_id);
         $provincesAssoc = array('0' => '') + $provincesAssoc;
     }
     $form->getElement('province_id')->setMultiOptions($provincesAssoc);
     $citiesAssoc = array();
     $province_id = $_post['province_id'];
     if ($province_id) {
         $citiesAssoc = Engine_Api::_()->getDbTable('locations', 'user')->getLocationsAssoc($province_id);
         $citiesAssoc = array('0' => '') + $citiesAssoc;
     }
     $form->getElement('city_id')->setMultiOptions($citiesAssoc);
     if (!$form->isValid($_post)) {
         return;
     }
     // Process
     $values = $form->getValues();
     if (isset($_post['input_start_specifys'])) {
         $values['input_start_specifys'] = $_post['input_start_specifys'];
         $values['input_end_specifys'] = $_post['input_end_specifys'];
         unset($_post);
     }
     //Start time of event
     $first_date = $values['starttime'];
     //End time of event
     $first_end_date = $values['endtime'];
     $maxInstance = Engine_Api::_()->getApi('settings', 'core')->getSetting('ynevent.instance', 50);
     if ($maxInstance == '') {
         $maxInstance = 50;
     }
     $arrStart = $endStart = array();
     //If repeat
     if ($values['repeat_type'] == 1 && $values['repeat_frequency'] != 99) {
         //End repeat date
         $configDate = Engine_Api::_()->getApi('settings', 'core')->getSetting('ynevent.day', '');
         if ($configDate != '' && $values['repeatenddate'] > $configDate) {
             $values['repeatenddate'] = $configDate;
         }
         $arr_temp = explode(' ', $values['repeatstarttime']);
         $repeat_start_time = $arr_temp['1'];
         $arr_temp = explode(' ', $values['repeatendtime']);
         $repeat_end_time = $arr_temp['1'];
         $repeat_start = strtotime($values['repeatstartdate']);
         $repeat_start = date('Y-m-d ' . $repeat_start_time, $repeat_start);
         $repeat_end = strtotime($values['repeatenddate']);
         $repeat_end = date('Y-m-d ' . $repeat_end_time, $repeat_end);
         if ($repeat_start > $repeat_end) {
             $form->addError("Start date of the event must be less than the end repeat date");
             return;
         }
         // 1, 7, monthly~30
         $step = $values['repeat_frequency'];
         //Duration between starttime and endtime
         $duration = Engine_Api::_()->ynevent()->dateDiffBySec($values['repeatstarttime'], $values['repeatendtime']);
         //Start of repeat
         $loopstart = $repeat_start;
         $i = 1;
         //When start date still <= end repeat date
         while ($loopstart <= $repeat_end) {
             //If not monthly repeat
             if ($step != 30) {
                 $arrStart[] = $loopstart;
                 $loopstart = Engine_Api::_()->ynevent()->dateAdd($loopstart, $step);
             } else {
                 $arrStart[] = $loopstart;
                 $loopstart = Engine_Api::_()->ynevent()->monthAdd($repeat_start, $i);
                 echo $day . "</br>";
                 $i++;
             }
         }
     } elseif ($values['repeat_type'] == 1 && $values['repeat_frequency'] == 99) {
         if (!$values['input_start_specifys']) {
             if ($values['spec_start_date'] >= ($endStart[] = $values['spec_end_date'])) {
                 $arrStart[] = $values['spec_start_date'];
                 $endStart[] = $values['spec_end_date'];
             }
         } else {
             foreach ($values['input_start_specifys'] as $id => $value) {
                 $arrStart[] = date('Y-m-d H:i:s', strtotime(str_replace('/', '-', $value)));
                 $endStart[] = date('Y-m-d H:i:s', strtotime(str_replace('/', '-', $values['input_end_specifys'][$id])));
             }
         }
     } else {
         if ($first_date > $first_end_date) {
             $form->addError("Start time of the event must be less than the end time.");
             return;
         }
         //Not repeat event
         $arrStart[] = $first_date;
     }
     if ($maxInstance <= count($arrStart)) {
         $str = $this->view->translate(array('You are allowed creating only %s event in the repeat chain.', 'You are allowed creating only %s events in the repeat chain.', $maxInstance), $this->view->locale()->toNumber($maxInstance));
         $form->addError($str);
         return;
     }
     //Set value
     $values['user_id'] = $viewer->getIdentity();
     $values['parent_type'] = $parent_type;
     $values['parent_id'] = $parent_id;
     if ($parent_type == 'group' && Engine_Api::_()->hasItemType('group') && empty($values['host'])) {
         $values['host'] = $group->getTitle();
     }
     $db = Engine_Api::_()->getDbtable('events', 'ynevent')->getAdapter();
     $db->beginTransaction();
     try {
         // Create event
         $table = Engine_Api::_()->getDbtable('events', 'ynevent');
         //Generate repeat group value
         $values['repeat_group'] = microtime(true) * 10000;
         //type = 0 : not repeat
         //type = 1 : repeat
         $type = $values['repeat_type'];
         $repeat_order = 0;
         if (is_array($arrStart)) {
             foreach ($arrStart as $key => $value) {
                 $repeat_order++;
                 $values['repeat_order'] = $repeat_order;
                 //check maxinstance
                 if ($maxInstance >= $repeat_order) {
                     $event = $table->createRow();
                     //Set viewer time zone
                     $oldTz = date_default_timezone_get();
                     date_default_timezone_set($viewer->timezone);
                     $start = strtotime($values['starttime']);
                     $end = strtotime($values['endtime']);
                     date_default_timezone_set($oldTz);
                     $values['starttime'] = date('Y-m-d H:i:s', $start);
                     $values['endtime'] = date('Y-m-d H:i:s', $end);
                     //Repeat
                     if ($type == 1) {
                         if ($values['repeat_frequency'] != 99) {
                             $values['starttime'] = $value;
                             $values['endtime'] = Engine_Api::_()->ynevent()->dateAddBySec($value, $duration);
                         } else {
                             $values['starttime'] = $value;
                             $values['endtime'] = $endStart[$key];
                         }
                         $oldTz = date_default_timezone_get();
                         date_default_timezone_set($viewer->timezone);
                         if ($values['repeat_frequency'] != 99) {
                             $repeat_end = strtotime($repeat_end);
                         }
                         $start = strtotime($values['starttime']);
                         $end = strtotime($values['endtime']);
                         date_default_timezone_set($oldTz);
                         $values['starttime'] = date('Y-m-d H:i:s', $start);
                         $values['endtime'] = date('Y-m-d H:i:s', $end);
                         if ($values['repeat_frequency'] != 99) {
                             $repeat_end = date('Y-m-d H:i:s', $repeat_end);
                             $values['end_repeat'] = $repeat_end;
                         }
                         $values['repeat_type'] = $values['repeat_frequency'];
                     }
                     $event->setFromArray($values);
                     $event->save();
                     // Add owner as member
                     $event->membership()->addMember($viewer)->setUserApproved($viewer)->setResourceApproved($viewer);
                     // Add owner rsvp
                     $event->membership()->getMemberInfo($viewer)->setFromArray(array('rsvp' => 2))->save();
                     // Add photo
                     if (!empty($values['photo'])) {
                         $event->setPhoto($form->photo);
                     }
                     // Add Cover photo
                     if (!empty($values['cover_thumb'])) {
                         $event->setCoverPhoto($form->cover_thumb);
                     }
                     // Add fields
                     $customfieldform = $form->getSubForm('fields');
                     $customfieldform->setItem($event);
                     $customfieldform->saveValues();
                     // Add tags
                     $tags = preg_split('/[,]+/', $values['tags']);
                     $event->tags()->addTagMaps($viewer, $tags);
                     //sendnotify host
                     if (strpos($values['host'], 'younetco_event_key_') !== FALSE) {
                         $user_id = substr($values['host'], 19, strlen($values['host']));
                         if ($user_id != $viewer->getIdentity()) {
                             $friend = Engine_Api::_()->getItem('user', $user_id);
                             $notifyApi = Engine_Api::_()->getDbtable('notifications', 'activity');
                             $notifyApi->addNotification($friend, $viewer, $event, 'friend_host');
                         }
                     }
                     //Add owner follow
                     Engine_Api::_()->ynevent()->setEventFollow($event, $viewer);
                     // Set auth
                     $auth = Engine_Api::_()->authorization()->context;
                     $roles = array('owner', 'member', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone');
                     if (empty($values['auth_view'])) {
                         $values['auth_view'] = 'everyone';
                     }
                     if (empty($values['auth_comment'])) {
                         $values['auth_comment'] = 'everyone';
                     }
                     $viewMax = array_search($values['auth_view'], $roles);
                     $commentMax = array_search($values['auth_comment'], $roles);
                     $photoMax = array_search($values['auth_photo'], $roles);
                     $videoMax = array_search($values['auth_video'], $roles);
                     foreach ($roles as $i => $role) {
                         $auth->setAllowed($event, $role, 'view', $i <= $viewMax);
                         $auth->setAllowed($event, $role, 'comment', $i <= $commentMax);
                         $auth->setAllowed($event, $role, 'photo', $i <= $photoMax);
                         $auth->setAllowed($event, $role, 'video', $i <= $videoMax);
                     }
                     $auth->setAllowed($event, 'member', 'invite', $values['auth_invite']);
                     //Add activity only one
                     if ($repeat_order <= 1) {
                         // Add action
                         $activityApi = Engine_Api::_()->getDbtable('actions', 'activity');
                         if ($values['parent_type'] == 'group') {
                             $action = $activityApi->addActivity($viewer, $group, 'advgroup_event_create');
                         } else {
                             $action = $activityApi->addActivity($viewer, $event, 'ynevent_create');
                         }
                         if ($action) {
                             $activityApi->attachActivity($action, $event);
                         }
                     }
                 }
                 //end check maxinstance
             }
             //End foreach
         }
         // Commit
         $db->commit();
         // Redirect
         if ($event->parent_type == 'group') {
             $group = $event->getParent('group');
             $this->_redirectCustom($group);
         } else {
             $this->_redirectCustom(array('route' => 'event_general', 'action' => 'manage'));
         }
         //return $this->_helper->redirector->gotoRoute(array('id' => $event->getIdentity()), 'event_profile', true);
     } catch (Engine_Image_Exception $e) {
         $db->rollBack();
         $form->addError(Zend_Registry::get('Zend_Translate')->_('The image you selected was too large.'));
     } catch (Exception $e) {
         $db->rollBack();
         throw $e;
     }
 }