dolibarr 23.0.3
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 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 $result = $object->delete($user);
221
222 if ($result >= 0) {
223 setEventMessages($langs->trans('RessourceSuccessfullyDeleted'), null);
224 header('Location: '.DOL_URL_ROOT.'/resource/list.php');
225 exit;
226 } else {
227 setEventMessages($object->error, $object->errors, 'errors');
228 }
229 } else {
230 setEventMessages($object->error, $object->errors, 'errors');
231 }
232 }
233}
234
235
236/*
237 * View
238 */
239
240$title = $langs->trans($action == 'create' ? 'AddResource' : 'ResourceSingular');
241$help_url = '';
242llxHeader('', $title, $help_url, '', 0, 0, '', '', '', 'mod-resource page-card');
243
244$form = new Form($db);
245$formresource = new FormResource($db);
246
247if ($action == 'create' || $object->fetch($id, $ref) > 0) {
248 if ($action == 'create') {
249 print load_fiche_titre($title, '', 'object_resource');
250 print dol_get_fiche_head();
251 } else {
252 $head = resource_prepare_head($object);
253 print dol_get_fiche_head($head, 'resource', $title, -1, 'resource');
254 }
255
256 if ($action == 'create' || $action == 'edit') {
257 if (!$user->hasRight('resource', 'write')) {
258 accessforbidden('', 0);
259 }
260
261 if (!empty($conf->use_javascript_ajax)) {
262 print '<script type="text/javascript">';
263 print '$(document).ready(function () {
264 $("#selectcountry_id").change(function() {
265 console.log("selectcountry_id change");
266 document.formresource.action.value="' . ($action == 'create' ? 'create' : 'edit') . '";
267 document.formresource.submit();
268 });
269 });';
270 print '</script>'."\n";
271 }
272
273
274 // Create/Edit object
275
276 print '<form enctype="multipart/form-data" action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="POST" name="formresource">';
277 print '<input type="hidden" name="token" value="'.newToken().'">';
278 print '<input type="hidden" name="action" value="'.($action == "create" ? "add" : "update").'">';
279
280 print '<table class="border centpercent">';
281
282 // Ref
283 print '<tr><td class="titlefieldcreate fieldrequired">'.$langs->trans("ResourceFormLabel_ref").'</td>';
284 print '<td><input class="minwidth200" name="ref" value="'.($ref ?: $object->ref).'" autofocus="autofocus" spellcheck="false"></td></tr>';
285
286 // Type
287 print '<tr><td>'.$langs->trans("ResourceType").'</td>';
288 print '<td>';
289 $formresource->select_types_resource($object->fk_code_type_resource, 'fk_code_type_resource', '', 2, 0, 0, 0, 1, 'minwidth200');
290 print '</td></tr>';
291
292 // Description
293 print '<tr><td class="tdtop">'.$langs->trans("Description").'</td>';
294 print '<td>';
295 require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
296 $doleditor = new DolEditor('description', ($description ?: $object->description), '', 200, 'dolibarr_notes');
297 $doleditor->Create();
298 print '</td></tr>';
299
300 // Address
301 print '<tr><td class="tdtop">'.$form->editfieldkey('Address', 'address', '', $object, 0).'</td>';
302 print '<td><textarea name="address" id="address" class="quatrevingtpercent" rows="3" wrap="soft">';
303 print dol_escape_htmltag(GETPOSTISSET('address') ? GETPOST('address') : $object->address, 0, 1);
304 print '</textarea>';
305 print $form->widgetForTranslation("address", $object, (bool) $permissiontoadd, 'textarea', 'alphanohtml', 'quatrevingtpercent');
306 print '</td></tr>';
307
308 // Zip
309 print '<tr><td>'.$form->editfieldkey('Zip', 'zipcode', '', $object, 0).'</td><td>';
310 print $formresource->select_ziptown(GETPOSTISSET('zipcode') ? GETPOST('zipcode') : $object->zip, 'zipcode', array('town', 'selectcountry_id', 'state_id'), 0, 0, '', 'maxwidth100');
311 print '</td>';
312 print '</tr>';
313
314 // Town
315 print '<tr>';
316 print '<td>'.$form->editfieldkey('Town', 'town', '', $object, 0).'</td><td>';
317 print $formresource->select_ziptown(GETPOSTISSET('town') ? GETPOST('town') : $object->town, 'town', array('zipcode', 'selectcountry_id', 'state_id'));
318 print $form->widgetForTranslation("town", $object, (bool) $permissiontoadd, 'string', 'alphanohtml', 'maxwidth100 quatrevingtpercent');
319 print '</td></tr>';
320
321 // Origin country
322 print '<tr><td>'.$langs->trans("CountryOrigin").'</td><td>';
323 print $form->select_country(GETPOSTISSET('country_id') ? (string) GETPOSTINT('country_id') : (string) $object->country_id, 'country_id');
324 if ($user->admin) {
325 print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
326 }
327 print '</td></tr>';
328
329 // State
330 $countryid = GETPOSTISSET('country_id') ? GETPOSTINT('country_id') : $object->country_id;
331 if (!getDolGlobalString('SOCIETE_DISABLE_STATE') && $countryid > 0) {
332 if ((getDolGlobalInt('MAIN_SHOW_REGION_IN_STATE_SELECT') == 1 || getDolGlobalInt('MAIN_SHOW_REGION_IN_STATE_SELECT') == 2)) {
333 print '<tr><td>'.$form->editfieldkey('Region-State', 'state_id', '', $object, 0).'</td><td class="maxwidthonsmartphone">';
334 } else {
335 print '<tr><td>'.$form->editfieldkey('State', 'state_id', '', $object, 0).'</td><td class="maxwidthonsmartphone">';
336 }
337
338 if ($country_id > 0) {
339 print img_picto('', 'state', 'class="pictofixedwidth"');
340 print $formresource->select_state($countryid, $country_id);
341 } else {
342 print '<span class="opacitymedium">'.$langs->trans("ErrorSetACountryFirst").' ('.$langs->trans("SeeAbove").')</span>';
343 }
344 print '</td></tr>';
345 }
346
347 // Phone
348 print '<td>'.$form->editfieldkey('Phone', 'phone', '', $object, 0).'</td>';
349 print '<td>';
350 print img_picto('', 'object_phoning', 'class="pictofixedwidth"');
351 print '<input type="tel" name="phone" id="phone" value="'.(GETPOSTISSET('phone') ? GETPOST('phone', 'alpha') : $object->phone).'"></td>';
352 print '</tr>';
353
354 // Email
355 print '<tr><td>'.$form->editfieldkey('EMail', 'email', '', $object, 0).'</td>';
356 print '<td>';
357 print img_picto('', 'object_email', 'class="pictofixedwidth"');
358 print '<input type="email" name="email" id="email" value="'.(GETPOSTISSET('email') ? GETPOST('email', 'alpha') : $object->email).'" spellcheck="false"></td>';
359 print '</tr>';
360
361 // Max users
362 print '<tr><td>'.$form->editfieldkey('MaxUsers', 'max_users', '', $object, 0, 'string', '', 0, 0, 'id', $langs->trans('MaxUsersResourceDesc')).'</td>';
363 print '<td>';
364 print img_picto('', 'object_user', 'class="pictofixedwidth"');
365 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>';
366 print '</tr>';
367
368 // URL
369 print '<tr><td>'.$form->editfieldkey('URL', 'url', '', $object, 0).'</td>';
370 print '<td>';
371 print img_picto('', 'object_url', 'class="pictofixedwidth"');
372 print '<input type="url" class="minwidth300" name="url" id="url" value="'.(GETPOSTISSET('url') ? GETPOST('url', 'alpha') : $object->url).'" spellcheck="false"></td>';
373 print '</tr>';
374
375 // Other attributes
376 $parameters = array();
377 $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
378 print $hookmanager->resPrint;
379 if (empty($reshook)) {
380 print $object->showOptionals($extrafields, 'edit');
381 }
382
383 print '</table>';
384
385 print dol_get_fiche_end();
386
387 $button_label = ($action == "create" ? "Create" : "Modify");
388 print $form->buttonsSaveCancel($button_label);
389
390 print '</div>';
391
392 print '</form>';
393 } else {
394 $formconfirm = '';
395
396 // Confirm deleting resource line
397 if ($action == 'delete' || ($conf->use_javascript_ajax && empty($conf->dol_use_jmobile))) {
398 $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$object->id, $langs->trans("DeleteResource"), $langs->trans("ConfirmDeleteResource"), "confirm_delete_resource", '', 0, "action-delete");
399 }
400
401 // Print form confirm
402 print $formconfirm;
403
404
405 $linkback = '<a href="'.DOL_URL_ROOT.'/resource/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
406
407 dol_banner_tab($object, 'ref', $linkback, 1, 'ref');
408
409
410 print '<div class="fichecenter">';
411 print '<div class="underbanner clearboth"></div>';
412
413 print '<table class="border tableforfield centpercent">';
414
415 // Resource type
416 print '<tr>';
417 print '<td class="titlefield">'.$langs->trans("ResourceType").'</td>';
418 print '<td>';
419 print $object->type_label;
420 print '</td>';
421 print '</tr>';
422
423 // Description
424 print '<tr>';
425 print '<td>'.$langs->trans("ResourceFormLabel_description").'</td>';
426 print '<td>';
427 print $object->description;
428 print '</td>';
429 print '</tr>';
430
431 // Max users
432 print '<tr>';
433 print '<td>'.$langs->trans("MaxUsers").'</td>';
434 print '<td>';
435 print $object->max_users > 0 ? $object->max_users : '';
436 print '</td>';
437 print '</tr>';
438
439 // Other attributes
440 include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
441
442 print '</tr>';
443
444 print '</table>';
445
446 print '</div>';
447
448 print '<div class="clearboth"></div><br>';
449
450 print dol_get_fiche_end();
451 }
452
453
454 /*
455 * Boutons actions
456 */
457 print '<div class="tabsAction">';
458 $parameters = array();
459 $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been
460 // modified by hook
461 if (empty($reshook)) {
462 if ($action != "create" && $action != "edit") {
463 // Edit resource
464 if ($user->hasRight('resource', 'write')) {
465 print '<div class="inline-block divButAction">';
466 print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&action=edit&token='.newToken().'" class="butAction">'.$langs->trans('Modify').'</a>';
467 print '</div>';
468 }
469 }
470 if ($action != "create" && $action != "edit") {
471 $deleteUrl = $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken();
472 $buttonId = 'action-delete-no-ajax';
473 if ($conf->use_javascript_ajax && empty($conf->dol_use_jmobile)) { // We can't use preloaded confirm form with jmobile
474 $deleteUrl = '';
475 $buttonId = 'action-delete';
476 }
477 print dolGetButtonAction('', $langs->trans("Delete"), 'delete', $deleteUrl, $buttonId, $permissiontodelete);
478 }
479 }
480 print '</div>';
481} else {
483}
484
485// End of page
486llxFooter();
487$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.
dol_move_dir($srcdir, $destdir, $overwriteifexists=1, $indexdatabase=1, $renamedircontent=1)
Move a directory into another name.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
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'.
dolGetButtonAction($label, $text='', $actionType='default', $url='', $id='', $userRight=1, $params=array())
Function dolGetButtonAction.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='', $picto='')
Show information in HTML for admin users or standard users.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
div refaddress div address
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, $mode=0)
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.