static public getCheckbox ( array $options ) : the | ||
$options | array | array of parameters: - title its title - name its name - id its id - value the value to set when checked - readonly can we edit it ? - massive_tags the tag to set for massive checkbox update - checked is it checked or not ? - zero_on_empty do we send 0 on submit when it is not checked ? - specific_tags HTML5 tags to add - criterion the criterion for massive checkbox |
return | the | HTML code for the checkbox |
static function getCheckbox(array $options)
{
global $CFG_GLPI;
$params = array();
$params['title'] = '';
$params['name'] = '';
$params['rand'] = mt_rand();
$params['id'] = "check_" . $params['rand'];
$params['value'] = 1;
$params['readonly'] = false;
$params['massive_tags'] = '';
$params['checked'] = false;
$params['zero_on_empty'] = true;
$params['specific_tags'] = array();
$params['criterion'] = array();
if (is_array($options) && count($options)) {
foreach ($options as $key => $val) {
$params[$key] = $val;
}
}
$out = "<span class='form-group-checkbox'>";
$out .= "<input type='checkbox' class='new_checkbox' ";
foreach (array('id', 'name', 'title', 'value') as $field) {
if (!empty($params[$field])) {
$out .= " {$field}='" . $params[$field] . "'";
}
}
$criterion = self::getCriterionForMassiveCheckboxes($params['criterion']);
if (!empty($criterion)) {
$out .= " onClick='massiveUpdateCheckbox(\"{$criterion}\", this)'";
}
if ($params['zero_on_empty']) {
$out .= " data-glpicore-cb-zero-on-empty='1'";
$CFG_GLPI['checkbox-zero-on-empty'] = true;
}
if (!empty($params['massive_tags'])) {
$params['specific_tags']['data-glpicore-cb-massive-tags'] = $params['massive_tags'];
}
if (!empty($params['specific_tags'])) {
foreach ($params['specific_tags'] as $tag => $values) {
if (is_array($values)) {
$values = implode(' ', $values);
}
$out .= " {$tag}='{$values}'";
}
}
if ($params['readonly']) {
$out .= " disabled='disabled'";
}
if ($params['checked']) {
$out .= " checked";
}
$out .= ">";
$out .= "<label class='label-checkbox' title=\"" . $params['title'] . "\" for='" . $params['id'] . "'>";
$out .= " <span class='check'></span>";
$out .= " <span class='box'></span>";
$out .= " ";
$out .= "</label>";
$out .= "</span>";
if (!empty($criterion)) {
$out .= Html::scriptBlock("\$('{$criterion}').shiftSelectable();");
}
return $out;
}
/** * Get right linear right choice. * * @since version 0.85 * * @param $elements array all pair identifier => label * @param $options array possible: * 'canedit' * 'field' name of the HTML field * 'value' the value inside the database * 'max_per_line' maximum number of elements per line * 'check_all' add a checkbox to check or uncheck every checkbox * 'rand' random value used to generate the ids * 'zero_on_empty' do we send 0 when checkbox is not checked ? * 'display' * 'check_method' method used to check the right * * @return content if !display **/ static function getLinearRightChoice(array $elements, array $options = array()) { $param = array(); $param['canedit'] = true; $param['field'] = ''; $param['value'] = ''; $param['max_per_line'] = 10; $param['check_all'] = false; $param['rand'] = mt_rand(); $param['zero_on_empty'] = true; $param['display'] = true; $param['check_method'] = function ($element, $field) { return ($field & $element) == $element; }; if (is_array($options) && count($options)) { foreach ($options as $key => $val) { $param[$key] = $val; } } if (empty($param['field'])) { return; } $nb_cbs = count($elements); $cb_options = array('readonly' => !$param['canedit']); if ($param['check_all']) { $nb_cbs++; $massive_tag = 'checkall_' . $param['field'] . '_' . $param['rand']; $cb_options['massive_tags'] = $massive_tag; } $nb_lines = ceil($nb_cbs / $param['max_per_line']); $nb_item_per_line = ceil($nb_cbs / $nb_lines); $out = ''; $count = 0; $nb_checked = 0; foreach ($elements as $element => $label) { if ($count != 0) { if ($count % $nb_item_per_line == 0) { $out .= "<br>\n"; } else { $out .= " -\n\t\t "; } } else { $out .= "\n\t\t"; } $out .= $label . ' '; $cb_options['name'] = $param['field'] . '[' . $element . ']'; $cb_options['id'] = Html::cleanId('checkbox_linear_' . $cb_options['name'] . '_' . $param['rand']); $cb_options['zero_on_empty'] = $param['zero_on_empty']; $cb_options['checked'] = $param['check_method']($element, $param['value']); $out .= Html::getCheckbox($cb_options); $count++; if ($cb_options['checked']) { $nb_checked++; } } if ($param['check_all']) { $cb_options = array('criterion' => array('tag_for_massive' => $massive_tag), 'id' => Html::cleanId('checkbox_linear_' . $param['rand'])); if ($nb_checked > count($elements) / 2) { $cb_options['checked'] = true; } $out .= " - <i><b>" . __('Select/unselect all') . "</b></i> " . Html::getCheckbox($cb_options); } if (!$param['display']) { return $out; } echo $out; }