| Conditions | 35 |
| Paths | > 20000 |
| Total Lines | 184 |
| Code Lines | 96 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 49 | public function index(bool $executeActions = true): bool |
||
| 50 | { |
||
| 51 | global $conf; |
||
| 52 | global $db; |
||
| 53 | global $user; |
||
| 54 | global $hookmanager; |
||
| 55 | global $user; |
||
| 56 | global $menumanager; |
||
| 57 | global $langs; |
||
| 58 | |||
| 59 | |||
| 60 | // Load translation files required by the page |
||
| 61 | $langs->loadLangs(["agenda", "other"]); |
||
| 62 | |||
| 63 | // Get parameters |
||
| 64 | $id = GETPOSTINT('id'); |
||
| 65 | $ref = GETPOST('ref', 'alpha'); |
||
| 66 | $lineid = GETPOSTINT('lineid'); |
||
| 67 | |||
| 68 | $action = GETPOST('action', 'aZ09'); |
||
| 69 | $confirm = GETPOST('confirm', 'alpha'); |
||
| 70 | $cancel = GETPOST('cancel', 'aZ09'); |
||
| 71 | $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : str_replace('_', '', basename(dirname(__FILE__)) . basename(__FILE__, '.php')); // To manage different context of search |
||
| 72 | $backtopage = GETPOST('backtopage', 'alpha'); |
||
| 73 | $backtopageforcancel = GETPOST('backtopageforcancel', 'alpha'); |
||
| 74 | $dol_openinpopup = GETPOST('dol_openinpopup', 'aZ09'); |
||
| 75 | |||
| 76 | // Initialize technical objects |
||
| 77 | $object = new Availabilities($db); |
||
| 78 | $extrafields = new ExtraFields($db); |
||
| 79 | $diroutputmassaction = $conf->bookcal->dir_output . '/temp/massgeneration/' . $user->id; |
||
| 80 | $hookmanager->initHooks(['availabilitiescard', 'globalcard']); // Note that conf->hooks_modules contains array |
||
| 81 | |||
| 82 | // Fetch optionals attributes and labels |
||
| 83 | $extrafields->fetch_name_optionals_label($object->table_element); |
||
| 84 | |||
| 85 | $search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_'); |
||
| 86 | |||
| 87 | // Initialize array of search criteria |
||
| 88 | $search_all = GETPOST("search_all", 'alpha'); |
||
| 89 | $search = []; |
||
| 90 | foreach ($object->fields as $key => $val) { |
||
| 91 | if (GETPOST('search_' . $key, 'alpha')) { |
||
| 92 | $search[$key] = GETPOST('search_' . $key, 'alpha'); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | if (empty($action) && empty($id) && empty($ref)) { |
||
| 97 | $action = 'view'; |
||
| 98 | } |
||
| 99 | |||
| 100 | // Load object |
||
| 101 | include DOL_DOCUMENT_ROOT . '/core/actions_fetchobject.inc.php'; // Must be include, not include_once. |
||
| 102 | |||
| 103 | // There is several ways to check permission. |
||
| 104 | // Set $enablepermissioncheck to 1 to enable a minimum low level of checks |
||
| 105 | $enablepermissioncheck = 0; |
||
| 106 | if ($enablepermissioncheck) { |
||
| 107 | $permissiontoread = $user->hasRight('bookcal', 'availabilities', 'read'); |
||
| 108 | $permissiontoadd = $user->hasRight('bookcal', 'availabilities', 'write'); // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php |
||
| 109 | $permissiontodelete = $user->hasRight('bookcal', 'availabilities', 'delete') || ($permissiontoadd && isset($object->status) && $object->status == $object::STATUS_DRAFT); |
||
| 110 | $permissionnote = $user->hasRight('bookcal', 'availabilities', 'write'); // Used by the include of actions_setnotes.inc.php |
||
| 111 | $permissiondellink = $user->hasRight('bookcal', 'availabilities', 'write'); // Used by the include of actions_dellink.inc.php |
||
| 112 | } else { |
||
| 113 | $permissiontoread = 1; |
||
| 114 | $permissiontoadd = 1; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php |
||
| 115 | $permissiontodelete = 1; |
||
| 116 | $permissionnote = 1; |
||
| 117 | $permissiondellink = 1; |
||
| 118 | } |
||
| 119 | |||
| 120 | $upload_dir = $conf->bookcal->multidir_output[isset($object->entity) ? $object->entity : 1] . '/availabilities'; |
||
| 121 | |||
| 122 | // Security check (enable the most restrictive one) |
||
| 123 | //if ($user->socid > 0) accessforbidden(); |
||
| 124 | //if ($user->socid > 0) $socid = $user->socid; |
||
| 125 | //$isdraft = (isset($object->status) && ($object->status == $object::STATUS_DRAFT) ? 1 : 0); |
||
| 126 | //restrictedArea($user, $object->element, $object->id, $object->table_element, '', 'fk_soc', 'rowid', $isdraft); |
||
| 127 | if (!isModEnabled('bookcal')) { |
||
| 128 | accessforbidden(); |
||
| 129 | } |
||
| 130 | if (!$permissiontoread) { |
||
| 131 | accessforbidden(); |
||
| 132 | } |
||
| 133 | |||
| 134 | |||
| 135 | /* |
||
| 136 | * Actions |
||
| 137 | */ |
||
| 138 | |||
| 139 | $parameters = []; |
||
| 140 | $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks |
||
| 141 | if ($reshook < 0) { |
||
| 142 | setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); |
||
| 143 | } |
||
| 144 | |||
| 145 | if (empty($reshook)) { |
||
| 146 | $error = 0; |
||
| 147 | |||
| 148 | $backurlforlist = dol_buildpath('/bookcal/availabilities_list.php', 1); |
||
| 149 | |||
| 150 | if (empty($backtopage) || ($cancel && empty($id))) { |
||
| 151 | if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) { |
||
| 152 | if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) { |
||
| 153 | $backtopage = $backurlforlist; |
||
| 154 | } else { |
||
| 155 | $backtopage = dol_buildpath('/bookcal/availabilities_card.php', 1) . '?id=' . ((!empty($id) && $id > 0) ? $id : '__ID__'); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | $triggermodname = 'BOOKCAL_AVAILABILITIES_MODIFY'; // Name of trigger action code to execute when we modify record |
||
| 161 | |||
| 162 | |||
| 163 | $startday = GETPOST('startday', 'int'); |
||
| 164 | $startmonth = GETPOST('startmonth', 'int'); |
||
| 165 | $startyear = GETPOST('startyear', 'int'); |
||
| 166 | $starthour = GETPOST('startHour', 'int'); |
||
| 167 | |||
| 168 | $dateStartTimestamp = dol_mktime($starthour, 0, 0, $startmonth, $startday, $startyear); |
||
| 169 | |||
| 170 | $endday = GETPOST('endday', 'int'); |
||
| 171 | $endmonth = GETPOST('endmonth', 'int'); |
||
| 172 | $endyear = GETPOST('endyear', 'int'); |
||
| 173 | $endhour = GETPOST('endHour', 'int'); |
||
| 174 | |||
| 175 | |||
| 176 | $dateEndTimestamp = dol_mktime($endhour, 0, 0, $endmonth, $endday, $endyear); |
||
| 177 | |||
| 178 | // check hours |
||
| 179 | if ($starthour > $endhour) { |
||
| 180 | if ($dateStartTimestamp === $dateEndTimestamp) { |
||
| 181 | $error++; |
||
| 182 | setEventMessages($langs->trans("ErrorEndTimeMustBeGreaterThanStartTime"), null, 'errors'); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | // check date |
||
| 187 | if ($dateStartTimestamp > $dateEndTimestamp) { |
||
| 188 | $error++; |
||
| 189 | setEventMessages($langs->trans("ErrorIncoherentDates"), null, 'errors'); |
||
| 190 | } |
||
| 191 | |||
| 192 | |||
| 193 | // Actions cancel, add, update, update_extras, confirm_validate, confirm_delete, confirm_deleteline, confirm_clone, confirm_close, confirm_setdraft, confirm_reopen |
||
| 194 | include DOL_DOCUMENT_ROOT . '/core/actions_addupdatedelete.inc.php'; |
||
| 195 | |||
| 196 | // Actions when linking object each other |
||
| 197 | include DOL_DOCUMENT_ROOT . '/core/actions_dellink.inc.php'; |
||
| 198 | |||
| 199 | // Actions when printing a doc from card |
||
| 200 | include DOL_DOCUMENT_ROOT . '/core/actions_printing.inc.php'; |
||
| 201 | |||
| 202 | // Action to move up and down lines of object |
||
| 203 | //include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php'; |
||
| 204 | |||
| 205 | // Action to build doc |
||
| 206 | include DOL_DOCUMENT_ROOT . '/core/actions_builddoc.inc.php'; |
||
| 207 | |||
| 208 | if ($action == 'set_thirdparty' && $permissiontoadd) { |
||
| 209 | $object->setValueFrom('fk_soc', GETPOSTINT('fk_soc'), '', '', 'date', '', $user, $triggermodname); |
||
| 210 | } |
||
| 211 | if ($action == 'classin' && $permissiontoadd) { |
||
| 212 | $object->setProject(GETPOSTINT('projectid')); |
||
| 213 | } |
||
| 214 | |||
| 215 | // Actions to send emails |
||
| 216 | $triggersendname = 'BOOKCAL_AVAILABILITIES_SENTBYMAIL'; |
||
| 217 | $autocopy = 'MAIN_MAIL_AUTOCOPY_AVAILABILITIES_TO'; |
||
| 218 | $trackid = 'availabilities' . $object->id; |
||
| 219 | include DOL_DOCUMENT_ROOT . '/core/actions_sendmails.inc.php'; |
||
| 220 | } |
||
| 221 | |||
| 222 | |||
| 223 | /* |
||
| 224 | * View |
||
| 225 | * |
||
| 226 | * Put here all code to build page |
||
| 227 | */ |
||
| 228 | require_once realpath(BASE_PATH . '/../Dolibarr/Modules/BookCal/Views/availabilities_card.php'); |
||
| 229 | |||
| 230 | $db->close(); |
||
| 231 | |||
| 232 | return true; |
||
| 233 | |||
| 236 |