Hello! I am trying to build a custom add-on and because of lack of experience I decided to go with a simple task. It would be great to have a checkbox within repeatable region which should provide a way to select multiple or single elements. I tried to implement it and it seems to partly work but there is a problem when it's a single field inside a repeatable and if you uncheck a checkbox and click save - corresponding row gets deleted after save. How do I leave it just there with an empty/false/zero value in DB? It would be just wonderful to get some info on how Couch stores data and gets it from admin side post request. Thank you in advance! Source code listing(also in attachment):
- Code: Select all
<?php
if ( !defined('K_COUCH_DIR') ) die(); // cannot be loaded directly
class Select extends KUserDefinedField{
static function handle_params( $params ){
global $FUNCS, $AUTH;
if( $AUTH->user->access_level < K_ACCESS_LEVEL_SUPER_ADMIN ) return;
$attr = $FUNCS->get_named_vars(
array(
'single'=>'0',
),
$params
);
$attr['single'] = ( $attr['single']==1 ) ? 1 : 0;
return $attr;
}
function store_posted_changes( $post_val ){
global $FUNCS;
if( $this->deleted ) return; // no need to store
if( is_null($this->orig_data) ) $this->orig_data = $this->data;
$this->data = $FUNCS->cleanXSS( $post_val );
$this->modified = ( strcmp( $this->orig_data, $this->data )==0 ) ? false : true; // values unchanged
}
function get_data_to_save(){
return $this->get_data();
}
function get_data(){
global $Config;
$data = $this->data;
return $data;
}
function _render( $input_name, $input_id, $extra='', $dynamic_insertion=1 ){
global $FUNCS;
define( 'SELECT_URL', K_ADMIN_URL . 'addons/select/' );
$FUNCS->load_js( SELECT_URL . 'assets/single.js' );
$checked = ( $this->get_data() ) ? 'checked="checked"' : '';
if ($this->single) {
$input_id = $input_id . '_s';
}
$html = '<input type="checkbox" value="1" '.$checked.' name="'.$input_name.'" id="'.$input_id.'"/>';
if( $this->simple_mode ){
$html = '<label for="'.$input_id.'">' . $html . ' ' . $this->field_label . '</label>';
}
else{
$html = '<div class="ctrls-checkbox"><label for="'.$input_id.'">' . $html . '<span class="ctrl-option"></span>' . $this->field_label . '</label></div>';
}
return $html;
}
}
// Register
$FUNCS->register_udf( 'select', 'Select', 1/*repeatable*/ );