Files
prestashop-icons/src/MprIconsConfig.php
myprestarocks 99a8f5bf9b Add shape prefix to symbol IDs, fix brand naming, add assets
- Symbol IDs now include shape for uniqueness: {shape}_{brand}_{mode}
  e.g., text-only_paypal_lm, rectangle_visa_la, icon_mastercard_dm
- Fixed brand name inconsistencies:
  - amazon_pay → amazon
  - naverpay → naver_pay
  - p24 → przelewy24
  - direct_debit → bacs (unified BACS Direct Debit name)
- Added font assets (FontAwesome, Material Icons)
- Added MprIconsAssets and MprIconsConfig classes
- Updated preview.html with shape parameter support
- All 752 icons complete (38 payment × 4 shapes × 4 modes + 12 social × 3 shapes × 4 modes)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-28 10:17:23 +00:00

173 lines
5.2 KiB
PHP

<?php
/**
* MprIconsConfig - Form field generator for icon set configuration
*
* Usage in module admin:
* $fields = array_merge($fields, MprIconsConfig::getFormFields());
* // Then in postProcess:
* MprIconsConfig::processForm();
*
* @package myprestarocks/prestashop-icons
*/
namespace MyPrestaRocks\Icons;
class MprIconsConfig
{
const CONFIG_KEY_SELFHOST = 'front_icon_selfhost';
/**
* Get form fields for HelperForm integration
*
* @param string $formName Form name prefix (default: 'MPR_ICONS')
* @return array HelperForm-compatible field definitions
*/
public static function getFormFields(string $formName = 'MPR_ICONS'): array
{
return [
[
'type' => 'select',
'label' => 'Front Office Icon Set',
'name' => $formName . '_ICON_SET',
'desc' => 'Choose which icon font to use on the front office. Most themes include Font Awesome.',
'options' => [
'query' => [
['id' => MprIcons::FA, 'name' => 'Font Awesome (default)'],
['id' => MprIcons::MATERIAL, 'name' => 'Material Icons'],
],
'id' => 'id',
'name' => 'name',
],
],
[
'type' => 'switch',
'label' => 'Include Icon Fonts from Module',
'name' => $formName . '_SELFHOST',
'desc' => 'Enable this if your theme does not load icon fonts. The module will include the necessary CSS.',
'is_bool' => true,
'values' => [
['id' => 'active_on', 'value' => 1, 'label' => 'Yes'],
['id' => 'active_off', 'value' => 0, 'label' => 'No'],
],
],
];
}
/**
* Get current form values for HelperForm
*
* @param string $formName Form name prefix
* @return array Field values
*/
public static function getFormValues(string $formName = 'MPR_ICONS'): array
{
return [
$formName . '_ICON_SET' => MprIcons::getIconSet(),
$formName . '_SELFHOST' => self::isSelfHostEnabled() ? 1 : 0,
];
}
/**
* Process form submission
*
* @param string $formName Form name prefix
* @return bool Success
*/
public static function processForm(string $formName = 'MPR_ICONS'): bool
{
$iconSet = \Tools::getValue($formName . '_ICON_SET', MprIcons::FA);
$selfHost = (int) \Tools::getValue($formName . '_SELFHOST', 0);
$success = MprIcons::saveIconSet($iconSet);
$success = self::saveSelfHostOption($selfHost) && $success;
return $success;
}
/**
* Check if self-hosting is enabled
*/
public static function isSelfHostEnabled(): bool
{
$value = self::readConfig(self::CONFIG_KEY_SELFHOST);
return $value === '1';
}
/**
* Save self-host option
*/
public static function saveSelfHostOption(int $enabled): bool
{
return self::saveConfig(self::CONFIG_KEY_SELFHOST, $enabled ? '1' : '0');
}
/**
* Read config from mpr_config table
*/
private static function readConfig(string $key): ?string
{
if (!defined('_DB_PREFIX_')) {
return null;
}
try {
$table = _DB_PREFIX_ . MprIcons::CONFIG_TABLE;
$sql = "SELECT `value` FROM `{$table}`
WHERE `module` = '" . pSQL(MprIcons::CONFIG_MODULE) . "'
AND `key` = '" . pSQL($key) . "'";
$result = \Db::getInstance()->getValue($sql);
return $result !== false ? $result : null;
} catch (\Exception $e) {
return null;
}
}
/**
* Save config to mpr_config table
*/
private static function saveConfig(string $key, string $value): bool
{
if (!defined('_DB_PREFIX_')) {
return false;
}
// Ensure table exists
MprIcons::ensureConfigTable();
try {
$table = _DB_PREFIX_ . MprIcons::CONFIG_TABLE;
$now = date('Y-m-d H:i:s');
$sql = "INSERT INTO `{$table}` (`module`, `key`, `value`, `date_add`, `date_upd`)
VALUES ('" . pSQL(MprIcons::CONFIG_MODULE) . "', '" . pSQL($key) . "', '" . pSQL($value) . "', '{$now}', '{$now}')
ON DUPLICATE KEY UPDATE `value` = '" . pSQL($value) . "', `date_upd` = '{$now}'";
return \Db::getInstance()->execute($sql);
} catch (\Exception $e) {
return false;
}
}
/**
* Get complete form input array for a HelperForm
* Includes form wrapper with legend
*
* @param string $formName Form name prefix
* @return array Complete form input structure
*/
public static function getFormInput(string $formName = 'MPR_ICONS'): array
{
return [
'form' => [
'legend' => [
'title' => 'Icon Settings',
'icon' => 'icon-picture',
],
'input' => self::getFormFields($formName),
],
];
}
}