Skip to content

Commit

Permalink
New feature #18880: New imagefile option type for theme options (#3621)
Browse files Browse the repository at this point in the history
Co-authored-by: lapiudevgit <devgit@lapiu.biz>
  • Loading branch information
gabrieljenik and lapiudevgit committed Nov 29, 2023
1 parent d7df178 commit 37c0dea
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
6 changes: 3 additions & 3 deletions application/controllers/admin/Themes.php
Expand Up @@ -174,7 +174,7 @@ public function upload()
App()->getController()->forward("/surveyAdministration/uploadimagefile/");
App()->end();
}
$sTemplateName = trim(App()->request->getPost('templatename'));
$sTemplateName = trim(App()->request->getPost('templatename', ''));
if (Permission::model()->hasGlobalPermission('templates', 'import') || Permission::model()->hasTemplatePermission($sTemplateName)) {
App()->loadHelper('admin/template');
// NB: lid = label id
Expand Down Expand Up @@ -616,7 +616,7 @@ public function index(string $editfile = '', string $screenname = 'welcome', str
public function templatefiledelete()
{
if (Permission::model()->hasGlobalPermission('templates', 'update')) {
$sTemplateName = Template::templateNameFilter(trim(App()->request->getPost('templatename')));
$sTemplateName = Template::templateNameFilter(trim(App()->request->getPost('templatename', '')));
$oEditedTemplate = Template::getInstance($sTemplateName);
$templatedir = $oEditedTemplate->viewPath;
$sPostedFiletype = CHtml::decode(App()->request->getPost('filetype'));
Expand Down Expand Up @@ -887,7 +887,7 @@ public function templatesavechanges()
$action = returnGlobal('action');
$editfile = returnGlobal('editfile');
$relativePathEditfile = returnGlobal('relativePathEditfile');
$sTemplateName = Template::templateNameFilter(trim(App()->request->getPost('templatename')));
$sTemplateName = Template::templateNameFilter(trim(App()->request->getPost('templatename', '')));
$screenname = returnGlobal('screenname');
$oEditedTemplate = Template::model()->getTemplateConfiguration($sTemplateName, null, null, true)->prepareTemplateRendering($sTemplateName);

Expand Down
2 changes: 1 addition & 1 deletion application/models/TemplateConfiguration.php
Expand Up @@ -982,7 +982,7 @@ public function getOptionPageAttributes()
foreach ($fileList as $file) {
$imageInfo = $this->getImageInfo($basePath . $file['name'], $pathPrefix);
if ($imageInfo) {
$aData['imageFileList'][] = array_merge(
$aData['imageFileList'][$imageInfo['filepath']] = array_merge(
[
'group' => $category->title,
],
Expand Down
63 changes: 62 additions & 1 deletion application/views/themeOptions/options_core.php
Expand Up @@ -6,6 +6,42 @@
"inherited value:"
) . ' ' . (isset($oParentOptions['font']) ? $oParentOptions['font'] : '') . ']</option>' : '');

/** @var string The html for image file dropdown options */
$imageOptions = '';
$optgroup = '';
foreach ($aTemplateConfiguration['imageFileList'] as $image) {
// If group is different than the previous one, close the previous optgroup and open a new one
if ($image['group'] != $optgroup) {
if ($optgroup != '') {
$imageOptions .= '</optgroup>';
}
$imageOptions .= '<optgroup label="' . $image['group'] . '">';
$optgroup = $image['group'];
}
$imageOptions .= '<option data-lightbox-src="' . $image['preview'] . '" value="' . $image['filepath'] . '">' . $image['filename'] . '</option>';
}

// Add extra info needed for theme options of type "imagefile" (preview path and filename)
foreach ($aOptionAttributes['optionAttributes'] as $attributeName => &$attribute) {
if ($attribute['type'] == 'imagefile') {
if (!isset($oParentOptions[$attributeName])) {
continue;
}
if (isset($aTemplateConfiguration['imageFileList'][$oParentOptions[$attributeName]])) {
$image = $aTemplateConfiguration['imageFileList'][$oParentOptions[$attributeName]];
$attribute['preview'] = $image['preview'];
$attribute['filename'] = $image['filename'];
} else {
$attribute['preview'] = '';
$attribute['filename'] = '';
}
}
}
unset($attribute);

/**
* @todo: Convert backgroundimagefile and brandlogofile to 'imagefile' type
*/
// background file
$backgroundImageFile = '';
$backgroundfileInheritPreview = '';
Expand All @@ -23,6 +59,7 @@

if (isset($oParentOptions['backgroundimagefile']) && ($oParentOptions['backgroundimagefile'] == $image['filepath'] || $oParentOptions['backgroundimagefile'] == $image['filepathOptions'])) {
$backgroundfileInheritPreview = $image['preview'];
$backgroundfileInheritFilename = $image['filename'];
}

$backgroundImageFile .= '<option data-lightbox-src="' . $image['preview'] . '" value="' . $image['filepath'] . '">' . $image['filename'] . '</option>';
Expand Down Expand Up @@ -196,6 +233,30 @@
echo $aOptionAttributes['optionAttributes'][$attributeKey]['dropdownoptions'];
echo '</select>
</div>';
} elseif ($attribute['type'] == 'imagefile') {
if (!is_string($sParentOption)) {
// TODO: $aParentOptions is not loaded properly, it seems.
$sParentOption = 'N/A';
}
echo '<div class="col-12">';
// Fields linked to a parent option (Yes/No switch) need a class and data-parent attribute
if (!empty($attribute['parent'])) {
echo '<select class="form-select selector_option_value_field selector_radio_childfield selector_image_selector" data-parent="' . $attribute['parent'] . '" data-inheritvalue=\'' . $sParentOption . '\' id="simple_edit_options_' . $attributeKey . '" name="' . $attributeKey . '" >';
} else {
echo '<select class="form-select selector_option_value_field selector_image_selector" data-inheritvalue=\'' . $sParentOption . '\' id="simple_edit_options_' . $attributeKey . '" name="' . $attributeKey . '" >';
}
if ($bInherit) {
if (isset($attribute['preview'])) {
$inheritedValue = $attribute['preview'];
} else {
$inheritedValue = isset($sParentOption) ? $sParentOption : '';
}
echo '<option value="inherit">' . gT("Inherit") . ' [' . gT("inherited value:") . ' ' . $inheritedValue . ']</option>';
}
// Dropdown options for image files
echo $imageOptions;
echo '</select>';
echo '</div>';
} elseif ($attribute['type'] == 'icon') {
echo ' <div class="col-12 input-group">
<select class="selector_option_value_field form-select simple_edit_options_checkicon" data-parent="' . $attribute['parent'] . '" id="simple_edit_options_' . $attributeKey . '" name="' . $attributeKey . '" >';
Expand All @@ -220,7 +281,7 @@

echo '</div>';

if ($category == 'Images' && $attribute['type'] == 'dropdown') {
if ($attribute['type'] == 'imagefile' || ($category == 'Images' && $attribute['type'] == 'dropdown')) {
echo '<div class="col-2">
<label class="form-label">&nbsp;</label>
<div class="col-12">
Expand Down

0 comments on commit 37c0dea

Please sign in to comment.