Skip to content

Commit

Permalink
Updated Generic module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-KM committed Mar 15, 2020
1 parent a05fbae commit 9a0fc14
Showing 1 changed file with 54 additions and 42 deletions.
96 changes: 54 additions & 42 deletions src/Generic/AbstractModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -360,6 +362,8 @@ protected function manageAnySettings(SettingsInterface $settings, $settingsType,
*/
protected function handleAnySettings(Event $event, $settingsType)
{
global $globalNext;

$services = $this->getServiceLocator();

$settingsTypes = [
Expand Down Expand Up @@ -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 = [];
Expand All @@ -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 */
Expand All @@ -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.
*
Expand All @@ -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]);
Expand All @@ -511,16 +488,51 @@ 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);
});
$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;
}

/**
Expand Down

0 comments on commit 9a0fc14

Please sign in to comment.