/** * @param DC_General $dc */ public function renderPreviewView(EnvironmentInterface $environment) { /** @var EventDispatcher $eventDispatcher */ $eventDispatcher = $GLOBALS['container']['event-dispatcher']; $eventDispatcher->dispatch(ContaoEvents::SYSTEM_LOAD_LANGUAGE_FILE, new LoadLanguageFileEvent('avisota_message_preview')); $eventDispatcher->dispatch(ContaoEvents::SYSTEM_LOAD_LANGUAGE_FILE, new LoadLanguageFileEvent('orm_avisota_message')); $input = \Input::getInstance(); $messageRepository = EntityHelper::getRepository('Avisota\\Contao:Message'); $messageId = IdSerializer::fromSerialized($input->get('id') ? $input->get('id') : $input->get('pid')); $message = $messageRepository->find($messageId->getId()); if (!$message) { $environment = \Environment::getInstance(); $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_REDIRECT, new RedirectEvent(preg_replace('#&(act=preview|id=[a-f0-9\\-]+)#', '', $environment->request))); } $modules = new \StringBuilder(); /** @var \Avisota\Contao\Message\Core\Send\SendModuleInterface $module */ foreach ($GLOBALS['AVISOTA_SEND_MODULE'] as $className) { $class = new \ReflectionClass($className); $module = $class->newInstance(); $modules->append($module->run($message)); } $context = array('message' => $message, 'modules' => $modules); $template = new \TwigTemplate('avisota/backend/preview', 'html5'); return $template->parse($context); }
/** * Retrieve the options for the attributes. * * @param GetOptionsEvent $event The event. * * @return void */ public function getOptions(GetOptionsEvent $event) { if (self::isEventForMe($event)) { return; } $model = $event->getModel(); $metaModelId = $model->getProperty('pid'); if (!$metaModelId) { $metaModelId = IdSerializer::fromSerialized($event->getEnvironment()->getInputProvider()->getValue('pid'))->getId(); } $factory = $this->getServiceContainer()->getFactory(); $metaModelName = $factory->translateIdToMetaModelName($metaModelId); $metaModel = $factory->getMetaModel($metaModelName); if (!$metaModel) { return; } $result = array(); // Fetch all attributes except for the current attribute. foreach ($metaModel->getAttributes() as $attribute) { if ($attribute->get('id') === $model->getId()) { continue; } $result[$attribute->getColName()] = sprintf('%s [%s]', $attribute->getName(), $attribute->get('type')); } $event->setOptions($result); }
/** * @return MessageCategory|null */ public static function resolveCategoryFromInput() { $input = \Input::getInstance(); $id = $input->get('id'); $pid = $input->get('pid'); $modelId = null; $parentModelId = null; /** @var MessageCategory $category */ $category = null; /** @var Message $message */ $message = null; /** @var MessageContent $content */ $content = null; if ($id) { $modelId = IdSerializer::fromSerialized($id); } if ($pid) { $parentModelId = IdSerializer::fromSerialized($pid); } // $id or $pid is a category ID if ($modelId && $modelId->getDataProviderName() == 'orm_avisota_message_category') { $repository = EntityHelper::getRepository('Avisota\\Contao:MessageCategory'); $category = $repository->find($modelId->getId()); } else { if ($parentModelId && $parentModelId->getDataProviderName() == 'orm_avisota_message_category') { $repository = EntityHelper::getRepository('Avisota\\Contao:MessageCategory'); $category = $repository->find($parentModelId->getId()); } else { if ($modelId && $modelId->getDataProviderName() == 'orm_avisota_message') { $repository = EntityHelper::getRepository('Avisota\\Contao:Message'); $message = $repository->find($modelId->getId()); $category = $message->getCategory(); } else { if ($parentModelId && $parentModelId->getDataProviderName() == 'orm_avisota_message') { $repository = EntityHelper::getRepository('Avisota\\Contao:Message'); $message = $repository->find($parentModelId->getId()); $category = $message->getCategory(); } else { if ($modelId && $modelId->getDataProviderName() == 'orm_avisota_message_content') { $repository = EntityHelper::getRepository('Avisota\\Contao:MessageContent'); $content = $repository->find($modelId->getId()); $message = $content->getMessage(); $category = $message->getCategory(); } else { if ($parentModelId && $parentModelId->getDataProviderName() == 'orm_avisota_message_content') { $repository = EntityHelper::getRepository('Avisota\\Contao:MessageContent'); $content = $repository->find($parentModelId->getId()); $message = $content->getMessage(); $category = $message->getCategory(); } } } } } } return $category; }
/** * Prepares a option list with alias => name connection for all attributes. * * This is used in the attr_id select box. * * @param GetPropertyOptionsEvent $event The event. * * @return void */ public function getAttributeIdOptions(GetPropertyOptionsEvent $event) { // Check the context. $allowedProperties = array('first_attr_id', 'second_attr_id', 'single_attr_id'); if (!$this->isAllowedProperty($event, 'tl_metamodel_attribute', $allowedProperties)) { return; } $result = array(); $model = $event->getModel(); $metaModelId = $model->getProperty('pid'); if (!$metaModelId) { $metaModelId = IdSerializer::fromSerialized($event->getEnvironment()->getInputProvider()->getValue('pid'))->getId(); } $factory = $this->getServiceContainer()->getFactory(); $metaModelName = $factory->translateIdToMetaModelName($metaModelId); $metaModel = $factory->getMetaModel($metaModelName); if (!$metaModel) { return; } $typeFactory = $this->getServiceContainer()->getFilterFactory()->getTypeFactory($model->getProperty('type')); $typeFilter = array(); if ($typeFactory) { $typeFilter = $typeFactory->getKnownAttributeTypes(); } if ($event->getPropertyName() === 'single_attr_id') { $typeFilter = array('geolocation'); } else { $key = array_search('geolocation', $typeFilter); if ($key !== null) { unset($typeFilter[$key]); } } foreach ($metaModel->getAttributes() as $attribute) { $typeName = $attribute->get('type'); if ($typeFilter && !in_array($typeName, $typeFilter)) { continue; } $strSelectVal = $attribute->getColName(); $result[$strSelectVal] = $attribute->getName() . ' [' . $typeName . ']'; } $event->setOptions($result); }
public function handleAction(ActionEvent $event) { $environment = $event->getEnvironment(); if ($environment->getDataDefinition()->getName() != 'orm_avisota_queue') { return; } $action = $event->getAction(); if ($action->getName() == 'clear') { $input = $environment->getInputProvider(); $id = IdSerializer::fromSerialized($input->getParameter('id')); $repository = EntityHelper::getRepository('Avisota\\Contao:Queue'); /** @var \Avisota\Contao\Entity\Queue $queueData */ $queueData = $repository->find($id->getId()); /** @var QueueInterface $queue */ $queue = $GLOBALS['container'][sprintf('avisota.queue.%s', $queueData->getAlias())]; $queue->execute(new NullTransport()); $message = new AddMessageEvent(sprintf($GLOBALS['TL_LANG']['orm_avisota_queue']['queueCleared'], $queueData->getTitle()), AddMessageEvent::TYPE_CONFIRM); $event->getDispatcher()->dispatch(ContaoEvents::MESSAGE_ADD, $message); $redirect = new RedirectEvent('contao/main.php?do=avisota_queue&ref=' . TL_REFERER_ID); $event->getDispatcher()->dispatch(ContaoEvents::CONTROLLER_REDIRECT, $redirect); } }
/** * {@inheritDoc} */ public function enforceModelRelationship($model) { $environment = $this->getEnvironment(); $input = $environment->getInputProvider(); $controller = $environment->getController(); if ($input->hasParameter('into')) { $into = IdSerializer::fromSerialized($input->getParameter('into')); // If we have a null, it means insert into the tree root. if ($into->getId() == 0) { $controller->setRootModel($model); } else { $parent = $controller->fetchModelFromProvider($into); $controller->setParent($model, $parent); } } elseif ($input->hasParameter('after')) { $after = IdSerializer::fromSerialized($input->getParameter('after')); $sibling = $controller->fetchModelFromProvider($after); if (!$sibling || $controller->isRootModel($sibling)) { $controller->setRootModel($model); } else { $parent = $controller->searchParentOf($sibling); $controller->setParent($model, $parent); } } // Also enforce the parent condition of the parent provider (if any). if ($input->hasParameter('pid')) { $parent = $controller->fetchModelFromProvider($input->getParameter('pid')); $controller->setParent($model, $parent); } }
/** * Handle "old" add to clipboard actions. * * @param ActionEvent $event The action event. * * @return void */ private function addToClipboard(ActionEvent $event) { $actionName = $event->getAction()->getName(); $environment = $event->getEnvironment(); $input = $environment->getInputProvider(); $clipboard = $environment->getClipboard(); if ('create' === $actionName) { $modelId = IdSerializer::fromValues($environment->getDataDefinition()->getBasicDefinition()->getDataProvider(), null); } else { $modelIdRaw = $input->getParameter('source'); $modelId = IdSerializer::fromSerialized($modelIdRaw); } $parentIdRaw = $input->getParameter('pid'); if ($parentIdRaw) { $parentId = IdSerializer::fromSerialized($parentIdRaw); } else { $parentId = null; } // Push some entry into clipboard. if ($modelId) { $clipboardActionName = $this->translateActionName($actionName); if ($clipboardActionName) { // Remove other create items, there can only be one create item in the clipboard or many others if (Item::CREATE === $clipboardActionName) { $clipboard->clear(); } else { $filter = new Filter(); $filter->andActionIs(ItemInterface::CREATE); $items = $clipboard->fetch($filter); foreach ($items as $item) { $clipboard->remove($item); } } // Only push item to clipboard if manual sorting is used. if (Item::COPY === $clipboardActionName && !ViewHelpers::getManualSortingProperty($environment)) { return; } // create the new item $item = new Item($clipboardActionName, $parentId, $modelId); // Let the clipboard save it's values persistent. // TODO remove clear and allow adding multiple items $clipboard->clear()->push($item)->saveTo($environment); ViewHelpers::redirectHome($environment); } } }
/** * {@inheritdoc} * * @throws DcGeneralRuntimeException When the model could not be found by the data provider. */ public function edit(Action $action) { $environment = $this->getEnvironment(); $inputProvider = $environment->getInputProvider(); $modelId = $inputProvider->hasParameter('id') && $inputProvider->getParameter('id') ? IdSerializer::fromSerialized($inputProvider->getParameter('id')) : null; $dataProvider = $environment->getDataProvider($modelId ? $modelId->getDataProviderName() : null); $this->checkRestoreVersion(); if ($modelId && $modelId->getId()) { $model = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($modelId->getId())); } else { $model = $this->getEnvironment()->getController()->createEmptyModelWithDefaults(); } if (!$model) { throw new DcGeneralRuntimeException('Could not retrieve model with id ' . $modelId->getSerialized()); } // We need to keep the original data here. $originalModel = clone $model; $originalModel->setId($model->getId()); return $this->createEditMask($model, $originalModel, null, null); }
/** * Handle the persisting of the currently loaded model. * * @return bool True means everything is okay, False error. */ protected function doPersist() { $environment = $this->getEnvironment(); $dataProvider = $environment->getDataProvider($this->model->getProviderName()); $inputProvider = $environment->getInputProvider(); if (!$this->model->getMeta(ModelInterface::IS_CHANGED)) { return true; } $this->handlePrePersist(); if ($this->model->getId() === null && $this->getManualSortingProperty()) { $models = $dataProvider->getEmptyCollection(); $models->push($this->model); $controller = $environment->getController(); if ($inputProvider->hasParameter('after')) { $after = IdSerializer::fromSerialized($inputProvider->getParameter('after')); $previousDataProvider = $environment->getDataProvider($after->getDataProviderName()); $previousFetchConfig = $previousDataProvider->getEmptyConfig(); $previousFetchConfig->setId($after->getId()); $previousModel = $previousDataProvider->fetch($previousFetchConfig); if ($previousModel) { $controller->pasteAfter($previousModel, $models, $this->getManualSortingProperty()); } else { $controller->pasteTop($models, $this->getManualSortingProperty()); } } elseif ($inputProvider->hasParameter('into')) { $into = IdSerializer::fromSerialized($inputProvider->getParameter('into')); $parentDataProvider = $environment->getDataProvider($into->getDataProviderName()); $parentFetchConfig = $parentDataProvider->getEmptyConfig(); $parentFetchConfig->setId($into->getId()); $parentModel = $parentDataProvider->fetch($parentFetchConfig); if ($parentModel) { $controller->pasteInto($parentModel, $models, $this->getManualSortingProperty()); } else { $controller->pasteTop($models, $this->getManualSortingProperty()); } } else { $controller->pasteTop($models, $this->getManualSortingProperty()); } $environment->getClipboard()->clear()->saveTo($environment); } else { if (!$this->allValuesUnique()) { return false; } // Save the model. $dataProvider->save($this->model); } $this->handlePostPersist(); $this->storeVersion($this->model); return true; }
/** * {@inheritdoc} * * @throws DcGeneralRuntimeException When no source id is provided by the input provider. */ public function copy() { if ($this->environment->getDataDefinition()->getBasicDefinition()->isEditOnlyMode()) { return $this->edit(); } // TODO sorting is still hardcoded :-( if ($this->environment->getDataProvider()->fieldExists('sorting')) { $this->checkClipboard('copy'); $this->redirectHome(); } $this->checkLanguage(); $environment = $this->getEnvironment(); $dataProvider = $environment->getDataProvider(); $modelId = IdSerializer::fromSerialized($environment->getInputProvider()->getParameter('source')); if ($modelId) { $model = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($modelId->getId())); } else { throw new DcGeneralRuntimeException('Missing model id.'); } // We need to keep the original data here. $copyModel = $environment->getController()->createClonedModel($model); $copyModel->setId(null); $preFunction = function (EnvironmentInterface $environment, $model, $originalModel) { $copyEvent = new PreDuplicateModelEvent($environment, $model); $environment->getEventPropagator()->propagate($copyEvent::NAME, $copyEvent, array($environment->getDataDefinition()->getName())); }; $postFunction = function (EnvironmentInterface $environment, $model, $originalModel) { $copyEvent = new PostDuplicateModelEvent($environment, $model, $originalModel); $environment->getEventPropagator()->propagate($copyEvent::NAME, $copyEvent, array($environment->getDataDefinition()->getName())); }; return $this->createEditMask($copyModel, $model, $preFunction, $postFunction); }
/** * Check if the language has been switched. * * If so, the value in the session will be updated and the page reloaded. * * @param EnvironmentInterface $environment The environment. * * @return void */ private function checkLanguageSubmit($environment) { $sessionStorage = $environment->getSessionStorage(); $inputProvider = $environment->getInputProvider(); if ($inputProvider->getValue('FORM_SUBMIT') !== 'language_switch') { return; } $modelId = $inputProvider->getParameter('id') && $inputProvider->getParameter('id') ? IdSerializer::fromSerialized($inputProvider->getParameter('id'))->getId() : null; $languages = $environment->getController()->getSupportedLanguages($modelId); $providerName = $environment->getDataDefinition()->getName(); // Get/Check the new language. if ($inputProvider->hasValue('language') && array_key_exists($inputProvider->getValue('language'), $languages)) { $session['ml_support'][$providerName][$modelId] = $inputProvider->getValue('language'); $sessionStorage->set('dc_general', $session); } $environment->getEventDispatcher()->dispatch(ContaoEvents::CONTROLLER_RELOAD, new ReloadEvent()); }
public function handleListAction(EnvironmentInterface $environment) { $input = $environment->getInputProvider(); $id = IdSerializer::fromSerialized($input->getParameter('id')); $repository = EntityHelper::getRepository('Avisota\\Contao:RecipientSource'); /** @var \Avisota\Contao\Entity\RecipientSource $recipientSourceEntity */ $recipientSourceEntity = $repository->find($id->getId()); /** @var RecipientSourceInterface $recipientSource */ $recipientSource = $GLOBALS['container'][sprintf('avisota.recipientSource.%s', $recipientSourceEntity->getId())]; if ($input->getValue('page')) { /** @var EventDispatcher $eventDispatcher */ $eventDispatcher = $GLOBALS['container']['event-dispatcher']; $addToUrlEvent = new AddToUrlEvent('page=' . (int) $input->getValue('page')); $eventDispatcher->dispatch(ContaoEvents::BACKEND_ADD_TO_URL, $addToUrlEvent); $redirectEvent = new RedirectEvent($addToUrlEvent->getUrl()); $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_REDIRECT, $redirectEvent); } $total = $recipientSource->countRecipients(); $page = $input->getParameter('page') ?: 0; $pages = ceil($total / 30); $recipients = $recipientSource->getRecipients(30, $page * 30); $template = new \TwigBackendTemplate('avisota/backend/recipient_source_list'); $template->total = $total; $template->page = $page; $template->pages = $pages; $template->recipients = $recipients; return $template->parse(); }
/** * {@inheritDoc} */ public function fetchModelFromProvider($id, $providerName = null) { if ($providerName === null) { if (is_string($id)) { $id = IdSerializer::fromSerialized($id); } } else { $id = IdSerializer::fromValues($providerName, $id); } $dataProvider = $this->getEnvironment()->getDataProvider($id->getDataProviderName()); $item = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($id->getId())); return $item; }
/** * Create the "new" button. * * @return CommandInterface|null */ protected function getCreateModelCommand() { $environment = $this->getEnvironment(); $definition = $environment->getDataDefinition(); $basicDefinition = $definition->getBasicDefinition(); $providerName = $environment->getDataDefinition()->getName(); $mode = $basicDefinition->getMode(); $config = $this->getEnvironment()->getController()->getBaseConfig(); $sorting = $this->getManualSortingProperty(); if ($serializedPid = $environment->getInputProvider()->getParameter('pid')) { $pid = IdSerializer::fromSerialized($serializedPid); } else { $pid = new IdSerializer(); } if ($basicDefinition->isClosed()) { return null; } $command = new Command(); $parameters = $command->getParameters(); $extra = $command->getExtra(); $extra['class'] = 'header_new'; $extra['accesskey'] = 'n'; $extra['attributes'] = 'onclick="Backend.getScrollOffset();"'; $command->setName('button_new')->setLabel($this->translate('new.0', $providerName))->setDescription($this->translate('new.1', $providerName)); $this->getPanel()->initialize($config); // Add new button. if ($mode == BasicDefinitionInterface::MODE_FLAT || $mode == BasicDefinitionInterface::MODE_PARENTEDLIST && !$sorting) { $parameters['act'] = 'create'; // Add new button. if ($pid->getDataProviderName() && $pid->getId()) { $parameters['pid'] = $pid->getSerialized(); } } elseif ($mode == BasicDefinitionInterface::MODE_PARENTEDLIST || $mode == BasicDefinitionInterface::MODE_HIERARCHICAL) { if ($environment->getClipboard()->isNotEmpty()) { return null; } $after = IdSerializer::fromValues($definition->getName(), 0); $parameters['act'] = 'paste'; $parameters['mode'] = 'create'; if ($mode == BasicDefinitionInterface::MODE_PARENTEDLIST) { $parameters['after'] = $after->getSerialized(); } if ($pid->getDataProviderName() && $pid->getId()) { $parameters['pid'] = $pid->getSerialized(); } } return $command; }
/** * Reload the file tree. * * @param string $strType The type. * * @param DataContainerInterface $objDc The data container. * * @return void */ protected function reloadTree($strType, DataContainerInterface $objDc) { $environment = $objDc->getEnvironment(); $input = $environment->getInputProvider(); $serializedId = $input->hasParameter('id') ? $input->getParameter('id') : null; $fieldName = $input->hasValue('name') ? $input->getValue('name') : null; // Handle the keys in "edit multiple" mode. if (self::getGet('act') == 'editAll') { // TODO: change here when implementing editAll. $serializedId = preg_replace('/.*_([0-9a-zA-Z]+)$/', '$1', $fieldName); $field = preg_replace('/(.*)_[0-9a-zA-Z]+$/', '$1', $fieldName); } if (!is_null($serializedId)) { $id = IdSerializer::fromSerialized($serializedId); $dataProvider = $objDc->getEnvironment()->getDataProvider($id->getDataProviderName()); $model = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($id->getId())); if (is_null($model)) { $this->log('A record with the ID "' . $serializedId . '" does not exist in "' . $objDc->getEnvironment()->getDataDefinition()->getName() . '"', 'Ajax executePostActions()', TL_ERROR); header('HTTP/1.1 400 Bad Request'); die('Bad Request'); } } $varValue = $this->getTreeValue($strType, $input->getValue('value')); $strKey = $strType . 'Tree'; // Set the new value. if (isset($model)) { $model->setProperty($fieldName, $varValue); $arrAttribs['activeRecord'] = $model; } else { $arrAttribs['activeRecord'] = null; } $arrAttribs['id'] = $fieldName; $arrAttribs['name'] = $fieldName; $arrAttribs['value'] = $varValue; $arrAttribs['strTable'] = $objDc->getEnvironment()->getDataDefinition()->getName(); $arrAttribs['strField'] = $fieldName; /** @var \Widget $objWidget */ $objWidget = new $GLOBALS['BE_FFL'][$strKey]($arrAttribs); echo $objWidget->generate(); exit; }