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