/** * Perform the advanced node action */ public function indexAction(Cms $cms, SecurityManager $securityManager, $locale, $site, $revision, $node) { if (!$cms->resolveNode($site, $revision, $node)) { return; } $this->setContentLocale($locale); $cms->setLastAction(self::NAME); $translator = $this->getTranslator(); $referer = $this->request->getQueryParameter('referer'); $security = $node->get(Node::PROPERTY_SECURITY, 'inherit', false); switch ($security) { case 'inherit': case Node::AUTHENTICATION_STATUS_EVERYBODY: case Node::AUTHENTICATION_STATUS_ANONYMOUS: $permissions = null; break; case Node::AUTHENTICATION_STATUS_AUTHENTICATED: default: $permissions = array_flip(explode(',', $security)); $security = Node::AUTHENTICATION_STATUS_AUTHENTICATED; break; } $data = array('published' => $node->get(Node::PROPERTY_PUBLISH, 'inherit', false), 'publishStart' => $node->get(Node::PROPERTY_PUBLISH_START, null, false), 'publishStop' => $node->get(Node::PROPERTY_PUBLISH_STOP, null, false), 'security' => $security, 'permissions' => $permissions); $permissions = $securityManager->getSecurityModel()->getPermissions(); $nodeType = $cms->getNodeType($node); $isFrontendNode = $nodeType->getFrontendCallback() || $node->getLevel() === 0 ? true : false; if ($isFrontendNode) { $data['hide'] = array(); if ($node->hideInMenu()) { $data['hide']['menu'] = 'menu'; } if ($node->hideInBreadcrumbs()) { $data['hide']['breadcrumbs'] = 'breadcrumbs'; } if ($node->hideForAnonymousUsers()) { $data['hide']['anonymous'] = 'anonymous'; } if ($node->hideForAuthenticatedUsers()) { $data['hide']['authenticated'] = 'authenticated'; } } $form = $this->createFormBuilder($data); $form->addRow('published', 'option', array('label' => $translator->translate('label.publish'), 'options' => $this->getPublishedOptions($node, $translator))); $form->addRow('publishStart', 'string', array('label' => $translator->translate('label.publish.start'), 'description' => $translator->translate('label.publish.start.description'), 'filters' => array('trim' => array()), 'validators' => array('regex' => array('required' => false, 'regex' => '/2([0-9]){3}-([0-9]){2}-([0-9]){2} ([0-9]){2}:([0-9]){2}:([0-9]){2}/', 'error.regex' => 'error.validation.date.cms')))); $form->addRow('publishStop', 'string', array('label' => $translator->translate('label.publish.stop'), 'filters' => array('trim' => array()), 'validators' => array('regex' => array('required' => false, 'regex' => '/2([0-9]){3}-([0-9]){2}-([0-9]){2} ([0-9]){2}:([0-9]){2}:([0-9]){2}/', 'error.regex' => 'error.validation.date.cms')))); $form->addRow('security', 'option', array('label' => $translator->translate('label.allow'), 'attributes' => array('data-toggle-dependant' => 'option-security'), 'options' => $this->getSecurityOptions($node, $translator), 'validators' => array('required' => array()))); if ($permissions) { $form->addRow('permissions', 'option', array('label' => $translator->translate('label.permissions.required'), 'attributes' => array('class' => 'option-security option-security-authenticated'), 'multiple' => true, 'options' => $permissions)); } if ($isFrontendNode) { $form->addRow('hide', 'option', array('label' => $translator->translate('label.hide'), 'options' => array('menu' => $translator->translate('label.hide.menu'), 'breadcrumbs' => $translator->translate('label.hide.breadcrumbs'), 'anonymous' => $translator->translate('label.hide.anonymous'), 'authenticated' => $translator->translate('label.hide.authenticated')), 'multiple' => true)); } $form = $form->build(); if ($form->isSubmitted()) { try { $form->validate(); $data = $form->getData(); $security = $this->getSecurityValue($data['security']); if ($security == Node::AUTHENTICATION_STATUS_AUTHENTICATED && $permissions && $data['permissions']) { $security = implode(',', $data['permissions']); } $node->set(Node::PROPERTY_PUBLISH, $this->getPublishedValue($data['published'])); $node->set(Node::PROPERTY_PUBLISH_START, $data['publishStart']); $node->set(Node::PROPERTY_PUBLISH_STOP, $data['publishStop']); $node->set(Node::PROPERTY_SECURITY, $security); if ($isFrontendNode) { if ($node->getLevel() === 0) { $inherit = false; } else { $inherit = null; } $node->setHideInMenu(isset($data['hide']['menu']), $inherit); $node->setHideInBreadcrumbs(isset($data['hide']['breadcrumbs']), $inherit); $node->setHideForAnonymousUsers(isset($data['hide']['anonymous']), $inherit); $node->setHideForAuthenticatedUsers(isset($data['hide']['authenticated']), $inherit); } $cms->saveNode($node, 'Set visibility of ' . $node->getName()); $this->addSuccess('success.node.saved', array('node' => $node->getName($locale))); $url = $this->getUrl(self::ROUTE, array('site' => $site->getId(), 'revision' => $node->getRevision(), 'locale' => $locale, 'node' => $node->getId())); if ($referer) { $url .= '?referer=' . urlencode($referer); } $this->response->setRedirect($url); return; } catch (ValidationException $exception) { $validationException = new ValidationException(); $errors = $exception->getAllErrors(); foreach ($errors as $field => $fieldErrors) { if ($field == Node::PROPERTY_PUBLISH) { $validationException->addErrors('published', $fieldErrors); } elseif ($field == Node::PROPERTY_PUBLISH_START) { $validationException->addErrors('publishStart', $fieldErrors); } elseif ($field == Node::PROPERTY_PUBLISH_STOP) { $validationException->addErrors('publishStop', $fieldErrors); } else { $validationException->addErrors($field, $fieldErrors); } } $this->setValidationException($validationException, $form); } } $view = $this->setTemplateView('cms/backend/node.visibility', array('site' => $site, 'node' => $node, 'form' => $form->getView(), 'referer' => $referer, 'locale' => $locale, 'locales' => $cms->getLocales())); $form->processView($view); }
/** * Applies the validation rules * @param \ride\library\validation\exception\ValidationException $validationException * @return null */ public function applyValidation(ValidationException $validationException) { foreach ($this->filters as $filter) { $this->data = $filter->filter($this->data); } if (isset($this->widget)) { $this->widget->setValue($this->data); $name = $this->widget->getName(); } else { $name = $this->name; } foreach ($this->validators as $validator) { if (!$validator->isValid($this->data)) { $validationException->addErrors($name, $validator->getErrors()); } } }
/** * Action to dispatch to the properties of a widget * @param \ride\web\cms\Cms $cms * @param \ride\library\security\SecurityManager $securityManager * @param string $locale * @param string $site * @param string $revision * @param string $node * @param string $region * @param string $widget * @return null */ public function indexAction(Cms $cms, SecurityManager $securityManager, $locale, $site, $revision, $node, $region, $widget) { if (!$cms->resolveNode($site, $revision, $node) || !$cms->resolveRegion($node, $locale, $region)) { return; } $widgetId = $widget; $widgetProperties = $node->getWidgetProperties($widgetId); $widget = $site->getWidget($widgetId); $widget = clone $cms->getWidget($widget); $widget->setRequest($this->request); $widget->setResponse($this->response); $widget->setProperties($widgetProperties); $widget->setLocale($locale); $widget->setRegion($region); if ($widget instanceof AbstractController) { $widget->setConfig($this->config); $widget->setDependencyInjector($this->dependencyInjector); } $translator = $this->getTranslator(); $referer = $this->request->getQueryParameter('referer'); $security = $widgetProperties->getWidgetProperty(Node::PROPERTY_SECURITY, Node::AUTHENTICATION_STATUS_EVERYBODY); switch ($security) { case 'inherit': case Node::AUTHENTICATION_STATUS_EVERYBODY: case Node::AUTHENTICATION_STATUS_ANONYMOUS: $permissions = null; break; case Node::AUTHENTICATION_STATUS_AUTHENTICATED: default: $permissions = array_flip(explode(',', $security)); $security = Node::AUTHENTICATION_STATUS_AUTHENTICATED; break; } $data = array('published' => $widgetProperties->getWidgetProperty(Node::PROPERTY_PUBLISH, true), 'publishStart' => $widgetProperties->getWidgetProperty(Node::PROPERTY_PUBLISH_START, null), 'publishStop' => $widgetProperties->getWidgetProperty(Node::PROPERTY_PUBLISH_STOP, null), 'security' => $security, 'permissions' => $permissions); $permissions = $securityManager->getSecurityModel()->getPermissions(); $form = $this->createFormBuilder($data); $form->addRow('published', 'option', array('label' => $translator->translate('label.publish'), 'options' => $this->getPublishedOptions($translator))); $form->addRow('publishStart', 'string', array('label' => $translator->translate('label.publish.start'), 'description' => $translator->translate('label.publish.start.description'), 'filters' => array('trim' => array()), 'validators' => array('regex' => array('required' => false, 'regex' => '/2([0-9]){3}-([0-9]){2}-([0-9]){2} ([0-9]){2}:([0-9]){2}:([0-9]){2}/', 'error.regex' => 'error.validation.date.cms')))); $form->addRow('publishStop', 'string', array('label' => $translator->translate('label.publish.stop'), 'filters' => array('trim' => array()), 'validators' => array('regex' => array('required' => false, 'regex' => '/2([0-9]){3}-([0-9]){2}-([0-9]){2} ([0-9]){2}:([0-9]){2}:([0-9]){2}/', 'error.regex' => 'error.validation.date.cms')))); $form->addRow('security', 'option', array('label' => $translator->translate('label.allow'), 'options' => $this->getSecurityOptions($translator), 'validators' => array('required' => array()))); if ($permissions) { $form->addRow('permissions', 'option', array('label' => $translator->translate('label.permissions.required'), 'attributes' => array('class' => 'option-security option-security-authenticated'), 'multiple' => true, 'options' => $permissions)); } $form = $form->build(); if ($form->isSubmitted()) { try { $form->validate(); $data = $form->getData(); if ($data['security'] == Node::AUTHENTICATION_STATUS_AUTHENTICATED && $permissions && $data['permissions']) { $data['security'] = implode(',', $data['permissions']); } $widgetProperties->setWidgetProperty(Node::PROPERTY_PUBLISH, $data['published']); $widgetProperties->setWidgetProperty(Node::PROPERTY_PUBLISH_START, $data['publishStart']); $widgetProperties->setWidgetProperty(Node::PROPERTY_PUBLISH_STOP, $data['publishStop']); $widgetProperties->setWidgetProperty(Node::PROPERTY_SECURITY, $data['security']); $cms->saveNode($node, 'Updated visibility properties for widget ' . $widgetId . ' in ' . $node->getName()); $this->addSuccess('success.widget.saved', array('widget' => $translator->translate('widget.' . $widget->getName()))); $this->response->setRedirect($this->getUrl('cms.node.content.region', array('locale' => $locale, 'site' => $site->getId(), 'revision' => $node->getRevision(), 'node' => $node->getId(), 'region' => $region))); return; } catch (ValidationException $exception) { $validationException = new ValidationException(); $errors = $exception->getAllErrors(); foreach ($errors as $field => $fieldErrors) { if ($field == Node::PROPERTY_PUBLISH) { $validationException->addErrors('published', $fieldErrors); } elseif ($field == Node::PROPERTY_PUBLISH_START) { $validationException->addErrors('publishStart', $fieldErrors); } elseif ($field == Node::PROPERTY_PUBLISH_STOP) { $validationException->addErrors('publishStop', $fieldErrors); } else { $validationException->addErrors($field, $fieldErrors); } } $this->setValidationException($validationException, $form); } } $referer = $this->request->getQueryParameter('referer'); $this->setTemplateView('cms/backend/widget.visibility', array('site' => $site, 'node' => $node, 'referer' => $referer, 'locale' => $locale, 'locales' => $cms->getLocales(), 'region' => $region, 'widget' => $widget, 'widgetId' => $widgetId, 'widgetName' => $translator->translate('widget.' . $widget->getName()), 'form' => $form->getView())); }
/** * Adds the errors of the validation exception to the document * @param \ride\library\validation\exception\ValidationException $exception * @return null */ private function handleValidationException(ValidationException $exception, ModelMeta $meta) { foreach ($exception->getAllErrors() as $fieldName => $fieldErrors) { $field = $meta->getField($fieldName); if ($field instanceof PropertyField) { $source = '/data/attributes/' . $fieldName; } else { $source = '/data/relationships/' . $fieldName; } foreach ($fieldErrors as $error) { $error = $this->api->createError(Response::STATUS_CODE_BAD_REQUEST, $error->getCode(), $error->getMessage(), (string) $error); $error->setSourcePointer($source); $this->document->addError($error); } } }
/** * Validate a date configuration value * @param string $date date configuration value * @param \ride\library\validation\exception\ValidationException $exception * when a ValidationError occures, it will be added to this exception * @param string $fieldName name of the field to register possible errors * to the ValidationException * @return null */ protected function validateDate($date, ValidationException $exception, $fieldName) { $dateTime = DateTime::createFromFormat(NodeProperty::DATE_FORMAT, $date); if ($dateTime) { return; } $error = new ValidationError('error.value.invalid', '%value% is invalid', array('value' => $date)); $exception->addErrors($fieldName, array($error)); }
/** * Parses the provided INI in an array of NodeProperty instances * @param string $ini * @return array * @throws \ride\library\validation\exception\ValidationException when the * ini is not valid */ protected function getNodePropertiesFromIni($ini) { $properties = @parse_ini_string($ini); if ($properties === false) { $error = error_get_last(); $error = new ValidationError('error', '%error%', array('error' => $error['message'])); $exception = new ValidationException(); $exception->addErrors('properties', array($error)); throw $exception; } $inheritPrefixLength = strlen(NodeProperty::INHERIT_PREFIX); foreach ($properties as $key => $value) { $inherit = false; unset($properties[$key]); if (strpos($key, NodeProperty::INHERIT_PREFIX) === 0) { $key = substr($key, $inheritPrefixLength); $inherit = true; } $properties[$key] = new NodeProperty($key, $value, $inherit); } return $properties; }