Total Complexity | 44 |
Total Lines | 184 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |
||
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) |
||
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 = ''); |
||
|
|||
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"> </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") . '"> '; |
||
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) |
||
218 | } |
||
219 | } |
||
220 |