'month', Entity::CALENDAR_BASICWEEK => 'week', Entity::CALENDAR_BASICDAY => 'day', Entity::CALENDAR_AGENDAWEEK => 'agenda-week', Entity::CALENDAR_AGENDADAY => 'agenda-day', ]; protected $_viewName; protected $_actions = ['list', 'print', 'view', 'insert', 'back', 'load', 'renderEvent', 'renderAttendee', 'iCal']; protected function _init() { $this->_messageTemplates[static::MSG_ADDED] = 'Attendee has been added'; $this->_messageTemplates[static::MSG_ADDED_MULTI] = 'Attendees have been added'; $this->_messageTemplates[static::MSG_ADDED_CART] = 'Attendee has been added to Shopping Cart'; $this->_messageTemplates[static::MSG_ADDED_CART_MULTI] = 'Attendees have been added to Shopping Cart'; return parent::_init(); } protected function _initAction() { $obj = $this->_getDataObj(); $blockType = $this->getConfig('type', ConfigForm::TYPE_CALENDAR); $params = $this->getRestParams(); $count = count($params); $request = array_merge_recursive(Qs_Request::get('get'), Qs_Request::get('post')); $requestAction = Qs_Request::getPostValue('action'); $auth = UserAuth::getInstance(); if ($auth->isLoggedIn()) { $this->_getDataObj()->setUserId($auth->getData('id')); } if (ConfigForm::TYPE_CALENDAR === $blockType) { if (0 === $count) { if (empty($request)) { $obj->setDate(new DateTime()); $this->_action = 'list'; } elseif (in_array($requestAction, ['load', 'renderEvent', true])) { $this->_action = $requestAction; } else { $this->_do404(); } } elseif (2 === $count && in_array($params[0], self::$calendarAlias) && false !== ($date = DateTime::createFromFormat('Y-m-d|', $params[1])) ) { $this->_viewName = array_search($params[0], self::$calendarAlias); $obj->setDate($date); if (empty($request)) { $this->_action = 'list'; } else { $this->_do404(); } } elseif (2 === $count && 'print' === $params[0] && false !== ($date = DateTime::createFromFormat('Y-m|', $params[1])) ) { if (empty($request)) { $obj->setDate($date); $this->_action = 'print'; } else { $this->_do404(); } } else { $this->_do404(); } } elseif (ConfigForm::TYPE_DETAILS === $blockType) { if ($this->_doc->isPagePreviewMode()) { $obj->initFromForm($this->getConfig('previewData')->dbData->toArray()); $this->_action = 'view'; } else if (1 === $count && $params[0] && ($data = $obj->setPrimaryKeyByAlias($params[0])->getData())) { if (empty($request)) { $this->_action = 'view'; } elseif (in_array($requestAction, ['renderAttendee', 'insert'])) { $this->_action = $requestAction; } else { $this->_do404(); } } elseif (2 === $count && 'ical' === $params[0] && ($data = $obj->setPrimaryKeyByAlias($params[1])->getData())) { if (empty($request)) { $this->_action = 'iCal'; } else { $this->_do404(); } } else { $this->_do404(); } } else { throw new Exception('Unknown block type "' . $blockType . '"'); } return parent::_initAction(); } public static function getPageUrlByType($blockType) { return Qs_SiteMap::findFirst( null, ['type' => Qs_SiteMap::classToType(get_called_class())], ['type' => $blockType], 'url' ); } public static function getViewUrl($alias = null) { if (!$alias) { $alias = '%s'; } return Qs_SiteMap::findFirst(null, ['type' => 'Event\\'], ['type' => EventConfigForm::TYPE_DETAILS], 'url') . '/' . $alias; } protected function _getListItem() { $obj = $this->_getDataObj(); $list = $obj->getList(); $baseId = Qs_SiteMap::classToId($this); $item = [ 'containerId' => $baseId . '-block', 'calendarId' => $baseId . '-calendar', 'eventListId' => $baseId . '-list', 'eventTypes' => Model::getEventTypes4Select(), 'finalUrl' => Qs_Request::getFinalUrl(), 'detailsUrl' => static::getPageUrlByType(ConfigForm::TYPE_DETAILS), 'date' => $obj->getDate(), 'listHtml' => $this->_renderEventList($list), 'tpl' => $this->getTemplate('list.tpl') ]; $this->_doc->addScript('js/moment.js'); $this->_doc->addStylesheet('css/thirdpart/fullcalendar/fullcalendar.css'); $this->_doc->addScript('js/fullcalendar/fullcalendar.js'); $this->_doc->addScript('js/fullcalendar/lang/en-gb.js'); $this->_doc->addStylesheet('css/thirdpart/qtip/jquery.qtip.css'); $this->_doc->addScript('js/qtip/jquery.qtip.js'); $this->_doc->addScript('js/jquery.required.js'); $this->_doc->addScript('js/app/event/calendar.js'); $options = [ 'containerId' => $item['containerId'], 'calendarId' => $item['calendarId'], 'eventListId' => $item['eventListId'], 'finalUrl' => $item['finalUrl'], 'detailsUrl' => $item['detailsUrl'], 'calendarAlias' => self::$calendarAlias, 'defaultDate' => $obj->getDate()->format('Y-m-d'), 'defaultView' => $this->_viewName, 'list' => $list ]; if ($this->_doc->isPagePreviewMode()) { $options['historySupported'] = false; } $this->_doc->addInitObject('app.event.Calendar', [$options]); return $item; } protected function _doLoad() { exit; } /** * Return list of events for specified period * POST param: start 'YYY-MM-DD' * POST param: end 'YYY-MM-DD' * POST param: timezone */ protected function _doLoadAjax() { $obj = $this->_getDataObj(); $obj->setDate(Qs_Request::getPostValue('date')); $range = new Range(Qs_Request::getPostValue('start'), Qs_Request::getPostValue('end')); $obj->setRange($range); $list = $obj->getList(); $this->_displayJson(['list' => $list, 'listHtml' => $this->_renderEventList($list)]); } protected function _renderEventList(array $list) { $date = $this->_getDataObj()->getDate(); $prev = clone $date; $prev->modify('first day of this month -1 month'); $next = clone $date; $next->modify('first day of this month +1 month'); $monthStart = clone $date; $monthStart = $monthStart->modify('first day of this month')->format('Y-m-d H:i:s'); $monthEnd = clone $date; $monthEnd = $monthEnd->modify('last day of this month')->format('Y-m-d H:i:s'); // unset non month events foreach ($list as $idx => $row) { if (!($row['start'] <= $monthEnd && $row['end'] >= $monthStart)) { unset($list[$idx]); } } $today = new DateTime(); $maxDate = clone $today; $maxDate->modify('+1 year'); $minDate = clone $today; $minDate->modify('-1 year'); $finalUrl = Qs_Request::getFinalUrl(); $item = [ 'title' => $date->format('F Y'), 'prevUrl' => ($prev->format('Y-m') < $minDate->format('Y-m')) ? null : $finalUrl . '/month/' . $prev->format('Y-m-d'), 'nextUrl' => ($next->format('Y-m') > $maxDate->format('Y-m')) ? null : $finalUrl . '/month/' . $next->format('Y-m-d'), 'todayUrl' => ($date->format('Y-m') === date('Y-m')) ? null : $finalUrl . '/month/' . date('Y-m-01'), 'list' => $list, 'detailsUrl' => static::getPageUrlByType(ConfigForm::TYPE_DETAILS), 'tpl' => $this->getTemplate('eventList.tpl') ]; $this->_doc->assign('item', new Qs_Doc_Item($item)); return $this->_doc->fetchTemplate($item['tpl']); } protected function _doPrint() { $this->_doc->setLayoutTemplate(Qs_SiteMap::getTemplate('PrintDoc/index.tpl')); $this->_doc->setBodyTemplate($this->getTemplate('print.tpl')); $this->_getDataObj()->setListIncludeTimeRanges(true); $item = [ 'containerId' => Qs_SiteMap::classToId($this), 'date' => $this->_getDataObj()->getDate(), 'list' => $this->_getDataObj()->getList(), ]; $this->_doc->assign('item', new Qs_Doc_Item($item)); Qs_Constant::set('DEBUG', false); return $this; } /** * @param \Qs_Form $form Initialized form from _doInsert action * @return array|null */ protected function _getViewItem(Qs_Form $form = null) { $item = $this->_getDataObj()->getData(); $item['userLoggedIn'] = false; $item['containerId'] = Qs_SiteMap::classToId($this) . '-view'; $item['calendarUrl'] = static::getPageUrlByType(ConfigForm::TYPE_CALENDAR); $item['detailsUrl'] = static::getPageUrlByType(ConfigForm::TYPE_DETAILS); $item['tpl'] = $this->getTemplate('view.tpl'); if ($item['registrationAllowed']) { if (UserAuth::getInstance()->isLoggedIn()) { $item['userLoggedIn'] = true; $this->_initSignupForm($item, $form); } else { if (null === $form) { $this->_initLoginBlock($item); } if (Entity::TYPE_REGISTRATION === $item['type']) { $this->_initSignupForm($item, $form); } } } return $item; } protected function _doView() { $item = $this->_getViewItem(); $this->_initPageSeo($item); $this->_addItem($item); $this->_postView(); return $this; } protected function _initLoginBlock(&$item) { if ('view' === $this->_action) { /** @var \App\User\Login\Form\LoginBlockForm $loginForm */ LoginView::setSessionRedirectPageAlias(CURRENT_PAGE); $loginView = new LoginView(); $loginView->setDoc($this->_doc); $loginForm = $loginView->getLoginBlockForm(); $loginForm->prependId(); $loginForm->initRender(); $item['loginBlock'] = [ 'containerId' => 'event-login-block', 'formHtml' => $loginForm->render(), 'registerUrl' => Qs_SiteMap::find(null, ['type' => 'User\\Register\\'], null, 'url') ]; /** @var $doc \Qs_Doc */ $doc = Zend_Registry::get('doc'); $doc->addScript('js/jquery.defaultHint.js'); $doc->addInitFunction('$("#' . $loginForm->getId() . '").defaultHint', [['useWrapper' => true]]); } return $this; } protected function _doInsert() { $attendeeObj = $this->_getDataObj()->getAttendeeObj(); $form = $this->_getSignupForm(); if ($form->validate()) { $data = $form->getValues(); $attendeeObj->initFromForm($data); if (false === $attendeeObj->insert()) { $this->_setBackErrors($attendeeObj->getErrors()); } else { $this->_postInsert(); } $this->_doBack(); } else { $item = $this->_getViewItem($form); $this->_addItem($item); } return $this; } protected function _postInsert() { parent::_postInsert(); $data = $this->_getDataObj()->getAttendeeObj()->getData(); if (empty($data['total'])) { $this->_setBackMessage(1 < count($data['attendee']) ? self::MSG_ADDED_MULTI : self::MSG_ADDED); $this->_doBack(); } else { if (!$this->_doc->getAuth()->getIdentity()) { App_ECommerce_Checkout_LoginBlock_View::setSessionValue('skipLogin', true); } if (($url = Qs_SiteMap::findFirst(null, ['type' => 'ECommerce_Cart_'], null, 'url'))) { Qs_Http::redirect($url); } else { $this->_setBackMessage(1 < count($data['attendee']) ? self::MSG_ADDED_CART_MULTI : self::MSG_ADDED_CART); } } return $this; } protected function _getSignupForm(array $options = []) { $options = array_merge($this->_getSignupFormOptions(), $options); $form = $this->_getFormInstance('new', $options); $form->setDefaults(); return $form; } protected function _getSignupFormOptions() { $obj = $this->_getDataObj(); $attendeeObj = $obj->getAttendeeObj(); $auth = UserAuth::getInstance(); $isLoggedIn = $auth->isLoggedIn(); $options = [ 'allowCurrentUser' => ($isLoggedIn) ? (false == $attendeeObj->getAttendeeRegistered($auth->getData('id'), $auth->getData('email'))) : true, 'allowMembers' => $isLoggedIn, 'currentUserId' => $isLoggedIn ? $auth->getData('id') : null, 'allowNonMembers' => $obj->getNonMembersAllowed(), 'userAutocompleteUrl' => UserAutocompleteView::getUserAutocompleteUrl4Event(), ]; return $options; } protected function _getNewForm(array $options = []) { return $this->_getSignupForm($options); } protected function _initSignupForm(&$item, Qs_Form $form = null) { if (null === $form) { $options = []; if (isset($item['loginBlock'])) { $options['loginBlockId'] = $item['loginBlock']['containerId']; } $form = $this->_getNewForm($options); } $item['signupFormHtml'] = $form->render(); return $this; } protected function doRenderEvent() { exit; } /** * request POST: * int $eventId * * response JSON: * int eventId * string eventHtml */ protected function _doRenderEventAjax() { if (null == ($eventId = Qs_Request::getPostValue('eventId'))) { $this->_displayJson(['isError' => true, 'message' => 'Wrong request']); } $this->_getDataObj()->setPrimaryKey($eventId); if (null == ($item = $this->_getDataObj()->getData())) { $this->_displayJson(['isError' => true, 'message' => 'Wrong eventId']); } $item['viewUrl'] = static::getPageUrlByType(ConfigForm::TYPE_DETAILS) . '/' . $item['alias']; $item['tpl'] = $this->getTemplate('event.tpl'); $item['config'] = $this->getConfig()->toArray(); $this->_doc->assign('item', new Qs_Doc_Item($item)); $html = $this->_doc->fetchTemplate($item['tpl']); $response = [ 'eventId' => $eventId, 'eventHtml' => $html ]; $this->_displayJson($response); } protected function _getFormOptions($type = null) { $options = parent::_getFormOptions($type); $options['eventObj'] = $this->_getDataObj(); $options['attendeeObj'] = $this->_getDataObj()->getAttendeeObj(); return $options; } protected function _doICal() { $data = $this->_getDataObj()->getData(); $calendar = new VCalendar(SITE_NAME); $hostSuffix = '@' . SITE_NAME; $detailsUrl = static::getPageUrlByType(ConfigForm::TYPE_DETAILS) . '/'; foreach ($data['timeRanges'] as $idx => $range) { $event = new VEvent(); $event->setUniqueId($data['id'] . '-' . $idx . $hostSuffix); $event->setDtStart(new DateTime($data['startDate'] . ' ' . $range['startTime'])); $event->setDtEnd(new DateTime($data['endDate'] . ' ' . $range['endTime'])); $event->setLocation($data['location']); $event->setUrl($detailsUrl . $data['alias']); $event->setSummary($data['title']); $calendar->addEvent($event); } $filename = preg_replace('/\W+/', '_', $data['title']); header('Content-Type: text/calendar; charset=utf-8'); header('Content-Disposition: attachment; filename="' . $filename . '.ics"'); echo $calendar->render(); exit; } protected function _doRenderAttendee() { exit; } /** * post param: int $lastIndex */ protected function _doRenderAttendeeAjax() { if (null === ($lastIndex = Qs_Request::getPostValue('lastIndex'))) { $this->_displayJson(['isError' => 'true', 'message' => 'Wrong request']); exit; } $this->_doc ->clearInlineScripts() ->clearInitFunctions() ->clearScripts() ->clearStylesheets(); $formIndex = (int) $lastIndex + 1; $form = $this->_getSignupForm([ 'renderMode' => Form\NewForm::RENDER_MODE_ATTENDEE, 'attendeeIndex' => $formIndex, ]); $response = [ 'formHtml' => $form->render(), 'formIndex' => $formIndex, 'evalData' => $this->_doc->getResources() ]; $this->_displayJson($response); exit; } public function writeXmlSitemap(App_Sitemap_Xml_Writer $xml) { if (ConfigForm::TYPE_DETAILS != $this->getConfig('type')) { return $this; } return $this->writeDefaultXmlSitemap($xml); } }