Exemple #1
0
 /**
  * Method to save the group
  * @return false if create fail, return the group id if create is successful
  * */
 public function save($event, $isDuplicate = false)
 {
     // Check for request forgeries
     JSession::checkToken('post') or jexit(JText::_('COM_COMMUNITY_INVALID_TOKEN'));
     $mainframe = JFactory::getApplication();
     $jinput = $mainframe->input;
     $document = JFactory::getDocument();
     $viewType = $document->getType();
     $viewName = $jinput->get('view', $this->getName(), 'String');
     $view = $this->getView($viewName, '', $viewType);
     if (JString::strtoupper($jinput->getMethod()) != 'POST') {
         $view->addWarning(JText::_('COM_COMMUNITY_PERMISSION_DENIED_WARNING'));
         return false;
     }
     // Get my current data.
     $my = CFactory::getUser();
     $validated = true;
     $model = $this->getModel('events');
     $eventId = $jinput->get->get('eventid', 0, 'Int');
     $isNew = $eventId == 0 ? true : false;
     $postData = JRequest::get('post');
     $repeataction = $jinput->get('repeataction', null, 'NONE');
     $inviteAllMembers = $jinput->get('invitegroupmembers', 0, 'INT');
     if (!isset($postData['allday'])) {
         $postData['allday'] = 0;
     }
     //format startdate and eendate with time before we bind into event object
     $postData = $this->_formatStartEndDate($postData);
     $event->load($eventId);
     // record event original start and end date
     $postData['oldstartdate'] = $event->startdate;
     $postData['oldenddate'] = $event->enddate;
     $postData['unlisted'] = JRequest::getVar('unlisted', 0, 'post', 'int', JREQUEST_ALLOWRAW);
     if (CFactory::getConfig()->get('eventshowtimezone')) {
         $timezoneName = $postData['offset'];
         $postData['offset'] = CTimeHelper::getOffsetByTimezone($postData['offset']);
         //update offset before binding
     }
     $event->bind($postData);
     if (!array_key_exists('permission', $postData)) {
         $event->permission = 0;
     }
     if (!array_key_exists('allowinvite', $postData)) {
         $event->allowinvite = 0;
     } elseif (isset($postData['endtime-ampm']) && $postData['endtime-ampm'] == 'AM' && $postData['endtime-hour'] == 12) {
         $postData['endtime-hour'] = 00;
     }
     $inputFilter = CFactory::getInputFilter(true);
     // Despite the bind, we would still need to capture RAW description
     $event->description = JRequest::getVar('description', '', 'post', 'string', JREQUEST_ALLOWRAW);
     $event->description = $inputFilter->clean($event->description);
     // binding the params
     $params = new CParameter('');
     $photoPermissionAdmin = $jinput->get('photopermission-admin', 0, 'STRING');
     $photoPermissionMember = $jinput->get('photopermission-member', 0, 'STRING');
     $videoPermissionAdmin = $jinput->get('videopermission-admin', 0, 'STRING');
     $videoPermissionMember = $jinput->get('videopermission-member', 0, 'STRING');
     $eventRecentPhotos = $jinput->get('eventrecentphotos', 6, 'STRING');
     $eventRecentVideos = $jinput->get('eventrecentvideos', 6, 'STRING');
     $params->set('eventrecentphotos', $eventRecentPhotos);
     $params->set('eventrecentvideos', $eventRecentVideos);
     $params->set('timezone', $timezoneName);
     if ($photoPermissionAdmin) {
         $params->set('photopermission', EVENT_PHOTO_PERMISSION_ADMINS);
         if ($photoPermissionMember) {
             $params->set('photopermission', EVENT_PHOTO_PERMISSION_ALL);
         }
     } else {
         $params->set('photopermission', EVENT_PHOTO_PERMISSION_DISABLE);
     }
     if ($videoPermissionAdmin) {
         $params->set('videopermission', EVENT_VIDEO_PERMISSION_ADMINS);
         if ($videoPermissionMember) {
             $params->set('videopermission', EVENT_VIDEO_PERMISSION_ALL);
         }
     } else {
         $params->set('videopermission', EVENT_VIDEO_PERMISSION_DISABLE);
     }
     $event->params = $params->toString();
     // @rule: Test for emptyness
     if (empty($event->title)) {
         $validated = false;
         $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_EVENTS_TITLE_ERROR'), 'error');
     }
     if (empty($event->location)) {
         $validated = false;
         $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_EVENTS_LOCATION_ERR0R'), 'error');
     }
     // @rule: Test if the event is exists
     if ($model->isEventExist($event->title, $event->location, $event->startdate, $event->enddate, $eventId, $event->parent)) {
         $validated = false;
         $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_EVENTS_TAKEN_ERROR'), 'error');
     }
     // @rule: Start date cannot be empty
     if (empty($event->startdate)) {
         $validated = false;
         $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_EVENTS_STARTDATE_ERROR'), 'error');
     }
     // @rule: End date cannot be empty
     if (empty($event->enddate)) {
         $validated = false;
         $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_EVENTS_ENDDATE_ERROR'), 'error');
     }
     // @rule: Number of ticket must at least be 0
     if (Jstring::strlen($event->ticket) <= 0) {
         $validated = false;
         $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_EVENTS_TICKET_EMPTY_ERROR'), 'error');
     }
     if (!is_numeric($event->ticket)) {
         $validated = false;
         $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_EVENTS_TICKET_INVALID_ERROR'), 'error');
     }
     $now = CTimeHelper::getLocaleDate();
     require_once JPATH_COMPONENT . '/helpers/time.php';
     if (CTimeHelper::timeIntervalDifference($event->startdate, $event->enddate) > 0) {
         $validated = false;
         $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_EVENTS_STARTDATE_GREATER_ERROR'), 'error');
     }
     // if all day event.
     $isToday = false;
     if ($postData['allday'] == '1') {
         $isToday = date("Y-m-d", strtotime($event->enddate)) == date("Y-m-d", strtotime($now->toSql(true))) ? true : $isToday;
     }
     // @rule: Event must not end in the past
     if (CTimeHelper::timeIntervalDifference($now->toSql(true), $event->enddate) > 0 && !$isToday && $isNew) {
         $validated = false;
         $mainframe->enqueueMessage(JText::_('COM_COMMUNITY_EVENTS_ENDDATE_GREATER_ERROR'), 'error');
     }
     $eventChild = array();
     // check event recurrence limit.
     if (!empty($event->repeat) && ($isNew || $postData['repeataction'] == 'future')) {
         $repeatLimit = 'COMMUNITY_EVENT_RECURRING_LIMIT_' . strtoupper($event->repeat);
         if (defined($repeatLimit)) {
             $eventChild = $this->_generateRepeatList($event);
             if (count($eventChild) > constant($repeatLimit)) {
                 $validated = false;
                 $mainframe->enqueueMessage(sprintf(JText::_('COM_COMMUNITY_EVENTS_REPEAT_LIMIT_ERROR'), constant($repeatLimit)), 'error');
             }
         }
     }
     if ($validated) {
         // If show event timezone is disabled, we need to set the event offset to 0.
         $config = CFactory::getConfig();
         if (!$config->get('eventshowtimezone')) {
             $event->offset = 0;
         }
         if ($isDuplicate) {
             $event->id = 0;
             $isNew = 1;
         }
         // Set the default thumbnail and avatar for the event just in case
         // the user decides to skip this
         if ($isNew) {
             $event->creator = $my->id;
             $config = CFactory::getConfig();
             //@rule: If event moderation is enabled, event should be unpublished by default
             $event->published = $this->isPublished();
             $event->created = JFactory::getDate()->toSql();
             $handler = CEventHelper::getHandler($event);
             $event->contentid = $handler->getContentId();
             $event->type = $handler->getType();
         }
         $event->store();
         // Save event members
         if ($isNew && !$event->isRecurring()) {
             $this->_saveMember($event);
             // Increment the member count
             $event->updateGuestStats();
             $event->store();
         }
         if ($isNew) {
             $event->parent = !empty($event->repeat) ? $event->id : 0;
         }
         // Save recurring event's child.
         $this->_saveRepeatChild($event, $eventChild, $isNew, $postData);
         // Stream and notification
         if ($isNew) {
             // add activity stream
             $this->_addActivityStream($event);
             //add user points
             $action_str = 'events.create';
             CUserPoints::assignPoint($action_str);
             //add notification: New group event is added
             $this->_addGroupNotification($event);
         }
         $validated = $event->id;
         $this->cacheClean(array(COMMUNITY_CACHE_TAG_FRONTPAGE, COMMUNITY_CACHE_TAG_EVENTS, COMMUNITY_CACHE_TAG_EVENTS_CAT, COMMUNITY_CACHE_TAG_ACTIVITIES));
     }
     //if saved and we should invite all members of the group
     if ($inviteAllMembers && $event->id && $event->contentid) {
         $groupid = $event->contentid;
         $groupsModel = CFactory::getModel('groups');
         $members = $groupsModel->getMembers($groupid, 0, true, false, SHOW_GROUP_ADMIN);
         $membersArr = array();
         foreach ($members as $member) {
             if ($member->id == $my->id) {
                 continue;
             }
             $membersArr[] = $member->id;
         }
         $inviteMail = $this->inviteUsers($event->id, $membersArr, '', '');
         if ($inviteMail instanceof CInvitationMail) {
             // Once stored, we need to store selected user so they wont be invited again
             $callback = "events,inviteUsers";
             $invitation = JTable::getInstance('Invitation', 'CTable');
             $invitation->load($callback, $event->id);
             if ($membersArr) {
                 if (!$invitation->id) {
                     // If the record doesn't exists, we need add them into the
                     $invitation->cid = $event->id;
                     $invitation->callback = $callback;
                 }
                 $invitation->users = implode(',', $membersArr);
                 $invitation->store();
             }
             //start sending email and notification
             CNotificationLibrary::add($inviteMail->getCommand(), $my->id, $membersArr, $inviteMail->getTitle(), $inviteMail->getContent(), '', $inviteMail->getParams());
         }
     }
     return $validated;
 }