add.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /* Copyright (C) 2013 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2015 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
  4. * Copyright (C) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file resource/add.php
  21. * \ingroup resource
  22. * \brief Page to manage resource object
  23. */
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/resource/class/dolresource.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/resource/class/html.formresource.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  28. // Load traductions files required by page
  29. $langs->load("resource");
  30. $langs->load("companies");
  31. $langs->load("other");
  32. $langs->load("resource");
  33. // Get parameters
  34. $id = GETPOST('id','int');
  35. $action = GETPOST('action','alpha');
  36. $cancel = GETPOST('cancel','alpha');
  37. if (empty($sortorder)) $sortorder="DESC";
  38. if (empty($sortfield)) $sortfield="t.rowid";
  39. if (empty($arch)) $arch = 0;
  40. if ($page == -1) {
  41. $page = 0 ;
  42. }
  43. $limit = $conf->global->limit;
  44. $offset = $limit * $page ;
  45. $pageprev = $page - 1;
  46. $pagenext = $page + 1;
  47. // Protection if external user
  48. if ($user->societe_id > 0)
  49. {
  50. accessforbidden();
  51. }
  52. $object = new DolResource($db);
  53. $extrafields = new ExtraFields($db);
  54. // fetch optionals attributes and labels
  55. $extralabels=$extrafields->fetch_name_optionals_label($object->table_element);
  56. if ($action == 'confirm_add_resource')
  57. {
  58. if (! $cancel)
  59. {
  60. $error='';
  61. $ref=GETPOST('ref','alpha');
  62. $description=GETPOST('description','alpha');
  63. $fk_code_type_resource=GETPOST('fk_code_type_resource','alpha');
  64. if (empty($ref))
  65. {
  66. $mesg=$langs->trans("ErrorFieldRequired",$langs->transnoentities("Ref"));
  67. setEventMessages($mesg, null, 'errors');
  68. $error++;
  69. }
  70. if (! $error)
  71. {
  72. $object=new Dolresource($db);
  73. $object->ref=$ref;
  74. $object->description=$description;
  75. $object->fk_code_type_resource=$fk_code_type_resource;
  76. // Fill array 'array_options' with data from add form
  77. $ret = $extrafields->setOptionalsFromPost($extralabels, $object);
  78. if ($ret < 0) {
  79. $error ++;
  80. }
  81. $result=$object->create($user);
  82. if ($result > 0)
  83. {
  84. // Creation OK
  85. $db->commit();
  86. setEventMessages($langs->trans('ResourceCreatedWithSuccess'), null, 'mesgs');
  87. Header("Location: card.php?id=" . $object->id);
  88. return;
  89. }
  90. else
  91. {
  92. // Creation KO
  93. setEventMessages($object->error, $object->errors, 'errors');
  94. $action = '';
  95. }
  96. }
  97. else
  98. {
  99. $action = '';
  100. }
  101. }
  102. else
  103. {
  104. Header("Location: list.php");
  105. }
  106. }
  107. /*
  108. * View
  109. */
  110. $form=new Form($db);
  111. $formresource = new FormResource($db);
  112. if (! $action)
  113. {
  114. $pagetitle=$langs->trans('AddResource');
  115. llxHeader('',$pagetitle,'');
  116. print load_fiche_titre($pagetitle,'','title_generic');
  117. print '<form method="post" action="'.$_SERVER['PHP_SELF'].'" name="add_resource">';
  118. print '<input type="hidden" name="action" value="confirm_add_resource" />';
  119. dol_fiche_head('');
  120. print '<table class="border" width="100%">';
  121. // Ref / label
  122. $field = 'ref';
  123. print '<tr>';
  124. print '<td class="titlefieldcreate fieldrequired">';
  125. print $langs->trans('ResourceFormLabel_'.$field);
  126. print '</td>';
  127. print '<td>';
  128. print '<input class="flat maxwidthonsmartphone" type="text" name="'.$field.'" value="'.$$field.'" />';
  129. print '</td>';
  130. print '</tr>';
  131. // Type
  132. print '<tr><td>'.$langs->trans("ResourceType").'</td>';
  133. print '<td>';
  134. $ret = $formresource->select_types_resource($object->fk_code_type_resource, 'fk_code_type_resource', '', 2, 1);
  135. print '</td></tr>';
  136. // Description
  137. $field = 'description';
  138. print '<tr>';
  139. print '<td class="tdtop">';
  140. print $langs->trans('ResourceFormLabel_'.$field);
  141. print '</td>';
  142. print '<td>';
  143. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  144. $doleditor = new DolEditor('description', $description, '', '200', 'dolibarr_notes', false);
  145. $doleditor->Create();
  146. print '</td>';
  147. print '</tr>';
  148. // Other attributes
  149. $parameters=array('objectsrc' => $objectsrc);
  150. $reshook=$hookmanager->executeHooks('formObjectOptions',$parameters,$object,$action); // Note that $action and $object may have been modified by hook
  151. print $hookmanager->resPrint;
  152. if (empty($reshook) && ! empty($extrafields->attribute_label))
  153. {
  154. print $object->showOptionals($extrafields,'edit');
  155. }
  156. print '</table>';
  157. dol_fiche_end('');
  158. print '<div class="center">';
  159. print '<input type="submit" class="button" value="' . $langs->trans("Save") . '">';
  160. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  161. print '<input type="button" class="button" value="' . $langs->trans("Cancel") . '">';
  162. print '</div>';
  163. print '</form>';
  164. }
  165. // End of page
  166. llxFooter();
  167. $db->close();