1*12a4e4d1SAndreas Gohr<?php 2*12a4e4d1SAndreas Gohrnamespace dokuwiki\Form; 3*12a4e4d1SAndreas Gohr 4*12a4e4d1SAndreas Gohr/** 5*12a4e4d1SAndreas Gohr * Class CheckableElement 6*12a4e4d1SAndreas Gohr * 7*12a4e4d1SAndreas Gohr * For Radio- and Checkboxes 8*12a4e4d1SAndreas Gohr * 9*12a4e4d1SAndreas Gohr * @package DokuForm 10*12a4e4d1SAndreas Gohr */ 11*12a4e4d1SAndreas Gohrclass CheckableElement extends InputElement { 12*12a4e4d1SAndreas Gohr 13*12a4e4d1SAndreas Gohr /** 14*12a4e4d1SAndreas Gohr * @param string $type The type of this element 15*12a4e4d1SAndreas Gohr * @param string $name The name of this form element 16*12a4e4d1SAndreas Gohr * @param string $label The label text for this element 17*12a4e4d1SAndreas Gohr */ 18*12a4e4d1SAndreas Gohr public function __construct($type, $name, $label) { 19*12a4e4d1SAndreas Gohr parent::__construct($type, $name, $label); 20*12a4e4d1SAndreas Gohr // default value is 1 21*12a4e4d1SAndreas Gohr $this->attr('value', 1); 22*12a4e4d1SAndreas Gohr } 23*12a4e4d1SAndreas Gohr 24*12a4e4d1SAndreas Gohr /** 25*12a4e4d1SAndreas Gohr * Handles the useInput flag and sets the checked attribute accordingly 26*12a4e4d1SAndreas Gohr */ 27*12a4e4d1SAndreas Gohr protected function prefillInput() { 28*12a4e4d1SAndreas Gohr global $INPUT; 29*12a4e4d1SAndreas Gohr list($name, $key) = $this->getInputName(); 30*12a4e4d1SAndreas Gohr $myvalue = $this->val(); 31*12a4e4d1SAndreas Gohr 32*12a4e4d1SAndreas Gohr if(!$INPUT->has($name)) return; 33*12a4e4d1SAndreas Gohr 34*12a4e4d1SAndreas Gohr if($key === null) { 35*12a4e4d1SAndreas Gohr // no key - single value 36*12a4e4d1SAndreas Gohr $value = $INPUT->str($name); 37*12a4e4d1SAndreas Gohr if($value == $myvalue) { 38*12a4e4d1SAndreas Gohr $this->attr('checked', 'checked'); 39*12a4e4d1SAndreas Gohr } else { 40*12a4e4d1SAndreas Gohr $this->rmattr('checked'); 41*12a4e4d1SAndreas Gohr } 42*12a4e4d1SAndreas Gohr } else { 43*12a4e4d1SAndreas Gohr // we have an array, there might be several values in it 44*12a4e4d1SAndreas Gohr $input = $INPUT->arr($name); 45*12a4e4d1SAndreas Gohr if(isset($input[$key])) { 46*12a4e4d1SAndreas Gohr $this->rmattr('checked'); 47*12a4e4d1SAndreas Gohr 48*12a4e4d1SAndreas Gohr // values seem to be in another sub array 49*12a4e4d1SAndreas Gohr if(is_array($input[$key])) { 50*12a4e4d1SAndreas Gohr $input = $input[$key]; 51*12a4e4d1SAndreas Gohr } 52*12a4e4d1SAndreas Gohr 53*12a4e4d1SAndreas Gohr foreach($input as $value) { 54*12a4e4d1SAndreas Gohr if($value == $myvalue) { 55*12a4e4d1SAndreas Gohr $this->attr('checked', 'checked'); 56*12a4e4d1SAndreas Gohr } 57*12a4e4d1SAndreas Gohr } 58*12a4e4d1SAndreas Gohr } 59*12a4e4d1SAndreas Gohr } 60*12a4e4d1SAndreas Gohr } 61*12a4e4d1SAndreas Gohr 62*12a4e4d1SAndreas Gohr} 63