Skip to content

Commit

Permalink
Merge pull request #43 from alexplusde/choice_status
Browse files Browse the repository at this point in the history
Neues Feld: choice_status (Select-Feld in Übersicht wechseln können)
  • Loading branch information
alxndr-w authored May 18, 2023
2 parents c8fb54d + 32c55b1 commit 3a698fd
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 2 deletions.
52 changes: 52 additions & 0 deletions assets/choice_status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
$(document).on('rex:ready', function (event, container) {
initStatusToggle(container);
});


function initStatusToggle(container) {
// status select
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('div[data-status="choice_status"] button').click(statusChangeButtons);
*/
}
}

function updateDatasetStatus($this, status, callback) {

$('#rex-js-ajax-loader').addClass('rex-visible');
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'),
value: status
}, function (resp) {
callback(resp);
$('#rex-js-ajax-loader').removeClass('rex-visible');
});

} else {
window.location.reload();
}
}
15 changes: 15 additions & 0 deletions boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +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) {
$list = $ep->getSubject();

$table = $ep->getParam('table');

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')]);
}
};

});
22 changes: 22 additions & 0 deletions fragments/choice_status_select.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
$data_id = $this->getVar("data_id");
$table = $this->getVar("table");
$field = $this->getVar('field');
$options = $this->getVar("options");
$selected = $this->getVar("selected");
$token = $this->getVar("token");
?>
<select data-table="<?= $table ?>"
data-token="<?= $token ?>"
data-field="<?= $field ?>"
data-id="<?= $data_id ?>" class="form-control"
data-status="choice_status_select" style="width: auto;">
<?php foreach ($options as $value => $option) {
?>
<option value="<?= $value ?>" <?php if ($value == $selected) {
echo "selected";
} ?>><?= $option ?></option>
<?php
} // foreach $options
?>
</select>
6 changes: 5 additions & 1 deletion install.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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)));
}
72 changes: 72 additions & 0 deletions lib/yform/value/choice_status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

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]';
}

public function getDefinitions(): array
{
return [
'type' => '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)'],
];
}

public static function select($a)
{
$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::getToken($data_id, $table_name);
$selected_status = $a['value'];

$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 getToken($data_id, $table_name)
{
$secret = rex_config::get('yform_field', 'choice_status_secret');

return password_hash($secret . $data_id . $table_name, PASSWORD_DEFAULT);
}

}
42 changes: 42 additions & 0 deletions lib/yform_api_choice_status.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

class rex_api_choice_status extends rex_api_function
{
protected $published = true;

public function execute()
{
if (!rex::isBackend() || !rex_backend_login::hasSession()) {
exit();
}

$token = rex_request('token', 'string', "");
$table = rex_request('table', 'string', "");
$data_id = rex_request('data_id', 'int', 0);
$field = rex_request('field', 'string', '');
$value = rex_request('value', 'string', '');
$secret = rex_config::get('yform_field', 'choice_status_secret');

$check = password_verify($secret . $data_id . $table, $token);

rex_response::cleanOutputBuffers();

if ($data_id && $table && $token && $field && $value && $check) {
$dataset = rex_yform_manager_dataset::get($data_id, $table);

if ($dataset) {
$dataset->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();
}
}
2 changes: 1 addition & 1 deletion package.yml
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 3a698fd

Please sign in to comment.