From 95bb6f387897cac613a9913b76de1fc5af8d3a6b Mon Sep 17 00:00:00 2001 From: Alexander Walther Date: Tue, 9 May 2023 14:46:36 +0200 Subject: [PATCH 1/3] init --- assets/choice_status.js | 36 +++++++++ boot.php | 31 ++++++++ lib/yform/value/choice_status.php | 123 ++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 assets/choice_status.js create mode 100644 lib/yform/value/choice_status.php diff --git a/assets/choice_status.js b/assets/choice_status.js new file mode 100644 index 0000000..c00220c --- /dev/null +++ b/assets/choice_status.js @@ -0,0 +1,36 @@ +$(document).on('rex:ready', function (event, container) { + initStatusToggle(container); +}); + + +function initStatusToggle(container) { + // status select + if (container.find('[data-status="choice"]').length) { + var statusChange = function () { + var $this = $(this); + + updateDatasetStatus($this, $this.val(), function (resp) { + }); + }; + container.find('[data-status="choice"]').change(statusChange); + } +} + +function updateDatasetStatus($this, status, callback) { + + $('#rex-js-ajax-loader').addClass('rex-visible'); + if (confirm(' Akkreditierung ändern?')) { + + $.get(document.URL + '&rex-api-call=choice_status', { + data_id: $this.data('id'), + table: $this.data('table'), + token: $this.data('token'), + status: status + }, function (resp) { + callback(resp); + $('#rex-js-ajax-loader').removeClass('rex-visible'); + }); + } else { + window.location.reload(); + } +} diff --git a/boot.php b/boot.php index 0a545cf..5808329 100644 --- a/boot.php +++ b/boot.php @@ -7,3 +7,34 @@ if (rex::isBackend()) { rex_view::addCssFile($this->getAssetsUrl('be.min.css')); } + +/* +rex_extension::register('YFORM_DATA_LIST', function ($ep) { + if ($ep->getParam('table')->getTableName() == "rex_akkreditieren") { + $list = $ep->getSubject(); + + + $list->setColumnFormat('status', 'custom', ["akkreditieren", "yform_data_list_status"], ["table" => $ep->getParam('table')]); + + $list->setColumnFormat( + 'name', + 'custom', + function ($a) { + $_csrf_key = rex_yform_manager_table::get('rex_akkreditieren')->getCSRFKey(); + $token = rex_csrf_token::factory($_csrf_key)->getUrlParams(); + + $params = array(); + $params['table_name'] = 'rex_akkreditieren'; + $params['rex_yform_manager_popup'] = '0'; + $params['_csrf_token'] = $token['_csrf_token']; + $params['data_id'] = $a['list']->getValue('id'); + $params['function'] = 'edit'; + + return ''. $a['list']->getValue('name').''; + } + ); + } +}); + +rex_view::addJsFile(rex_addon::get('yform_field')->getAssetsUrl('js/choice_status.js')); +*/ diff --git a/lib/yform/value/choice_status.php b/lib/yform/value/choice_status.php new file mode 100644 index 0000000..a057992 --- /dev/null +++ b/lib/yform/value/choice_status.php @@ -0,0 +1,123 @@ + 'value', + 'name' => 'choice_status', + 'values' => [ + 'name' => ['type' => 'name', 'label' => rex_i18n::msg('yform_values_defaults_name')], + 'label' => ['type' => 'text', 'label' => rex_i18n::msg('yform_values_defaults_label')], + 'choices' => ['type' => 'text', 'label' => rex_i18n::msg('yform_values_choice_choices'), 'notice' => rex_i18n::msg('yform_values_choice_choices_notice').rex_i18n::rawMsg('yform_values_choice_choices_table')], + 'expanded' => ['type' => 'boolean', 'label' => rex_i18n::msg('yform_values_choice_expanded'), 'notice' => rex_i18n::msg('yform_values_choice_expanded_notice')], + 'multiple' => ['type' => 'boolean', 'label' => rex_i18n::msg('yform_values_choice_multiple'), 'notice' => rex_i18n::msg('yform_values_choice_multiple_notice').rex_i18n::rawMsg('yform_values_choice_expanded_multiple_table')], + 'default' => ['type' => 'text', 'label' => rex_i18n::msg('yform_values_choice_default'), 'notice' => rex_i18n::msg('yform_values_choice_default_notice')], + 'group_by' => ['type' => 'text', 'label' => rex_i18n::msg('yform_values_choice_group_by'), 'notice' => rex_i18n::msg('yform_values_choice_group_by_notice')], + 'preferred_choices' => ['type' => 'text', 'label' => rex_i18n::msg('yform_values_choice_preferred_choices'), 'notice' => rex_i18n::msg('yform_values_choice_preferred_choices_notice')], + 'placeholder' => ['type' => 'text', 'label' => rex_i18n::msg('yform_values_choice_placeholder'), 'notice' => rex_i18n::msg('yform_values_choice_placeholder_notice')], + 'group_attributes' => ['type' => 'text', 'label' => rex_i18n::msg('yform_values_choice_group_attributes'), 'notice' => rex_i18n::msg('yform_values_choice_group_attributes_notice')], + 'attributes' => ['type' => 'text', 'label' => rex_i18n::msg('yform_values_choice_attributes'), 'notice' => rex_i18n::msg('yform_values_choice_attributes_notice')], + 'choice_attributes' => ['type' => 'text', 'label' => rex_i18n::msg('yform_values_choice_choice_attributes'), 'notice' => rex_i18n::msg('yform_values_choice_choice_attributes_notice')], + 'notice' => ['type' => 'text', 'label' => rex_i18n::msg('yform_values_defaults_notice')], + 'no_db' => ['type' => 'no_db', 'label' => rex_i18n::msg('yform_values_defaults_table'), 'default' => 0], + 'choice_label' => ['type' => 'text', 'label' => rex_i18n::msg('yform_values_choice_choice_label'), 'notice' => rex_i18n::msg('yform_values_choice_choice_label_notice')], + ], + 'description' => rex_i18n::msg('yform_values_choice_description'), + 'db_type' => ['text', 'int', 'tinyint(1)', 'varchar(191)'], + 'famous' => true, + ]; + } + + public static function getListValue($params) + { + $listValues = self::getListValues($params); + $return = []; + foreach (explode(',', $params['value']) as $value) { + if (isset($listValues[$value])) { + $return[] = rex_i18n::translate($listValues[$value]); + } + } + + return implode('
', $return); + } + + public static function getListValues($params) + { + $fieldName = $params['field']; + if (!isset(self::$yform_list_values[$fieldName])) { + $field = $params['params']['field']; + + $choiceList = self::createChoiceList([ + 'choice_attributes' => (isset($field['choice_attributes'])) ? $field['choice_attributes'] : '', + 'choice_label' => (isset($field['choice_label'])) ? $field['choice_label'] : '', + 'choices' => (isset($field['choices'])) ? $field['choices'] : [], + 'expanded' => (isset($field['expanded'])) ? $field['expanded'] : '', + 'group_by' => (isset($field['group_by'])) ? $field['group_by'] : '', + 'multiple' => (isset($field['multiple'])) ? $field['multiple'] : false, + 'placeholder' => (isset($field['placeholder'])) ? $field['placeholder'] : '', + 'preferred_choices' => (isset($field['preferred_choices'])) ? $field['preferred_choices'] : [], + ]); + + $choices = $choiceList->getChoicesByValues(); + foreach ($choices as $value => $label) { + self::$yform_list_values[$fieldName][$value] = $label; + } + } + return self::$yform_list_values[$fieldName]; + } + + + /* Spezifisches */ + public static function yform_data_list_action_button(\rex_extension_point $ep) + { + $table_name = $ep->getParam('table')->getTableName(); + + if ($table_name == "rex_akkreditieren") { + $subject = $ep->getSubject(); + $subject[] = '
'; + $subject[] = '✅ akzeptieren'; + $subject[] = '❌ ablehnen'; + $subject[] = '
'; + return $subject; + } + } + + public static function yform_data_list_status($a) + { + $status_field = $a['params']['table']->getValueField('status'); + if ($status_field->getTypeName() == "choice") { + $status_options = \rex_yform_value_choice::getListValues([ + 'field' => 'status', + 'params' => ['field' => $status_field], + ]); + + $table_name = $a['params']['table']->getTableName(); + $data_id = $a['list']->getValue('id'); + $token = self::yform_data_list_status_token($data_id, $table_name); + $selected = $a['value']; + + $fragment = new rex_fragment(); + $fragment->setVar("options", $status_options); + $fragment->setVar("selected", $selected); + $fragment->setVar("table", $table_name); + $fragment->setVar("data_id", $data_id); + $fragment->setVar("token", $token); + return $fragment->parse('akkreditieren/status_select.php'); + } + } + + public static function yform_data_list_status_token($data_id, $table_name) + { + $secret = rex_config::get('yform_field', 'choice_status_secret'); + + return password_hash($secret . $data_id . $table_name, PASSWORD_DEFAULT); + } +} From 223c37e41ec46ebd40d14960671877605aa6f2ac Mon Sep 17 00:00:00 2001 From: Alexander Walther Date: Thu, 18 May 2023 23:59:27 +0200 Subject: [PATCH 2/3] update choice_status --- assets/choice_status.js | 30 ++++++++++++++++----- boot.php | 34 +++++++----------------- fragments/choice_status_select.php | 22 ++++++++++++++++ lib/yform_api_choice_status.php | 42 ++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 32 deletions(-) create mode 100644 fragments/choice_status_select.php create mode 100644 lib/yform_api_choice_status.php diff --git a/assets/choice_status.js b/assets/choice_status.js index c00220c..62e263f 100644 --- a/assets/choice_status.js +++ b/assets/choice_status.js @@ -5,31 +5,47 @@ $(document).on('rex:ready', function (event, container) { function initStatusToggle(container) { // status select - if (container.find('[data-status="choice"]').length) { - var statusChange = function () { + if (container.find('[data-status="choice_status_select"]').length) { + + var statusChangeSelect = function () { + var $this = $(this); + + updateDatasetStatus($this, $this.val(), function (resp) { + }); + }; + container.find('select[data-status="choice_status_select"]').change(statusChangeSelect); + + /* + var statusChangeButtons = function () { var $this = $(this); updateDatasetStatus($this, $this.val(), function (resp) { + var $parent = $this.parent(); + $parent.html(resp.message.element); + $parent.children('select:first').change(statusChange); }); }; - container.find('[data-status="choice"]').change(statusChange); + container.find('div[data-status="choice_status"] button').click(statusChangeButtons); + */ } } function updateDatasetStatus($this, status, callback) { $('#rex-js-ajax-loader').addClass('rex-visible'); - if (confirm(' Akkreditierung ändern?')) { - - $.get(document.URL + '&rex-api-call=choice_status', { + if (confirm('Ändern?')) { + url = window.location.origin; + $.get(url + '/redaxo/index.php?page=content&rex-api-call=choice_status', { data_id: $this.data('id'), table: $this.data('table'), + field: $this.data('field'), token: $this.data('token'), - status: status + value: status }, function (resp) { callback(resp); $('#rex-js-ajax-loader').removeClass('rex-visible'); }); + } else { window.location.reload(); } diff --git a/boot.php b/boot.php index 5808329..fc3d168 100644 --- a/boot.php +++ b/boot.php @@ -6,35 +6,19 @@ if (rex::isBackend()) { rex_view::addCssFile($this->getAssetsUrl('be.min.css')); + rex_view::addJsFile(rex_addon::get('yform_field')->getAssetsUrl('choice_status.js')); + } -/* rex_extension::register('YFORM_DATA_LIST', function ($ep) { - if ($ep->getParam('table')->getTableName() == "rex_akkreditieren") { - $list = $ep->getSubject(); - + $list = $ep->getSubject(); - $list->setColumnFormat('status', 'custom', ["akkreditieren", "yform_data_list_status"], ["table" => $ep->getParam('table')]); + $table = $ep->getParam('table'); - $list->setColumnFormat( - 'name', - 'custom', - function ($a) { - $_csrf_key = rex_yform_manager_table::get('rex_akkreditieren')->getCSRFKey(); - $token = rex_csrf_token::factory($_csrf_key)->getUrlParams(); + foreach($table->getFields() as $field) { + if($field->getTypeName() == "choice_status") { + $list->setColumnFormat($field->getName(), 'custom', ["rex_yform_value_choice_status", "select"], ["table" => $ep->getParam('table')]); + } + }; - $params = array(); - $params['table_name'] = 'rex_akkreditieren'; - $params['rex_yform_manager_popup'] = '0'; - $params['_csrf_token'] = $token['_csrf_token']; - $params['data_id'] = $a['list']->getValue('id'); - $params['function'] = 'edit'; - - return ''. $a['list']->getValue('name').''; - } - ); - } }); - -rex_view::addJsFile(rex_addon::get('yform_field')->getAssetsUrl('js/choice_status.js')); -*/ diff --git a/fragments/choice_status_select.php b/fragments/choice_status_select.php new file mode 100644 index 0000000..c68cea4 --- /dev/null +++ b/fragments/choice_status_select.php @@ -0,0 +1,22 @@ +getVar("data_id"); +$table = $this->getVar("table"); +$field = $this->getVar('field'); +$options = $this->getVar("options"); +$selected = $this->getVar("selected"); +$token = $this->getVar("token"); +?> + diff --git a/lib/yform_api_choice_status.php b/lib/yform_api_choice_status.php new file mode 100644 index 0000000..074c19b --- /dev/null +++ b/lib/yform_api_choice_status.php @@ -0,0 +1,42 @@ +setValue($field, $value); + if (!$dataset->save()) { + rex_logger::factory()->log('Error', 'error: API Call: Status not saved'); + rex_response::setStatus(rex_response::HTTP_BAD_REQUEST); + } else { + rex_response::setStatus(rex_response::HTTP_OK); + } + } + } else { + rex_logger::factory()->log('Error', 'error: API Parameter not correct: token:'.$token); + rex_response::setStatus(rex_response::HTTP_BAD_REQUEST); + } + exit(); + } +} From 32c55b1de5cc059aba9b5283ee12fc8709627e9c Mon Sep 17 00:00:00 2001 From: Alexander Walther Date: Fri, 19 May 2023 00:12:26 +0200 Subject: [PATCH 3/3] fix wrong copy&paste files --- install.php | 6 +- lib/yform/value/choice_status.php | 103 ++++++++---------------------- lib/yform_api_choice_status.php | 2 +- package.yml | 2 +- 4 files changed, 33 insertions(+), 80 deletions(-) diff --git a/install.php b/install.php index bc98fb9..4000ff5 100644 --- a/install.php +++ b/install.php @@ -11,7 +11,7 @@ if (class_exists('rex_scss_compiler')) { $compiler = new rex_scss_compiler(); - if( rex::isDebugMode() || false === $this->getProperty('compress_assets',true)) { + if(rex::isDebugMode() || false === $this->getProperty('compress_assets', true)) { // Klartext-Ausgabe falls man für Tests "lesbares" CSS erzeugen möchte $compiler->setFormatter(\ScssPhp\ScssPhp\Formatter\Expanded::class); } @@ -27,3 +27,7 @@ $compiler->setCssFile(__DIR__ . '/assets/be.min.css'); $compiler->compile(); } + +if (null == (rex_config::get('yform_field', 'choice_status_secret'))) { + rex_config::set('yform_field', 'choice_status_secret', bin2hex(random_bytes(16))); +} diff --git a/lib/yform/value/choice_status.php b/lib/yform/value/choice_status.php index a057992..b6ae6da 100644 --- a/lib/yform/value/choice_status.php +++ b/lib/yform/value/choice_status.php @@ -2,7 +2,6 @@ class rex_yform_value_choice_status extends rex_yform_value_choice { - public function getDescription(): string { return 'choice_status|name|label|choices|[expanded type: boolean; default: 0, 0,1]|[multiple type: boolean; default: 0, 0,1]|[default]|[group_by]|[preferred_choices]|[placeholder]|[group_attributes]|[attributes]|[choice_attributes]|[notice]|[no_db]'; @@ -32,92 +31,42 @@ public function getDefinitions(): array ], 'description' => rex_i18n::msg('yform_values_choice_description'), 'db_type' => ['text', 'int', 'tinyint(1)', 'varchar(191)'], - 'famous' => true, ]; } - - public static function getListValue($params) - { - $listValues = self::getListValues($params); - $return = []; - foreach (explode(',', $params['value']) as $value) { - if (isset($listValues[$value])) { - $return[] = rex_i18n::translate($listValues[$value]); - } - } - - return implode('
', $return); - } - - public static function getListValues($params) - { - $fieldName = $params['field']; - if (!isset(self::$yform_list_values[$fieldName])) { - $field = $params['params']['field']; - - $choiceList = self::createChoiceList([ - 'choice_attributes' => (isset($field['choice_attributes'])) ? $field['choice_attributes'] : '', - 'choice_label' => (isset($field['choice_label'])) ? $field['choice_label'] : '', - 'choices' => (isset($field['choices'])) ? $field['choices'] : [], - 'expanded' => (isset($field['expanded'])) ? $field['expanded'] : '', - 'group_by' => (isset($field['group_by'])) ? $field['group_by'] : '', - 'multiple' => (isset($field['multiple'])) ? $field['multiple'] : false, - 'placeholder' => (isset($field['placeholder'])) ? $field['placeholder'] : '', - 'preferred_choices' => (isset($field['preferred_choices'])) ? $field['preferred_choices'] : [], - ]); - - $choices = $choiceList->getChoicesByValues(); - foreach ($choices as $value => $label) { - self::$yform_list_values[$fieldName][$value] = $label; - } - } - return self::$yform_list_values[$fieldName]; - } - - - /* Spezifisches */ - public static function yform_data_list_action_button(\rex_extension_point $ep) + + public static function select($a) { - $table_name = $ep->getParam('table')->getTableName(); - - if ($table_name == "rex_akkreditieren") { - $subject = $ep->getSubject(); - $subject[] = '
'; - $subject[] = '✅ akzeptieren'; - $subject[] = '❌ ablehnen'; - $subject[] = '
'; - return $subject; - } - } - - public static function yform_data_list_status($a) - { - $status_field = $a['params']['table']->getValueField('status'); - if ($status_field->getTypeName() == "choice") { - $status_options = \rex_yform_value_choice::getListValues([ - 'field' => 'status', - 'params' => ['field' => $status_field], - ]); + $field = $a['field']; + $status_field = $a['params']['table']->getValueField($field); + $status_options = \rex_yform_value_choice::getListValues([ + 'field' => $field, + 'params' => ['field' => $status_field], + ]); - $table_name = $a['params']['table']->getTableName(); - $data_id = $a['list']->getValue('id'); - $token = self::yform_data_list_status_token($data_id, $table_name); - $selected = $a['value']; + $table_name = $a['params']['table']->getTableName(); + $data_id = $a['list']->getValue('id'); + $token = self::getToken($data_id, $table_name); + $selected_status = $a['value']; - $fragment = new rex_fragment(); - $fragment->setVar("options", $status_options); - $fragment->setVar("selected", $selected); - $fragment->setVar("table", $table_name); - $fragment->setVar("data_id", $data_id); - $fragment->setVar("token", $token); - return $fragment->parse('akkreditieren/status_select.php'); - } + $fragment = new rex_fragment(); + $fragment->setVar("options", $status_options); + $fragment->setVar("selected", $selected_status); + $fragment->setVar("table", $table_name); + $fragment->setVar("field", $field); + $fragment->setVar("data_id", $data_id); + $fragment->setVar("token", $token); + + // if ($selected_status == 0) { + return $fragment->parse('choice_status_select.php'); + // } + return $status_options[$selected_status]; } - public static function yform_data_list_status_token($data_id, $table_name) + public static function getToken($data_id, $table_name) { $secret = rex_config::get('yform_field', 'choice_status_secret'); return password_hash($secret . $data_id . $table_name, PASSWORD_DEFAULT); } + } diff --git a/lib/yform_api_choice_status.php b/lib/yform_api_choice_status.php index 074c19b..b2bd622 100644 --- a/lib/yform_api_choice_status.php +++ b/lib/yform_api_choice_status.php @@ -15,7 +15,7 @@ public function execute() $data_id = rex_request('data_id', 'int', 0); $field = rex_request('field', 'string', ''); $value = rex_request('value', 'string', ''); - $secret = rex_config::get('choice_status', 'secret'); + $secret = rex_config::get('yform_field', 'choice_status_secret'); $check = password_verify($secret . $data_id . $table, $token); diff --git a/package.yml b/package.yml index d4ed0ce..feb83b6 100644 --- a/package.yml +++ b/package.yml @@ -1,5 +1,5 @@ package: yform_field -version: '2.3.0-dev' +version: '2.3.0' author: 'Alexander Walther' supportpage: https://github.com/alexplusde/yform_field