From 978845a6d1fa026e12ca965581a8fe888dc627db Mon Sep 17 00:00:00 2001 From: JAMES_BOND Date: Sat, 29 Sep 2018 16:35:10 +1000 Subject: [PATCH] 3.0.6 --- admin.php | 158 ++++++++++-------- assets/admin/field.tpl | 12 ++ changelog.md | 4 + models/xforms_model.php | 40 ++--- module_info.php | 20 +-- templates/show_form.tpl | 4 +- xforms.php | 354 +++++++++++++++++++++------------------- xforms_widgets.php | 18 +- 8 files changed, 332 insertions(+), 278 deletions(-) diff --git a/admin.php b/admin.php index 2ad93ac..b9c27bc 100755 --- a/admin.php +++ b/admin.php @@ -16,8 +16,8 @@ class Admin extends BaseAdminController { - public function __construct() { - + public function __construct() + { parent::__construct(); $lang = new MY_Lang(); $lang->load('xforms'); @@ -29,8 +29,8 @@ public function __construct() { * Изменение статуса, видим или не видим, для поля формы * @param int $field_id */ - public function change_visible($field_id) { - + public function change_visible($field_id) + { $field = $this->xforms_model->get_field($field_id); if ($field['visible'] == 1) { @@ -47,32 +47,32 @@ public function change_visible($field_id) { * @param string $data * @return bool */ - public function check_emails($data) { - + public function check_emails($data) + { return $this->form_validation->valid_emails($data); } /** * Удаление полей формы */ - public function delete_fields() { - + public function delete_fields() + { $this->xforms_model->delete_fields($this->input->post('id')); } /** * Удаление формы */ - public function delete_form() { - + public function delete_form() + { if (count($this->input->post()) > 0) { - $this->db->where('id', (int) $this->input->post('id'))->delete('xforms'); - $this->db->where('fid', (int) $this->input->post('id'))->delete('xforms_field'); + $this->db->where('id', (int)$this->input->post('id'))->delete('xforms'); + $this->db->where('fid', (int)$this->input->post('id'))->delete('xforms_field'); // Удаляем email шаблон $email_template = $this->db->select('id')->where('name', 'xforms_send_form_' . $this->input->post('id'))->get('mod_email_paterns')->row_array(); - $this->db->where('id', (int) $email_template['id'])->delete('mod_email_paterns'); - $this->db->where('id', (int) $email_template['id'])->delete('mod_email_paterns_i18n'); + $this->db->where('id', (int)$email_template['id'])->delete('mod_email_paterns'); + $this->db->where('id', (int)$email_template['id'])->delete('mod_email_paterns_i18n'); } } @@ -81,8 +81,8 @@ public function delete_form() { * @param null|int $fid * @param null|string $field */ - public function field($fid = null, $field = null) { - + public function field($fid = null, $field = null) + { if ($this->input->post('type')) { $this->form_validation->set_rules('value', 'Значение', 'trim|xss_clean'); $this->form_validation->set_rules('desc', 'Описание', 'trim|xss_clean|max_length[255]'); @@ -96,21 +96,22 @@ public function field($fid = null, $field = null) { showMessage(validation_errors(), false, 'r'); } else { $data = [ - 'fid' => $fid, - 'type' => $this->input->post('type'), - 'label' => $this->input->post('name'), - 'value' => $this->input->post('value'), - 'desc' => $this->input->post('desc'), - 'operation' => $this->input->post('operation'), - 'position' => $this->input->post('position'), - 'maxlength' => $this->input->post('maxlength'), - 'checked' => $this->input->post('check'), - 'disabled' => $this->input->post('disable'), - 'require' => $this->input->post('required'), - 'validation' => $this->input->post('validation'), - 'visible' => $this->input->post('visible'), - 'allowed_types' => $this->input->post('allowed_types'), - ]; + 'fid' => $fid, + 'type' => $this->input->post('type'), + 'label' => $this->input->post('name'), + 'value' => $this->input->post('value'), + 'error_message' => $this->input->post('error_message'), + 'desc' => $this->input->post('desc'), + 'operation' => $this->input->post('operation'), + 'position' => $this->input->post('position'), + 'maxlength' => $this->input->post('maxlength'), + 'checked' => $this->input->post('check'), + 'disabled' => $this->input->post('disable'), + 'require' => $this->input->post('required'), + 'validation' => $this->input->post('validation'), + 'visible' => $this->input->post('visible'), + 'allowed_types' => $this->input->post('allowed_types'), + ]; if (!$field) { $field_id = $this->xforms_model->add_field($data); @@ -124,7 +125,7 @@ public function field($fid = null, $field = null) { $path = '/admin/components/cp/xforms/field/' . $fid . '/' . $field_id; } else { - $this->xforms_model->update_field((int) $field, $data); + $this->xforms_model->update_field((int)$field, $data); showMessage(lang('Changes has been saved', 'xforms')); $path = '/admin/components/cp/xforms/field/' . $fid . '/' . $field; } @@ -138,7 +139,7 @@ public function field($fid = null, $field = null) { } else { if ($field) { assetManager::create() - ->setData('field', $this->xforms_model->get_field((int) $field)) + ->setData('field', $this->xforms_model->get_field((int)$field)) ->setData('fid', $fid) ->renderAdmin('field'); } else { @@ -151,14 +152,13 @@ public function field($fid = null, $field = null) { * EDIT and ADD field for form * @param int $id */ - public function fields($id) { - + public function fields($id) + { assetManager::create() ->setData('fields', $this->xforms_model->get_form_fields($id)) ->setData('form_name', $this->xforms_model->get_form_name($id)) ->setData('form_id', $id) ->renderAdmin('fields'); - } /** @@ -166,8 +166,8 @@ public function fields($id) { * создание / редактирование формы * @param null|int $id */ - public function form($id = null) { - + public function form($id = null) + { // Сохраняем if ($this->input->post()) { @@ -177,7 +177,7 @@ public function form($id = null) { $this->form_validation->set_rules('good', lang('Successful message', 'xforms'), 'trim|required'); // Валидируем заголовок темы и email только когда форма еще не создана - if(!$id) { + if (!$id) { $this->form_validation->set_rules('subject', lang('Email subject', 'xforms'), 'trim|xss_clean|required|min_length[1]|max_length[255]'); $this->form_validation->set_rules('email', 'Email', 'callback_check_emails'); } @@ -187,19 +187,19 @@ public function form($id = null) { } else { // Собираем данные $data = [ - 'title' => $this->input->post('page_title'), - 'url' => $this->input->post('page_url'), - 'desc' => $this->input->post('desc'), - 'success' => $this->input->post('good'), - 'captcha' => $this->input->post('captcha'), - 'direct_url' => $this->input->post('direct_url'), - 'user_message_active' => $this->input->post('user_message_active'), - 'action_files' => $this->input->post('action_files'), - ]; + 'title' => $this->input->post('page_title'), + 'url' => $this->input->post('page_url'), + 'desc' => $this->input->post('desc'), + 'success' => $this->input->post('good'), + 'captcha' => $this->input->post('captcha'), + 'direct_url' => $this->input->post('direct_url'), + 'user_message_active' => $this->input->post('user_message_active'), + 'action_files' => $this->input->post('action_files'), + ]; $email_data = [ - 'subject' => $this->input->post('subject'), - 'email' => $this->input->post('email'), + 'subject' => $this->input->post('subject'), + 'email' => $this->input->post('email'), ]; // Создаем / сохраняем @@ -215,26 +215,26 @@ public function form($id = null) { // Добавим шаблон "xforms_send" для отправки почты через модуль cmsemail $this->load->dbforge(); $email_paterns = [ - 'name' => 'xforms_send_form_' . $id, - 'from' => 'Администрация сайта', - 'from_email' => '', - 'type' => 'HTML', - 'patern' => '', - 'user_message_active' => 0, - 'admin_message_active' => 1, - 'admin_email' => $email_data['email'] + 'name' => 'xforms_send_form_' . $id, + 'from' => 'Администрация сайта', + 'from_email' => '', + 'type' => 'HTML', + 'patern' => '', + 'user_message_active' => 0, + 'admin_message_active' => 1, + 'admin_email' => $email_data['email'] ]; $this->db->insert('mod_email_paterns', $email_paterns); $email_patterns_id = $this->db->insert_id(); $email_paterns_i18n = [ - 'id' => $email_patterns_id, - 'locale' => 'ru', - 'theme' => $email_data['subject'] ? $email_data['subject'] : 'ImageCMS - Отправка формы - ' . $id, - 'user_message' => 'Здравствуйте. Мы свяжемся с вами в ближайшее время.', + 'id' => $email_patterns_id, + 'locale' => 'ru', + 'theme' => $email_data['subject'] ? $email_data['subject'] : 'ImageCMS - Отправка формы - ' . $id, + 'user_message' => 'Здравствуйте. Мы свяжемся с вами в ближайшее время.', 'admin_message' => '

Пришла заявка.

$message$

', - 'description' => 'Отправка формы xforms - ' . $data['title'], - 'variables' => 'a:1:{s:9:"$message$";s:46:"Список заполненных полей";}' + 'description' => 'Отправка формы xforms - ' . $data['title'], + 'variables' => 'a:1:{s:9:"$message$";s:46:"Список заполненных полей";}' ]; $this->db->insert('mod_email_paterns_i18n', $email_paterns_i18n); @@ -264,8 +264,8 @@ public function form($id = null) { /** * Show list forms in admin panel */ - public function index() { - + public function index() + { $xforms = $this->xforms_model->get_forms(); foreach ($xforms as $key => $f) { $xforms[$key]['cmsemail_id'] = $this->db->select('id')->where('name', 'xforms_send_form_' . $f['id'])->get('mod_email_paterns')->row_array()['id']; @@ -277,14 +277,34 @@ public function index() { /** * Sort fields in the form */ - public function update_positions() { - + public function update_positions() + { $positions = $this->input->post('positions'); foreach ($positions as $key => $value) { - $this->db->where('id', (int) $value)->set('position', $key)->update('xforms_field'); + $this->db->where('id', (int)$value)->set('position', $key)->update('xforms_field'); } showMessage('Позиция обновлена'); } + + + /** + * Обновление модуля + */ + public function update() + { + /** + * с 3.0 версии до 3.0.6 + * Добавим колонку с сообщением об ошибке + */ + $xforms_field = [ + 'error_message' => [ + 'type' => 'varchar', + 'constraint' => 255, + ] + ]; + $this->load->dbforge(); + $this->dbforge->add_column('xforms_field', $xforms_field); + } } \ No newline at end of file diff --git a/assets/admin/field.tpl b/assets/admin/field.tpl index c645af1..3d420e8 100755 --- a/assets/admin/field.tpl +++ b/assets/admin/field.tpl @@ -132,6 +132,18 @@ +
+ +
+
+ +
+ Показывается когда есть какое-нибудь Условие проверки или поле обязательно. (по умолчанию используются системные сообщения) +
+
+
diff --git a/changelog.md b/changelog.md index b1ad834..46eb2e9 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,7 @@ +##### UPD: 07/08/18 - Версия 3.0.6 +* Форматирование php кода. +* добавлен функционал вывода кастомных ошибок для полей. + ##### UPD: 07/08/18 - Версия 3.0.5.1 * (bugfix) Ошибка создания формы из-за наличия отсутствующих полей в БД subject и email. diff --git a/models/xforms_model.php b/models/xforms_model.php index a533393..c9c7c90 100755 --- a/models/xforms_model.php +++ b/models/xforms_model.php @@ -9,8 +9,8 @@ class Xforms_model extends CI_Model * @param array $data * @return int */ - public function add_form($data = []) { - + public function add_form($data = []) + { $this->db->insert('xforms', $data); return $this->db->insert_id(); } @@ -19,8 +19,8 @@ public function add_form($data = []) { * @param int $id * @return array form */ - public function get_form($id) { - + public function get_form($id) + { $this->db->limit(1); if (is_int($id)) { @@ -36,8 +36,8 @@ public function get_form($id) { * @param array $data * @return bool */ - public function update_form($id, $data = []) { - + public function update_form($id, $data = []) + { $this->db->where('id', $id); $this->db->update('xforms', $data); @@ -49,8 +49,8 @@ public function update_form($id, $data = []) { * @param array $param [visible, type] * @return array */ - public function get_form_fields($id, $param = []) { - + public function get_form_fields($id, $param = []) + { if (isset($param['visible'])) { $this->db->where('visible', $param['visible']); } @@ -66,8 +66,8 @@ public function get_form_fields($id, $param = []) { * @param integer $id * @return int */ - public function get_form_name($id) { - + public function get_form_name($id) + { $q = $this->db->select('title')->where('id', $id)->get('xforms')->row_array(); return $q['title']; } @@ -76,8 +76,8 @@ public function get_form_name($id) { * list forms * @return mixed */ - public function get_forms() { - + public function get_forms() + { return $this->db->get('xforms')->result_array(); } @@ -86,8 +86,8 @@ public function get_forms() { * @param array $data * @return int */ - public function add_field($data = []) { - + public function add_field($data = []) + { $this->db->insert('xforms_field', $data); return $this->db->insert_id(); } @@ -96,8 +96,8 @@ public function add_field($data = []) { * @param int $id * @return array */ - public function get_field($id) { - + public function get_field($id) + { return $this->db->where('id', $id)->get('xforms_field')->row_array(); } @@ -106,8 +106,8 @@ public function get_field($id) { * @param array $data * @return integer */ - public function update_field($id, $data) { - + public function update_field($id, $data) + { $this->db->where('id', $id); $this->db->update('xforms_field', $data); return $id; @@ -116,8 +116,8 @@ public function update_field($id, $data) { /** * @param array $ids */ - public function delete_fields($ids = []) { - + public function delete_fields($ids = []) + { foreach ($ids as $id) { $this->db->where('id', $id)->delete('xforms_field'); } diff --git a/module_info.php b/module_info.php index 6a06089..bd5bb81 100755 --- a/module_info.php +++ b/module_info.php @@ -5,15 +5,15 @@ } $com_info = [ - 'menu_name' => 'Конструктор форм', // Menu name - 'description' => 'Конструктор форм', // Module Description - 'admin_type' => 'inside', // Open admin class in new window or not. Possible values window/inside - 'window_type' => 'xhr', // Load method. Possible values xhr/iframe - 'w' => 600, // Window width - 'h' => 550, // Window height - 'version' => '3.0.4', // Module version - 'author' => 'x404@bk.ru', // Author info - 'icon_class' => 'fa fa-cubes', - ]; + 'menu_name' => 'Конструктор форм', // Menu name + 'description' => 'Конструктор форм', // Module Description + 'admin_type' => 'inside', // Open admin class in new window or not. Possible values window/inside + 'window_type' => 'xhr', // Load method. Possible values xhr/iframe + 'w' => 600, // Window width + 'h' => 550, // Window height + 'version' => '3.0.6', // Module version + 'author' => 'x404@bk.ru', // Author info + 'icon_class' => 'fa fa-cubes', +]; /* End of file module_info.php */ \ No newline at end of file diff --git a/templates/show_form.tpl b/templates/show_form.tpl index c642706..b2145fa 100755 --- a/templates/show_form.tpl +++ b/templates/show_form.tpl @@ -34,9 +34,7 @@
{$field.label}
{else:} {if (!$checkbox_value AND $field.type=='checkbox') || $field.type=='text' || $field.type=='textarea' || $field.type=='file'} - + {/if} {/if} diff --git a/xforms.php b/xforms.php index ddf4cdf..b4b6a9d 100755 --- a/xforms.php +++ b/xforms.php @@ -12,8 +12,8 @@ class Xforms extends MY_Controller { - public function __construct() { - + public function __construct() + { parent::__construct(); $this->load->module('core'); $lang = new MY_Lang(); @@ -31,7 +31,7 @@ public function __construct() { $this->form_validation->set_message('valid_emails', lang('Enter correct email addresses', 'xforms')); // Введите корректные email адреса $this->form_validation->set_message('valid_ip', lang('Enter the correct IP address', 'xforms')); // Введите корректный IP адрес $this->form_validation->set_message('valid_url', lang('Еnter a valid URL', 'xforms')); // Введите корректный URL адрес - $this->form_validation->set_message('numeric', lang('The field must contain only a numeric value', 'xforms')); // Поле должно содержать только числовое значение + $this->form_validation->set_message('numeric', lang('The field must contain only a numeric value', 'xforms')); // Поле должно содержать только числовое значение $this->form_validation->set_message('integer', lang('The field must contain an integer', 'xforms')); // Поле дожно содержать целое число //$this->form_validation->set_message('min_length', preg_replace('//is', '', lang(' Must have at least %s characters', 'xforms'))); // В поле должно быть не менее %s символов $this->form_validation->set_message('min_length', lang(' Must have at least %s characters', 'xforms')); // В поле должно быть не менее %s символов @@ -40,8 +40,8 @@ public function __construct() { $this->load->helper(array('form', 'url')); } - public function _deinstall() { - + public function _deinstall() + { if ($this->dx_auth->is_admin() == FALSE) { exit; } @@ -51,8 +51,8 @@ public function _deinstall() { $this->dbforge->drop_table('xforms_field'); } - public function _install() { - + public function _install() + { if ($this->dx_auth->is_admin() == FALSE) { exit; } @@ -60,108 +60,112 @@ public function _install() { $this->load->dbforge(); $xforms = [ - 'id' => [ - 'type' => 'INT', - 'constraint' => 11, - 'auto_increment' => TRUE, - ], - 'title' => [ - 'type' => 'varchar', - 'constraint' => 255, - ], - 'url' => [ - 'type' => 'varchar', - 'constraint' => 255, - ], - 'desc' => ['type' => 'text'], - 'success' => ['type' => 'text'], - 'captcha' => [ - 'type' => 'int', - 'constraint' => 2, - 'default' => 1, - ], - 'direct_url' => [ - 'type' => 'int', - 'constraint' => 1, - 'default' => 0, - ], - 'action_files' => [ - 'type' => 'smallint', - 'constraint' => '1', - 'default' => 1 - ], - 'user_message_active' => [ - 'type' => 'int', - 'constraint' => '3' - ] - ]; + 'id' => [ + 'type' => 'INT', + 'constraint' => 11, + 'auto_increment' => TRUE, + ], + 'title' => [ + 'type' => 'varchar', + 'constraint' => 255, + ], + 'url' => [ + 'type' => 'varchar', + 'constraint' => 255, + ], + 'desc' => ['type' => 'text'], + 'success' => ['type' => 'text'], + 'captcha' => [ + 'type' => 'int', + 'constraint' => 2, + 'default' => 1, + ], + 'direct_url' => [ + 'type' => 'int', + 'constraint' => 1, + 'default' => 0, + ], + 'action_files' => [ + 'type' => 'smallint', + 'constraint' => '1', + 'default' => 1 + ], + 'user_message_active' => [ + 'type' => 'int', + 'constraint' => '3' + ] + ]; $this->dbforge->add_key('id', TRUE); $this->dbforge->add_field($xforms); $this->dbforge->create_table('xforms', TRUE); $xforms_field = [ - 'id' => [ - 'type' => 'INT', - 'constraint' => 11, - 'auto_increment' => TRUE, - ], - 'fid' => [ - 'type' => 'int', - 'constraint' => 11, - ], - 'type' => [ - 'type' => 'varchar', - 'constraint' => 255, - ], - 'label' => [ - 'type' => 'varchar', - 'constraint' => 255, - ], - 'value' => ['type' => 'text'], - 'desc' => [ - 'type' => 'varchar', - 'constraint' => 255, - ], - 'position' => [ - 'type' => 'int', - 'constraint' => 11, - 'default' => 0, - ], - 'maxlength' => [ - 'type' => 'int', - 'constraint' => 11, - ], - 'checked' => [ - 'type' => 'int', - 'constraint' => 2, - 'default' => 0, - ], - 'disabled' => [ - 'type' => 'int', - 'constraint' => 2, - 'default' => 0, - ], - 'visible' => [ - 'type' => 'int', - 'constraint' => 1, - 'default' => 1, - ], - 'require' => [ - 'type' => 'int', - 'constraint' => 2, - 'default' => 0, - ], - 'operation' => ['type' => 'text'], - 'validation' => [ - 'type' => 'varchar', - 'constraint' => 500, - ], - 'allowed_types' => [ - 'type' => 'varchar', - 'constraint' => 500, - ] - ]; + 'id' => [ + 'type' => 'INT', + 'constraint' => 11, + 'auto_increment' => TRUE, + ], + 'fid' => [ + 'type' => 'int', + 'constraint' => 11, + ], + 'type' => [ + 'type' => 'varchar', + 'constraint' => 255, + ], + 'label' => [ + 'type' => 'varchar', + 'constraint' => 255, + ], + 'error_message' => [ + 'type' => 'varchar', + 'constraint' => 255, + ], + 'value' => ['type' => 'text'], + 'desc' => [ + 'type' => 'varchar', + 'constraint' => 255, + ], + 'position' => [ + 'type' => 'int', + 'constraint' => 11, + 'default' => 0, + ], + 'maxlength' => [ + 'type' => 'int', + 'constraint' => 11, + ], + 'checked' => [ + 'type' => 'int', + 'constraint' => 2, + 'default' => 0, + ], + 'disabled' => [ + 'type' => 'int', + 'constraint' => 2, + 'default' => 0, + ], + 'visible' => [ + 'type' => 'int', + 'constraint' => 1, + 'default' => 1, + ], + 'require' => [ + 'type' => 'int', + 'constraint' => 2, + 'default' => 0, + ], + 'operation' => ['type' => 'text'], + 'validation' => [ + 'type' => 'varchar', + 'constraint' => 500, + ], + 'allowed_types' => [ + 'type' => 'varchar', + 'constraint' => 500, + ] + ]; $this->dbforge->add_key('id', TRUE); $this->dbforge->add_field($xforms_field); @@ -171,7 +175,8 @@ public function _install() { $this->db->update('components', ['enabled' => '1', 'in_menu' => '1', 'autoload' => '0', 'settings' => serialize(['version' => '3.0.4'])]); } - public function autoload() { + public function autoload() + { } @@ -180,8 +185,8 @@ public function autoload() { * @param string $code * @return boolean */ - public function captcha_check($code) { - + public function captcha_check($code) + { if (!$this->dx_auth->captcha_check($code)) { return FALSE; } else { @@ -193,8 +198,8 @@ public function captcha_check($code) { /** * Valid Date (europe format) */ - public function valid_date($str) { - + public function valid_date($str) + { if (preg_match('/([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{4})/', $str)) { $arr = explode('.', $str); $dd = $arr[0]; @@ -210,8 +215,8 @@ public function valid_date($str) { /** * Validate time */ - public function valid_time($str) { - + public function valid_time($str) + { if (preg_match('/([0-9]{1,2})\:([0-9]{1,2})\:([0-9]{1,2})/', $str)) { return TRUE; } else { @@ -234,13 +239,14 @@ public function valid_time($str) { * Дополнительно ПРОСТО ПРОВЕРКА РОССИЯ,УКРАИНА, БЕЛКА - без заморочек, просто межгород и 8 * ^(8|\+7|\+038|\+38|\+375)\d{9,10}$ */ - public function valid_phone($number) { - return (bool) preg_match('/^(?:8(?:(?:21|22|23|24|51|52|53|54|55)|(?:15\d\d))?|\+7|\+375|\+38|\+038)?(?:(?:3[04589]|4[012789]|8[^89\D]|9\d)\d)?\d{7}$/', $number); + public function valid_phone($number) + { + return (bool)preg_match('/^(?:8(?:(?:21|22|23|24|51|52|53|54|55)|(?:15\d\d))?|\+7|\+375|\+38|\+038)?(?:(?:3[04589]|4[012789]|8[^89\D]|9\d)\d)?\d{7}$/', $number); } - public function index() { - + public function index() + { $this->core->error_404(); } @@ -248,11 +254,10 @@ public function index() { * @param string $url * @return string */ - public function show($url = '') { - - $is_widget = ($this->input->post('is_widget')) ? 1 : 0; // TODO: Remove - $is_ajax = $this->input->is_ajax_request() ? 1 : 0; - $form = $this->xforms_model->get_form($url); + public function show($url = '') + { + $is_ajax = $this->input->is_ajax_request() ? 1 : 0; + $form = $this->xforms_model->get_form($url); if (!$is_ajax) { if (!$form['id'] OR !$form['direct_url']) { @@ -275,9 +280,9 @@ public function show($url = '') { // Если нажали отправить форму, то перебираем все входящие значения if ($this->input->post()) { - $msg_email = []; // Текст для почты - $user_email = ''; // Email клиента, для отправки ему письма - $attach_email = []; // Файлы, прикрепляемые к письму + $msg_email = []; // Текст для почты + $user_email = ''; // Email клиента, для отправки ему письма + $attach_email = []; // Файлы, прикрепляемые к письму $post_data = $this->input->post(); @@ -293,10 +298,9 @@ public function show($url = '') { $this->form_validation->set_rules($key_post, $field['label'], 'trim|max_length[3]|integer|' . $require . $field['validation']); $checked = explode("\n", $field['value']); $data_msg = $checked[$post_data[$key_post]]; - } - elseif($field['type'] == 'checkbox') { + } elseif ($field['type'] == 'checkbox') { - if($require) + if ($require) $this->form_validation->set_rules($key_post, $field['label'], $require); $checked = explode("\n", $field['value']); @@ -304,22 +308,21 @@ public function show($url = '') { foreach ($post_data[$key_post] as $key => $val) { $data_msg .= $checked[$val] . '
'; } - } - elseif($field['type'] == 'file') { + } elseif ($field['type'] == 'file') { - if($require) + if ($require) $this->form_validation->set_rules($key_post, $field['label'], $require); $files = []; - if(!empty($post_data[$key_post])) { + if (!empty($post_data[$key_post])) { foreach ($post_data[$key_post] as $key => $val) { - foreach($val as $k => $v) { + foreach ($val as $k => $v) { $files[$k][$key] = $v; } } - foreach($files as $key => $file) { - if($form['action_files'] == 1 OR $form['action_files'] == 3) { + foreach ($files as $key => $file) { + if ($form['action_files'] == 1 OR $form['action_files'] == 3) { // Вставляем ссылки файлы в текст письма $data_msg .= '' . $file['name'] . ' - '; $data_msg .= '×
'; @@ -334,14 +337,14 @@ public function show($url = '') { $data_msg = $post_data[$key_post]; // Найдем поле с email'ом отправителя - if($form['user_message_active'] AND $field['id'] == $form['user_message_active']) + if ($form['user_message_active'] AND $field['id'] == $form['user_message_active']) $user_email = $post_data[$key_post]; } - if($field['type'] != 'file' OR ($field['type'] == 'file' AND $form['action_files'] != 2)) + if ($field['type'] != 'file' OR ($field['type'] == 'file' AND $form['action_files'] != 2)) $msg_email[$field['id']]['field'] = $field; - if(!empty($data_msg)) + if (!empty($data_msg)) $msg_email[$field['id']]['data'] = $data_msg; } @@ -361,7 +364,7 @@ public function show($url = '') { $this->load->library('email'); $this->email->clear(); - $locale = MY_Controller::defaultLocale(); + $locale = MY_Controller::defaultLocale(); $pattern_name = 'xforms_send_form_' . $form['id']; $default_settings = ModuleSettings::ofModule('cmsemail')->get($locale ?: null); $pattern_settings = email::getInstance()->cmsemail_model->getPaternSettings($pattern_name); @@ -397,7 +400,7 @@ public function show($url = '') { /** * Отправляем письмо клиенту */ - if($form['user_message_active'] AND $pattern_settings['user_message_active']) { + if ($form['user_message_active'] AND $pattern_settings['user_message_active']) { $this->email->from($pattern_settings['from_email'], $pattern_settings['from']); $this->email->to($user_email); @@ -423,9 +426,9 @@ public function show($url = '') { $this->email->message(email::getInstance()->replaceVariables($pattern_settings['admin_message'], $replaceData)); $this->email->set_newline("\r\n"); // Добавляем вложения - if($attach_email AND ($form['action_files'] == 2 OR $form['action_files'] == 3)) { + if ($attach_email AND ($form['action_files'] == 2 OR $form['action_files'] == 3)) { foreach ($attach_email as $file) { - if(file_exists(FCPATH . 'uploads/xforms/' . $file)) { + if (file_exists(FCPATH . 'uploads/xforms/' . $file)) { $this->email->attach(FCPATH . 'uploads/xforms/' . $file); } } @@ -443,7 +446,7 @@ public function show($url = '') { } // Удаляем вложения - if($attach_email AND $form['action_files'] == 2) { + if ($attach_email AND $form['action_files'] == 2) { foreach ($attach_email as $file) { $this->deleteFile($file); } @@ -452,7 +455,22 @@ public function show($url = '') { $notify['success'] = $form['success']; } else { - $notify['errors'] = $this->form_validation->getErrorsArray(); + + // Кастомные ошибки для полей. + $field_errors = []; + foreach ($this->form_validation->getErrorsArray() as $field_id => $error) { + $f_id = preg_replace('/[^0-9]/', '', $field_id); + $result = array_search($f_id, array_column($fields, 'id')); + + if ($fields[$result]['error_message']) { + $field_errors[$field_id] = $fields[$result]['error_message']; + } else { + $field_errors[$field_id] = $error; + } + } + + $notify['errors'] = $field_errors; + $notify['fields'] = $fields; $notify['group_errors'] = validation_errors(); $notify['captcha_image'] = $form['captcha_image']; } @@ -463,7 +481,7 @@ public function show($url = '') { } else { // Если есть поля "файл" в форме. Что бы не загружать лишние скрипты... - $field_file_exists = array_filter($fields, function($lines){ + $field_file_exists = array_filter($fields, function ($lines) { return ($lines['type'] == 'file'); //Поиск по первому значению }); @@ -477,7 +495,7 @@ public function show($url = '') { ->registerScript('xforms') ->registerStyle('xforms'); - if($field_file_exists) { + if ($field_file_exists) { $xform_fetch ->registerScript('jquery.ui.widget') ->registerScript('jquery.iframe-transport') @@ -485,7 +503,7 @@ public function show($url = '') { ->registerScript('xforms_files'); } - if($form['direct_url']) { + if ($form['direct_url']) { $xform_fetch ->render('../templates/wrapper'); } else { @@ -498,16 +516,16 @@ public function show($url = '') { /** * Upload file */ - public function upload($field_id) { - + public function upload($field_id) + { // Найдем форму. - if(!$field = $this->xforms_model->get_field($field_id)) + if (!$field = $this->xforms_model->get_field($field_id)) return false; $form = $this->xforms_model->get_form(intval($field['fid'])); // расширния файлов доступные к загрузке - if($field['allowed_types']) + if ($field['allowed_types']) $config['allowed_types'] = $field['allowed_types']; else $config['allowed_types'] = 'jpg|png|rar|zip|doc|docx|psd|pdf'; @@ -527,13 +545,13 @@ public function upload($field_id) { // fixed multiple file upload. Fucking codeigninter... $files = $_FILES; - $cpt = count($_FILES['f'.$field_id]['name']); - for($i=0; $i<$cpt; $i++) { - $_FILES['userfile']['name'] = $files['f'.$field_id]['name'][$i]; - $_FILES['userfile']['type'] = $files['f'.$field_id]['type'][$i]; - $_FILES['userfile']['tmp_name'] = $files['f'.$field_id]['tmp_name'][$i]; - $_FILES['userfile']['error'] = $files['f'.$field_id]['error'][$i]; - $_FILES['userfile']['size'] = $files['f'.$field_id]['size'][$i]; + $cpt = count($_FILES['f' . $field_id]['name']); + for ($i = 0; $i < $cpt; $i++) { + $_FILES['userfile']['name'] = $files['f' . $field_id]['name'][$i]; + $_FILES['userfile']['type'] = $files['f' . $field_id]['type'][$i]; + $_FILES['userfile']['tmp_name'] = $files['f' . $field_id]['tmp_name'][$i]; + $_FILES['userfile']['error'] = $files['f' . $field_id]['error'][$i]; + $_FILES['userfile']['size'] = $files['f' . $field_id]['size'][$i]; $this->upload->initialize($config); @@ -542,15 +560,15 @@ public function upload($field_id) { } else { $info = $this->upload->data(); - $file = new StdClass; - $file->name = $_FILES['userfile']['name']; - $file->url = $info['file_name']; - $file->size = $info['size']; - $file->extension = $info['extension']; - $file->full_url = $upload_path_url . $info['file_name']; - $file->deleteUrl = base_url() . 'xforms/deleteFile/' . $info['file_name']; + $file = new StdClass; + $file->name = $_FILES['userfile']['name']; + $file->url = $info['file_name']; + $file->size = $info['size']; + $file->extension = $info['extension']; + $file->full_url = $upload_path_url . $info['file_name']; + $file->deleteUrl = base_url() . 'xforms/deleteFile/' . $info['file_name']; $file->deleteType = 'DELETE'; - $file->error = null; + $file->error = null; echo json_encode($file); } @@ -561,12 +579,13 @@ public function upload($field_id) { /** * Download file */ - public function download($file) { + public function download($file) + { $this->load->helper('download'); $data = file_get_contents("./uploads/xforms/$file"); - if(empty($data)) { + if (empty($data)) { $this->core->error_404(); } else { force_download($file, $data); @@ -579,7 +598,8 @@ public function download($file) { * @param $file * TODO: Доделать проверку безопасности и вывод ошибок, если файл отсутствует. */ - public function deleteFile($file) { + public function deleteFile($file) + { $success = unlink(FCPATH . 'uploads/xforms/' . $file); //info to see if it is doing what it is supposed to $info = new StdClass; @@ -587,7 +607,7 @@ public function deleteFile($file) { $info->path = base_url() . 'uploads/' . $file; $info->file = is_file(FCPATH . 'uploads/' . $file); - $is_ajax = $this->input->is_ajax_request() ? 1 : 0; + $is_ajax = $this->input->is_ajax_request() ? 1 : 0; if ($is_ajax) { //I don't think it matters if this is set but good for error checking in the console/firebug diff --git a/xforms_widgets.php b/xforms_widgets.php index 7a5929d..36fa6af 100755 --- a/xforms_widgets.php +++ b/xforms_widgets.php @@ -14,8 +14,8 @@ class Xforms_Widgets extends MY_Controller private $defaults = ['form_id' => 0]; - public function __construct() { - + public function __construct() + { parent::__construct(); $lang = new MY_Lang(); $lang->load('xforms'); @@ -27,15 +27,15 @@ public function __construct() { * @param array $widget * @return string */ - public function show_form($widget = []) { - + public function show_form($widget = []) + { if ($widget['settings'] === FALSE) { $settings = $this->defaults; } else { $settings = $widget['settings']; } - $form = $this->xforms_model->get_form((int) $settings['form_id']); + $form = $this->xforms_model->get_form((int)$settings['form_id']); if ($form['captcha'] == 1) { $this->dx_auth->captcha(); @@ -45,11 +45,11 @@ public function show_form($widget = []) { $fields = $this->xforms_model->get_form_fields($settings['form_id'], ['visible' => 1]); // Если есть поля "файл" в форме. Что бы не загружать лишние скрипты... - $result = array_filter($fields, function($lines){ + $result = array_filter($fields, function ($lines) { return ($lines['type'] == 'file'); //Поиск по первому значению }); - if($result) { + if ($result) { return assetManager::create() ->setData('widget', $widget) ->setData('fields', $fields) @@ -77,8 +77,8 @@ public function show_form($widget = []) { * @param string $action * @param array $widget_data */ - public function show_form_configure($action = 'show_settings', array $widget_data = []) { - + public function show_form_configure($action = 'show_settings', array $widget_data = []) + { if ($this->dx_auth->is_admin() == FALSE) { exit; }