dolibarr  16.0.1
card.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2015 Alexandre Spangaro <aspangaro@open-dsi.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 
23 require '../../main.inc.php';
24 require_once DOL_DOCUMENT_ROOT.'/core/lib/hrm.lib.php';
25 require_once DOL_DOCUMENT_ROOT.'/hrm/class/establishment.class.php';
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
28 
29 // Load translation files required by the page
30 $langs->loadLangs(array('admin', 'hrm'));
31 
32 $error = 0;
33 
34 $action = GETPOST('action', 'aZ09');
35 $cancel = GETPOST('cancel', 'alpha');
36 $confirm = GETPOST('confirm', 'alpha');
37 $id = GETPOST('id', 'int');
38 
39 // List of status
40 static $tmpstatus2label = array(
41  '0'=>'CloseEtablishment',
42  '1'=>'OpenEtablishment'
43 );
44 $status2label = array('');
45 foreach ($tmpstatus2label as $key => $val) {
46  $status2label[$key] = $langs->trans($val);
47 }
48 
49 $object = new Establishment($db);
50 
51 // Load object
52 include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once
53 
54 $permissiontoread = $user->admin;
55 $permissiontoadd = $user->admin; // Used by the include of actions_addupdatedelete.inc.php
56 $upload_dir = $conf->hrm->multidir_output[isset($object->entity) ? $object->entity : 1];
57 
58 // Security check - Protection if external user
59 //if ($user->socid > 0) accessforbidden();
60 //if ($user->socid > 0) $socid = $user->socid;
61 //$isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0);
62 //restrictedArea($user, $object->element, $object->id, '', '', 'fk_soc', 'rowid', 0);
63 if (empty($conf->hrm->enabled)) accessforbidden();
64 if (empty($permissiontoread)) accessforbidden();
65 
66 
67 /*
68  * Actions
69  */
70 
71 if ($action == 'confirm_delete' && $confirm == "yes") {
72  $result = $object->delete($id);
73  if ($result >= 0) {
74  header("Location: ../admin/admin_establishment.php");
75  exit;
76  } else {
77  setEventMessages($object->error, $object->errors, 'errors');
78  }
79 } elseif ($action == 'add') {
80  if (!$cancel) {
81  $error = 0;
82 
83  $object->label = GETPOST('label', 'alpha');
84  if (empty($object->label)) {
85  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Label")), null, 'errors');
86  $error++;
87  }
88 
89  if (empty($error)) {
90  $object->address = GETPOST('address', 'alpha');
91  $object->zip = GETPOST('zipcode', 'alpha');
92  $object->town = GETPOST('town', 'alpha');
93  $object->country_id = GETPOST("country_id", 'int');
94  $object->status = GETPOST('status', 'int');
95  $object->fk_user_author = $user->id;
96  $object->datec = dol_now();
97  $object->entity = GETPOST('entity', 'int') > 0 ?GETPOST('entity', 'int') : $conf->entity;
98 
99  $id = $object->create($user);
100 
101  if ($id > 0) {
102  header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
103  exit;
104  } else {
105  setEventMessages($object->error, $object->errors, 'errors');
106  }
107  } else {
108  $action = 'create';
109  }
110  } else {
111  header("Location: ../admin/admin_establishment.php");
112  exit;
113  }
114 } elseif ($action == 'update') {
115  // Update record
116  $error = 0;
117 
118  if (!$cancel) {
119  $name = GETPOST('label', 'alpha');
120  if (empty($name)) {
121  setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Label')), null, 'errors');
122  $error++;
123  }
124 
125  if (empty($error)) {
126  $object->label = GETPOST('label', 'alphanohtml');
127  $object->address = GETPOST('address', 'alpha');
128  $object->zip = GETPOST('zipcode', 'alpha');
129  $object->town = GETPOST('town', 'alpha');
130  $object->country_id = GETPOST('country_id', 'int');
131  $object->fk_user_mod = $user->id;
132  $object->status = GETPOST('status', 'int');
133  $object->entity = GETPOST('entity', 'int') > 0 ?GETPOST('entity', 'int') : $conf->entity;
134 
135  $result = $object->update($user);
136 
137  if ($result > 0) {
138  header("Location: ".$_SERVER["PHP_SELF"]."?id=".GETPOST('id', 'int'));
139  exit;
140  } else {
141  setEventMessages($object->error, $object->errors, 'errors');
142  }
143  }
144  } else {
145  header("Location: ".$_SERVER["PHP_SELF"]."?id=".GETPOST('id', 'int'));
146  exit;
147  }
148 }
149 
150 /*
151  * View
152  */
153 
154 llxHeader();
155 
156 $form = new Form($db);
157 $formcompany = new FormCompany($db);
158 
159 /*
160  * Action create
161  */
162 if ($action == 'create') {
163  print load_fiche_titre($langs->trans("NewEstablishment"));
164 
165  print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
166  print '<input type="hidden" name="token" value="'.newToken().'">';
167  print '<input type="hidden" name="action" value="add">';
168 
169  print dol_get_fiche_head();
170 
171  print '<table class="border centpercent">';
172 
173  // Name
174  print '<tr>';
175  print '<td>'.$form->editfieldkey('Label', 'label', '', $object, 0, 'string', '', 1).'</td>';
176  print '<td><input name="label" id="label" value="'.GETPOST("label", "alphanohtml").'" autofocus></td>';
177  print '</tr>';
178 
179  // Entity
180  /*
181  if (! empty($conf->multicompany->enabled)) {
182  print '<tr>';
183  print '<td>'.$form->editfieldkey('Parent', 'entity', '', $object, 0, 'string', '', 1).'</td>';
184  print '<td class="maxwidthonsmartphone">';
185  print $form->selectEstablishments(GETPOST('entity', 'int') > 0 ?GETPOST('entity', 'int') : $conf->entity, 'entity', 1);
186  print '</td>';
187  print '</tr>';
188  } */
189 
190  // Address
191  print '<tr>';
192  print '<td>'.$form->editfieldkey('Address', 'address', '', $object, 0).'</td>';
193  print '<td>';
194  print '<input name="address" id="address" class="qutrevingtpercent" value="'.GETPOST('address', 'alphanohtml').'">';
195  print '</td>';
196  print '</tr>';
197 
198  // Zipcode
199  print '<tr>';
200  print '<td>'.$form->editfieldkey('Zip', 'zipcode', '', $object, 0).'</td>';
201  print '<td>';
202  print $formcompany->select_ziptown(
203  GETPOST('zipcode', 'alpha'),
204  'zipcode',
205  array(
206  'town',
207  'selectcountry_id'
208  ),
209  6
210  );
211  print '</td>';
212  print '</tr>';
213 
214  // Town
215  print '<tr>';
216  print '<td>'.$form->editfieldkey('Town', 'town', '', $object, 0).'</td>';
217  print '<td>';
218  print $formcompany->select_ziptown(GETPOSTISSET('town') ? GETPOST('town', 'alpha') : $object->town, 'town', array(
219  'zipcode',
220  'selectcountry_id'
221  ));
222  print '</td>';
223  print '</tr>';
224 
225  // Country
226  print '<tr>';
227  print '<td>'.$form->editfieldkey('Country', 'selectcountry_id', '', $object, 0).'</td>';
228  print '<td class="maxwidthonsmartphone">';
229  print $form->select_country(GETPOSTISSET('country_id') ? GETPOST('country_id', 'int') : ($object->country_id ? $object->country_id : $mysoc->country_id), 'country_id');
230  if ($user->admin) {
231  print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
232  }
233  print '</td>';
234  print '</tr>';
235 
236  // Status
237  print '<tr>';
238  print '<td>'.$form->editfieldkey('Status', 'status', '', $object, 0, 'string', '', 1).'</td>';
239  print '<td>';
240  print $form->selectarray('status', $status2label, GETPOSTISSET('status') ? GETPOST('status', 'alpha') : 1);
241  print '</td></tr>';
242 
243  print '</table>';
244 
245  print dol_get_fiche_end();
246 
247  print '<div class="center">';
248  print '<input class="button button-save" type="submit" value="'.$langs->trans("Save").'">';
249  print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
250  print '<input class="button button-cancel" type="submit" name="cancel" value="'.$langs->trans("Cancel").'">';
251  print '</div>';
252 
253  print '</form>';
254 }
255 
256 // Part to edit record
257 if (($id || $ref) && $action == 'edit') {
258  $result = $object->fetch($id);
259  if ($result > 0) {
260  $head = establishment_prepare_head($object);
261 
262  if ($action == 'edit') {
263  print dol_get_fiche_head($head, 'card', $langs->trans("Establishment"), 0, $object->picto);
264 
265  print '<form name="update" action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
266  print '<input type="hidden" name="token" value="'.newToken().'">';
267  print '<input type="hidden" name="action" value="update">';
268  print '<input type="hidden" name="id" value="'.$id.'">';
269 
270  print '<table class="border centpercent">';
271 
272  // Ref
273  print "<tr>";
274  print '<td class="titlefield">'.$langs->trans("Ref").'</td><td>';
275  print $object->id;
276  print '</td></tr>';
277 
278  // Name
279  print '<tr><td>'.$form->editfieldkey('Label', 'label', '', $object, 0, 'string', '', 1).'</td><td>';
280  print '<input name="label" id="label" class="flat" value="'.$object->label.'">';
281  print '</td></tr>';
282 
283  // Entity
284  /*
285  if (! empty($conf->multicompany->enabled)) {
286  print '<tr><td>'.$form->editfieldkey('Parent', 'entity', '', $object, 0, 'string', '', 1).'</td>';
287  print '<td class="maxwidthonsmartphone">';
288  print $object->entity > 0 ? $object->entity : $conf->entity;
289  print '</td></tr>';
290  }*/
291 
292  // Address
293  print '<tr><td>'.$form->editfieldkey('Address', 'address', '', $object, 0).'</td>';
294  print '<td>';
295  print '<input name="address" id="address" value="'.$object->address.'">';
296  print '</td></tr>';
297 
298  // Zipcode / Town
299  print '<tr><td>'.$form->editfieldkey('Zip', 'zipcode', '', $object, 0).'</td><td>';
300  print $formcompany->select_ziptown($object->zip, 'zipcode', array(
301  'town',
302  'selectcountry_id'
303  ), 6).'</tr>';
304  print '<tr><td>'.$form->editfieldkey('Town', 'town', '', $object, 0).'</td><td>';
305  print $formcompany->select_ziptown($object->town, 'town', array(
306  'zipcode',
307  'selectcountry_id'
308  )).'</td></tr>';
309 
310  // Country
311  print '<tr><td>'.$form->editfieldkey('Country', 'selectcountry_id', '', $object, 0).'</td>';
312  print '<td class="maxwidthonsmartphone">';
313  print $form->select_country($object->country_id, 'country_id');
314  if ($user->admin) {
315  print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
316  }
317  print '</td>';
318  print '</tr>';
319 
320  // Status
321  print '<tr><td>'.$form->editfieldkey('Status', 'status', '', $object, 0, 'string', '', 1).'</td><td>';
322  print $form->selectarray('status', $status2label, $object->status);
323  print '</td></tr>';
324 
325  print '</table>';
326 
327  print dol_get_fiche_end();
328 
329  print $form->buttonsSaveCancel();
330 
331  print '</form>';
332  }
333  } else {
334  dol_print_error($db);
335  }
336 }
337 
338 if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
339  $res = $object->fetch_optionals();
340 
341  $head = establishment_prepare_head($object);
342  print dol_get_fiche_head($head, 'card', $langs->trans("Establishment"), -1, $object->picto);
343 
344  // Confirmation to delete
345  if ($action == 'delete') {
346  print $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$id, $langs->trans("DeleteEstablishment"), $langs->trans("ConfirmDeleteEstablishment"), "confirm_delete");
347  }
348 
349 
350  // Object card
351  // ------------------------------------------------------------
352 
353  $linkback = '<a href="'.DOL_URL_ROOT.'/hrm/admin/admin_establishment.php'.(!empty($socid) ? '?socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
354 
355  $morehtmlref = '<div class="refidno">';
356  $morehtmlref .= '</div>';
357 
358  dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'id', $morehtmlref);
359 
360 
361  print '<div class="fichecenter">';
362  //print '<div class="fichehalfleft">';
363  print '<div class="underbanner clearboth"></div>';
364  print '<table class="border centpercent">'."\n";
365 
366  // Name
367  print '<tr>';
368  print '<td class="titlefield">'.$langs->trans("Label").'</td>';
369  print '<td>'.$object->label.'</td>';
370  print '</tr>';
371 
372  // Entity
373  /*
374  if ($conf->multicompany->enabled) {
375  print '<tr>';
376  print '<td class="titlefield">'.$langs->trans("Entity").'</td>';
377  print '<td>'.$object->entity.'</td>';
378  print '</tr>';
379  }*/
380 
381  // Address
382  print '<tr>';
383  print '<td>'.$langs->trans("Address").'</td>';
384  print '<td>'.$object->address.'</td>';
385  print '</tr>';
386 
387  // Zipcode
388  print '<tr>';
389  print '<td>'.$langs->trans("Zip").'</td>';
390  print '<td>'.$object->zip.'</td>';
391  print '</tr>';
392 
393  // Town
394  print '<tr>';
395  print '<td>'.$langs->trans("Town").'</td>';
396  print '<td>'.$object->town.'</td>';
397  print '</tr>';
398 
399  // Country
400  print '<tr>';
401  print '<td>'.$langs->trans("Country").'</td>';
402  print '<td>';
403  if ($object->country_id > 0) {
404  $img = picto_from_langcode($object->country_code);
405  print $img ? $img.' ' : '';
406  print getCountry($object->getCountryCode(), 0, $db);
407  }
408  print '</td>';
409  print '</tr>';
410 
411  print '</table>';
412  print '</div>';
413 
414  print '<div class="clearboth"></div><br>';
415 
416  print dol_get_fiche_end();
417 
418  /*
419  * Action bar
420  */
421  print '<div class="tabsAction">';
422  print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit&token='.newToken().'&id='.$id.'">'.$langs->trans('Modify').'</a>';
423  print '<a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?action=delete&token='.newToken().'&id='.$id.'">'.$langs->trans('Delete').'</a>';
424  print '</div>';
425 }
426 
427 // End of page
428 llxFooter();
429 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
if($cancel &&!$id) if($action== 'add'&&!$cancel) if($action== 'delete') if($id) $form
Actions.
Definition: card.php:142
dol_now($mode= 'auto')
Return date for now.
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
Class to build HTML component for third parties management Only common components are here...
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.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form...
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin= '1', $morecss= 'hideonsmartphone', $textfordropdown= '')
Show information for admin users or standard users.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
Class to manage establishments.
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 ...
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
establishment_prepare_head($object)
Return head table for establishment tabs screen.
Definition: hrm.lib.php:30
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.
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:73
picto_from_langcode($codelang, $moreatt= '', $notitlealt=0)
Return img flag of country for a language code or country code.
getCountry($searchkey, $withcode= '', $dbtouse=0, $outputlangs= '', $entconv=1, $searchlabel= '')
Return country label, code or id from an id, code or label.