Passed
Push — develop ( d30961...dd7b3a )
by Nikolay
34:55 queued 29:58
created

BaseForm::addCheckBox()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 3
1
<?php
2
3
/*
4
 * MikoPBX - free phone system for small business
5
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along with this program.
18
 * If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace MikoPBX\AdminCabinet\Forms;
22
23
use MikoPBX\Common\Providers\PBXConfModulesProvider;
24
use MikoPBX\Modules\Config\WebUIConfigInterface;
25
use Phalcon\Forms\Element\Check;
26
use Phalcon\Forms\Element\TextArea;
27
use Phalcon\Forms\Form;
28
29
/**
30
 * @property \MikoPBX\Common\Providers\TranslationProvider translation
31
 */
32
abstract class BaseForm extends Form
33
{
34
    public function initialize($entity = null, $options = null): void
35
    {
36
        PBXConfModulesProvider::hookModulesMethod(
37
            WebUIConfigInterface::ON_BEFORE_FORM_INITIALIZE,
38
            [$this, $entity, $options]
39
        );
40
    }
41
42
    /**
43
     * Add a textarea form element.
44
     *
45
     * @param string $areaName The name of the textarea field.
46
     * @param string $areaValue The initial value for the textarea field.
47
     * @param int $areaWidth The width of the textarea field in columns (optional, default: 85).
48
     * @param array $options Additional options for TextArea element
49
     * @return void
50
     */
51
    public function addTextArea(string $areaName, string $areaValue, int $areaWidth = 90, array $options = []): void
52
    {
53
        $rows = 1;
54
        $strings = '';
55
        if (array_key_exists('placeholder', $options) && !empty($options["placeholder"])) {
56
            $strings = explode("\n", $options["placeholder"]);
57
        }
58
        if (!empty($areaValue)) {
59
            $strings = explode("\n", $areaValue);
60
        }
61
        foreach ($strings as $string) {
62
            $rows += ceil(strlen($string) / $areaWidth);
63
        }
64
        $options["rows"] = max($rows, 2);
65
        $options["value"] = $areaValue;
66
        $this->add(new TextArea($areaName, $options));
67
    }
68
69
    /**
70
     * Adds a checkbox to the form field with the given name.
71
     *
72
     * @param string $fieldName The name of the form field.
73
     * @param bool $checked Indicates whether the checkbox is checked by default.
74
     * @param string $checkedValue The value assigned to the checkbox when it is checked.
75
     * @return void
76
     */
77
    public function addCheckBox(string $fieldName, bool $checked, string $checkedValue = 'on'): void
78
    {
79
        $checkAr = ['value' => null];
80
        if ($checked) {
81
            $checkAr = ['checked' => $checkedValue,'value' => $checkedValue];
82
        }
83
        $this->add(new Check($fieldName, $checkAr));
84
    }
85
}
86