Forms.php 3.2 KB
Newer Older
1 2
<?php
namespace ACP3\Core\Helpers;
Tino Goratsch's avatar
Tino Goratsch committed
3

Tino Goratsch's avatar
Tino Goratsch committed
4 5
use ACP3\Core\Http\Request;
use ACP3\Core\Http\RequestInterface;
6 7 8 9 10 11 12

/**
 * Class Forms
 * @package ACP3\Core\Helpers
 */
class Forms
{
13
    /**
Tino Goratsch's avatar
Tino Goratsch committed
14
     * @var \ACP3\Core\Http\RequestInterface
15 16 17 18
     */
    protected $request;

    /**
Tino Goratsch's avatar
Tino Goratsch committed
19
     * @param \ACP3\Core\Http\RequestInterface $request
20 21 22 23 24 25
     */
    public function __construct(RequestInterface $request)
    {
        $this->request = $request;
    }

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    /**
     * Liefert ein Array zur Ausgabe als Dropdown-Menü
     * für die Anzahl der anzuzeigenden Datensätze je Seite
     *
     * @param integer $currentValue
     * @param integer $steps
     * @param integer $maxValue
     *
     * @return array
     */
    public function recordsPerPage($currentValue, $steps = 5, $maxValue = 50)
    {
        $records = [];
        for ($i = 0, $j = $steps; $j <= $maxValue; $i++, $j += $steps) {
            $records[$i]['value'] = $j;
            $records[$i]['selected'] = $this->selectEntry('entries', $j, $currentValue);
        }
        return $records;
    }

    /**
     * Selektion eines Eintrages in einem Dropdown-Menü
     *
49 50 51 52
     * @param string               $name
     * @param mixed                $value
     * @param string|integer|array $currentValue
     * @param string               $attr
53 54 55
     *
     * @return string
     */
56
    public function selectEntry($name, $value, $currentValue = '', $attr = 'selected')
57 58
    {
        $attr = ' ' . $attr . '="' . $attr . '"';
59
        $currentValue = $this->request->getPost()->get($name, $currentValue);
60

61
        if (is_array($currentValue) === false && $currentValue == $value) {
62 63 64
            return $attr;
        } elseif (is_array($currentValue) === true) {
            foreach ($currentValue as $row) {
65
                if ($row == $value) {
66 67 68 69 70 71 72 73 74 75
                    return $attr;
                }
            }
        }

        return '';
    }

    /**
     *
76 77 78 79 80
     * @param string               $name
     * @param array                $values
     * @param array                $lang
     * @param string|integer|array $currentValue
     * @param string               $selected
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
     *
     * @return array
     */
    public function selectGenerator($name, array $values, array $lang, $currentValue = '', $selected = 'selected')
    {
        $select = [];
        if (count($values) == count($lang)) {
            $c_values = count($values);
            $id = str_replace('_', '-', $name);
            for ($i = 0; $i < $c_values; ++$i) {
                $select[] = [
                    'value' => $values[$i],
                    'id' => ($selected === 'checked' ? $id . '-' . $values[$i] : ''),
                    $selected => $this->selectEntry($name, $values[$i], $currentValue, $selected),
                    'lang' => $lang[$i]
                ];
            }
        }
        return $select;
    }
101 102 103 104 105 106 107 108 109 110 111 112 113

    /**
     * @param string               $name
     * @param array                $values
     * @param array                $lang
     * @param string|integer|array $currentValue
     *
     * @return array
     */
    public function checkboxGenerator($name, array $values, array $lang, $currentValue = '')
    {
        return $this->selectGenerator($name, $values, $lang, $currentValue, 'checked');
    }
114
}