| Total Complexity | 94 |
| Total Lines | 413 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like FormActions 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 FormActions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class FormActions |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var DoliDB Database handler. |
||
| 40 | */ |
||
| 41 | public $db; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string Error code (or message) |
||
| 45 | */ |
||
| 46 | public $error = ''; |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * Constructor |
||
| 51 | * |
||
| 52 | * @param DoliDB $db Database handler |
||
| 53 | */ |
||
| 54 | public function __construct($db) |
||
| 55 | { |
||
| 56 | $this->db = $db; |
||
| 57 | } |
||
| 58 | |||
| 59 | |||
| 60 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 61 | /** |
||
| 62 | * Show list of action status |
||
| 63 | * |
||
| 64 | * @param string $formname Name of form where select is included |
||
| 65 | * @param string $selected Preselected value (-1..100) |
||
| 66 | * @param int $canedit 1=can edit, 0=read only |
||
| 67 | * @param string $htmlname Name of html prefix for html fields (selectX and valX) |
||
| 68 | * @param integer $showempty Show an empty line if select is used |
||
| 69 | * @param integer $onlyselect 0=Standard, 1=Hide percent of completion and force usage of a select list, 2=Same than 1 and add "Incomplete (Todo+Running) |
||
| 70 | * @param string $morecss More css on select field |
||
| 71 | * @return void |
||
| 72 | */ |
||
| 73 | public function form_select_status_action($formname, $selected, $canedit = 1, $htmlname = 'complete', $showempty = 0, $onlyselect = 0, $morecss = 'maxwidth100') |
||
| 136 | var value = (selected>0?selected:(defaultvalue>=0?defaultvalue:'')); |
||
| 137 | |||
| 138 | percentage.val(value); |
||
| 139 | |||
| 140 | if (defaultvalue == 'na' || defaultvalue == -1) { |
||
| 141 | percentage.prop('disabled', true); |
||
| 142 | $('.hideifna').hide(); |
||
| 143 | } |
||
| 144 | else if (defaultvalue == 0) { |
||
| 145 | percentage.val(0); |
||
| 146 | percentage.removeAttr('disabled'); /* Not disabled, we want to change it to higher value */ |
||
| 147 | $('.hideifna').show(); |
||
| 148 | } |
||
| 149 | else if (defaultvalue == 100) { |
||
| 150 | percentage.val(100); |
||
| 151 | percentage.prop('disabled', true); |
||
| 152 | $('.hideifna').show(); |
||
| 153 | } |
||
| 154 | else { |
||
| 155 | if (defaultvalue == 50 && (percentage.val() == 0 || percentage.val() == 100)) { percentage.val(50); } |
||
| 156 | percentage.removeAttr('disabled'); |
||
| 157 | $('.hideifna').show(); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | </script>\n"; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * Show list of actions for element |
||
| 167 | * |
||
| 168 | * @param Object $object Object |
||
| 169 | * @param string $typeelement 'invoice', 'propal', 'order', 'invoice_supplier', 'order_supplier', 'fichinter' |
||
| 170 | * @param int $socid Socid of user |
||
| 171 | * @param int $forceshowtitle Show title even if there is no actions to show |
||
| 172 | * @param string $morecss More css on table |
||
| 173 | * @param int $max Max number of record |
||
| 174 | * @param string $moreparambacktopage More param for the backtopage |
||
| 175 | * @param string $morehtmlcenter More html text on center of title line |
||
| 176 | * @param int $assignedtouser Assign event by default to this user id (will be ignored if not enough permissions) |
||
| 177 | * @return int Return integer <0 if KO, >=0 if OK |
||
| 178 | */ |
||
| 179 | public function showactions($object, $typeelement, $socid = 0, $forceshowtitle = 0, $morecss = 'listactions', $max = 0, $moreparambacktopage = '', $morehtmlcenter = '', $assignedtouser = 0) |
||
| 180 | { |
||
| 181 | global $langs, $conf, $user, $hookmanager; |
||
| 182 | |||
| 183 | require_once constant('DOL_DOCUMENT_ROOT') . '/comm/action/class/actioncomm.class.php'; |
||
| 184 | |||
| 185 | $sortfield = 'a.datep,a.id'; |
||
| 186 | $sortorder = 'DESC,DESC'; |
||
| 187 | |||
| 188 | $actioncomm = new ActionComm($this->db); |
||
| 189 | $listofactions = $actioncomm->getActions($socid, $object->id, $typeelement, '', $sortfield, $sortorder, ($max ? ($max + 1) : 0)); |
||
| 190 | if (!is_array($listofactions)) { |
||
| 191 | dol_print_error($this->db, 'FailedToGetActions'); |
||
| 192 | } |
||
| 193 | |||
| 194 | require_once constant('DOL_DOCUMENT_ROOT') . '/comm/action/class/cactioncomm.class.php'; |
||
| 195 | $caction = new CActionComm($this->db); |
||
| 196 | $arraylist = $caction->liste_array(1, 'code', '', (!getDolGlobalString('AGENDA_USE_EVENT_TYPE') ? 1 : 0), '', 1); |
||
| 197 | |||
| 198 | $num = count($listofactions); |
||
| 199 | if ($num || $forceshowtitle) { |
||
| 200 | $title = $langs->trans("LatestLinkedEvents", $max ? $max : ''); |
||
| 201 | |||
| 202 | $urlbacktopage = $_SERVER['PHP_SELF'] . '?id=' . $object->id . ($moreparambacktopage ? '&' . $moreparambacktopage : ''); |
||
| 203 | |||
| 204 | $projectid = $object->fk_project; |
||
| 205 | if ($typeelement == 'project') { |
||
| 206 | $projectid = $object->id; |
||
| 207 | } |
||
| 208 | $taskid = 0; |
||
| 209 | if ($typeelement == 'task') { |
||
| 210 | $taskid = $object->id; |
||
| 211 | } |
||
| 212 | |||
| 213 | $usercanaddaction = 0; |
||
| 214 | if (empty($assignedtouser) || $assignedtouser == $user->id) { |
||
| 215 | $usercanaddaction = $user->hasRight('agenda', 'myactions', 'create'); |
||
| 216 | $assignedtouser = 0; |
||
| 217 | } else { |
||
| 218 | $usercanaddaction = $user->hasRight('agenda', 'allactions', 'create'); |
||
| 219 | } |
||
| 220 | |||
| 221 | $url = ''; |
||
| 222 | $morehtmlright = ''; |
||
| 223 | if (isModEnabled('agenda') && $usercanaddaction) { |
||
| 224 | $url = constant('BASE_URL') . '/comm/action/card.php?action=create&token=' . newToken() . '&datep=' . urlencode(dol_print_date(dol_now(), 'dayhourlog', 'tzuser')); |
||
| 225 | $url .= '&origin=' . urlencode($typeelement) . '&originid=' . ((int) $object->id) . ((!empty($object->socid) && $object->socid > 0) ? '&socid=' . ((int) $object->socid) : ((!empty($socid) && $socid > 0) ? '&socid=' . ((int) $socid) : '')); |
||
| 226 | $url .= ($projectid > 0 ? '&projectid=' . ((int) $projectid) : '') . ($taskid > 0 ? '&taskid=' . ((int) $taskid) : ''); |
||
| 227 | $url .= ($assignedtouser > 0 ? '&assignedtouser=' . $assignedtouser : ''); |
||
| 228 | $url .= '&backtopage=' . urlencode($urlbacktopage); |
||
| 229 | $morehtmlright .= dolGetButtonTitle($langs->trans("AddEvent"), '', 'fa fa-plus-circle', $url); |
||
| 230 | } |
||
| 231 | |||
| 232 | $parameters = array( |
||
| 233 | 'title' => &$title, |
||
| 234 | 'morehtmlright' => &$morehtmlright, |
||
| 235 | 'morehtmlcenter' => &$morehtmlcenter, |
||
| 236 | 'usercanaddaction' => $usercanaddaction, |
||
| 237 | 'url' => &$url, |
||
| 238 | 'typeelement' => $typeelement, |
||
| 239 | 'projectid' => $projectid, |
||
| 240 | 'assignedtouser' => $assignedtouser, |
||
| 241 | 'taskid' => $taskid, |
||
| 242 | 'urlbacktopage' => $urlbacktopage |
||
| 243 | ); |
||
| 244 | |||
| 245 | $reshook = $hookmanager->executeHooks('showActionsLoadFicheTitre', $parameters, $object); |
||
| 246 | |||
| 247 | if ($reshook < 0) { |
||
| 248 | setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); |
||
| 249 | } |
||
| 250 | |||
| 251 | $error = 0; |
||
| 252 | if (empty($reshook)) { |
||
| 253 | print '<!-- formactions->showactions -->' . "\n"; |
||
| 254 | print load_fiche_titre($title, $morehtmlright, '', 0, 0, '', $morehtmlcenter); |
||
| 255 | } |
||
| 256 | |||
| 257 | $page = 0; |
||
| 258 | $param = ''; |
||
| 259 | |||
| 260 | print '<div class="div-table-responsive-no-min">'; |
||
| 261 | print '<table class="centpercent noborder' . ($morecss ? ' ' . $morecss : '') . '">'; |
||
| 262 | print '<tr class="liste_titre">'; |
||
| 263 | print getTitleFieldOfList('Ref', 0, $_SERVER["PHP_SELF"], '', $page, $param, '', $sortfield, $sortorder, '', 1); |
||
| 264 | print getTitleFieldOfList('Date', 0, $_SERVER["PHP_SELF"], 'a.datep', $page, $param, '', $sortfield, $sortorder, 'center ', 1); |
||
| 265 | print getTitleFieldOfList('By', 0, $_SERVER["PHP_SELF"], '', $page, $param, '', $sortfield, $sortorder, '', 1); |
||
| 266 | print getTitleFieldOfList('Type', 0, $_SERVER["PHP_SELF"], '', $page, $param, '', $sortfield, $sortorder, '', 1); |
||
| 267 | print getTitleFieldOfList('Title', 0, $_SERVER["PHP_SELF"], '', $page, $param, '', $sortfield, $sortorder, '', 1); |
||
| 268 | print getTitleFieldOfList('', 0, $_SERVER["PHP_SELF"], '', $page, $param, '', $sortfield, $sortorder, 'right ', 1); |
||
| 269 | print '</tr>'; |
||
| 270 | print "\n"; |
||
| 271 | |||
| 272 | if (is_array($listofactions) && count($listofactions)) { |
||
| 273 | $cacheusers = array(); |
||
| 274 | |||
| 275 | $cursorevent = 0; |
||
| 276 | foreach ($listofactions as $actioncomm) { |
||
| 277 | if ($max && $cursorevent >= $max) { |
||
| 278 | break; |
||
| 279 | } |
||
| 280 | |||
| 281 | print '<tr class="oddeven">'; |
||
| 282 | |||
| 283 | // Ref |
||
| 284 | print '<td class="nowraponall">' . $actioncomm->getNomUrl(1, -1) . '</td>'; |
||
| 285 | |||
| 286 | // Date |
||
| 287 | print '<td class="center nowraponall">' . dol_print_date($actioncomm->datep, 'dayhour', 'tzuserrel'); |
||
| 288 | if ($actioncomm->datef) { |
||
| 289 | $tmpa = dol_getdate($actioncomm->datep); |
||
| 290 | $tmpb = dol_getdate($actioncomm->datef); |
||
| 291 | if ($tmpa['mday'] == $tmpb['mday'] && $tmpa['mon'] == $tmpb['mon'] && $tmpa['year'] == $tmpb['year']) { |
||
| 292 | if ($tmpa['hours'] != $tmpb['hours'] || $tmpa['minutes'] != $tmpb['minutes']) { |
||
| 293 | print '-' . dol_print_date($actioncomm->datef, 'hour', 'tzuserrel'); |
||
| 294 | } |
||
| 295 | } else { |
||
| 296 | print '-' . dol_print_date($actioncomm->datef, 'dayhour', 'tzuserrel'); |
||
| 297 | } |
||
| 298 | } |
||
| 299 | print '</td>'; |
||
| 300 | |||
| 301 | // Owner |
||
| 302 | print '<td class="nowraponall tdoverflowmax125">'; |
||
| 303 | if (!empty($actioncomm->userownerid)) { |
||
| 304 | if (isset($cacheusers[$actioncomm->userownerid]) && is_object($cacheusers[$actioncomm->userownerid])) { |
||
| 305 | $tmpuser = $cacheusers[$actioncomm->userownerid]; |
||
| 306 | } else { |
||
| 307 | $tmpuser = new User($this->db); |
||
|
|
|||
| 308 | $tmpuser->fetch($actioncomm->userownerid); |
||
| 309 | $cacheusers[$actioncomm->userownerid] = $tmpuser; |
||
| 310 | } |
||
| 311 | if ($tmpuser->id > 0) { |
||
| 312 | print $tmpuser->getNomUrl(-1, '', 0, 0, 16, 0, 'firstelselast', ''); |
||
| 313 | } |
||
| 314 | } |
||
| 315 | print '</td>'; |
||
| 316 | |||
| 317 | $actionstatic = $actioncomm; |
||
| 318 | |||
| 319 | // Example: Email sent from invoice card |
||
| 320 | //$actionstatic->code = 'AC_BILL_SENTBYMAIL |
||
| 321 | //$actionstatic->type_code = 'AC_OTHER_AUTO' |
||
| 322 | |||
| 323 | // Type |
||
| 324 | $labeltype = $actionstatic->type_code; |
||
| 325 | if (!getDolGlobalString('AGENDA_USE_EVENT_TYPE') && empty($arraylist[$labeltype])) { |
||
| 326 | $labeltype = 'AC_OTH'; |
||
| 327 | } |
||
| 328 | if (preg_match('/^TICKET_MSG/', $actionstatic->code)) { |
||
| 329 | $labeltype = $langs->trans("Message"); |
||
| 330 | } else { |
||
| 331 | if (!empty($arraylist[$labeltype])) { |
||
| 332 | $labeltype = $arraylist[$labeltype]; |
||
| 333 | } |
||
| 334 | if ($actionstatic->type_code == 'AC_OTH_AUTO' && ($actionstatic->type_code != $actionstatic->code) && $labeltype && !empty($arraylist[$actionstatic->code])) { |
||
| 335 | $labeltype .= ' - ' . $arraylist[$actionstatic->code]; // Use code in priority on type_code |
||
| 336 | } |
||
| 337 | } |
||
| 338 | print '<td class="tdoverflowmax100" title="' . dol_escape_htmltag($labeltype) . '">'; |
||
| 339 | print $actioncomm->getTypePicto(); |
||
| 340 | print $labeltype; |
||
| 341 | print '</td>'; |
||
| 342 | |||
| 343 | // Label |
||
| 344 | print '<td class="tdoverflowmax200">'; |
||
| 345 | print $actioncomm->getNomUrl(0); |
||
| 346 | print '</td>'; |
||
| 347 | |||
| 348 | // Status |
||
| 349 | print '<td class="right">'; |
||
| 350 | print $actioncomm->getLibStatut(3); |
||
| 351 | print '</td>'; |
||
| 352 | print '</tr>'; |
||
| 353 | |||
| 354 | $cursorevent++; |
||
| 355 | } |
||
| 356 | } else { |
||
| 357 | print '<tr class="oddeven"><td colspan="6"><span class="opacitymedium">' . $langs->trans("None") . '</span></td></tr>'; |
||
| 358 | } |
||
| 359 | |||
| 360 | if ($max && $num > $max) { |
||
| 361 | print '<tr class="oddeven"><td colspan="6"><span class="opacitymedium">' . $langs->trans("More") . '...</span></td></tr>'; |
||
| 362 | } |
||
| 363 | |||
| 364 | print '</table>'; |
||
| 365 | print '</div>'; |
||
| 366 | } |
||
| 367 | |||
| 368 | return $num; |
||
| 369 | } |
||
| 370 | |||
| 371 | |||
| 372 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 373 | /** |
||
| 374 | * Output html select list of type of event |
||
| 375 | * |
||
| 376 | * @param array|string $selected Type pre-selected (can be 'manual', 'auto' or 'AC_xxx'). Can be an array too. |
||
| 377 | * @param string $htmlname Name of select field |
||
| 378 | * @param string $excludetype A type to exclude ('systemauto', 'system', '') |
||
| 379 | * @param integer $onlyautoornot 1=Group all type AC_XXX into 1 line AC_MANUAL. 0=Keep details of type, -1=Keep details and add a combined line "All manual", -2=Combined line is disabled (not implemented yet) |
||
| 380 | * @param int $hideinfohelp 1=Do not show info help, 0=Show, -1=Show+Add info to tell how to set default value |
||
| 381 | * @param int $multiselect 1=Allow multiselect of action type |
||
| 382 | * @param int $nooutput 1=No output |
||
| 383 | * @param string $morecss More css to add to SELECT component. |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | public function select_type_actions($selected = '', $htmlname = 'actioncode', $excludetype = '', $onlyautoornot = 0, $hideinfohelp = 0, $multiselect = 0, $nooutput = 0, $morecss = 'minwidth300') |
||
| 449 | } |
||
| 450 | } |
||
| 451 |