Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add switch, radio input and file upload support for settings form #6

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 78 additions & 4 deletions ps_emailsmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,14 @@ public function saveTemplateConf()
$userSettings['inputs'][$input['name']][$lang['id_lang']] = $value;
}
} else {
$value = Tools::getValue($input['name']);
//check if no file selected
if ($input['type'] == 'file' && !Tools::getValue($input['name'])) {
//get saved value instead of empty value
$currentFieldsValue = $this->getFieldsValue($settings);
$value = $currentFieldsValue[$input['name']];
} else {
$value = Tools::getValue($input['name']);
}
$userSettings['inputs'][$input['name']] = $value;
}
}
Expand Down Expand Up @@ -504,9 +511,11 @@ public function getContent()

// Process template configuration
if (Tools::isSubmit('submitconf_'.$this->name)) {
if ($this->saveTemplateImgs()) {
if ($this->saveTemplateConf()) {
$this->_confirmations[] = $this->l('Template changed with success!');
if ($this->uploadFiles()) {
if ($this->saveTemplateImgs()) {
if ($this->saveTemplateConf()) {
$this->_confirmations[] = $this->l('Template changed with success!');
}
}
}
} elseif (Tools::getValue('select_template') === self::DEFAULT_THEME_NAME) {
Expand Down Expand Up @@ -742,13 +751,44 @@ public function displayForm($tplName)
$iso = Context::getContext()->language->iso_code;
$inputs = array();
foreach ($settings['inputs'] as $input) {
if (isset($input['values'])) {
foreach ($input['values'] as &$value) {
if (isset($value['label'][$iso])) {
$value['label'] = $value['label'][$iso];
} else {
$value['label'] = $value['label']['en'];
}
}
}

// PS15 switch type not supported, use radio box instead
if (version_compare(_PS_VERSION_, '1.6', '<') && $input['type'] == 'switch') {
$input['type'] = 'radio';
$input['is_bool'] = true;
$input['class'] = 't';
}
if (version_compare(_PS_VERSION_, '1.6', '<') && $input['type'] == 'radio') {
$input['class'] = 't';
}
if ($input['type'] == 'select') {
if (isset($input['values'])) {
$input['options'] = array('query' => $input['values'], 'id' => 'id', 'name' => 'label');
$input['values'] = array();
}
}

$inputs[] = array(
'required' => isset($input['required']) ? $input['required'] : false,
'name' => $input['name'],
'desc' => isset($input['desc'][$iso]) ? $input['desc'][$iso] : $input['desc']['en'],
'type' => $input['type'],
'label' => isset($input['label'][$iso]) ? $input['label'][$iso] : $input['label']['en'],
'lang' => isset($input['lang']) ? $input['lang'] : '',
'values' => isset($input['values']) ? $input['values'] : array(),
'options' => isset($input['options']) ? $input['options'] : array(),
'multiple' => isset($input['multiple']) ? $input['multiple'] : false,
'is_bool' => isset($input['is_bool']) ? $input['is_bool'] : false,
'class' => isset($input['class']) ? $input['class'] : ''
);
}
$inputs[] = array(
Expand Down Expand Up @@ -1070,4 +1110,38 @@ private function getAddonsProducts()
}
return false;
}

public function uploadFiles()
{
$tplName = basename(Tools::getValue('select_template'));
if (!$tplName || $tplName == '') {
$this->_errors[] = $this->l('Invalid template\'s name');
return false;
}

$tplPath = $this->importsPath . $tplName;

$settings = $this->getTemplateSettings($tplName);

foreach ($_FILES as $input_name => $file) {
$filename = $file['name'];
foreach ($settings['inputs'] as $input) {
if ($input['name'] == $input_name) {
if (isset($file) && isset($file['tmp_name']) && !empty($file['tmp_name'])) {
if ($error = ImageManager::validateUpload($file)) {
$this->_errors[] = $error;
return false;
} else {
if (!move_uploaded_file($file['tmp_name'], $tplPath . "/img/" . $filename)) {
$this->_errors[] = $this->l('An error occurred while attempting to upload the file.');
return false;
}
$_POST[$input['name']] = $filename;
}
}
}
}
}
return true;
}
}