dolibarr 24.0.0-beta
html.formcardwebportal.class.php
1<?php
2/* Copyright (C) 2023-2024 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2023-2024 Lionel Vessiller <lvessiller@easya.solutions>
4 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
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
28require_once DOL_DOCUMENT_ROOT . '/webportal/class/html.formwebportal.class.php';
29require_once DOL_DOCUMENT_ROOT . '/webportal/class/webportalfieldsmanager.class.php';
30require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php';
31
37{
41 public $action = '';
42
46 public $backtopage = '';
47
51 public $backtopageforcancel = '';
52
56 public $cancel = '';
57
61 public $db;
62
66 public $elementEn = '';
67
71 public $form;
72
76 public $id;
77
81 public $object;
82
86 public $extrafields;
87
91 public $permissiontoread = 0;
92
96 public $permissiontoadd = 0;
97
101 public $permissiontodelete = 0;
102
106 public $permissionnote = 0;
107
111 public $permissiondellink = 0;
112
116 public $ref;
117
121 public $titleKey = '';
122
126 public $titleDescKey = '';
127
131 public $key_for_break = '';
132
136 public $modal = false;
137
141 public $fieldsmanager;
142
146 public $updateType = 1;
147
148
154 public function __construct($db)
155 {
156 $this->db = $db;
157 $this->form = new FormWebPortal($this->db);
158 $this->fieldsmanager = new WebPortalFieldsManager($this->db, $this->form);
159 }
160
173 public function init($elementEn, $id = 0, $permissiontoread = 0, $permissiontoadd = 0, $permissiontodelete = 0, $permissionnote = 0, $permissiondellink = 0)
174 {
175 global $hookmanager, $langs;
176
177 $elementEnUpper = strtoupper($elementEn);
178 $objectclass = 'WebPortal' . ucfirst($elementEn);
179
180 $elementCardAccess = getDolGlobalString('WEBPORTAL_' . $elementEnUpper . '_CARD_ACCESS', 'hidden');
181 if ($elementCardAccess == 'hidden' || $id <= 0) {
183 }
184
186
187 // load module libraries
188 dol_include_once('/webportal/class/webportal' . $elementEn . '.class.php');
189
190 // Load translation files required by the page
191 $langs->loadLangs(array('website', 'other'));
192
193 // Get parameters
194 //$id = $id > 0 ? $id : GETPOST('id', 'int');
195 $ref = GETPOST('ref', 'alpha');
196 $action = GETPOST('action', 'aZ09');
197 $confirm = GETPOST('confirm', 'alpha');
198 $cancel = GETPOST('cancel');
199 $contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'webportal' . $elementEn . 'card'; // To manage different context of search
200 $backtopage = GETPOST('backtopage', 'alpha'); // if not set, a default page will be used
201 $backtopageforcancel = GETPOST('backtopageforcancel', 'alpha'); // if not set, $backtopage will be used
202 $modal = GETPOSTINT('modal') == 1;
203
204 // Initialize a technical objects
205 $object = new $objectclass($this->db);
206 '@phan-var-force CommonObject $object';
208 //$extrafields = new ExtraFields($db);
209 $hookmanager->initHooks(array('webportal' . $elementEn . 'card', 'globalcard')); // Note that conf->hooks_modules contains array
210
211 // Fetch optionals attributes and labels
212 $this->extrafields = new ExtraFields($this->db);
213 $this->extrafields->fetch_name_optionals_label($object->table_element);
214 //$search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
215
216 if (empty($action) && empty($id) && empty($ref)) {
217 $action = 'view';
218 }
219
220 // Load object
221 if (($id > 0 || (!empty($ref) && !in_array($action, array('create', 'createtask', 'add')))) && (empty($cancel) || $id > 0)) {
222 if (($id > 0 && is_numeric((string) $id)) || !empty($ref)) { // To discard case when id is list of ids like '1,2,3...'
223 if ($object->element == 'usergroup') {
224 $ret = $object->fetch($id, (empty($ref) ? '' : $ref), true); // to load $object->members
225 } else {
226 $ret = $object->fetch($id, (empty($ref) ? '' : $ref));
227 }
228 if ($ret > 0) {
229 $object->fetch_thirdparty();
230 $id = $object->id;
231 } else {
232 if (empty($object->error) && !count($object->errors)) {
233 if ($ret < 0) { // if $ret == 0, it means not found.
234 $context->setEventMessages('Fetch on object (type ' . get_class($object) . ') return an error without filling $object->error nor $object->errors', null, 'errors');
235 }
236 } else {
237 $context->setEventMessages($object->error, $object->errors, 'errors');
238 }
239 $action = '';
240 }
241 }
242 }
243
244 // Security check (enable the most restrictive one)
245 if (!isModEnabled('webportal')) {
247 }
248 if (!$permissiontoread) {
250 }
251
252 // set form card
253 $this->action = $action;
254 $this->backtopage = $backtopage;
255 $this->backtopageforcancel = $backtopageforcancel;
256 $this->cancel = $cancel;
257 $this->elementEn = $elementEn;
258 $this->id = (int) $id;
259 $this->object = $object;
260 $this->permissiontoread = $permissiontoread;
261 $this->permissiontoadd = $permissiontoadd;
262 $this->permissiontodelete = $permissiontodelete;
263 $this->permissionnote = $permissionnote;
264 $this->permissiondellink = $permissiondellink;
265 $this->titleKey = $objectclass . 'CardTitle';
266 $this->ref = $ref;
267 $this->modal = $modal;
268 }
269
275 public function doActions()
276 {
277 // initialize
278 $action = $this->action;
279 $backtopage = $this->backtopage;
280 $backtopageforcancel = $this->backtopageforcancel;
281 $cancel = $this->cancel;
282 $elementEn = $this->elementEn;
283 $id = $this->id;
284 $object = $this->object;
285 //$permissiontoread = $this->permissiontoread;
286 $permissiontoadd = $this->permissiontoadd;
287
289
290 $backurlforlist = $context->getControllerUrl('default');
291 $noback = 1;
292
293 if (empty($backtopage) || ($cancel && empty($id))) {
294 if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) {
295 $backtopage = $context->getControllerUrl($elementEn . 'card');
296 }
297 }
298
299 // Action to cancel record
300 if ($cancel) {
301 if (!empty($backtopageforcancel)) {
302 header("Location: " . $backtopageforcancel);
303 exit;
304 } elseif (!empty($backtopage)) {
305 header("Location: " . $backtopage);
306 exit;
307 }
308 $action = '';
309 }
310
311 // Action to update record
312 if ($action == 'update' && !empty($permissiontoadd)) {
313 $error = 0;
314
315 $result = $this->fieldsmanager->setFieldValuesFromPost($object, $this->extrafields, '', '', 'edit');
316 if ($result < 0) {
317 $error++;
318 $context->setEventMessages($this->fieldsmanager->error, $this->fieldsmanager->errors, 'errors');
319 } else {
320 if ($this->updateType == 2) {
321 $result = $object->update($this->id, $context->logged_user);
322 } else { // $this->updateType == 1
323 $result = $object->update($context->logged_user);
324 }
325 if ($result < 0) {
326 $error++;
327 $context->setEventMessages($object->error, $object->errors, 'errors');
328 }
329 }
330
331 if ($error) {
332 $action = 'edit';
333 } else {
334 $action = 'view';
335 $urltogo = $backtopage ? str_replace('__ID__', (string) $object->id, $backtopage) : $backurlforlist;
336 $urltogo = preg_replace('/--IDFORBACKTOPAGE--/', (string) $object->id, $urltogo); // New method to autoselect project after a New on another form object creation
337 if ($urltogo && empty($noback)) {
338 header("Location: " . $urltogo);
339 exit;
340 }
341 }
342 }
343
344 $this->object = $object;
345 $this->action = $action;
346 }
347}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
$object ref
Definition info.php:90
static getInstance()
Singleton method to create one instance of this object.
Class to manage standard extra fields.
Class to manage generation of HTML components Only common components for WebPortal must be here.
Class to manage generation of HTML components Only common components for WebPortal must be here.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
$context
@method int call_trigger(string $triggerName, ?User $user)
Definition logout.php:42
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.