Exemplo n.º 1
0
 public function add()
 {
     $language = OW::getLanguage();
     $this->setPageTitle($language->text('eventx', 'add_page_title'));
     $this->setPageHeading($language->text('eventx', 'add_page_heading'));
     $this->setPageHeadingIconClass('ow_ic_add');
     OW::getNavigation()->activateMenuItem(OW_Navigation::MAIN, 'eventx', 'main_menu_item');
     if (!OW::getUser()->isAuthenticated() || !OW::getUser()->isAuthorized('eventx', 'add_event')) {
         $this->assign('err_msg', OW::getLanguage()->text('base', 'authorization_failed_feedback'));
         return;
     }
     $eventParams = array('pluginKey' => 'eventx', 'action' => 'add_event');
     $credits = OW::getEventManager()->call('usercredits.check_balance', $eventParams);
     if ($credits === false) {
         $this->assign('err_msg', OW::getEventManager()->call('usercredits.error_message', $eventParams));
         return;
     }
     $form = new EventAddForm('event_add');
     if (date('n', time()) == 12 && date('j', time()) == 31) {
         $defaultDate = date('Y', time()) + 1 . '/1/1';
     } else {
         if (date('j', time()) + 1 > date('t')) {
             $defaultDate = date('Y', time()) . '/' . (date('n', time()) + 1) . '/1';
         } else {
             $defaultDate = date('Y', time()) . '/' . date('n', time()) . '/' . (date('j', time()) + 1);
         }
     }
     $form->getElement('start_date')->setValue($defaultDate);
     $form->getElement('end_date')->setValue($defaultDate);
     $form->getElement('start_time')->setValue('all_day');
     $form->getElement('end_time')->setValue('all_day');
     $form->getElement('who_can_view')->setValue(EVENTX_BOL_EventService::CAN_VIEW_ANYBODY);
     $form->getElement('who_can_invite')->setValue(EVENTX_BOL_EventService::CAN_INVITE_PARTICIPANT);
     $form->getElement('max_invites')->setValue(0);
     $checkboxId = UTIL_HtmlTag::generateAutoId('chk');
     $tdId = UTIL_HtmlTag::generateAutoId('td');
     $this->assign('tdId', $tdId);
     $this->assign('chId', $checkboxId);
     OW::getDocument()->addScript(OW::getPluginManager()->getPlugin("eventx")->getStaticJsUrl() . 'eventx.js');
     $enableMapSuggestion = OW::getConfig()->getValue('eventx', 'enableMapSuggestion');
     if ($enableMapSuggestion == '1') {
         OW::getDocument()->addScript("http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places");
         OW::getDocument()->addScript(OW::getPluginManager()->getPlugin("eventx")->getStaticJsUrl() . 'jquery.geocomplete.min.js');
     }
     $this->assign('enableMapSuggestion', $enableMapSuggestion);
     OW::getDocument()->addOnloadScript("new eventAddForm(" . json_encode(array('checkbox_id' => $checkboxId, 'end_date_id' => $form->getElement('end_date')->getId(), 'tdId' => $tdId)) . ")");
     if (OW::getRequest()->isPost()) {
         if (!empty($_POST['endDateFlag'])) {
             $this->assign('endDateFlag', true);
         }
         if ($form->isValid($_POST)) {
             $data = $form->getValues();
             $serviceEvent = new OW_Event(EVENTX_BOL_EventService::EVENTX_BEFORE_EVENTX_CREATE, array(), $data);
             OW::getEventManager()->trigger($serviceEvent);
             $data = $serviceEvent->getData();
             $dateArray = explode('/', $data['start_date']);
             $startStamp = mktime(0, 0, 0, $dateArray[1], $dateArray[2], $dateArray[0]);
             if ($data['start_time'] != 'all_day') {
                 $startStamp = mktime($data['start_time']['hour'], $data['start_time']['minute'], 0, $dateArray[1], $dateArray[2], $dateArray[0]);
             }
             if (!empty($_POST['endDateFlag']) && !empty($data['end_date'])) {
                 $dateArray = explode('/', $data['end_date']);
                 $endStamp = mktime(0, 0, 0, $dateArray[1], $dateArray[2], $dateArray[0]);
                 $endStamp = strtotime("+1 day", $endStamp);
                 if ($data['end_time'] != 'all_day') {
                     $hour = 0;
                     $min = 0;
                     if ($data['end_time'] != 'all_day') {
                         $hour = $data['end_time']['hour'];
                         $min = $data['end_time']['minute'];
                     }
                     $dateArray = explode('/', $data['end_date']);
                     $endStamp = mktime($hour, $min, 0, $dateArray[1], $dateArray[2], $dateArray[0]);
                 }
             }
             $imageValid = true;
             $datesAreValid = true;
             $imagePosted = false;
             if (!empty($_FILES['image']['name'])) {
                 if ((int) $_FILES['image']['error'] !== 0 || !is_uploaded_file($_FILES['image']['tmp_name']) || !UTIL_File::validateImage($_FILES['image']['name'])) {
                     $imageValid = false;
                     OW::getFeedback()->error($language->text('base', 'not_valid_image'));
                 } else {
                     $imagePosted = true;
                 }
             }
             if (empty($endStamp)) {
                 $endStamp = strtotime("+1 day", $startStamp);
                 $endStamp = mktime(0, 0, 0, date('n', $endStamp), date('j', $endStamp), date('Y', $endStamp));
             }
             if (!empty($endStamp) && $endStamp < $startStamp) {
                 $datesAreValid = false;
                 OW::getFeedback()->error($language->text('eventx', 'add_form_invalid_end_date_error_message'));
             }
             if ($imageValid && $datesAreValid) {
                 $event = new EVENTX_BOL_Event();
                 $event->setStartTimeStamp($startStamp);
                 $event->setEndTimeStamp($endStamp);
                 $event->setCreateTimeStamp(time());
                 $event->setTitle(htmlspecialchars($data['title']));
                 $event->setLocation(UTIL_HtmlTag::autoLink(strip_tags($data['location'])));
                 $event->setWhoCanView((int) $data['who_can_view']);
                 $event->setWhoCanInvite((int) $data['who_can_invite']);
                 $event->setDescription($data['desc']);
                 $event->setUserId(OW::getUser()->getId());
                 $event->setEndDateFlag(!empty($_POST['endDateFlag']));
                 $event->setStartTimeDisable($data['start_time'] == 'all_day');
                 $event->setEndTimeDisable($data['end_time'] == 'all_day');
                 $event->setMaxInvites($data['max_invites']);
                 $event->status = OW::getConfig()->getValue('eventx', 'itemApproval') == 'auto' ? 'approved' : 'pending';
                 if ($imagePosted) {
                     $event->setImage(uniqid());
                 }
                 $serviceEvent = new OW_Event(EVENTX_BOL_EventService::EVENTX_ON_CREATE_EVENT, array('eventDto' => $event));
                 OW::getEventManager()->trigger($serviceEvent);
                 $this->eventService->saveEvent($event);
                 if ($imagePosted) {
                     $this->eventService->saveEventImage($_FILES['image']['tmp_name'], $event->getImage());
                 }
                 $eventUser = new EVENTX_BOL_EventUser();
                 $eventUser->setEventId($event->getId());
                 $eventUser->setUserId(OW::getUser()->getId());
                 $eventUser->setTimeStamp(time());
                 $eventUser->setStatus(EVENTX_BOL_EventService::USER_STATUS_YES);
                 $this->eventService->saveEventUser($eventUser);
                 $eventCategory = isset($data['event_category']) ? $data['event_category'] : 1;
                 $this->eventService->setItemCategories($event->getId(), $eventCategory);
                 if (isset($data['tags'])) {
                     $tags = array();
                     $tags = $data['tags'];
                     foreach ($tags as $id => $tag) {
                         $tags[$id] = UTIL_HtmlTag::stripTags($tag);
                     }
                     BOL_TagService::getInstance()->updateEntityTags($event->id, 'eventx', $tags);
                 }
                 OW::getFeedback()->info($language->text('eventx', 'add_form_success_message'));
                 if ($event->getWhoCanView() == EVENTX_BOL_EventService::CAN_VIEW_ANYBODY) {
                     $eventObj = new OW_Event('feed.action', array('pluginKey' => 'eventx', 'entityType' => 'eventx', 'entityId' => $event->getId(), 'userId' => $event->getUserId()));
                     OW::getEventManager()->trigger($eventObj);
                 }
                 if ($credits === true) {
                     OW::getEventManager()->call('usercredits.track_action', $eventParams);
                 }
                 $serviceEvent = new OW_Event(EVENTX_BOL_EventService::EVENTX_AFTER_CREATE_EVENT, array('eventDto' => $event));
                 OW::getEventManager()->trigger($serviceEvent);
                 $this->redirect(OW::getRouter()->urlForRoute('eventx.view', array('eventId' => $event->getId())));
             }
         }
     }
     if (empty($_POST['endDateFlag'])) {
         $form->getElement('end_date')->addAttribute('disabled', 'disabled');
         $form->getElement('end_date')->addAttribute('style', 'display:none;');
         $form->getElement('end_time')->addAttribute('disabled', 'disabled');
         $form->getElement('end_time')->addAttribute('style', 'display:none;');
     }
     $this->addForm($form);
 }