card.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. * \file htdocs/hrm/establishment/card.php
  19. * \brief Page to show an establishment
  20. */
  21. // Load Dolibarr environment
  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. // Load translation files required by the page
  28. $langs->loadLangs(array('admin', 'hrm'));
  29. $error = 0;
  30. $action = GETPOST('action', 'aZ09');
  31. $cancel = GETPOST('cancel', 'alpha');
  32. $confirm = GETPOST('confirm', 'alpha');
  33. $id = GETPOST('id', 'int');
  34. // List of status
  35. static $tmpstatus2label = array(
  36. '0'=>'CloseEtablishment',
  37. '1'=>'OpenEtablishment'
  38. );
  39. $status2label = array('');
  40. foreach ($tmpstatus2label as $key => $val) {
  41. $status2label[$key] = $langs->trans($val);
  42. }
  43. $object = new Establishment($db);
  44. // Load object
  45. include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once
  46. $permissiontoread = $user->admin;
  47. $permissiontoadd = $user->admin; // Used by the include of actions_addupdatedelete.inc.php
  48. $permissiontodelete = $user->admin;
  49. $upload_dir = $conf->hrm->multidir_output[isset($object->entity) ? $object->entity : 1];
  50. // Security check - Protection if external user
  51. //if ($user->socid > 0) accessforbidden();
  52. //if ($user->socid > 0) $socid = $user->socid;
  53. //$isdraft = (($object->status == $object::STATUS_DRAFT) ? 1 : 0);
  54. //restrictedArea($user, $object->element, $object->id, '', '', 'fk_soc', 'rowid', 0);
  55. if (!isModEnabled('hrm')) {
  56. accessforbidden();
  57. }
  58. if (empty($permissiontoread)) {
  59. accessforbidden();
  60. }
  61. /*
  62. * Actions
  63. */
  64. if ($action == 'confirm_delete' && $confirm == "yes") {
  65. $result = $object->delete($id);
  66. if ($result >= 0) {
  67. header("Location: ../admin/admin_establishment.php");
  68. exit;
  69. } else {
  70. setEventMessages($object->error, $object->errors, 'errors');
  71. }
  72. } elseif ($action == 'add') {
  73. if (!$cancel) {
  74. $error = 0;
  75. $object->label = GETPOST('label', 'alpha');
  76. if (empty($object->label)) {
  77. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Label")), null, 'errors');
  78. $error++;
  79. }
  80. if (empty($error)) {
  81. $object->address = GETPOST('address', 'alpha');
  82. $object->zip = GETPOST('zipcode', 'alpha');
  83. $object->town = GETPOST('town', 'alpha');
  84. $object->country_id = GETPOST("country_id", 'int');
  85. $object->status = GETPOST('status', 'int');
  86. $object->fk_user_author = $user->id;
  87. $object->datec = dol_now();
  88. $object->entity = GETPOST('entity', 'int') > 0 ? GETPOST('entity', 'int') : $conf->entity;
  89. $id = $object->create($user);
  90. if ($id > 0) {
  91. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
  92. exit;
  93. } else {
  94. setEventMessages($object->error, $object->errors, 'errors');
  95. }
  96. } else {
  97. $action = 'create';
  98. }
  99. } else {
  100. header("Location: ../admin/admin_establishment.php");
  101. exit;
  102. }
  103. } elseif ($action == 'update') {
  104. // Update record
  105. $error = 0;
  106. if (!$cancel) {
  107. $name = GETPOST('label', 'alpha');
  108. if (empty($name)) {
  109. setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Label')), null, 'errors');
  110. $error++;
  111. }
  112. if (empty($error)) {
  113. $object->label = GETPOST('label', 'alphanohtml');
  114. $object->address = GETPOST('address', 'alpha');
  115. $object->zip = GETPOST('zipcode', 'alpha');
  116. $object->town = GETPOST('town', 'alpha');
  117. $object->country_id = GETPOST('country_id', 'int');
  118. $object->fk_user_mod = $user->id;
  119. $object->status = GETPOST('status', 'int');
  120. $object->entity = GETPOST('entity', 'int') > 0 ? GETPOST('entity', 'int') : $conf->entity;
  121. $result = $object->update($user);
  122. if ($result > 0) {
  123. header("Location: ".$_SERVER["PHP_SELF"]."?id=".GETPOST('id', 'int'));
  124. exit;
  125. } else {
  126. setEventMessages($object->error, $object->errors, 'errors');
  127. }
  128. }
  129. } else {
  130. header("Location: ".$_SERVER["PHP_SELF"]."?id=".GETPOST('id', 'int'));
  131. exit;
  132. }
  133. }
  134. /*
  135. * View
  136. */
  137. llxHeader();
  138. $form = new Form($db);
  139. $formcompany = new FormCompany($db);
  140. /*
  141. * Action create
  142. */
  143. if ($action == 'create') {
  144. print load_fiche_titre($langs->trans("NewEstablishment"));
  145. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  146. print '<input type="hidden" name="token" value="'.newToken().'">';
  147. print '<input type="hidden" name="action" value="add">';
  148. print dol_get_fiche_head();
  149. print '<table class="border centpercent">';
  150. // Name
  151. print '<tr>';
  152. print '<td>'.$form->editfieldkey('Label', 'label', '', $object, 0, 'string', '', 1).'</td>';
  153. print '<td><input name="label" id="label" value="'.GETPOST("label", "alphanohtml").'" autofocus></td>';
  154. print '</tr>';
  155. // Entity
  156. /*
  157. if (isModEnabled('multicompany')) {
  158. print '<tr>';
  159. print '<td>'.$form->editfieldkey('Parent', 'entity', '', $object, 0, 'string', '', 1).'</td>';
  160. print '<td class="maxwidthonsmartphone">';
  161. print $form->selectEstablishments(GETPOST('entity', 'int') > 0 ?GETPOST('entity', 'int') : $conf->entity, 'entity', 1);
  162. print '</td>';
  163. print '</tr>';
  164. } */
  165. // Address
  166. print '<tr>';
  167. print '<td>'.$form->editfieldkey('Address', 'address', '', $object, 0).'</td>';
  168. print '<td>';
  169. print '<input name="address" id="address" class="qutrevingtpercent" value="'.GETPOST('address', 'alphanohtml').'">';
  170. print '</td>';
  171. print '</tr>';
  172. // Zipcode
  173. print '<tr>';
  174. print '<td>'.$form->editfieldkey('Zip', 'zipcode', '', $object, 0).'</td>';
  175. print '<td>';
  176. print $formcompany->select_ziptown(
  177. GETPOST('zipcode', 'alpha'),
  178. 'zipcode',
  179. array(
  180. 'town',
  181. 'selectcountry_id'
  182. ),
  183. 6
  184. );
  185. print '</td>';
  186. print '</tr>';
  187. // Town
  188. print '<tr>';
  189. print '<td>'.$form->editfieldkey('Town', 'town', '', $object, 0).'</td>';
  190. print '<td>';
  191. print $formcompany->select_ziptown(GETPOSTISSET('town') ? GETPOST('town', 'alpha') : $object->town, 'town', array(
  192. 'zipcode',
  193. 'selectcountry_id'
  194. ));
  195. print '</td>';
  196. print '</tr>';
  197. // Country
  198. print '<tr>';
  199. print '<td>'.$form->editfieldkey('Country', 'selectcountry_id', '', $object, 0).'</td>';
  200. print '<td class="maxwidthonsmartphone">';
  201. print $form->select_country(GETPOSTISSET('country_id') ? GETPOST('country_id', 'int') : ($object->country_id ? $object->country_id : $mysoc->country_id), 'country_id');
  202. if ($user->admin) {
  203. print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  204. }
  205. print '</td>';
  206. print '</tr>';
  207. // Status
  208. print '<tr>';
  209. print '<td>'.$form->editfieldkey('Status', 'status', '', $object, 0, 'string', '', 1).'</td>';
  210. print '<td>';
  211. print $form->selectarray('status', $status2label, GETPOSTISSET('status') ? GETPOST('status', 'alpha') : 1);
  212. print '</td></tr>';
  213. print '</table>';
  214. print dol_get_fiche_end();
  215. print '<div class="center">';
  216. print '<input class="button button-save" type="submit" value="'.$langs->trans("Save").'">';
  217. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  218. print '<input class="button button-cancel" type="submit" name="cancel" value="'.$langs->trans("Cancel").'">';
  219. print '</div>';
  220. print '</form>';
  221. }
  222. // Part to edit record
  223. if ((!empty($id) || !empty($ref)) && $action == 'edit') {
  224. $result = $object->fetch($id);
  225. if ($result > 0) {
  226. $head = establishment_prepare_head($object);
  227. if ($action == 'edit') {
  228. print dol_get_fiche_head($head, 'card', $langs->trans("Establishment"), 0, $object->picto);
  229. print '<form name="update" action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
  230. print '<input type="hidden" name="token" value="'.newToken().'">';
  231. print '<input type="hidden" name="action" value="update">';
  232. print '<input type="hidden" name="id" value="'.$id.'">';
  233. print '<table class="border centpercent">';
  234. // Ref
  235. print "<tr>";
  236. print '<td class="titlefield">'.$langs->trans("Ref").'</td><td>';
  237. print $object->id;
  238. print '</td></tr>';
  239. // Name
  240. print '<tr><td>'.$form->editfieldkey('Label', 'label', '', $object, 0, 'string', '', 1).'</td><td>';
  241. print '<input name="label" id="label" class="flat" value="'.$object->label.'">';
  242. print '</td></tr>';
  243. // Entity
  244. /*
  245. if (isModEnabled('multicompany')) {
  246. print '<tr><td>'.$form->editfieldkey('Parent', 'entity', '', $object, 0, 'string', '', 1).'</td>';
  247. print '<td class="maxwidthonsmartphone">';
  248. print $object->entity > 0 ? $object->entity : $conf->entity;
  249. print '</td></tr>';
  250. }*/
  251. // Address
  252. print '<tr><td>'.$form->editfieldkey('Address', 'address', '', $object, 0).'</td>';
  253. print '<td>';
  254. print '<input name="address" id="address" value="'.$object->address.'">';
  255. print '</td></tr>';
  256. // Zipcode / Town
  257. print '<tr><td>'.$form->editfieldkey('Zip', 'zipcode', '', $object, 0).'</td><td>';
  258. print $formcompany->select_ziptown($object->zip, 'zipcode', array(
  259. 'town',
  260. 'selectcountry_id'
  261. ), 6).'</tr>';
  262. print '<tr><td>'.$form->editfieldkey('Town', 'town', '', $object, 0).'</td><td>';
  263. print $formcompany->select_ziptown($object->town, 'town', array(
  264. 'zipcode',
  265. 'selectcountry_id'
  266. )).'</td></tr>';
  267. // Country
  268. print '<tr><td>'.$form->editfieldkey('Country', 'selectcountry_id', '', $object, 0).'</td>';
  269. print '<td class="maxwidthonsmartphone">';
  270. print $form->select_country($object->country_id, 'country_id');
  271. if ($user->admin) {
  272. print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
  273. }
  274. print '</td>';
  275. print '</tr>';
  276. // Status
  277. print '<tr><td>'.$form->editfieldkey('Status', 'status', '', $object, 0, 'string', '', 1).'</td><td>';
  278. print $form->selectarray('status', $status2label, $object->status);
  279. print '</td></tr>';
  280. print '</table>';
  281. print dol_get_fiche_end();
  282. print $form->buttonsSaveCancel();
  283. print '</form>';
  284. }
  285. } else {
  286. dol_print_error($db);
  287. }
  288. }
  289. if ($object->id > 0 && (empty($action) || ($action != 'edit' && $action != 'create'))) {
  290. $res = $object->fetch_optionals();
  291. $head = establishment_prepare_head($object);
  292. print dol_get_fiche_head($head, 'card', $langs->trans("Establishment"), -1, $object->picto);
  293. // Confirmation to delete
  294. if ($action == 'delete') {
  295. print $form->formconfirm($_SERVER["PHP_SELF"]."?id=".$id, $langs->trans("DeleteEstablishment"), $langs->trans("ConfirmDeleteEstablishment"), "confirm_delete");
  296. }
  297. // Object card
  298. // ------------------------------------------------------------
  299. $linkback = '<a href="'.DOL_URL_ROOT.'/hrm/admin/admin_establishment.php'.(!empty($socid) ? '?socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
  300. $morehtmlref = '<div class="refidno">';
  301. $morehtmlref .= '</div>';
  302. dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'id', $morehtmlref);
  303. print '<div class="fichecenter">';
  304. //print '<div class="fichehalfleft">';
  305. print '<div class="underbanner clearboth"></div>';
  306. print '<table class="border centpercent">'."\n";
  307. // Name
  308. print '<tr>';
  309. print '<td class="titlefield">'.$langs->trans("Label").'</td>';
  310. print '<td>'.$object->label.'</td>';
  311. print '</tr>';
  312. // Entity
  313. /*
  314. if (!isModEnabled('multicompany') {
  315. print '<tr>';
  316. print '<td class="titlefield">'.$langs->trans("Entity").'</td>';
  317. print '<td>'.$object->entity.'</td>';
  318. print '</tr>';
  319. }*/
  320. // Address
  321. print '<tr>';
  322. print '<td>'.$langs->trans("Address").'</td>';
  323. print '<td>'.$object->address.'</td>';
  324. print '</tr>';
  325. // Zipcode
  326. print '<tr>';
  327. print '<td>'.$langs->trans("Zip").'</td>';
  328. print '<td>'.$object->zip.'</td>';
  329. print '</tr>';
  330. // Town
  331. print '<tr>';
  332. print '<td>'.$langs->trans("Town").'</td>';
  333. print '<td>'.$object->town.'</td>';
  334. print '</tr>';
  335. // Country
  336. print '<tr>';
  337. print '<td>'.$langs->trans("Country").'</td>';
  338. print '<td>';
  339. if ($object->country_id > 0) {
  340. $img = picto_from_langcode($object->country_code);
  341. print $img ? $img.' ' : '';
  342. print getCountry($object->getCountryCode(), 0, $db);
  343. }
  344. print '</td>';
  345. print '</tr>';
  346. print '</table>';
  347. print '</div>';
  348. print '<div class="clearboth"></div><br>';
  349. print dol_get_fiche_end();
  350. /*
  351. * Action bar
  352. */
  353. print '<div class="tabsAction">';
  354. // Modify
  355. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit&token='.newToken().'&id='.$id.'">'.$langs->trans('Modify').'</a>';
  356. // Delete
  357. print dolGetButtonAction($langs->trans("Delete"), '', 'delete', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken(), 'delete', $permissiontodelete);
  358. print '</div>';
  359. }
  360. // End of page
  361. llxFooter();
  362. $db->close();