editor.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /* Copyright (C) 2014 Ion Agorria <ion@agorria.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 <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/product/dynamic_price/editor.php
  19. * \ingroup product
  20. * \brief Page for editing expression
  21. */
  22. // Load Dolibarr environment
  23. require '../../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  25. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_expression.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_global_variable.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array('products', 'accountancy')); //"Back" translation is on this accountancy file
  31. $id = GETPOST('id', 'int');
  32. $eid = GETPOST('eid', 'int');
  33. $action = GETPOST('action', 'aZ09');
  34. $title = GETPOST('expression_title', 'alpha');
  35. $expression = GETPOST('expression');
  36. $tab = GETPOST('tab', 'alpha');
  37. $tab = (!empty($tab)) ? $tab : 'card';
  38. $tab = strtolower($tab);
  39. // Security check
  40. $result = restrictedArea($user, 'produit|service&fournisseur', $id, 'product&product', '', '', 'rowid');
  41. //Initialize objects
  42. $product = new Product($db);
  43. $product->fetch($id, '');
  44. $price_expression = new PriceExpression($db);
  45. $price_globals = new PriceGlobalVariable($db);
  46. //Fetch expression data
  47. if (empty($eid)) { //This also disables fetch when eid == 0
  48. $eid = 0;
  49. } elseif ($action != 'delete') {
  50. $price_expression->fetch($eid);
  51. }
  52. /*
  53. * Actions
  54. */
  55. if ($action == 'add') {
  56. if ($eid == 0) {
  57. $result = $price_expression->find_title($title);
  58. if ($result == 0) { //No existing entry found with title, ok
  59. // Check the expression validity by parsing it
  60. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
  61. $priceparser = new PriceParser($db);
  62. $price_result = $priceparser->testExpression($id, $expression);
  63. if ($price_result < 0) { //Expression is not valid
  64. setEventMessages($priceparser->translatedError(), null, 'errors');
  65. } else {
  66. $price_expression->title = $title;
  67. $price_expression->expression = $expression;
  68. $result = $price_expression->create($user);
  69. if ($result > 0) { //created successfully, set the eid to newly created entry
  70. $eid = $price_expression->id;
  71. setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
  72. } else {
  73. setEventMessages("add: ".$price_expression->error, $price_expression->errors, 'errors');
  74. }
  75. }
  76. } elseif ($result < 0) {
  77. setEventMessages("add find: ".$price_expression->error, $price_expression->errors, 'errors');
  78. } else {
  79. setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
  80. }
  81. }
  82. }
  83. if ($action == 'update') {
  84. if ($eid != 0) {
  85. $result = $price_expression->find_title($title);
  86. if ($result == 0 || $result == $eid) { //No existing entry found with title or existing one is the current one, ok
  87. // Check the expression validity by parsing it
  88. require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
  89. $priceparser = new PriceParser($db);
  90. $price_result = $priceparser->testExpression($id, $expression);
  91. if ($price_result < 0) { //Expression is not valid
  92. setEventMessages($priceparser->translatedError(), null, 'errors');
  93. } else {
  94. $price_expression->id = $eid;
  95. $price_expression->title = $title;
  96. $price_expression->expression = $expression;
  97. $result = $price_expression->update($user);
  98. if ($result < 0) {
  99. setEventMessages("update: ".$price_expression->error, $price_expression->errors, 'errors');
  100. } else {
  101. setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
  102. }
  103. }
  104. } elseif ($result < 0) {
  105. setEventMessages("update find: ".$price_expression->error, $price_expression->errors, 'errors');
  106. } else {
  107. setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
  108. }
  109. }
  110. }
  111. if ($action == 'delete') {
  112. if ($eid != 0) {
  113. $price_expression->fetch($eid);
  114. $result = $price_expression->delete($user);
  115. if ($result < 0) {
  116. setEventMessages("delete: ".$price_expression->error, $price_expression->errors, 'errors');
  117. }
  118. $eid = 0;
  119. }
  120. }
  121. /*
  122. * View
  123. */
  124. $form = new Form($db);
  125. llxHeader("", "", $langs->trans("CardProduct".$product->type));
  126. print load_fiche_titre($langs->trans("PriceExpressionEditor"));
  127. //Form/Table
  128. print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$id.'&amp;tab='.$tab.'&amp;eid='.$eid.'" method="POST">';
  129. print '<input type="hidden" name="token" value="'.newToken().'">';
  130. print '<input type="hidden" name="action" value='.($eid == 0 ? 'add' : 'update').'>';
  131. print dol_get_fiche_head();
  132. print '<table class="border centpercent">';
  133. // Price expression selector
  134. print '<tr><td class="titlefield fieldrequired">'.$langs->trans("PriceExpressionSelected").'</td><td>';
  135. $price_expression_list = array(0 => $langs->trans("New")); //Put the new as first option
  136. foreach ($price_expression->list_price_expression() as $entry) {
  137. $price_expression_list[$entry->id] = $entry->title;
  138. }
  139. print $form->selectarray('expression_selection', $price_expression_list, $eid);
  140. print '</td></tr>';
  141. // Title input
  142. print '<tr><td class="fieldrequired">'.$langs->trans("Name").'</td><td>';
  143. print '<input class="flat" name="expression_title" size="15" value="'.($price_expression->title ? $price_expression->title : '').'">';
  144. print '</td></tr>';
  145. //Help text
  146. $help_text = $langs->trans("PriceExpressionEditorHelp1");
  147. $help_text .= '<br><br>'.$langs->trans("PriceExpressionEditorHelp2");
  148. $help_text .= '<br><br>'.$langs->trans("PriceExpressionEditorHelp3");
  149. $help_text .= '<br><br>'.$langs->trans("PriceExpressionEditorHelp4");
  150. $help_text .= '<br><br>'.$langs->trans("PriceExpressionEditorHelp5");
  151. foreach ($price_globals->listGlobalVariables() as $entry) {
  152. $help_text .= '<br><b>#globals_'.$entry->code.'#</b> '.$entry->description.' = '.$entry->value;
  153. }
  154. //Price expression editor
  155. print '<tr><td class="fieldrequired">'.$form->textwithpicto($langs->trans("PriceExpressionEditor"), $help_text, 1).'</td><td>';
  156. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  157. $doleditor = new DolEditor('expression', isset($price_expression->expression) ? $price_expression->expression : '', '', 300, '', '', false, false, false, ROWS_4, '90%');
  158. $doleditor->Create();
  159. print '</td></tr>';
  160. print '</table>';
  161. print dol_get_fiche_end();
  162. //Buttons
  163. print '<div class="center">';
  164. print '<input type="submit" class="butAction button-save" value="'.$langs->trans("Save").'">';
  165. print '<span id="back" class="butAction">'.$langs->trans("Back").'</span>';
  166. if ($eid == 0) {
  167. print '<div class="inline-block divButAction"><span id="action-delete" class="butActionRefused classfortooltip">'.$langs->trans('Delete').'</span></div>'."\n";
  168. } else {
  169. print '<div class="inline-block divButAction"><a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?id='.$id.'&tab='.$tab.'&eid='.$eid.'&action=delete&token='.newToken().'">'.$langs->trans("Delete").'</a></div>';
  170. }
  171. print '</div>';
  172. print '</form>';
  173. // This code reloads the page depending of selected option, goes to page selected by tab when back is pressed
  174. print '<script type="text/javascript">
  175. jQuery(document).ready(run);
  176. function run() {
  177. jQuery("#back").click(on_click);
  178. jQuery("#expression_selection").change(on_change);
  179. }
  180. function on_click() {
  181. window.location = "'.str_replace('dynamic_price/editor.php', $tab.'.php', $_SERVER["PHP_SELF"]).'?id='.$id.($tab == 'price' ? '&action=edit_price&token='.newToken() : '').'";
  182. }
  183. function on_change() {
  184. window.location = "'.$_SERVER["PHP_SELF"].'?id='.$id.'&tab='.$tab.'&eid=" + $("#expression_selection").val();
  185. }
  186. </script>';
  187. // End of page
  188. llxFooter();
  189. $db->close();