diff --git a/src/Generic/AbstractModule.php b/src/Generic/AbstractModule.php index 1cc1130c..4bbc9399 100644 --- a/src/Generic/AbstractModule.php +++ b/src/Generic/AbstractModule.php @@ -322,6 +322,8 @@ protected function manageUserSettings($process, array $values = []) /** * Set, delete or update all settings of a specific type. * + * It processes main settings, or one site, or one user. + * * @param SettingsInterface $settings * @param string $settingsType * @param string $process @@ -360,6 +362,8 @@ protected function manageAnySettings(SettingsInterface $settings, $settingsType, */ protected function handleAnySettings(Event $event, $settingsType) { + global $globalNext; + $services = $this->getServiceLocator(); $settingsTypes = [ @@ -400,6 +404,13 @@ protected function handleAnySettings(Event $event, $settingsType) break; } + // Simplify config of settings. + if (empty($globalNext)) { + $globalNext = true; + $ckEditorHelper = $services->get('ViewHelperManager')->get('ckEditor'); + $ckEditorHelper(); + } + // Allow to use a form without an id, for example to create a user. if ($settingsType !== 'settings' && !$id) { $data = []; @@ -411,10 +422,6 @@ protected function handleAnySettings(Event $event, $settingsType) } } - // Simplify config of settings. - $ckEditorHelper = $services->get('ViewHelperManager')->get('ckEditor'); - $ckEditorHelper(); - $space = strtolower(static::NAMESPACE); /** @var \Zend\Form\Form $form */ @@ -436,38 +443,6 @@ protected function handleAnySettings(Event $event, $settingsType) } } - /** - * Prepare data for a form or a fieldset. - * - * To be overridden by module for specific keys. - * - * @todo Use form methods to populate. - * - * @param SettingsInterface $settings - * @param string $settingsType - * @return array|null - */ - protected function prepareDataToPopulate(SettingsInterface $settings, $settingsType) - { - $config = $this->getConfig(); - $space = strtolower(static::NAMESPACE); - // Use isset() instead of empty() to give the possibility to display a - // specific form. - if (!isset($config[$space][$settingsType])) { - return null; - } - - $defaultSettings = $config[$space][$settingsType]; - - $data = []; - foreach ($defaultSettings as $name => $value) { - $val = $settings->get($name, is_array($value) ? [] : null); - $data[$name] = $val; - } - - return $data; - } - /** * Initialize each original settings, if not ready. * @@ -478,26 +453,28 @@ protected function prepareDataToPopulate(SettingsInterface $settings, $settingsT * @param SettingsInterface $settings * @param string $settingsType * @param int $id Site id or user id. + * @param array $values Specific values to populate, e.g. translated strings. + * @param bool True if processed. */ - protected function initDataToPopulate(SettingsInterface $settings, $settingsType, $id = null) + protected function initDataToPopulate(SettingsInterface $settings, $settingsType, $id = null, array $values = []) { // This method is not in the interface, but is set for config, site and // user settings. if (!method_exists($settings, 'getTableName')) { - return; + return false; } $config = $this->getConfig(); $space = strtolower(static::NAMESPACE); if (empty($config[$space][$settingsType])) { - return; + return false; } /** @var \Doctrine\DBAL\Connection $connection */ $connection = $this->getServiceLocator()->get('Omeka\Connection'); if ($id) { if (!method_exists($settings, 'getTargetIdColumnName')) { - return; + return false; } $sql = sprintf('SELECT id, value FROM %s WHERE %s = :target_id', $settings->getTableName(), $settings->getTargetIdColumnName()); $stmt = $connection->executeQuery($sql, ['target_id' => $id]); @@ -511,7 +488,8 @@ protected function initDataToPopulate(SettingsInterface $settings, $settingsType // Skip settings that are arrays, because the fields "multi-checkbox" // and "multi-select" are removed when no value are selected, so it's // not possible to determine if it's a new setting or an old empty - // setting currently. So fill them via upgrade in that case. + // setting currently. So fill them via upgrade in that case or fill the + // values. // TODO Find a way to save empty multi-checkboxes and multi-selects (core fix). $defaultSettings = array_filter($defaultSettings, function ($v) { return !is_array($v); @@ -519,8 +497,42 @@ protected function initDataToPopulate(SettingsInterface $settings, $settingsType $missingSettings = array_diff_key($defaultSettings, $currentSettings); foreach ($missingSettings as $name => $value) { - $settings->set($name, $value); + $settings->set($name, array_key_exists($name, $values) ? $values[$name] : $value); + } + + return true; + } + + /** + * Prepare data for a form or a fieldset. + * + * To be overridden by module for specific keys. + * + * @todo Use form methods to populate. + * + * @param SettingsInterface $settings + * @param string $settingsType + * @return array|null + */ + protected function prepareDataToPopulate(SettingsInterface $settings, $settingsType) + { + $config = $this->getConfig(); + $space = strtolower(static::NAMESPACE); + // Use isset() instead of empty() to give the possibility to display a + // specific form. + if (!isset($config[$space][$settingsType])) { + return null; } + + $defaultSettings = $config[$space][$settingsType]; + + $data = []; + foreach ($defaultSettings as $name => $value) { + $val = $settings->get($name, is_array($value) ? [] : null); + $data[$name] = $val; + } + + return $data; } /**