/** * Get the node settings from this form * @param boolean $updateValues set to true to get the submitted node settings, false to get the initial node settings * @return joppa\model\NodeSettings * @throws zibo\ZiboException when the form is not submitted and $updateValues is set to true * @throws zibo\library\validation\exception\ValidationException when the submitted settings are not valid and $updateValues is set to true */ public function getNodeSettings($updateValues = true) { if (!$updateValues) { return $this->nodeSettings; } if (!$this->isSubmitted()) { throw new ZiboException('Form not submitted'); } $settings = @parse_ini_string($this->getValue(self::FIELD_SETTINGS)); if ($settings === false) { $error = error_get_last(); $error = new ValidationError('error', '%error%', array('error' => $error['message'])); $exception = new ValidationException(); $exception->addErrors(self::FIELD_SETTINGS, array($error)); throw $exception; } $nodeSettings = new NodeSettings($this->nodeSettings->getNode(), $this->nodeSettings->getInheritedNodeSettings()); // set the values from the form $inheritPrefixLength = strlen(NodeSettings::INHERIT_PREFIX); foreach ($settings as $key => $value) { $inherit = false; if (strlen($key) > $inheritPrefixLength && strncmp($key, NodeSettings::INHERIT_PREFIX, $inheritPrefixLength) == 0) { $key = substr($key, $inheritPrefixLength); $inherit = true; } $nodeSettings->set($key, $value, $inherit); } return $nodeSettings; }
/** * Get a html representation of a NodeSettings object * @param joppa\model\NodeSettings $nodesettings * @return string html representation of the NodeSettings object */ private function getHtmlFromNodeSettings(NodeSettings $nodeSettings) { $inheritedSettings = $nodeSettings->getInheritedNodeSettings(); if ($inheritedSettings) { $arrayInheritedSettings = $inheritedSettings->getArray(true, true); } else { $arrayInheritedSettings = array(); } $arraySettings = $nodeSettings->getArray(); $settings = Structure::merge($arrayInheritedSettings, $arraySettings); ksort($settings); $html = ''; foreach ($settings as $key => $setting) { $value = $setting->getIniString(true); if ($setting->inherit) { $value = substr($value, 1); } if (isset($arraySettings[$key])) { $html .= '<strong>' . $value . '</strong>'; } else { $html .= $value; } $html .= "<br />\n"; } return $html; }
/** * Order the widgets of a region * @param string $region name of the region * @param string|array $widgets array with widget ids or a string with widget ids separated by a comma. * @return null * @throws zibo\ZiboException when the $widgets variable does not contain the same widgets as the current widgets * @throws zibo\ZiboException when the NodeSettings are not set to this node */ public function orderWidgets($region, $widgets) { $this->checkSettings(); if (!is_array($widgets)) { $widgets = explode(',', $widgets); } $widgetsKey = NodeSettingModel::SETTING_WIDGETS . '.' . $region; $currentWidgets = explode(NodeSettingModel::WIDGETS_SEPARATOR, $this->settings->get($widgetsKey, '', false)); $widgetsValue = ''; foreach ($widgets as $widgetId) { $widgetId = trim($widgetId); $key = array_search($widgetId, $currentWidgets); if ($key === false) { throw new ZiboException('Widget ' . $widgetId . ' is not currently set to region ' . $region); } $widgetsValue .= ($widgetsValue ? NodeSettingModel::WIDGETS_SEPARATOR : '') . $widgetId; unset($currentWidgets[$key]); } $numCurrentWidgets = count($currentWidgets); if ($numCurrentWidgets) { if ($numCurrentWidgets > 1) { throw new ZiboException('Widgets ' . implode(NodeSettingModel::WIDGETS_SEPARATOR, $currentWidgets) . ' are not found in the new widget order'); } $widget = array_pop($currentWidgets); throw new ZiboException('Widget ' . $widget . ' is not found in the new widget order'); } $this->settings->set($widgetsKey, $widgetsValue); }
/** * Validate the Joppa node configuration values * @param NodeSettings $nodeSettings * @return null * @throws zibo\library\validation\exception\ValidationException when a Joppa configuration setting in not validated */ public function validateNodeSettings(NodeSettings $nodeSettings) { $validationException = new ValidationException(); $publishStart = $nodeSettings->get(NodeSettingModel::SETTING_PUBLISH_START, null, false); $publishStop = $nodeSettings->get(NodeSettingModel::SETTING_PUBLISH_STOP, null, false); $isPublishStartEmpty = empty($publishStart); $isPublishStopEmpty = empty($publishStop); if (!$isPublishStartEmpty) { if ($date = $this->validateDate($publishStart, $validationException, NodeSettingModel::SETTING_PUBLISH_START)) { $nodeSettings->set(NodeSettingModel::SETTING_PUBLISH_START, $date); } } if (!$isPublishStopEmpty) { if ($date = $this->validateDate($publishStop, $validationException, NodeSettingModel::SETTING_PUBLISH_STOP)) { $nodeSettings->set(NodeSettingModel::SETTING_PUBLISH_STOP, $date); } } if (!$isPublishStartEmpty && !$isPublishStopEmpty && $publishStart >= $publishStop) { $error = new ValidationError('joppa.error.date.publish.negative', 'Publish stop date cannot be smaller or equal to publish start date'); $validationException->addErrors(NodeSettingModel::SETTING_PUBLISH_STOP, array($error)); } $permissions = $nodeSettings->get(NodeSettingModel::SETTING_PERMISSIONS, null, false); if ($permissions) { if ($permissions = $this->validatePermissions($permissions, $validationException)) { $nodeSettings->set(NodeSettingModel::SETTING_PERMISSIONS, $permissions); } } if ($validationException->hasErrors()) { throw $validationException; } }
/** * Get a value for a setting key * @param string $key key of the setting * @param mixed $default default value for when the setting is not set * @param boolean $inherited true to look in inherited settings, false to only look in this container * @param boolean $inheritedSettingRequired true to only return the value if the setting will inherit, needed internally for recursive lookup * @return mixed the value of the settings key if found, the provided default value otherwise * @throws zibo\ZiboException when an invalid key is provided */ public function get($key, $default = null, $inherited = true, $inheritedSettingRequired = false) { $this->checkKey($key); if (array_key_exists($key, $this->settings) && (!$inheritedSettingRequired || $inheritedSettingRequired && $this->settings[$key]->inherit)) { return $this->settings[$key]->value; } if ($inherited && $this->inheritedSettings) { return $this->inheritedSettings->get($key, $default, true, true); } return $default; }
/** * Get a NodeSettings object for a node * @param int|Node $node * @return NodeSettings */ public function getNodeSettings($node) { if (is_numeric($node)) { $nodeId = $node; $node = null; } else { $nodeId = $node->id; } $cache = Module::getCache(); $nodeSettings = $cache->get(Module::CACHE_TYPE_NODE_SETTINGS, $nodeId); if ($nodeSettings !== null) { return $nodeSettings; } if (!$node) { $nodeModel = $this->getModel(NodeModel::NAME); $node = $nodeModel->getNode($nodeId, 0); } $inheritedSettings = null; if ($node->parent) { $inheritedSettings = $this->getNodeSettings($node->getParentNodeId()); } $nodeSettings = new NodeSettings($node, $inheritedSettings); $settings = $this->findByNodeId($nodeId); foreach ($settings as $setting) { $nodeSettings->setNodeSetting($setting); } $cache->set(Module::CACHE_TYPE_NODE_SETTINGS, $nodeId, $nodeSettings); return $nodeSettings; }
/** * Get a suffix for the permission inherit label based on the inherited settings * @param joppa\model\NodeSettings $nodeSettings * @param zibo\library\i18n\translation\Translator $translator * @return string if a permission setting is found the suffix will be " ($securityLevel)" */ private function getPermissionInheritSuffix(NodeSettings $nodeSettings, Translator $translator) { $permission = $nodeSettings->get(NodeSettingModel::SETTING_PERMISSIONS, NodeSettingModel::AUTHENTICATION_STATUS_EVERYBODY, true, true); if ($permission == null) { return ''; } $suffix = ' ('; if ($permission == NodeSettingModel::AUTHENTICATION_STATUS_ANONYMOUS) { $suffix .= $translator->translate(self::TRANSLATION_ANONYMOUS); } elseif ($permission == NodeSettingModel::AUTHENTICATION_STATUS_AUTHENTICATED) { $suffix .= $translator->translate(self::TRANSLATION_AUTHENTICATED); } elseif ($permission == NodeSettingModel::AUTHENTICATION_STATUS_EVERYBODY) { $suffix .= $translator->translate(self::TRANSLATION_EVERYBODY); } else { $suffix .= $permission; } $suffix .= ')'; return $suffix; }
/** * Get the authentication options * @param zibo\library\i18n\translation\Translator $translator * @return array Array with the authentication status as key and the translation as value */ private function getAuthenticationStatusOptions(Translator $translator) { $options = array(); $inheritedNodeSettings = $this->nodeSettings->getInheritedNodeSettings(); if ($inheritedNodeSettings) { $options[self::INHERIT] = $translator->translate(self::TRANSLATION_INHERIT) . $this->getPermissionInheritSuffix($inheritedNodeSettings, $translator); } $options[NodeSettingModel::AUTHENTICATION_STATUS_EVERYBODY] = $translator->translate(self::TRANSLATION_EVERYBODY); $options[NodeSettingModel::AUTHENTICATION_STATUS_ANONYMOUS] = $translator->translate(self::TRANSLATION_ANONYMOUS); $options[NodeSettingModel::AUTHENTICATION_STATUS_AUTHENTICATED] = $translator->translate(self::TRANSLATION_AUTHENTICATED); return $options; }
/** * Get a suffix for the theme inherit label based on the inherited settings * @param joppa\model\NodeSettings $nodeSettings * @return string if a theme is found the suffix will be " ($theme)" */ private function getLocalesInheritSuffix(NodeSettings $nodeSettings) { $locales = $nodeSettings->get(NodeSettingModel::SETTING_LOCALES, null, true, true); if ($locales !== null) { return ' (' . $locales . ')'; } return null; }