yf_form2_stars class

Fileplugins/form2/classes/form2/yf_form2_stars.class.php
Use_class('form2_stars', 'classes/form2/')

Public properties

PropertyDefaultDescription
None declared in this class.

Public methods 2

stars

public function stars($name = '', $desc = '', $extra = [], $replace = [], $form = null)
    public function stars($name = '', $desc = '', $extra = [], $replace = [], $form = null)
    {
        if (is_array($desc)) {
            $extra += $desc;
            $desc = '';
        }
        if ( ! is_array($extra)) {
            $extra = [];
        }
        $extra['name'] = ($extra['name'] ?? null) ?: ($name ?: 'stars');
        $extra['desc'] = $form->_prepare_desc($extra, $desc);
        $func = function ($extra, $r, $form) {
            $extra['id'] = $extra['name'];
            $color_ok = ($extra['color_ok'] ?? null) ?: 'yellow';
            $color_ko = ($extra['color_ko'] ?? null) ?: '';
            $class = ($extra['class'] ?? null) ?: 'icon-star icon-large';
            $class_ok = ($extra['class_ok'] ?? null) ?: 'star-ok';
            $class_ko = ($extra['class_ko'] ?? null) ?: 'star-ko';
            $max = ($extra['max'] ?? null) ?: 5;
            $stars = ($extra['stars'] ?? null) ?: 5;
            // dd_table() names a field by its value, which can be a float: PHP truncates float keys, without the deprecation
            $name = $extra['name'] ?? '';
            $key = is_float($name) ? (int) $name : $name;
            $input = isset($r[$key]) ? $r[$key] : $name;
            $title = _escape_tpl_braces($input);
            foreach (range(1, $stars) as $num) {
                $is_ok = $input >= ($num * $max / $stars) ? 1 : 0;
                $body[] = '<i class="' . $class . ' ' . ($is_ok ? $class_ok : $class_ko) . '" style="color:' . ($is_ok ? $color_ok : $color_ko) . ';" title="' . $title . '"></i>';
            }
            return $form->_row_html(implode(PHP_EOL, $body), $extra, $r);
        };
        if ($form->_chained_mode) {
            $form->_body[] = ['func' => $func, 'extra' => $extra, 'replace' => $replace, 'name' => __FUNCTION__];
            return $form;
        }
        return $func($extra, $replace, $form);
    }

stars_select

public function stars_select($name = '', $desc = '', $extra = [], $replace = [], $form = null)

Star selector, got from http://fontawesome.io/examples/#custom. Require this CSS: '<style> .rating { unicode-bidi:bidi-override;direction:rtl;font-size:20px } .rating span.star { font-family:FontAwesome;font-weight:normal;font-style:normal;display:inline-block } .rating span.star:hover { cursor:pointer } .rating span.star:before { content:"\f006";padding-right:0.2em;color:#999 } .rating span.star:hover:before, .rating span.star:hover~span.star:before{ content:"\f005";color:#e3cf7a } </style>';.

    public function stars_select($name = '', $desc = '', $extra = [], $replace = [], $form = null)
    {
        if (is_array($desc)) {
            $extra += $desc;
            $desc = '';
        }
        if ( ! is_array($extra)) {
            $extra = [];
        }
        $extra['name'] = ($extra['name'] ?? null) ?: ($name ?: 'stars');
        $extra['desc'] = $form->_prepare_desc($extra, $desc);
        $func = function ($extra, $r, $form) {
            $form->_prepare_inline_error($extra);
            $max = ($extra['max'] ?? null) ?: 5;
            $stars = ($extra['stars'] ?? null) ?: 5;
            $class = ($extra['class'] ?? null) ?: 'star';
            $body[] = '<span class="rating">';
            foreach (range(1, $stars) as $num) {
                $body[] = '<span class="' . $class . ' ' . $extra['name'] . '" data-name="' . $extra['name'] . '" data-value="' . ($stars - $num + 1) . '"></span>';
            }
            $body[] = '</span>';
            $body[] = '<input type="hidden" name="' . $extra['name'] . '" id=' . $extra['name'] . ' value="0">';

            jquery(
                '$(".' . $class . '.' . $extra['name'] . '").on("click",function() {
					var value = $(this).attr("data-value");
					$("#"+$(this).attr("data-name")).val(value);
					$(".rating.star.' . $extra['name'] . '").each(function() {
						$(this).attr("data-value");
						if (value>=$(this).attr("data-value")) {
							$(this).addClass("rating_selected");
						} else {
							$(this).removeClass("rating_selected");
						}
					});
				});'
            );
            return $form->_row_html(implode('', $body), $extra, $r);
        };
        if ($form->_chained_mode) {
            $form->_body[] = ['func' => $func, 'extra' => $extra, 'replace' => $replace, 'name' => __FUNCTION__];
            return $form;
        }
        return $func($extra, $replace, $form);
    }