From 9fbdfa9ffe45a03fd109337ffdb6d6f6b2c2776b Mon Sep 17 00:00:00 2001 From: Dvit Date: Fri, 7 Apr 2017 16:50:16 +0200 Subject: [PATCH 1/6] add switch and radio input support for settings form --- ps_emailsmanager.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ps_emailsmanager.php b/ps_emailsmanager.php index 54bd564..e13d118 100644 --- a/ps_emailsmanager.php +++ b/ps_emailsmanager.php @@ -742,6 +742,17 @@ 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']; + } + } + } + $inputs[] = array( 'required' => isset($input['required']) ? $input['required'] : false, 'name' => $input['name'], @@ -749,6 +760,8 @@ public function displayForm($tplName) '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'] : '', + 'is_bool' => isset($input['is_bool']) ? $input['is_bool'] : '' ); } $inputs[] = array( From 01ff18900eaf9ef00f5fd3e83492de09d80c0983 Mon Sep 17 00:00:00 2001 From: Dvit Date: Mon, 10 Apr 2017 18:16:25 +0200 Subject: [PATCH 2/6] add image file upload support in settings form --- ps_emailsmanager.php | 45 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/ps_emailsmanager.php b/ps_emailsmanager.php index e13d118..02fbb6c 100644 --- a/ps_emailsmanager.php +++ b/ps_emailsmanager.php @@ -504,9 +504,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) { @@ -1083,4 +1085,41 @@ private function getAddonsProducts() } return false; } + + 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; + } + } From b4c4419a74dd22efd5845e76bffedd0f38d9e986 Mon Sep 17 00:00:00 2001 From: Dvit Date: Thu, 13 Apr 2017 08:17:56 +0200 Subject: [PATCH 3/6] PSR-2 fix --- ps_emailsmanager.php | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/ps_emailsmanager.php b/ps_emailsmanager.php index 02fbb6c..ff36e67 100644 --- a/ps_emailsmanager.php +++ b/ps_emailsmanager.php @@ -744,17 +744,16 @@ 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']; - } - } - } - + 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']; + } + } + } + $inputs[] = array( 'required' => isset($input['required']) ? $input['required'] : false, 'name' => $input['name'], @@ -762,8 +761,8 @@ public function displayForm($tplName) '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'] : '', - 'is_bool' => isset($input['is_bool']) ? $input['is_bool'] : '' + 'values' => isset($input['values']) ? $input['values'] : '', + 'is_bool' => isset($input['is_bool']) ? $input['is_bool'] : '' ); } $inputs[] = array( @@ -1086,9 +1085,8 @@ private function getAddonsProducts() return false; } - function uploadFiles() + public function uploadFiles() { - $tplName = basename(Tools::getValue('select_template')); if (!$tplName || $tplName == '') { $this->_errors[] = $this->l('Invalid template\'s name'); @@ -1103,7 +1101,6 @@ function uploadFiles() $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; @@ -1121,5 +1118,4 @@ function uploadFiles() } return true; } - } From da87b11aa8cf4509131b9cc80fbd6303c7277d37 Mon Sep 17 00:00:00 2001 From: Dvit Date: Thu, 21 Sep 2017 11:31:07 +0200 Subject: [PATCH 4/6] fix switch field support for PS15 and fix display --- ps_emailsmanager.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/ps_emailsmanager.php b/ps_emailsmanager.php index ff36e67..0ec0be5 100644 --- a/ps_emailsmanager.php +++ b/ps_emailsmanager.php @@ -754,6 +754,16 @@ public function displayForm($tplName) } } + // 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'; + } + $inputs[] = array( 'required' => isset($input['required']) ? $input['required'] : false, 'name' => $input['name'], @@ -761,8 +771,9 @@ public function displayForm($tplName) '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'] : '', - 'is_bool' => isset($input['is_bool']) ? $input['is_bool'] : '' + 'values' => isset($input['values']) ? $input['values'] : array(), + 'is_bool' => isset($input['is_bool']) ? $input['is_bool'] : false, + 'class' => isset($input['class']) ? $input['class'] : '' ); } $inputs[] = array( From 934d4e62fa59a6793366bbbd35c658b352700156 Mon Sep 17 00:00:00 2001 From: Dvit Date: Thu, 21 Sep 2017 13:50:07 +0200 Subject: [PATCH 5/6] add select input support for settings form --- ps_emailsmanager.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ps_emailsmanager.php b/ps_emailsmanager.php index 0ec0be5..83746b8 100644 --- a/ps_emailsmanager.php +++ b/ps_emailsmanager.php @@ -763,6 +763,12 @@ public function displayForm($tplName) 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, @@ -772,6 +778,8 @@ public function displayForm($tplName) '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'] : '' ); From 56b474ce362945e8c48ac344a94304a7a34a2b5e Mon Sep 17 00:00:00 2001 From: Dvit Date: Thu, 28 Sep 2017 15:21:43 +0200 Subject: [PATCH 6/6] check if not file selected in settings form --- ps_emailsmanager.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ps_emailsmanager.php b/ps_emailsmanager.php index 83746b8..546c768 100644 --- a/ps_emailsmanager.php +++ b/ps_emailsmanager.php @@ -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; } }