dolibarr  16.0.5
card.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2013-2014 Jean-François Ferry <jfefe@aternatik.fr>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16  */
17 
25 require '../main.inc.php';
26 require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
27 require_once DOL_DOCUMENT_ROOT.'/resource/class/dolresource.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/resource/class/html.formresource.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/resource.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
32 
33 // Load translation files required by the page
34 $langs->loadLangs(array('resource', 'companies', 'other', 'main'));
35 
36 // Get parameters
37 $id = GETPOST('id', 'int');
38 $action = GETPOST('action', 'aZ09');
39 $cancel = GETPOST('cancel', 'alpha');
40 $ref = GETPOST('ref', 'alpha');
41 $description = GETPOST('description', 'restricthtml');
42 $confirm = GETPOST('confirm', 'aZ09');
43 $fk_code_type_resource = GETPOST('fk_code_type_resource', 'alpha');
44 $country_id = GETPOST('country_id', 'int');
45 
46 // Protection if external user
47 if ($user->socid > 0) {
49 }
50 
51 $object = new Dolresource($db);
52 
53 $extrafields = new ExtraFields($db);
54 
55 // fetch optionals attributes and labels
56 $extrafields->fetch_name_optionals_label($object->table_element);
57 
58 // Load object
59 include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
60 
61 
62 $result = restrictedArea($user, 'resource', $object->id, 'resource');
63 
64 $permissiontoadd = $user->rights->resource->write; // Used by the include of actions_addupdatedelete.inc.php and actions_lineupdown.inc.php
65 
66 
67 
68 /*
69  * Actions
70  */
71 
72 $hookmanager->initHooks(array('resource', 'resource_card', 'globalcard'));
73 $parameters = array('resource_id'=>$id);
74 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
75 if ($reshook < 0) {
76  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
77 }
78 
79 if (empty($reshook)) {
80  if ($cancel) {
81  if (!empty($backtopage)) {
82  header("Location: ".$backtopage);
83  exit;
84  }
85  if ($action == 'add') {
86  header("Location: ".DOL_URL_ROOT.'/resource/list.php');
87  exit;
88  }
89  $action = '';
90  }
91 
92  if ($action == 'add' && $user->rights->resource->write) {
93  if (!$cancel) {
94  $error = '';
95 
96  if (empty($ref)) {
97  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
98  $action = 'create';
99  } else {
100  $object->ref = $ref;
101  $object->description = $description;
102  $object->fk_code_type_resource = $fk_code_type_resource;
103  $object->country_id = $country_id;
104 
105  // Fill array 'array_options' with data from add form
106  $ret = $extrafields->setOptionalsFromPost(null, $object);
107  if ($ret < 0) {
108  $error++;
109  }
110 
111  $result = $object->create($user);
112  if ($result > 0) {
113  // Creation OK
114  setEventMessages($langs->trans('ResourceCreatedWithSuccess'), null, 'mesgs');
115  Header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
116  exit;
117  } else {
118  // Creation KO
119  setEventMessages($object->error, $object->errors, 'errors');
120  $action = 'create';
121  }
122  }
123  } else {
124  Header("Location: list.php");
125  exit;
126  }
127  }
128 
129  if ($action == 'update' && !$cancel && $user->rights->resource->write) {
130  $error = 0;
131 
132  if (empty($ref)) {
133  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
134  $error++;
135  }
136 
137  if (!$error) {
138  $res = $object->fetch($id);
139  if ($res > 0) {
140  $object->ref = $ref;
141  $object->description = $description;
142  $object->fk_code_type_resource = $fk_code_type_resource;
143  $object->country_id = $country_id;
144 
145  // Fill array 'array_options' with data from add form
146  $ret = $extrafields->setOptionalsFromPost(null, $object, '@GETPOSTISSET');
147  if ($ret < 0) {
148  $error++;
149  }
150 
151  $result = $object->update($user);
152  if ($result > 0) {
153  Header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
154  exit;
155  } else {
156  setEventMessages($object->error, $object->errors, 'errors');
157  $error++;
158  }
159  } else {
160  setEventMessages($object->error, $object->errors, 'errors');
161  $error++;
162  }
163  }
164 
165  if ($error) {
166  $action = 'edit';
167  }
168  }
169 
170  if ($action == 'confirm_delete_resource' && $user->rights->resource->delete && $confirm === 'yes') {
171  $res = $object->fetch($id);
172  if ($res > 0) {
173  $result = $object->delete($id);
174 
175  if ($result >= 0) {
176  setEventMessages($langs->trans('RessourceSuccessfullyDeleted'), null, 'mesgs');
177  Header('Location: '.DOL_URL_ROOT.'/resource/list.php');
178  exit;
179  } else {
180  setEventMessages($object->error, $object->errors, 'errors');
181  }
182  } else {
183  setEventMessages($object->error, $object->errors, 'errors');
184  }
185  }
186 }
187 
188 
189 /*
190  * View
191  */
192 
193 $title = $langs->trans($action == 'create' ? 'AddResource' : 'ResourceSingular');
194 llxHeader('', $title, '');
195 
196 $form = new Form($db);
197 $formresource = new FormResource($db);
198 
199 if ($action == 'create' || $object->fetch($id, $ref) > 0) {
200  if ($action == 'create') {
201  print load_fiche_titre($title, '', 'object_resource');
202  print dol_get_fiche_head('');
203  } else {
204  $head = resource_prepare_head($object);
205  print dol_get_fiche_head($head, 'resource', $title, -1, 'resource');
206  }
207 
208  if ($action == 'create' || $action == 'edit') {
209  if (!$user->rights->resource->write) {
210  accessforbidden('', 0, 1);
211  }
212 
213  // Create/Edit object
214 
215  print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="POST">';
216  print '<input type="hidden" name="token" value="'.newToken().'">';
217  print '<input type="hidden" name="action" value="'.($action == "create" ? "add" : "update").'">';
218 
219  print '<table class="border centpercent">';
220 
221  // Ref
222  print '<tr><td class="titlefieldcreate fieldrequired">'.$langs->trans("ResourceFormLabel_ref").'</td>';
223  print '<td><input class="minwidth200" name="ref" value="'.($ref ? $ref : $object->ref).'" autofocus="autofocus"></td></tr>';
224 
225  // Type
226  print '<tr><td>'.$langs->trans("ResourceType").'</td>';
227  print '<td>';
228  $ret = $formresource->select_types_resource($object->fk_code_type_resource, 'fk_code_type_resource', '', 2);
229  print '</td></tr>';
230 
231  // Description
232  print '<tr><td class="tdtop">'.$langs->trans("Description").'</td>';
233  print '<td>';
234  require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
235  $doleditor = new DolEditor('description', ($description ? $description : $object->description), '', '200', 'dolibarr_notes', false);
236  $doleditor->Create();
237  print '</td></tr>';
238 
239  // Origin country
240  print '<tr><td>'.$langs->trans("CountryOrigin").'</td><td>';
241  print $form->select_country($object->country_id, 'country_id');
242  if ($user->admin) {
243  print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
244  }
245  print '</td></tr>';
246 
247  // Other attributes
248  $parameters = array('objectsrc' => $objectsrc);
249  $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
250  print $hookmanager->resPrint;
251  if (empty($reshook)) {
252  print $object->showOptionals($extrafields, 'edit');
253  }
254 
255  print '</table>';
256 
257  print dol_get_fiche_end();
258 
259  $button_label = ($action == "create" ? "Create" : "Modify");
260  print $form->buttonsSaveCancel($button_label);
261 
262  print '</div>';
263 
264  print '</form>';
265  } else {
266  $formconfirm = '';
267 
268  // Confirm deleting resource line
269  if ($action == 'delete') {
270  $formconfirm = $form->formconfirm("card.php?&id=".$object->id, $langs->trans("DeleteResource"), $langs->trans("ConfirmDeleteResource"), "confirm_delete_resource", '', '', 1);
271  }
272 
273  // Print form confirm
274  print $formconfirm;
275 
276 
277  $linkback = '<a href="'.DOL_URL_ROOT.'/resource/list.php?restore_lastsearch_values=1'.(!empty($socid) ? '&id='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
278 
279 
280  $morehtmlref = '<div class="refidno">';
281  $morehtmlref .= '</div>';
282 
283 
284  dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
285 
286 
287  print '<div class="fichecenter">';
288  print '<div class="underbanner clearboth"></div>';
289 
290  /*---------------------------------------
291  * View object
292  */
293  print '<table class="border tableforfield centpercent">';
294 
295  // Resource type
296  print '<tr>';
297  print '<td class="titlefield">'.$langs->trans("ResourceType").'</td>';
298  print '<td>';
299  print $object->type_label;
300  print '</td>';
301  print '</tr>';
302 
303  // Description
304  print '<tr>';
305  print '<td>'.$langs->trans("ResourceFormLabel_description").'</td>';
306  print '<td>';
307  print $object->description;
308  print '</td>';
309  print '</tr>';
310 
311  // Origin country code
312  print '<tr>';
313  print '<td>'.$langs->trans("CountryOrigin").'</td>';
314  print '<td>';
315  print getCountry($object->country_id, 0, $db);
316  print '</td>';
317 
318 
319  // Other attributes
320  include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
321 
322  print '</tr>';
323 
324  print '</table>';
325 
326  print '</div>';
327 
328  print '<div class="clearboth"></div><br>';
329 
330  print dol_get_fiche_end();
331  }
332 
333 
334  /*
335  * Boutons actions
336  */
337  print '<div class="tabsAction">';
338  $parameters = array();
339  $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been
340  // modified by hook
341  if (empty($reshook)) {
342  if ($action != "create" && $action != "edit") {
343  // Edit resource
344  if ($user->rights->resource->write) {
345  print '<div class="inline-block divButAction">';
346  print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&action=edit&token='.newToken().'" class="butAction">'.$langs->trans('Modify').'</a>';
347  print '</div>';
348  }
349  }
350  if ($action != "delete" && $action != "create" && $action != "edit") {
351  // Delete resource
352  if ($user->rights->resource->delete) {
353  print '<div class="inline-block divButAction">';
354  print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&action=delete&token='.newToken().'" class="butActionDelete">'.$langs->trans('Delete').'</a>';
355  print '</div>';
356  }
357  }
358  }
359  print '</div>';
360 } else {
361  dol_print_error();
362 }
363 
364 // End of page
365 llxFooter();
366 $db->close();
restrictedArea
restrictedArea($user, $features, $objectid=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.
Definition: security.lib.php:234
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
dol_banner_tab
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
Definition: functions.lib.php:2046
$formconfirm
$formconfirm
if ($action == 'delbookkeepingyear') {
Definition: listbyaccount.php:576
Dolresource
DAO Resource object.
Definition: dolresource.class.php:30
resource_prepare_head
resource_prepare_head($object)
Prepare head for tabs.
Definition: resource.lib.php:32
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
info_admin
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='')
Show information for admin users or standard users.
Definition: functions.lib.php:4800
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:10878
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
ExtraFields
Class to manage standard extra fields.
Definition: extrafields.class.php:39
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
$parameters
$parameters
Actions.
Definition: card.php:78
getCountry
getCountry($searchkey, $withcode='', $dbtouse=0, $outputlangs='', $entconv=1, $searchlabel='')
Return country label, code or id from an id, code or label.
Definition: company.lib.php:489
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
FormResource
Class to manage forms for the module resource.
Definition: html.formresource.class.php:36
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59
DolEditor
Class to manage a WYSIWYG editor.
Definition: doleditor.class.php:30