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-15 16:36:53 +01:00
* Options :
* - url : The URL on which to perform the POST
* - url_params_vars : Variables to be injected into the URL using the DataFromPath helper
* - toggle_data . skip_full_reload : If true , the index will not be reloaded and the checkbox will be flipped on success
* - toggle_data . editRequirement . function : A function to be called to assess if the checkbox can be toggled
* - toggle_data . editRequirement . options : Option that will be passed to the function
* - toggle_data . editRequirement . options . datapath : If provided , entries will have their datapath values converted into their extracted value
* - toggle_data . confirm . [ enable / disable ] . title :
* - toggle_data . confirm . [ enable / disable ] . titleHtml :
* - toggle_data . confirm . [ enable / disable ] . body :
* - toggle_data . confirm . [ enable / disable ] . bodyHtml :
* - toggle_data . confirm . [ enable / disable ] . type :
2020-05-29 13:41:58 +02:00
*
*/
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
2020-12-15 16:36:53 +01:00
$requirementMet = false ;
if ( isset ( $field [ 'toggle_data' ][ 'editRequirement' ])) {
if ( isset ( $field [ 'toggle_data' ][ 'editRequirement' ][ 'options' ][ 'datapath' ])) {
foreach ( $field [ 'toggle_data' ][ 'editRequirement' ][ 'options' ][ 'datapath' ] as $name => $path ) {
$field [ 'toggle_data' ][ 'editRequirement' ][ '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-15 16:36:53 +01:00
$options = isset ( $field [ 'toggle_data' ][ 'editRequirement' ][ 'options' ]) ? $field [ 'toggle_data' ][ 'editRequirement' ][ 'options' ] : array ();
$requirementMet = $field [ 'toggle_data' ][ 'editRequirement' ][ 'function' ]( $row , $options );
2020-12-08 09:07:00 +01:00
}
2020-05-29 13:41:58 +02:00
echo sprintf (
2021-01-11 14:21:46 +01:00
'<input type="checkbox" id="%s" class="change-cursor" %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' ])) {
2021-01-13 14:17:55 +01:00
$field [ 'toggle_data' ][ 'confirm' ][ 'enable' ][ 'arguments' ] = isset ( $field [ 'toggle_data' ][ 'confirm' ][ 'enable' ][ 'arguments' ]) ? $field [ 'toggle_data' ][ 'confirm' ][ 'enable' ][ 'arguments' ] : [];
$field [ 'toggle_data' ][ 'confirm' ][ 'disable' ][ 'arguments' ] = isset ( $field [ 'toggle_data' ][ 'confirm' ][ 'disable' ][ 'arguments' ]) ? $field [ 'toggle_data' ][ 'confirm' ][ 'disable' ][ 'arguments' ] : [];
$stringArrayEnable = $field [ 'toggle_data' ][ 'confirm' ][ 'enable' ];
unset ( $stringArrayEnable [ 'arguments' ]);
$stringArrayDisable = $field [ 'toggle_data' ][ 'confirm' ][ 'disable' ];
unset ( $stringArrayDisable [ 'arguments' ]);
2021-01-18 08:51:16 +01:00
$confirmOptions = [
'enable' => $this -> DataFromPath -> buildStringsInArray ( $stringArrayEnable , $row , $field [ 'toggle_data' ][ 'confirm' ][ 'enable' ][ 'arguments' ], [ 'highlight' => true ]),
'disable' => $this -> DataFromPath -> buildStringsInArray ( $stringArrayDisable , $row , $field [ 'toggle_data' ][ 'confirm' ][ 'disable' ][ 'arguments' ], [ '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 )
},
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 ']) ?>'
2021-03-10 09:40:08 +01:00
return api . fetchAndPostForm ( url , {}, false , true )
2020-12-10 15:18:02 +01:00
. 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 ; ?>