dolibarr 25.0.0-alpha
card.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2013-2014 Jean-François Ferry <jfefe@aternatik.fr>
3 * Copyright (C) 2023-2024 William Mead <william.mead@manchenumerique.fr>
4 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024-2026 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
28// Load Dolibarr environment
29require '../main.inc.php';
30require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
31require_once DOL_DOCUMENT_ROOT.'/resource/class/dolresource.class.php';
32require_once DOL_DOCUMENT_ROOT.'/resource/class/html.formresource.class.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/resource.lib.php';
34require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
35require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
36
45// Load translation files required by the page
46$langs->loadLangs(array('resource', 'companies', 'other', 'main'));
47
48// Get parameters
49$action = GETPOST('action', 'aZ09');
50$cancel = GETPOST('cancel', 'alpha');
51$backtopage = GETPOST('backtopage', 'alpha');
52
53$id = GETPOSTINT('id');
54$ref = GETPOST('ref', 'alpha');
55$address = GETPOST('address', 'alpha');
56$zip = GETPOST('zipcode', 'alpha');
57$town = GETPOST('town', 'alpha');
58$country_id = GETPOSTINT('country_id');
59$state_id = GETPOSTINT('state_id');
60$description = GETPOST('description', 'restricthtml');
61$phone = GETPOST('phone', 'alpha');
62$email = GETPOST('email', 'alpha');
63$max_users = GETPOSTINT('max_users');
64$url = GETPOST('url', 'alpha');
65$confirm = GETPOST('confirm', 'aZ09');
66$fk_code_type_resource = GETPOST('fk_code_type_resource', 'aZ09');
67
68// Protection if external user
69if ($user->socid > 0) {
71}
72
73$object = new Dolresource($db);
74$extrafields = new ExtraFields($db);
75
76// fetch optionals attributes and labels
77$extrafields->fetch_name_optionals_label($object->table_element);
78
79// Load object
80include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be 'include', not 'include_once'.
81
82$hookmanager->initHooks(array('resource', 'resource_card', 'globalcard'));
83
84$result = restrictedArea($user, 'resource', $object->id, 'resource');
85
86$permissiontoadd = $user->hasRight('resource', 'write'); // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
87$permissiontodelete = $user->hasRight('resource', 'delete');
88
89
90/*
91 * Actions
92 */
93
94$parameters = array('resource_id' => $id);
95$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
96if ($reshook < 0) {
97 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
98}
99
100if (empty($reshook)) {
101 if ($cancel) {
102 if (!empty($backtopage)) {
103 header("Location: ".$backtopage);
104 exit;
105 }
106 if ($action == 'add') { // Test on permission not required here
107 header("Location: ".DOL_URL_ROOT.'/resource/list.php');
108 exit;
109 }
110 $action = '';
111 }
112
113 if ($action == 'add' && $permissiontoadd) {
114 if (!$cancel) {
115 $error = '';
116
117 if (empty($ref)) {
118 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
119 $action = 'create';
120 } else {
121 $object->ref = $ref;
122 $object->address = $address;
123 $object->zip = $zip;
124 $object->town = $town;
125 $object->country_id = $country_id;
126 $object->state_id = $state_id;
127 $object->description = $description;
128 $object->phone = $phone;
129 $object->email = $email;
130 $object->max_users = $max_users;
131 $object->url = $url;
132 $object->fk_code_type_resource = $fk_code_type_resource;
133
134 // Fill array 'array_options' with data from add form
135 $ret = $extrafields->setOptionalsFromPost(null, $object);
136 if ($ret < 0) {
137 $error++;
138 }
139
140 $result = $object->create($user);
141 if ($result > 0) {
142 // Creation OK
143 setEventMessages($langs->trans('ResourceCreatedWithSuccess'), null);
144 header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
145 exit;
146 } else {
147 // Creation KO
148 setEventMessages($object->error, $object->errors, 'errors');
149 $action = 'create';
150 }
151 }
152 } else {
153 header("Location: list.php");
154 exit;
155 }
156 }
157
158 if ($action == 'update' && !$cancel && $permissiontoadd) {
159 $error = 0;
160
161 if (empty($ref)) {
162 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
163 $error++;
164 }
165
166 if (!$error) {
167 $res = $object->fetch($id);
168 if ($res > 0) {
169 $oldref = $object->ref;
170
171 $object->ref = $ref;
172 $object->address = $address;
173 $object->zip = $zip;
174 $object->town = $town;
175 $object->country_id = $country_id;
176 $object->state_id = $state_id;
177 $object->description = $description;
178 $object->phone = $phone;
179 $object->email = $email;
180 $object->max_users = $max_users;
181 $object->url = $url;
182 $object->fk_code_type_resource = $fk_code_type_resource;
183
184 // Fill array 'array_options' with data from add form
185 $ret = $extrafields->setOptionalsFromPost(null, $object, '@GETPOSTISSET');
186 if ($ret < 0) {
187 $error++;
188 }
189
190 $result = $object->update($user);
191 if ($result > 0) {
192 if ($oldref != $ref) {
193 // We renamed the ref so we must change the directory too
194 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
195 $srcdir = $conf->resource->dir_output.'/'.dol_sanitizeFileName($oldref);
196 $destdir = $conf->resource->dir_output.'/'.dol_sanitizeFileName($ref);
197 dol_move_dir($srcdir, $destdir);
198 }
199
200 header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
201 exit;
202 } else {
203 setEventMessages($object->error, $object->errors, 'errors');
204 $error++;
205 }
206 } else {
207 setEventMessages($object->error, $object->errors, 'errors');
208 $error++;
209 }
210 }
211
212 if ($error) {
213 $action = 'edit';
214 }
215 }
216
217 if ($action == 'confirm_delete_resource' && $permissiontodelete && $confirm === 'yes') {
218 $res = $object->fetch($id);
219 if ($res > 0) {
220 $object->oldcopy = dol_clone($object, 2); // @phan-suppress-current-line PhanTypeMismatchProperty
221 $result = $object->delete($user);
222
223 if ($result >= 0) {
224 setEventMessages($langs->trans('RessourceSuccessfullyDeleted'), null);
225 header('Location: '.DOL_URL_ROOT.'/resource/list.php');
226 exit;
227 } else {
228 setEventMessages($object->error, $object->errors, 'errors');
229 }
230 } else {
231 setEventMessages($object->error, $object->errors, 'errors');
232 }
233 }
234}
235
236
237/*
238 * View
239 */
240
241$title = $langs->trans($action == 'create' ? 'AddResource' : 'ResourceSingular');
242$help_url = '';
243llxHeader('', $title, $help_url, '', 0, 0, '', '', '', 'mod-resource page-card');
244
245$form = new Form($db);
246$formresource = new FormResource($db);
247
248if ($action == 'create' || $object->fetch($id, $ref) > 0) {
249 if ($action == 'create') {
250 print load_fiche_titre($title, '', 'object_resource');
251 print dol_get_fiche_head();
252 } else {
253 $head = resource_prepare_head($object);
254 print dol_get_fiche_head($head, 'resource', $title, -1, 'resource', 0, '', '', 0, '', ($action == 'edit' ? 0 : 1));
255 }
256
257 if ($action == 'create' || $action == 'edit') {
258 if (!$user->hasRight('resource', 'write')) {
259 accessforbidden('', 0);
260 }
261
262 if (!empty($conf->use_javascript_ajax)) {
263 print '<script type="text/javascript">';
264 print '$(document).ready(function () {
265 $("#selectcountry_id").change(function() {
266 console.log("selectcountry_id change");
267 document.formresource.action.value="' . ($action == 'create' ? 'create' : 'edit') . '";
268 document.formresource.submit();
269 });
270 });';
271 print '</script>'."\n";
272 }
273
274
275 // Create/Edit object
276
277 print '<form enctype="multipart/form-data" action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="POST" name="formresource">';
278 print '<input type="hidden" name="token" value="'.newToken().'">';
279 print '<input type="hidden" name="action" value="'.($action == "create" ? "add" : "update").'">';
280
281 print '<table class="border centpercent">';
282
283 // Ref
284 print '<tr><td class="titlefieldcreate fieldrequired">'.$langs->trans("ResourceFormLabel_ref").'</td>';
285 print '<td><input class="minwidth200" name="ref" value="'.($ref ?: $object->ref).'" autofocus="autofocus" spellcheck="false"></td></tr>';
286
287 // Type
288 print '<tr><td>'.$langs->trans("ResourceType").'</td>';
289 print '<td>';
290 $formresource->select_types_resource($object->fk_code_type_resource, 'fk_code_type_resource', '', 2, 0, 0, 0, 1, 'minwidth200');
291 print '</td></tr>';
292
293 // Description
294 print '<tr><td class="tdtop">'.$langs->trans("Description").'</td>';
295 print '<td>';
296 require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
297 $doleditor = new DolEditor('description', ($description ?: $object->description), '', 200, 'dolibarr_notes');
298 $doleditor->Create();
299 print '</td></tr>';
300
301 // Address
302 print '<tr><td class="tdtop">'.$form->editfieldkey('Address', 'address', '', $object, 0).'</td>';
303 print '<td><textarea name="address" id="address" class="quatrevingtpercent" rows="3" wrap="soft">';
304 print dol_escape_htmltag(GETPOSTISSET('address') ? GETPOST('address') : $object->address, 0, 1);
305 print '</textarea>';
306 print $form->widgetForTranslation("address", $object, (bool) $permissiontoadd, 'textarea', 'alphanohtml', 'quatrevingtpercent');
307 print '</td></tr>';
308
309 // Zip
310 print '<tr><td>'.$form->editfieldkey('Zip', 'zipcode', '', $object, 0).'</td><td>';
311 print $formresource->select_ziptown(GETPOSTISSET('zipcode') ? GETPOST('zipcode') : $object->zip, 'zipcode', array('town', 'selectcountry_id', 'state_id'), 0, 0, '', 'maxwidth100');
312 print '</td>';
313 print '</tr>';
314
315 // Town
316 print '<tr>';
317 print '<td>'.$form->editfieldkey('Town', 'town', '', $object, 0).'</td><td>';
318 print $formresource->select_ziptown(GETPOSTISSET('town') ? GETPOST('town') : $object->town, 'town', array('zipcode', 'selectcountry_id', 'state_id'));
319 print $form->widgetForTranslation("town", $object, (bool) $permissiontoadd, 'string', 'alphanohtml', 'maxwidth100 quatrevingtpercent');
320 print '</td></tr>';
321
322 // Origin country
323 print '<tr><td>'.$langs->trans("CountryOrigin").'</td><td>';
324 print $form->select_country(GETPOSTISSET('country_id') ? (string) GETPOSTINT('country_id') : (string) $object->country_id, 'country_id');
325 if ($user->admin) {
326 print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
327 }
328 print '</td></tr>';
329
330 // State
331 $countryid = GETPOSTISSET('country_id') ? GETPOSTINT('country_id') : $object->country_id;
332 if (!getDolGlobalString('SOCIETE_DISABLE_STATE') && $countryid > 0) {
333 if ((getDolGlobalInt('MAIN_SHOW_REGION_IN_STATE_SELECT') == 1 || getDolGlobalInt('MAIN_SHOW_REGION_IN_STATE_SELECT') == 2)) {
334 print '<tr><td>'.$form->editfieldkey('Region-State', 'state_id', '', $object, 0).'</td><td class="maxwidthonsmartphone">';
335 } else {
336 print '<tr><td>'.$form->editfieldkey('State', 'state_id', '', $object, 0).'</td><td class="maxwidthonsmartphone">';
337 }
338
339 if ($country_id > 0) {
340 print img_picto('', 'state', 'class="pictofixedwidth"');
341 print $formresource->select_state($countryid, $country_id);
342 } else {
343 print '<span class="opacitymedium">'.$langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')</span>';
344 }
345 print '</td></tr>';
346 }
347
348 // Phone
349 print '<td>'.$form->editfieldkey('Phone', 'phone', '', $object, 0).'</td>';
350 print '<td>';
351 print $form->showPhoneInput($object->phone, 'phone', $countryid, 'object_phoning', 'maxwidth150 widthcentpercentminusx');
352 print '</td>';
353 print '</tr>';
354
355 // Email
356 print '<tr><td>'.$form->editfieldkey('EMail', 'email', '', $object, 0).'</td>';
357 print '<td>';
358 print img_picto('', 'object_email', 'class="pictofixedwidth"');
359 print '<input type="email" name="email" id="email" value="'.(GETPOSTISSET('email') ? GETPOST('email', 'alpha') : $object->email).'" spellcheck="false"></td>';
360 print '</tr>';
361
362 // Max users
363 print '<tr><td>'.$form->editfieldkey('MaxUsers', 'max_users', '', $object, 0, 'string', '', 0, 0, 'id', $langs->trans('MaxUsersResourceDesc')).'</td>';
364 print '<td>';
365 print img_picto('', 'object_user', 'class="pictofixedwidth"');
366 print '<input type="text" class="width75 right" name="max_users" id="max_users" value="'.(GETPOSTISSET('max_users') ? GETPOST('max_users', 'int') : ($object->max_users > 0 ? $object->max_users : '')).'"></td>';
367 print '</tr>';
368
369 // URL
370 print '<tr><td>'.$form->editfieldkey('URL', 'url', '', $object, 0).'</td>';
371 print '<td>';
372 print img_picto('', 'object_url', 'class="pictofixedwidth"');
373 print '<input type="url" class="minwidth300" name="url" id="url" value="'.(GETPOSTISSET('url') ? GETPOST('url', 'alpha') : $object->url).'" spellcheck="false"></td>';
374 print '</tr>';
375
376 // Other attributes
377 $parameters = array();
378 $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
379 print $hookmanager->resPrint;
380 if (empty($reshook)) {
381 print $object->showOptionals($extrafields, 'edit');
382 }
383
384 print '</table>';
385
386 print dol_get_fiche_end();
387
388 $button_label = ($action == "create" ? "Create" : "Modify");
389 print $form->buttonsSaveCancel($button_label);
390
391 print '</div>';
392
393 print '</form>';
394 } else {
395 $formconfirm = '';
396
397 // Confirm deleting resource line
398 if ($action == 'delete' || ($conf->use_javascript_ajax && empty($conf->dol_use_jmobile))) {
399 $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$object->id, $langs->trans("DeleteResource"), $langs->trans("ConfirmDeleteResource"), "confirm_delete_resource", '', 0, "action-delete");
400 }
401
402 // Print form confirm
403 print $formconfirm;
404
405
406 $linkback = '<a href="'.DOL_URL_ROOT.'/resource/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
407
408 dol_banner_tab($object, 'ref', $linkback, 1, 'ref');
409
410
411 print '<div class="fichecenter">';
412 print '<div class="underbanner clearboth"></div>';
413
414 print '<table class="border tableforfield centpercent">';
415
416 // Resource type
417 print '<tr>';
418 print '<td class="titlefield">'.$langs->trans("ResourceType").'</td>';
419 print '<td>';
420 print $object->type_label;
421 print '</td>';
422 print '</tr>';
423
424 // Description
425 print '<tr>';
426 print '<td>'.$langs->trans("ResourceFormLabel_description").'</td>';
427 print '<td>';
428 print $object->description;
429 print '</td>';
430 print '</tr>';
431
432 // Max users
433 print '<tr>';
434 print '<td>'.$langs->trans("MaxUsers").'</td>';
435 print '<td>';
436 print $object->max_users > 0 ? $object->max_users : '';
437 print '</td>';
438 print '</tr>';
439
440 // Other attributes
441 include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
442
443 print '</tr>';
444
445 print '</table>';
446
447 print '</div>';
448
449 print '<div class="clearboth"></div><br>';
450
451 print dol_get_fiche_end();
452 }
453
454
455 /*
456 * Boutons actions
457 */
458 print '<div class="tabsAction">';
459 $parameters = array();
460 $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been
461 // modified by hook
462 if (empty($reshook)) {
463 if ($action != "create" && $action != "edit") {
464 // Edit resource
465 if ($user->hasRight('resource', 'write')) {
466 print '<div class="inline-block divButAction">';
467 print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&action=edit&token='.newToken().'" class="butAction">'.$langs->trans('Modify').'</a>';
468 print '</div>';
469 }
470 }
471 if ($action != "create" && $action != "edit") {
472 $deleteUrl = $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken();
473 $buttonId = 'action-delete-no-ajax';
474 if ($conf->use_javascript_ajax && empty($conf->dol_use_jmobile)) { // We can't use preloaded confirm form with jmobile
475 $deleteUrl = '';
476 $buttonId = 'action-delete';
477 }
478 print dolGetButtonAction($langs->trans("Delete"), $langs->trans("Delete"), 'delete', $deleteUrl, $buttonId, $permissiontodelete, array('attr' => array('class' => 'reposition')))."\n";
479 }
480 }
481 print '</div>';
482} else {
483 dol_print_error();
484}
485
486// End of page
487llxFooter();
488$db->close();
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to manage a WYSIWYG editor.
DAO Resource object.
Class to manage standard extra fields.
Class to manage generation of HTML components Only common components must be here.
Class to manage forms for the module resource.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as p label as s rowid as s nom as s email
Sender: Who sends the email ("Sender" has sent emails on behalf of "From").
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_move_dir($srcdir, $destdir, $overwriteifexists=1, $indexdatabase=1, $renamedircontent=1)
Move a directory into another name.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_clone($srcobject, $native=2)
Create a clone of instance of object (new instance with same value for each properties) With native =...
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
div refaddress div address
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
Definition html.lib.php:540
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition html.lib.php:738
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='', $picto='', $textonpictotooltip='', $cssfordropdown='info_admin')
Show information in HTML for admin users or standard users.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Definition html.lib.php:181
resource_prepare_head($object)
Prepare head for tabs.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $nodie=0, $mode='')
Check permissions of a user to show a page and an object.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.