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