dolibarr  13.0.2
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)
48 {
50 }
51 
52 if (!$user->rights->resource->read)
53 {
55 }
56 
57 $object = new Dolresource($db);
58 
59 $extrafields = new ExtraFields($db);
60 
61 // fetch optionals attributes and labels
62 $extrafields->fetch_name_optionals_label($object->table_element);
63 
64 
65 
66 /*
67  * Actions
68  */
69 
70 $hookmanager->initHooks(array('resource', 'resource_card', 'globalcard'));
71 $parameters = array('resource_id'=>$id);
72 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
73 if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
74 
75 if (empty($reshook))
76 {
77  if ($cancel)
78  {
79  if (!empty($backtopage))
80  {
81  header("Location: ".$backtopage);
82  exit;
83  }
84  if ($action == 'add')
85  {
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  {
94  if (!$cancel)
95  {
96  $error = '';
97 
98  if (empty($ref))
99  {
100  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
101  $action = 'create';
102  } else {
103  $object->ref = $ref;
104  $object->description = $description;
105  $object->fk_code_type_resource = $fk_code_type_resource;
106  $object->country_id = $country_id;
107 
108  // Fill array 'array_options' with data from add form
109  $ret = $extrafields->setOptionalsFromPost(null, $object);
110  if ($ret < 0) $error++;
111 
112  $result = $object->create($user);
113  if ($result > 0)
114  {
115  // Creation OK
116  setEventMessages($langs->trans('ResourceCreatedWithSuccess'), null, 'mesgs');
117  Header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
118  exit;
119  } else {
120  // Creation KO
121  setEventMessages($object->error, $object->errors, 'errors');
122  $action = 'create';
123  }
124  }
125  } else {
126  Header("Location: list.php");
127  exit;
128  }
129  }
130 
131  if ($action == 'update' && !$cancel && $user->rights->resource->write)
132  {
133  $error = 0;
134 
135  if (empty($ref))
136  {
137  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Ref")), null, 'errors');
138  $error++;
139  }
140 
141  if (!$error)
142  {
143  $res = $object->fetch($id);
144  if ($res > 0)
145  {
146  $object->ref = $ref;
147  $object->description = $description;
148  $object->fk_code_type_resource = $fk_code_type_resource;
149  $object->country_id = $country_id;
150 
151  // Fill array 'array_options' with data from add form
152  $ret = $extrafields->setOptionalsFromPost(null, $object);
153  if ($ret < 0) {
154  $error++;
155  }
156 
157  $result = $object->update($user);
158  if ($result > 0)
159  {
160  Header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);
161  exit;
162  } else {
163  setEventMessages($object->error, $object->errors, 'errors');
164  $error++;
165  }
166  } else {
167  setEventMessages($object->error, $object->errors, 'errors');
168  $error++;
169  }
170  }
171 
172  if ($error)
173  {
174  $action = 'edit';
175  }
176  }
177 
178  if ($action == 'confirm_delete_resource' && $user->rights->resource->delete && $confirm === 'yes')
179  {
180  $res = $object->fetch($id);
181  if ($res > 0)
182  {
183  $result = $object->delete($id);
184 
185  if ($result >= 0)
186  {
187  setEventMessages($langs->trans('RessourceSuccessfullyDeleted'), null, 'mesgs');
188  Header('Location: '.DOL_URL_ROOT.'/resource/list.php');
189  exit;
190  } else {
191  setEventMessages($object->error, $object->errors, 'errors');
192  }
193  } else {
194  setEventMessages($object->error, $object->errors, 'errors');
195  }
196  }
197 }
198 
199 
200 /*
201  * View
202  */
203 
204 $title = $langs->trans($action == 'create' ? 'AddResource' : 'ResourceSingular');
205 llxHeader('', $title, '');
206 
207 $form = new Form($db);
208 $formresource = new FormResource($db);
209 
210 if ($action == 'create' || $object->fetch($id, $ref) > 0)
211 {
212  if ($action == 'create')
213  {
214  print load_fiche_titre($title, '', 'object_resource');
216  } else {
217  $head = resource_prepare_head($object);
218  print dol_get_fiche_head($head, 'resource', $title, -1, 'resource');
219  }
220 
221  if ($action == 'create' || $action == 'edit')
222  {
223  if (!$user->rights->resource->write) accessforbidden('', 0, 1);
224 
225  // Create/Edit object
226 
227  print '<form action="'.$_SERVER["PHP_SELF"].'?id='.$id.'" method="POST">';
228  print '<input type="hidden" name="token" value="'.newToken().'">';
229  print '<input type="hidden" name="action" value="'.($action == "create" ? "add" : "update").'">';
230 
231  print '<table class="border centpercent">';
232 
233  // Ref
234  print '<tr><td class="titlefieldcreate fieldrequired">'.$langs->trans("ResourceFormLabel_ref").'</td>';
235  print '<td><input class="minwidth200" name="ref" value="'.($ref ? $ref : $object->ref).'" autofocus="autofocus"></td></tr>';
236 
237  // Type
238  print '<tr><td>'.$langs->trans("ResourceType").'</td>';
239  print '<td>';
240  $ret = $formresource->select_types_resource($object->fk_code_type_resource, 'fk_code_type_resource', '', 2);
241  print '</td></tr>';
242 
243  // Description
244  print '<tr><td class="tdtop">'.$langs->trans("Description").'</td>';
245  print '<td>';
246  require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
247  $doleditor = new DolEditor('description', ($description ? $description : $object->description), '', '200', 'dolibarr_notes', false);
248  $doleditor->Create();
249  print '</td></tr>';
250 
251  // Origin country
252  print '<tr><td>'.$langs->trans("CountryOrigin").'</td><td>';
253  print $form->select_country($object->country_id, 'country_id');
254  if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
255  print '</td></tr>';
256 
257  // Other attributes
258  $parameters = array('objectsrc' => $objectsrc);
259  $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
260  print $hookmanager->resPrint;
261  if (empty($reshook))
262  {
263  print $object->showOptionals($extrafields, 'edit');
264  }
265 
266  print '</table>';
267 
268  print dol_get_fiche_end();
269 
270  print '<div class="center">';
271  print '<input type="submit" class="button" name="save" value="'.$langs->trans($action == "create" ? "Create" : "Modify").'">';
272  print ' &nbsp; &nbsp; ';
273  print '<input type="submit" class="button button-cancel" name="cancel" value="'.$langs->trans("Cancel").'">';
274  print '</div>';
275  print '</div>';
276 
277  print '</form>';
278  } else {
279  $formconfirm = '';
280 
281  // Confirm deleting resource line
282  if ($action == 'delete')
283  {
284  $formconfirm = $form->formconfirm("card.php?&id=".$object->id, $langs->trans("DeleteResource"), $langs->trans("ConfirmDeleteResource"), "confirm_delete_resource", '', '', 1);
285  }
286 
287  // Print form confirm
288  print $formconfirm;
289 
290 
291  $linkback = '<a href="'.DOL_URL_ROOT.'/resource/list.php?restore_lastsearch_values=1'.(!empty($socid) ? '&id='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
292 
293 
294  $morehtmlref = '<div class="refidno">';
295  $morehtmlref .= '</div>';
296 
297 
298  dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
299 
300 
301  print '<div class="fichecenter">';
302  print '<div class="underbanner clearboth"></div>';
303 
304  /*---------------------------------------
305  * View object
306  */
307  print '<table class="border tableforfield centpercent">';
308 
309  // Resource type
310  print '<tr>';
311  print '<td class="titlefield">'.$langs->trans("ResourceType").'</td>';
312  print '<td>';
313  print $object->type_label;
314  print '</td>';
315  print '</tr>';
316 
317  // Description
318  print '<tr>';
319  print '<td>'.$langs->trans("ResourceFormLabel_description").'</td>';
320  print '<td>';
321  print $object->description;
322  print '</td>';
323 
324  // Other attributes
325  include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
326 
327  print '</tr>';
328 
329  // Origin country code
330  print '<tr>';
331  print '<td>'.$langs->trans("CountryOrigin").'</td>';
332  print '<td>';
333  print getCountry($object->country_id, 0, $db);
334  print '</td>';
335  print '</tr>';
336 
337  print '</table>';
338 
339  print '</div>';
340 
341  print '<div class="clearboth"></div><br>';
342 
343  print dol_get_fiche_end();
344  }
345 
346 
347  /*
348  * Boutons actions
349  */
350  print '<div class="tabsAction">';
351  $parameters = array();
352  $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been
353  // modified by hook
354  if (empty($reshook))
355  {
356  if ($action != "create" && $action != "edit")
357  {
358  // Edit resource
359  if ($user->rights->resource->write)
360  {
361  print '<div class="inline-block divButAction">';
362  print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&amp;action=edit" class="butAction">'.$langs->trans('Modify').'</a>';
363  print '</div>';
364  }
365  }
366  if ($action != "delete" && $action != "create" && $action != "edit")
367  {
368  // Delete resource
369  if ($user->rights->resource->delete)
370  {
371  print '<div class="inline-block divButAction">';
372  print '<a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&amp;action=delete&amp;token='.newToken().'" class="butActionDelete">'.$langs->trans('Delete').'</a>';
373  print '</div>';
374  }
375  }
376  }
377  print '</div>';
378 } else {
379  dol_print_error();
380 }
381 
382 // End of page
383 llxFooter();
384 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Classe permettant la gestion des formulaire du module resource.
llxHeader()
Empty header.
Definition: wrapper.php:45
resource_prepare_head($object)
Prepare head for tabs.
Class to manage standard extra fields.
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
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 ...
print $_SERVER["PHP_SELF"]
Edit parameters.
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
Definition: index.php:89
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
newToken()
Return the value of token currently saved into session with name &#39;newtoken&#39;.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Class to manage a WYSIWYG editor.
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.
llxFooter()
Empty footer.
Definition: wrapper.php:59
DAO Resource object.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin= '1', $morecss= '', $textfordropdown= '')
Show information for admin users or standard users.
getCountry($searchkey, $withcode= '', $dbtouse=0, $outputlangs= '', $entconv=1, $searchlabel= '')
Return country label, code or id from an id, code or label.