Passed
Push — develop ( ceb972...24fddc )
by Nikolay
06:15
created

IncomingRouteEditForm   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 36
c 1
b 0
f 0
dl 0
loc 105
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 47 1
A prepareProviders() 0 19 2
A prepareForwardingExtensions() 0 11 2
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\AdminCabinet\Forms;
21
22
use MikoPBX\Common\Models\Extensions;
23
use MikoPBX\Common\Models\Providers;
24
use MikoPBX\Common\Providers\TranslationProvider;
25
use Phalcon\Forms\Element\Hidden;
26
use Phalcon\Forms\Element\Numeric;
27
use Phalcon\Forms\Element\Select;
28
use Phalcon\Forms\Element\Text;
29
30
/**
31
 * Class IncomingRouteEditForm
32
 * This class is responsible for creating the form used for editing incoming routes.
33
 * It extends from BaseForm to inherit common form functionality.
34
 * @package MikoPBX\AdminCabinet\Forms
35
 * @property TranslationProvider translation
36
 */
37
class IncomingRouteEditForm extends BaseForm
38
{
39
    /**
40
     * Initialize the form elements
41
     *
42
     * @param mixed $entity The entity for which the form is being initialized.
43
     * @param mixed $options Additional options that may be needed.
44
     */
45
    public function initialize($entity = null, $options = null): void
46
    {
47
        parent::initialize($entity, $options);
48
49
        // Add hidden field for ID
50
        $this->add(new Hidden('id'));
51
52
        // Add hidden field for Priority
53
        $this->add(new Hidden('priority'));
54
55
        // Add hidden field for Action, default value is 'extension'
56
        $this->add(new Hidden('action', ['value' => 'extension']));
57
58
        // Add text field for Rule Name
59
        $this->add(new Text('rulename'));
60
61
        // Add text field for Number
62
        $this->add(new Text('number'));
63
64
        // Add text area for Note
65
        $this->addTextArea('note', $entity->note ?? '', 65);
66
67
        // Add numeric field for Timeout with some styling
68
        $this->add(new Numeric('timeout', ['maxlength' => 3, 'style' => 'width: 80px;', 'defaultValue' => 120]));
69
70
        // Add select dropdown for Providers
71
        $providers = new Select(
72
            'provider', $this->prepareProviders(), [
73
                'using' => ['id', 'name'],
74
                'useEmpty' => false,
75
                'class' => 'ui selection dropdown provider-select',
76
            ]
77
        );
78
        $this->add($providers);
79
80
        // Add select dropdown for Extension
81
        $extension = new Select(
82
            'extension', $this->prepareForwardingExtensions($entity->extension ?? ''), [
83
                'using' => [
84
                    'id',
85
                    'name',
86
                ],
87
                'useEmpty' => false,
88
                'class' => 'ui selection dropdown search forwarding-select',
89
            ]
90
        );
91
        $this->add($extension);
92
    }
93
94
    /**
95
     * Prepare Providers
96
     *
97
     * Generate a list of providers for the select dropdown
98
     *
99
     * @return array The list of providers
100
     */
101
    private function prepareProviders(): array
102
    {
103
        // Initialize an empty array to hold the list of providers
104
        $providersList = [];
105
106
        // Add a "none" option for any provider
107
        $providersList['none'] = $this->translation->_('ir_AnyProvider');
108
109
        // Fetch all providers from the database
110
        $providers = Providers::find();
111
112
        // Loop through each provider to populate the providers list
113
        foreach ($providers as $provider) {
114
            $modelType = ucfirst($provider->type);
115
            $provByType = $provider->$modelType;
116
            $providersList[$provByType->uniqid] = $provByType->getRepresent();
117
118
        }
119
        return $providersList;
120
    }
121
122
    /**
123
     * Prepare Forwarding Extensions
124
     *
125
     * Generate a list of extensions for the select dropdown based on the provided extension.
126
     *
127
     * @param string $extension The extension to find
128
     *
129
     * @return array The list of forwarding extensions
130
     */
131
    private function prepareForwardingExtensions(string $extension): array
132
    {
133
        // Get a list of all used extensions
134
        $forwardingExtensions = [];
135
136
        // Add a default option for the select dropdown
137
        $forwardingExtensions[''] = $this->translation->_('ex_SelectNumber');
138
139
        $record = Extensions::findFirstByNumber($extension);
140
        $forwardingExtensions[$record->number] = $record ? $record->getRepresent() : '';
141
        return $forwardingExtensions;
142
    }
143
}