dolibarr  16.0.5
editor.php
Go to the documentation of this file.
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 
24 require '../../main.inc.php';
25 require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
26 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
27 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_expression.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_global_variable.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
30 
31 // Load translation files required by the page
32 $langs->loadLangs(array('products', 'accountancy')); //"Back" translation is on this accountancy file
33 
34 $id = GETPOST('id', 'int');
35 $eid = GETPOST('eid', 'int');
36 $action = GETPOST('action', 'aZ09');
37 $title = GETPOST('expression_title', 'alpha');
38 $expression = GETPOST('expression');
39 $tab = GETPOST('tab', 'alpha');
40 $tab = (!empty($tab)) ? $tab : 'card';
41 $tab = strtolower($tab);
42 
43 // Security check
44 $result = restrictedArea($user, 'produit|service&fournisseur', $id, 'product&product', '', '', 'rowid');
45 
46 //Initialize objects
47 $product = new Product($db);
48 $product->fetch($id, '');
49 
50 $price_expression = new PriceExpression($db);
51 $price_globals = new PriceGlobalVariable($db);
52 
53 //Fetch expression data
54 if (empty($eid)) { //This also disables fetch when eid == 0
55  $eid = 0;
56 } elseif ($action != 'delete') {
57  $price_expression->fetch($eid);
58 }
59 
60 
61 /*
62  * Actions
63  */
64 
65 if ($action == 'add') {
66  if ($eid == 0) {
67  $result = $price_expression->find_title($title);
68  if ($result == 0) { //No existing entry found with title, ok
69  //Check the expression validity by parsing it
70  $priceparser = new PriceParser($db);
71  $price_result = $priceparser->testExpression($id, $expression);
72  if ($price_result < 0) { //Expression is not valid
73  setEventMessages($priceparser->translatedError(), null, 'errors');
74  } else {
75  $price_expression->title = $title;
76  $price_expression->expression = $expression;
77  $result = $price_expression->create($user);
78  if ($result > 0) { //created successfully, set the eid to newly created entry
79  $eid = $price_expression->id;
80  setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
81  } else {
82  setEventMessages("add: ".$price_expression->error, $price_expression->errors, 'errors');
83  }
84  }
85  } elseif ($result < 0) {
86  setEventMessages("add find: ".$price_expression->error, $price_expression->errors, 'errors');
87  } else {
88  setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
89  }
90  }
91 }
92 
93 if ($action == 'update') {
94  if ($eid != 0) {
95  $result = $price_expression->find_title($title);
96  if ($result == 0 || $result == $eid) { //No existing entry found with title or existing one is the current one, ok
97  //Check the expression validity by parsing it
98  $priceparser = new PriceParser($db);
99  $price_result = $priceparser->testExpression($id, $expression);
100  if ($price_result < 0) { //Expression is not valid
101  setEventMessages($priceparser->translatedError(), null, 'errors');
102  } else {
103  $price_expression->id = $eid;
104  $price_expression->title = $title;
105  $price_expression->expression = $expression;
106  $result = $price_expression->update($user);
107  if ($result < 0) {
108  setEventMessages("update: ".$price_expression->error, $price_expression->errors, 'errors');
109  } else {
110  setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
111  }
112  }
113  } elseif ($result < 0) {
114  setEventMessages("update find: ".$price_expression->error, $price_expression->errors, 'errors');
115  } else {
116  setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
117  }
118  }
119 }
120 
121 if ($action == 'delete') {
122  if ($eid != 0) {
123  $price_expression->fetch($eid);
124  $result = $price_expression->delete($user);
125  if ($result < 0) {
126  setEventMessages("delete: ".$price_expression->error, $price_expression->errors, 'errors');
127  }
128  $eid = 0;
129  }
130 }
131 
132 
133 /*
134  * View
135  */
136 
137 $form = new Form($db);
138 
139 llxHeader("", "", $langs->trans("CardProduct".$product->type));
140 
141 print load_fiche_titre($langs->trans("PriceExpressionEditor"));
142 
143 //Form/Table
144 print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$id.'&amp;tab='.$tab.'&amp;eid='.$eid.'" method="POST">';
145 print '<input type="hidden" name="token" value="'.newToken().'">';
146 print '<input type="hidden" name="action" value='.($eid == 0 ? 'add' : 'update').'>';
147 
148 print dol_get_fiche_head();
149 
150 print '<table class="border centpercent">';
151 
152 // Price expression selector
153 print '<tr><td class="titlefield fieldrequired">'.$langs->trans("PriceExpressionSelected").'</td><td>';
154 $price_expression_list = array(0 => $langs->trans("New")); //Put the new as first option
155 foreach ($price_expression->list_price_expression() as $entry) {
156  $price_expression_list[$entry->id] = $entry->title;
157 }
158 print $form->selectarray('expression_selection', $price_expression_list, $eid);
159 print '</td></tr>';
160 
161 // Title input
162 print '<tr><td class="fieldrequired">'.$langs->trans("Name").'</td><td>';
163 print '<input class="flat" name="expression_title" size="15" value="'.($price_expression->title ? $price_expression->title : '').'">';
164 print '</td></tr>';
165 
166 //Help text
167 $help_text = $langs->trans("PriceExpressionEditorHelp1");
168 $help_text .= '<br><br>'.$langs->trans("PriceExpressionEditorHelp2");
169 $help_text .= '<br><br>'.$langs->trans("PriceExpressionEditorHelp3");
170 $help_text .= '<br><br>'.$langs->trans("PriceExpressionEditorHelp4");
171 $help_text .= '<br><br>'.$langs->trans("PriceExpressionEditorHelp5");
172 foreach ($price_globals->listGlobalVariables() as $entry) {
173  $help_text .= '<br><b>#globals_'.$entry->code.'#</b> '.$entry->description.' = '.$entry->value;
174 }
175 
176 //Price expression editor
177 print '<tr><td class="fieldrequired">'.$form->textwithpicto($langs->trans("PriceExpressionEditor"), $help_text, 1).'</td><td>';
178 require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
179 $doleditor = new DolEditor('expression', isset($price_expression->expression) ? $price_expression->expression : '', '', 300, '', '', false, false, false, ROWS_4, '90%');
180 $doleditor->Create();
181 print '</td></tr>';
182 print '</table>';
183 
184 print dol_get_fiche_end();
185 
186 //Buttons
187 print '<div class="center">';
188 print '<input type="submit" class="butAction button-save" value="'.$langs->trans("Save").'">';
189 print '<span id="back" class="butAction">'.$langs->trans("Back").'</span>';
190 if ($eid == 0) {
191  print '<div class="inline-block divButAction"><span id="action-delete" class="butActionRefused classfortooltip">'.$langs->trans('Delete').'</span></div>'."\n";
192 } else {
193  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>';
194 }
195 print '</div>';
196 
197 print '</form>';
198 
199 // This code reloads the page depending of selected option, goes to page selected by tab when back is pressed
200 print '<script type="text/javascript">
201  jQuery(document).ready(run);
202  function run() {
203  jQuery("#back").click(on_click);
204  jQuery("#expression_selection").change(on_change);
205  }
206  function on_click() {
207  window.location = "'.str_replace('dynamic_price/editor.php', $tab.'.php', $_SERVER["PHP_SELF"]).'?id='.$id.($tab == 'price' ? '&action=edit_price&token='.newToken() : '').'";
208  }
209  function on_change() {
210  window.location = "'.$_SERVER["PHP_SELF"].'?id='.$id.'&tab='.$tab.'&eid=" + $("#expression_selection").val();
211  }
212 </script>';
213 
214 // End of page
215 llxFooter();
216 $db->close();
restrictedArea
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
Definition: security.lib.php:234
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
PriceExpression
Class for accesing price expression table.
Definition: price_expression.class.php:30
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
PriceParser
Class to parse product price expressions.
Definition: price_parser.class.php:33
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:10878
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
Product
Class to manage products or services.
Definition: product.class.php:46
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59
DolEditor
Class to manage a WYSIWYG editor.
Definition: doleditor.class.php:30
PriceGlobalVariable
Class for accesing price global variables table.
Definition: price_global_variable.class.php:30