Passed
Push — main ( 9a81fe...875825 )
by Rafael
41:55
created

BookCalAvailabilitiesCardController   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 97
dl 0
loc 187
rs 9.6
c 0
b 0
f 0
wmc 35

1 Method

Rating   Name   Duplication   Size   Complexity  
F index() 0 184 35
1
<?php
2
3
/* Copyright (C) 2017       Laurent Destailleur     <[email protected]>
4
 * Copyright (C) 2022       Alice Adminson          <[email protected]>
5
 * Copyright (C) 2024       Rafael San José         <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace DoliModules\BookCal\Controller;
22
23
global $conf;
24
global $db;
25
global $user;
26
global $hookmanager;
27
global $user;
28
global $menumanager;
29
global $langs;
30
global $mysoc;
31
32
use DoliCore\Base\DolibarrController;
33
use DoliCore\Lib\ExtraFields;
34
use DoliModules\BookCal\Model\Availabilities;
35
36
/**
37
 *   \file       htdocs/bookcal/availabilities_card.php
38
 *   \ingroup    bookcal
39
 *   \brief      Page to create/edit/view availabilities
40
 */
41
42
// Load Dolibarr environment
43
require BASE_PATH . '/main.inc.php';
44
require_once BASE_PATH . '/../Dolibarr/Modules/BookCal/Lib/BookCalAvailabilities.php';
45
46
class BookCalAvailabilitiesCardController extends DolibarrController
47
{
48
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
234
    }
235
}
236