Test Failed
Push — master ( 2c8b6d...9d1fea )
by Alxarafe
45:51
created

FormResource   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 86
dl 0
loc 184
rs 8.8798
c 0
b 0
f 0
wmc 44

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
D select_types_resource() 0 54 25
D select_resource_list() 0 74 18

How to fix   Complexity   

Complex Class

Complex classes like FormResource often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FormResource, and based on these observations, apply Extract Interface, too.

1
<?php
2
/* Copyright (C) - 2013-2015 Jean-François FERRY	<[email protected]>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 * or see http://www.gnu.org/
17
 */
18
defined('BASE_PATH') or die('Single entry point through the index.php of the main folder');
19
20
/**
21
 *       \file       resource/class/html.formresource.class.php
22
 *       \ingroup    core
23
 *       \brief      Class file to manage forms into resource module
24
 */
25
require_once DOL_DOCUMENT_ROOT . "/core/class/html.form.class.php";
26
require_once DOL_DOCUMENT_ROOT . "/resource/class/dolresource.class.php";
27
28
/**
29
 * Classe permettant la gestion des formulaire du module resource
30
 *
31
 * \remarks Utilisation: $formresource = new FormResource($db)
32
 * \remarks $formplace->proprietes=1 ou chaine ou tableau de valeurs
33
 */
