PageRenderTime 44ms CodeModel.GetById 21ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/hrm/establishment/card.php

http://github.com/Dolibarr/dolibarr
PHP | 439 lines | 301 code | 76 blank | 62 comment | 38 complexity | fe478d9a5590d017e7f0c38e1b2ec7f2 MD5 | raw 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
 18/**
 19 *  \file       	htdocs/hrm/establishment/card.php
 20 *  \brief      	Page to show an establishment
 21 */
 22require '../../main.inc.php';
 23require_once DOL_DOCUMENT_ROOT.'/core/lib/hrm.lib.php';
 24require_once DOL_DOCUMENT_ROOT.'/hrm/class/establishment.class.php';
 25require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
 26require_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
 32if (!$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
 42static $tmpstatus2label = array(
 43		'0'=>'CloseEtablishment',
 44        '1'=>'OpenEtablishment'
 45);
 46$status2label = array('');
 47foreach ($tmpstatus2label as $key => $val) $status2label[$key] = $langs->trans($val);
 48
 49$object = new Establishment($db);
 50
 51// Load object
 52include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once
 53
 54
 55/*
 56 * Actions
 57 */
 58
 59if ($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
 73elseif ($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			$object->entity = GETPOST('entity', 'int') > 0 ?GETPOST('entity', 'int') : $conf->entity;
 96
 97			$id = $object->create($user);
 98
 99            if ($id > 0)
100            {
101                header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
102                exit;
103			}
104			else
105			{
106				setEventMessages($object->error, $object->errors, 'errors');
107			}
108        }
109        else
110        {
111            $action = 'create';
112        }
113    }
114    else
115    {
116        header("Location: ../admin/admin_establishment.php");
117        exit;
118    }
119}
120
121// Update record
122elseif ($action == 'update')
123{
124	$error = 0;
125
126	if (!$cancel) {
127		$name = GETPOST('name', 'alpha');
128		if (empty($name)) {
129			setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Name')), null, 'errors');
130			$error++;
131		}
132
133		if (empty($error))
134		{
135			$object->name = GETPOST('name', 'alpha');
136			$object->address = GETPOST('address', 'alpha');
137			$object->zip 			= GETPOST('zipcode', 'alpha');
138			$object->town			= GETPOST('town', 'alpha');
139			$object->country_id     = GETPOST('country_id', 'int');
140			$object->fk_user_mod = $user->id;
141			$object->status         = GETPOST('status', 'int');
142			$object->entity         = GETPOST('entity', 'int') > 0 ?GETPOST('entity', 'int') : $conf->entity;
143
144			$result = $object->update($user);
145
146            if ($result > 0)
147            {
148                header("Location: ".$_SERVER["PHP_SELF"]."?id=".$_POST['id']);
149                exit;
150            }
151			else
152			{
153				setEventMessages($object->error, $object->errors, 'errors');
154			}
155		}
156	} else {
157        header("Location: ".$_SERVER["PHP_SELF"]."?id=".$_POST['id']);
158        exit;
159	}
160}
161
162/*
163 * View
164 */
165
166llxHeader();
167
168$form = new Form($db);
169$formcompany = new FormCompany($db);
170
171/*
172 * Action create
173 */
174if ($action == 'create')
175{
176    print load_fiche_titre($langs->trans("NewEstablishment"));
177
178    print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
179    print '<input type="hidden" name="token" value="'.newToken().'">';
180    print '<input type="hidden" name="action" value="add">';
181
182	dol_fiche_head();
183
184    print '<table class="border centpercent">';
185
186	// Name
187	print '<tr>';
188	print '<td>'.$form->editfieldkey('Name', 'name', '', $object, 0, 'string', '', 1).'</td>';
189	print '<td><input name="name" id="name" size="32" value="'.GETPOST("name", "alpha").'"></td>';
190	print '</tr>';
191
192	// Parent
193	print '<tr>';
194	print '<td>'.$form->editfieldkey('Parent', 'entity', '', $object, 0, 'string', '', 1).'</td>';
195	print '<td class="maxwidthonsmartphone">';
196	print $form->selectEstablishments(GETPOST('entity', 'int') > 0 ?GETPOST('entity', 'int') : $conf->entity, 'entity', 1);
197	print '</td>';
198	print '</tr>';
199
200	// Address
201	print '<tr>';
202	print '<td>'.$form->editfieldkey('Address', 'address', '', $object, 0).'</td>';
203	print '<td>';
204	print '<input name="address" id="address" class="qutrevingtpercent" value="'.GETPOST('address', 'alpha').'">';
205	print '</td>';
206	print '</tr>';
207
208	// Zipcode
209	print '<tr>';
210	print '<td>'.$form->editfieldkey('Zip', 'zipcode', '', $object, 0).'</td>';
211	print '<td>';
212	print $formcompany->select_ziptown(
213		GETPOST('zipcode', 'alpha'),
214		'zipcode',
215		array(
216			'town',
217			'selectcountry_id'
218		),
219		6
220	);
221	print '</td>';
222	print '</tr>';
223
224	// Town
225	print '<tr>';
226	print '<td>'.$form->editfieldkey('Town', 'town', '', $object, 0).'</td>';
227	print '<td>';
228	print $formcompany->select_ziptown(GETPOSTISSET('town') ? GETPOST('town', 'alpha') : $object->town, 'town', array(
229			'zipcode',
230			'selectcountry_id'
231	));
232	print '</td>';
233	print '</tr>';
234
235	// Country
236	print '<tr>';
237	print '<td>'.$form->editfieldkey('Country', 'selectcountry_id', '', $object, 0).'</td>';
238	print '<td class="maxwidthonsmartphone">';
239	print $form->select_country(GETPOSTISSET('country_id') ? GETPOST('country_id', 'int') : ($object->country_id ? $object->country_id : $mysoc->country_id), 'country_id');
240		if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
241	print '</td>';
242	print '</tr>';
243
244	// Status
245    print '<tr>';
246    print '<td>'.$form->editfieldkey('Status', 'status', '', $object, 0, 'string', '', 1).'</td>';
247	print '<td>';
248	print $form->selectarray('status', $status2label, GETPOSTISSET('status') ? GETPOST('status', 'alpha') : 1);
249    print '</td></tr>';
250
251    print '</table>';
252
253	dol_fiche_end();
254
255    print '<div class="center">';
256	print '<input class="button" type="submit" value="'.$langs->trans("Save").'">';
257	print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
258    print '<input class="button" type="submit" name="cancel" value="'.$langs->trans("Cancel").'">';
259	print '</div>';
260
261    print '</form>';
262}
263
264// Part to edit record
265if (($id || $ref) && $action == 'edit')
266{
267    $result = $object->fetch($id);
268    if ($result > 0)
269    {
270        $head = establishment_prepare_head($object);
271
272        if ($action == 'edit')
273        {
274        	dol_fiche_head($head, 'card', $langs->trans("Establishment"), 0, 'building');
275
276        	print '<form name="update" action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
277            print '<input type="hidden" name="token" value="'.newToken().'">';
278            print '<input type="hidden" name="action" value="update">';
279            print '<input type="hidden" name="id" value="'.$id.'">';
280
281            print '<table class="border centpercent">';
282
283            // Ref
284            print "<tr>";
285            print '<td width="25%">'.$langs->trans("Ref").'</td><td>';
286            print $object->id;
287            print '</td></tr>';
288
289            // Name
290            print '<tr><td>'.$form->editfieldkey('Name', 'name', '', $object, 0, 'string', '', 1).'</td><td>';
291            print '<input name="name" id="name" class="flat" size="32" value="'.$object->name.'">';
292            print '</td></tr>';
293
294			// Parent
295            print '<tr><td>'.$form->editfieldkey('Parent', 'entity', '', $object, 0, 'string', '', 1).'</td>';
296			print '<td class="maxwidthonsmartphone">';
297			print $form->selectEstablishments($object->entity > 0 ? $object->entity : $conf->entity, 'entity', 1);
298            print '</td></tr>';
299
300			// Address
301			print '<tr><td>'.$form->editfieldkey('Address', 'address', '', $object, 0).'</td>';
302			print '<td>';
303			print '<input name="address" id="address" size="32" value="'.$object->address.'">';
304			print '</td></tr>';
305
306			// Zipcode / Town
307			print '<tr><td>'.$form->editfieldkey('Zip', 'zipcode', '', $object, 0).'</td><td>';
308			print $formcompany->select_ziptown($object->zip, 'zipcode', array(
309					'town',
310					'selectcountry_id'
311			), 6).'</tr>';
312			print '<tr><td>'.$form->editfieldkey('Town', 'town', '', $object, 0).'</td><td>';
313			print $formcompany->select_ziptown($object->town, 'town', array(
314					'zipcode',
315					'selectcountry_id'
316			)).'</td></tr>';
317
318			// Country
319			print '<tr><td>'.$form->editfieldkey('Country', 'selectcountry_id', '', $object, 0).'</td>';
320			print '<td class="maxwidthonsmartphone">';
321			print $form->select_country($object->country_id, 'country_id');
322				if ($user->admin) print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
323			print '</td>';
324			print '</tr>';
325
326			// Status
327			print '<tr><td>'.$form->editfieldkey('Status', 'status', '', $object, 0, 'string', '', 1).'</td><td>';
328			print $form->selectarray('status', $status2label, $object->status);
329			print '</td></tr>';
330
331            print '</table>';
332
333			dol_fiche_end();
334
335            print '<div class="center">';
336			print '<input type="submit" class="button" value="'.$langs->trans("Save").'">';
337			print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
338            print '<input type="submit" name="cancel" class="button" value="'.$langs->trans("Cancel").'">';
339            print '</div>';
340
341            print '</form>';
342        }
343    }
344    else dol_print_error($db);
345}
346
347if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create')))
348{
349    $res = $object->fetch_optionals();
350
351    $head = establishment_prepare_head($object);
352    dol_fiche_head($head, 'card', $langs->trans("Establishment"), -1, 'building');
353
354    // Confirmation to delete
355    if ($action == 'delete')
356    {
357        print $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$id, $langs->trans("DeleteEstablishment"), $langs->trans("ConfirmDeleteEstablishment"), "confirm_delete");
358    }
359
360
361	// Object card
362	// ------------------------------------------------------------
363
364	$linkback = '<a href="'.DOL_URL_ROOT.'/hrm/admin/admin_establishment.php'.(!empty($socid) ? '?socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
365
366	$morehtmlref = '<div class="refidno">';
367    $morehtmlref .= '</div>';
368
369    dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'id', $morehtmlref);
370
371
372    print '<div class="fichecenter">';
373    //print '<div class="fichehalfleft">';
374    print '<div class="underbanner clearboth"></div>';
375    print '<table class="border centpercent">'."\n";
376
377	// Name
378	print '<tr>';
379	print '<td class="titlefield">'.$langs->trans("Name").'</td>';
380	print '<td>'.$object->name.'</td>';
381	print '</tr>';
382
383	// Parent
384	print '<tr>';
385	print '<td class="titlefield">'.$langs->trans("Parent").'</td>';
386	print '<td>'.$object->getNomUrlParent($object->entity).'</td>';
387	print '</tr>';
388
389	// Address
390	print '<tr>';
391	print '<td>'.$langs->trans("Address").'</td>';
392	print '<td>'.$object->address.'</td>';
393	print '</tr>';
394
395	// Zipcode
396	print '<tr>';
397	print '<td>'.$langs->trans("Zipcode").'</td>';
398	print '<td>'.$object->zip.'</td>';
399	print '</tr>';
400
401	// Town
402	print '<tr>';
403	print '<td>'.$langs->trans("Town").'</td>';
404	print '<td>'.$object->town.'</td>';
405	print '</tr>';
406
407	// Country
408	print '<tr>';
409	print '<td>'.$langs->trans("Country").'</td>';
410	print '<td>';
411	if ($object->country_id > 0)
412	{
413		$img = picto_from_langcode($object->country_code);
414		print $img ? $img.' ' : '';
415		print getCountry($object->getCountryCode(), 0, $db);
416	}
417	print '</td>';
418	print '</tr>';
419
420    print '</table>';
421    print '</div>';
422
423    print '<div class="clearboth"></div><br>';
424
425    dol_fiche_end();
426
427    /*
428     * Barre d'actions
429    */
430
431    print '<div class="tabsAction">';
432    print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit&id='.$id.'">'.$langs->trans('Modify').'</a>';
433	print '<a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?action=delete&id='.$id.'">'.$langs->trans('Delete').'</a>';
434    print '</div>';
435}
436
437// End of page
438llxFooter();
439$db->close();