Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 80
Html
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
552.00
0.00% covered (danger)
0.00%
0 / 80
 activeCheckboxTree
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 3
 activeTreeInput
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 11
 checkboxTree
0.00% covered (danger)
0.00%
0 / 1
272.00
0.00% covered (danger)
0.00%
0 / 60
 setHeader
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 6
<?php
namespace common\helpers;
use yii\helpers\ArrayHelper;
class Html extends \yii\bootstrap\Html
{
    public static function activeCheckboxTree($model, $attribute, $items, $valueSelector, $labelSelector, $itemsSelector, $options = [])
    {
        return static::activeTreeInput('checkboxTree', $model, $attribute, $items, $valueSelector, $labelSelector, $itemsSelector, $options);
    }
    protected static function activeTreeInput($type, $model, $attribute, $items, $valueSelector, $labelSelector, $itemsSelector, $options = [])
    {
        $name = isset($options['name']) ? $options['name'] : static::getInputName($model, $attribute);
        $selection = isset($options['value']) ? $options['value'] : static::getAttributeValue($model, $attribute);
        if (!array_key_exists('unselect', $options)) {
            $options['unselect'] = '';
        }
        if (!array_key_exists('id', $options)) {
            $options['id'] = static::getInputId($model, $attribute);
        }
        return static::$type($name, $selection, $items, $valueSelector, $labelSelector, $itemsSelector, $options);
    }
    public static function checkboxTree($name, $selection = null, $items = [], $valueSelector, $labelSelector, $itemsSelector, $options = [])
    {
        if (substr($name, -2) !== '[]') {
            $name .= '[]';
        }
        if (ArrayHelper::isTraversable($selection)) {
            $selection = array_map('strval', (array)$selection);
        }
        $formatter = ArrayHelper::remove($options, 'item');
        $itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
        $encode = ArrayHelper::remove($options, 'encode', true);
        $separator = ArrayHelper::remove($options, 'separator', "\n");
        $tag = ArrayHelper::remove($options, 'tag', 'div');
        $lines = [];
        $index = 0;
        $level = 0;
        $stack = array_reverse($items, false);
        while (!empty($stack)) {
            $item = array_pop($stack);
            if (is_null($item)) {
                $level--;
            } else {
                $value = $item->$valueSelector;
                $label = $item->$labelSelector;
                $checked = $selection !== null &&
                    (!ArrayHelper::isTraversable($selection) && !strcmp($value, $selection)
                        || ArrayHelper::isTraversable($selection) && ArrayHelper::isIn((string)$value, $selection));
                if ($formatter !== null) {
                    $lines[] = call_user_func($formatter, $index, $label, $name, $checked, $value, $level);
                } else {
                    $lines[] = static::checkbox($name, $checked, array_merge([
                        'value' => $value,
                        'label' => $encode ? Html::encode($label) : $label,
                    ], $itemOptions));
                }
                $index++;
    
                $childItems = $item->$itemsSelector;
                if (!empty($childItems)) {
                    $level++;
                    $stack[] = null;
                    $stack = array_merge($stack, array_reverse($childItems));
                }
            }
        }
        if (isset($options['unselect'])) {
            // add a hidden field so that if the list box has no option being selected, it still submits a value
            $name2 = substr($name, -2) === '[]' ? substr($name, 0, -2) : $name;
            $hiddenOptions = [];
            // make sure disabled input is not sending any value
            if (!empty($options['disabled'])) {
                $hiddenOptions['disabled'] = $options['disabled'];
            }
            $hidden = static::hiddenInput($name2, $options['unselect'], $hiddenOptions);
            unset($options['unselect'], $options['disabled']);
        } else {
            $hidden = '';
        }
        $visibleContent = implode($separator, $lines);
        if ($tag === false) {
            return $hidden . $visibleContent;
        }
        return $hidden . static::tag($tag, $visibleContent, $options);
    }
    public static function setHeader($documentName = 'document.pdf')
    {
        header('Content-Type: application/pdf');
        header('Content-Disposition: inline; filename="' . $documentName . '"');
        header('Cache-Control: private, max-age=0, must-revalidate');
        header('Pragma: public');
    }
}