34
class FormResource
35
{
36
37
    /**
38
     * @var DoliDB Database handler.
39
     */
40
    public $db;
41
    public $substit = array();
42
    public $param = array();
43
44
    /**
45
     * @var string Error code (or message)
46
     */
47
    public $error = '';
48
49
    /**
50
     * Constructor
51
     *
52
     * @param DoliDB $db Database handler
53
     */
54
    function __construct($db)
55
    {
56
        $this->db = $db;
57
    }
58
59
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
60
    /**
61
     *  Output html form to select a resource
62
     *
63
     * 	@param	int   	$selected       Preselected resource id
64
     * 	@param  string	$htmlname       Name of field in form
65
     *  @param  string	$filter         Optionnal filters criteras (example: 's.rowid <> x')
66
     * 	@param	int		$showempty		Add an empty field
67
     * 	@param	int		$showtype		Show third party type in combolist (customer, prospect or supplier)
68
     * 	@param	int		$forcecombo		Force to use combo box
69
     *  @param	array	$event			Event options. Example: array(array('method'=>'getContacts', 'url'=>dol_buildpath('/core/ajax/contacts.php',1), 'htmlname'=>'contactid', 'params'=>array('add-customer-contact'=>'disabled')))
70
     *  @param	string	$filterkey		Filter on key value
71
     *  @param	int		$outputmode		0=HTML select string, 1=Array, 2=without form tag
72
     *  @param	int		$limit			Limit number of answers
73
     * 	@return	string					HTML string with
74
     */
75
    function select_resource_list($selected = '', $htmlname = 'fk_resource', $filter = '', $showempty = 0, $showtype = 0, $forcecombo = 0, $event = array(), $filterkey = '', $outputmode = 0, $limit = 20)
76
    {
77
        // phpcs:enable
78
        global $conf, $user, $langs;
79
80
        $out = '';
81
        $outarray = array();
82
83
        $resourcestat = new Dolresource($this->db);
84
85
        $resources_used = $resourcestat->fetch_all('ASC', 't.rowid', $limit, $offset, $filter = '');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $offset seems to be never defined.
Loading history...
86
87
        if ($outputmode != 2) {
88
            $out = '<form action="' . $_SERVER["PHP_SELF"] . '" method="POST">';
89
            $out .= '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
90
        }
91
        //$out.= '<input type="hidden" name="action" value="search">';
92
        //$out.= '<input type="hidden" name="id" value="'.$theme->id.'">';
93
94
        if ($resourcestat) {
95
            if (!empty($conf->use_javascript_ajax) && !empty($conf->global->RESOURCE_USE_SEARCH_TO_SELECT) && !$forcecombo) {
96
                //$minLength = (is_numeric($conf->global->RESOURCE_USE_SEARCH_TO_SELECT)?$conf->global->RESOURCE_USE_SEARCH_TO_SELECT:2);
97
                $out .= ajax_combobox($htmlname, $event, $conf->global->RESOURCE_USE_SEARCH_TO_SELECT);
98
            }
99
100
            // Construct $out and $outarray
101
            $out .= '<select id="' . $htmlname . '" class="flat minwidth200" name="' . $htmlname . '">' . "\n";
102
            if ($showempty) {
103
                $out .= '<option value="-1">&nbsp;</option>' . "\n";
104
            }
105
106
            $num = isset($resourcestat->lines) ? count($resourcestat->lines) : 0;
107
108
            //var_dump($resourcestat->lines);
109
            $i = 0;
110
            if ($num) {
111
                while ($i < $num) {
112
                    $resourceclass = ucfirst($resourcestat->lines[$i]->element);
113
114
                    $label = $resourcestat->lines[$i]->ref ? $resourcestat->lines[$i]->ref : '' . $resourcestat->lines[$i]->label;
115
                    if ($resourceclass != 'Dolresource') {
116
                        $label .= ' (' . $langs->trans($resourceclass) . ')';
117
                    }
118
                    if ($selected > 0 && $selected == $resourcestat->lines[$i]->id) {
119
                        $out .= '<option value="' . $resourcestat->lines[$i]->id . '" selected>' . $label . '</option>';
120
                    } else {
121
                        $out .= '<option value="' . $resourcestat->lines[$i]->id . '">' . $label . '</option>';
122
                    }
123
124
                    array_push($outarray, array('key' => $resourcestat->lines[$i]->id, 'value' => $resourcestat->lines[$i]->label, 'label' => $resourcestat->lines[$i]->label));
125
126
                    $i++;
127
                    if (($i % 10) == 0) {
128
                        $out .= "\n";
129
                    }
130
                }
131
            }
132
            $out .= '</select>' . "\n";
133
            $out .= ajax_combobox($htmlname);
134
135
            if ($outputmode != 2) {
136
137
                $out .= '<input type="submit" class="button" value="' . $langs->trans("Search") . '"> &nbsp; &nbsp; ';
138
139
                $out .= '</form>';
140
            }
141
        } else {
142
            dol_print_error($this->db);
143
        }
144
145
        if ($outputmode && $outputmode != 2) {
146
            return $outarray;
147
        }
148
        return $out;
149
    }
150
151
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
152
    /**
153
     *  Return html list of tickets type
154
     *
155
     *  @param	string	$selected       Id du type pre-selectionne
156
     *  @param  string	$htmlname       Nom de la zone select
157
     *  @param  string	$filtertype     To filter on field type in llx_c_ticket_type (array('code'=>xx,'label'=>zz))
158
     *  @param  int		$format         0=id+libelle, 1=code+code, 2=code+libelle, 3=id+code
159
     *  @param  int		$empty			1=peut etre vide, 0 sinon
160
     *  @param	int		$noadmininfo	0=Add admin info, 1=Disable admin info
161
     *  @param  int		$maxlength      Max length of label
162
     * 	@return	void
163
     */
164
    function select_types_resource($selected = '', $htmlname = 'type_resource', $filtertype = '', $format = 0, $empty = 0, $noadmininfo = 0, $maxlength = 0)
165
    {
166
        // phpcs:enable
167
        global $langs, $user;
168
169
        $resourcestat = new Dolresource($this->db);
170
171
        dol_syslog(get_class($this) . "::select_types_resource " . $selected . ", " . $htmlname . ", " . $filtertype . ", " . $format, LOG_DEBUG);
172
173
        $filterarray = array();
174
175
        if ($filtertype != '' && $filtertype != '-1')
176
            $filterarray = explode(',', $filtertype);
177
178
        $resourcestat->load_cache_code_type_resource();
179
        print '<select id="select' . $htmlname . '" class="flat maxwidthonsmartphone select_' . $htmlname . '" name="' . $htmlname . '">';
180
        if ($empty)
181
            print '<option value="">&nbsp;</option>';
182
        if (is_array($resourcestat->cache_code_type_resource) && count($resourcestat->cache_code_type_resource)) {
183
            foreach ($resourcestat->cache_code_type_resource as $id => $arraytypes) {
184
185
                // We discard empty line if showempty is on because an empty line has already been output.
186
                if ($empty && empty($arraytypes['code']))
187
                    continue;
188
189
                if ($format == 0)
190
                    print '<option value="' . $id . '"';
191
                if ($format == 1)
192
                    print '<option value="' . $arraytypes['code'] . '"';
193
                if ($format == 2)
194
                    print '<option value="' . $arraytypes['code'] . '"';
195
                if ($format == 3)
196
                    print '<option value="' . $id . '"';
197
                // Si selected est text, on compare avec code, sinon avec id
198
                if (preg_match('/[a-z]/i', $selected) && $selected == $arraytypes['code'])
199
                    print ' selected';
200
                elseif ($selected == $id)
201
                    print ' selected';
202
                print '>';
203
                if ($format == 0)
204
                    $value = ($maxlength ? dol_trunc($arraytypes['label'], $maxlength) : $arraytypes['label']);
205
                if ($format == 1)
206
                    $value = $arraytypes['code'];
207
                if ($format == 2)
208
                    $value = ($maxlength ? dol_trunc($arraytypes['label'], $maxlength) : $arraytypes['label']);
209
                if ($format == 3)
210
                    $value = $arraytypes['code'];
211
                print $value ? $value : '&nbsp;';
212
                print '</option>';
213
            }
214
        }
215
        print '</select>';
216
        if ($user->admin && !$noadmininfo)
217
            print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
218
    }
219
}
220