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