2020-05-29 13:41:58 +02:00
< ? php
/*
* Toggle element - a simple checkbox with the current state selected
* On click , issues a GET to a given endpoint , retrieving a form with the
* value flipped , which is immediately POSTed .
* to fetch it .
*
*/
2020-12-10 15:18:02 +01:00
$data = $this -> Hash -> get ( $row , $field [ 'data_path' ]);
2020-05-29 13:41:58 +02:00
$seed = rand ();
$checkboxId = 'GenericToggle-' . $seed ;
$tempboxId = 'TempBox-' . $seed ;
2020-12-08 09:07:00 +01:00
$requirementMet = true ;
2020-12-10 15:18:02 +01:00
if ( isset ( $field [ 'toggle_data' ][ 'requirement' ])) {
if ( isset ( $field [ 'toggle_data' ][ 'requirement' ][ 'options' ][ 'datapath' ])) {
foreach ( $field [ 'toggle_data' ][ 'requirement' ][ 'options' ][ 'datapath' ] as $name => $path ) {
$field [ 'toggle_data' ][ 'requirement' ][ 'options' ][ 'datapath' ][ $name ] = empty ( $this -> Hash -> extract ( $row , $path )[ 0 ]) ? null : $this -> Hash -> extract ( $row , $path )[ 0 ];
2020-12-08 09:07:00 +01:00
}
}
2020-12-10 15:18:02 +01:00
$options = isset ( $field [ 'toggle_data' ][ 'requirement' ][ 'options' ]) ? $field [ 'toggle_data' ][ 'requirement' ][ 'options' ] : array ();
$requirementMet = $field [ 'toggle_data' ][ 'requirement' ][ 'function' ]( $row , $options );
2020-12-08 09:07:00 +01:00
}
2020-05-29 13:41:58 +02:00
echo sprintf (
2020-12-10 15:18:02 +01:00
'<input type="checkbox" id="%s" %s %s><span id="%s" class="d-none"></span>' ,
2020-05-29 13:41:58 +02:00
$checkboxId ,
2020-12-10 15:18:02 +01:00
empty ( $data ) ? '' : 'checked' ,
2020-12-08 09:07:00 +01:00
$requirementMet ? '' : 'disabled="disabled"' ,
2020-05-29 13:41:58 +02:00
$tempboxId
);
2020-12-10 15:18:02 +01:00
2020-12-11 10:11:59 +01:00
// inject variables into the strings
2020-12-10 15:18:02 +01:00
if ( ! empty ( $field [ 'toggle_data' ][ 'confirm' ])) {
2020-12-11 10:11:59 +01:00
$instructions = [
'enable.title' => 'enable.title_vars' ,
'enable.titleHtml' => 'enable.titleHtml_vars' ,
'enable.body' => 'enable.body_vars' ,
'enable.bodyHtml' => 'enable.bodyHtml_vars' ,
'enable.type' => 'enable.type' ,
'disable.title' => 'disable.title_vars' ,
'disable.titleHtml' => 'disable.titleHtml_vars' ,
'disable.body' => 'disable.body_vars' ,
'disable.bodyHtml' => 'disable.bodyHtml_vars' ,
'disable.bodyHtml' => 'disable.bodyHtml_vars' ,
'disable.type' => 'disable.type' ,
];
$confirmOptions = $this -> DataFromPath -> buildStringsInArray ( $field [ 'toggle_data' ][ 'confirm' ], $row , $instructions , [ 'highlight' => true ]);
2020-12-10 15:18:02 +01:00
}
2020-12-11 10:11:59 +01:00
$url = $this -> DataFromPath -> buildStringFromDataPath ( $field [ 'url' ], $row , $field [ 'url_params_vars' ]);
2020-05-29 13:41:58 +02:00
?>
2020-12-08 09:07:00 +01:00
< ? php if ( $requirementMet ) : ?>
2020-05-29 13:41:58 +02:00
< script type = " text/javascript " >
2020-12-10 15:18:02 +01:00
( function () {
const url = " <?= h( $url ) ?> "
const confirmationOptions = < ? = isset ( $confirmOptions ) ? json_encode ( $confirmOptions ) : 'false' ?> ;
2020-12-07 14:17:10 +01:00
$ ( '#<?= $checkboxId ?>' ) . click ( function ( evt ) {
2020-12-10 15:18:02 +01:00
evt . preventDefault ()
if ( confirmationOptions !== false ) {
const correctOptions = $ ( '#<?= $checkboxId ?>' ) . prop ( 'checked' ) ? confirmationOptions [ 'enable' ] : confirmationOptions [ 'disable' ] // Adjust modal option based on checkbox state
const modalOptions = {
... correctOptions ,
APIConfirm : ( tmpApi ) => {
return submitForm ( tmpApi , url )
. catch ( e => {
// Provide feedback inside modal?
2020-12-07 14:17:10 +01:00
})
2020-12-10 15:18:02 +01:00
},
2020-05-29 13:41:58 +02:00
}
2020-12-10 15:18:02 +01:00
UI . modal ( modalOptions )
} else {
const tmpApi = new AJAXApi ({
statusNode : $ ( '#<?= $checkboxId ?>' )[ 0 ]
})
submitForm ( tmpApi , url )
}
})
function submitForm ( api , url ) {
2020-12-15 16:14:06 +01:00
const reloadUrl = '<?= isset($field[' toggle_data '][' reload_url ']) ? $field[' toggle_data '][' reload_url '] : $this->Url->build([' action ' => ' index ']) ?>'
2020-12-10 15:18:02 +01:00
return api . fetchAndPostForm ( url , {})
. then (() => {
2020-12-15 16:14:06 +01:00
< ? php if ( ! empty ( $field [ 'toggle_data' ][ 'skip_full_reload' ])) : ?>
const isChecked = $ ( '#<?= $checkboxId ?>' ) . prop ( 'checked' )
$ ( '#<?= $checkboxId ?>' ) . prop ( 'checked' , ! $ ( '#<?= $checkboxId ?>' ) . prop ( 'checked' ))
< ? php else : ?>
UI . reload ( reloadUrl , $ ( '#table-container-<?= $tableRandomValue ?>' ), $ ( '#table-container-<?= $tableRandomValue ?> table.table' ))
< ? php endif ; ?>
2020-12-10 15:18:02 +01:00
})
}
}())
2020-05-29 13:41:58 +02:00
</ script >
2020-12-08 09:07:00 +01:00
< ? php endif ; ?>