dolibarr 21.0.0-beta
editor.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2014 Ion Agorria <ion@agorria.com>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25// Load Dolibarr environment
26require '../../main.inc.php';
27require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
28require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
29require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_expression.class.php';
30require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_global_variable.class.php';
31require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
32
41// Load translation files required by the page
42$langs->loadLangs(array('products', 'accountancy')); //"Back" translation is on this accountancy file
43
44$id = GETPOSTINT('id');
45$eid = GETPOSTINT('eid');
46$action = GETPOST('action', 'aZ09');
47$title = GETPOST('expression_title', 'alpha');
48$expression = GETPOST('expression');
49$tab = GETPOST('tab', 'alpha');
50$tab = (!empty($tab)) ? $tab : 'card';
51$tab = strtolower($tab);
52
53// Security check
54$result = restrictedArea($user, 'produit|service&fournisseur', $id, 'product&product', '', '', 'rowid');
55
56//Initialize objects
57$product = new Product($db);
58$product->fetch($id, '');
59
60$price_expression = new PriceExpression($db);
61$price_globals = new PriceGlobalVariable($db);
62
63//Fetch expression data
64if (empty($eid)) { //This also disables fetch when eid == 0
65 $eid = 0;
66} elseif ($action != 'delete') {
67 $price_expression->fetch($eid);
68}
69
70$object = $product;
71
72$usercanread = (($object->type == Product::TYPE_PRODUCT && $user->hasRight('produit', 'lire')) || ($object->type == Product::TYPE_SERVICE && $user->hasRight('service', 'lire')));
73$usercancreate = (($object->type == Product::TYPE_PRODUCT && $user->hasRight('produit', 'creer')) || ($object->type == Product::TYPE_SERVICE && $user->hasRight('service', 'creer')));
74$usercandelete = (($object->type == Product::TYPE_PRODUCT && $user->hasRight('produit', 'supprimer')) || ($object->type == Product::TYPE_SERVICE && $user->hasRight('service', 'supprimer')));
75
76
77/*
78 * Actions
79 */
80
81if ($action == 'add' && $usercancreate) {
82 if ($eid == 0) {
83 $result = $price_expression->find_title($title);
84 if ($result == 0) { //No existing entry found with title, ok
85 // Check the expression validity by parsing it
86 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
87 $priceparser = new PriceParser($db);
88 $price_result = $priceparser->testExpression($id, $expression);
89 if ($price_result < 0) { //Expression is not valid
90 setEventMessages($priceparser->translatedError(), null, 'errors');
91 } else {
92 $price_expression->title = $title;
93 $price_expression->expression = $expression;
94 $result = $price_expression->create($user);
95 if ($result > 0) { //created successfully, set the eid to newly created entry
96 $eid = $price_expression->id;
97 setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
98 } else {
99 setEventMessages("add: ".$price_expression->error, $price_expression->errors, 'errors');
100 }
101 }
102 } elseif ($result < 0) {
103 setEventMessages("add find: ".$price_expression->error, $price_expression->errors, 'errors');
104 } else {
105 setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
106 }
107 }
108}
109
110if ($action == 'update' && $usercancreate) {
111 if ($eid != 0) {
112 $result = $price_expression->find_title($title);
113 if ($result == 0 || $result == $eid) { //No existing entry found with title or existing one is the current one, ok
114 // Check the expression validity by parsing it
115 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
116 $priceparser = new PriceParser($db);
117 $price_result = $priceparser->testExpression($id, $expression);
118 if ($price_result < 0) { //Expression is not valid
119 setEventMessages($priceparser->translatedError(), null, 'errors');
120 } else {
121 $price_expression->id = $eid;
122 $price_expression->title = $title;
123 $price_expression->expression = $expression;
124 $result = $price_expression->update($user);
125 if ($result < 0) {
126 setEventMessages("update: ".$price_expression->error, $price_expression->errors, 'errors');
127 } else {
128 setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
129 }
130 }
131 } elseif ($result < 0) {
132 setEventMessages("update find: ".$price_expression->error, $price_expression->errors, 'errors');
133 } else {
134 setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
135 }
136 }
137}
138
139if ($action == 'delete' && $usercancreate) {
140 if ($eid != 0) {
141 $price_expression->fetch($eid);
142 $result = $price_expression->delete($user);
143 if ($result < 0) {
144 setEventMessages("delete: ".$price_expression->error, $price_expression->errors, 'errors');
145 }
146 $eid = 0;
147 }
148}
149
150
151/*
152 * View
153 */
154
155$form = new Form($db);
156
157llxHeader("", "", $langs->trans("CardProduct".$product->type), '', 0, 0, '', '', '', 'mod-product page-dynamic_price_editor');
158
159print load_fiche_titre($langs->trans("PriceExpressionEditor"));
160
161//Form/Table
162print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$id.'&amp;tab='.$tab.'&amp;eid='.$eid.'" method="POST">';
163print '<input type="hidden" name="token" value="'.newToken().'">';
164print '<input type="hidden" name="action" value='.($eid == 0 ? 'add' : 'update').'>';
165
166print dol_get_fiche_head();
167
168print '<table class="border centpercent">';
169
170// Price expression selector
171print '<tr><td class="titlefield fieldrequired">'.$langs->trans("PriceExpressionSelected").'</td><td>';
172$price_expression_list = array(0 => $langs->trans("New")); //Put the new as first option
173foreach ($price_expression->list_price_expression() as $entry) {
174 $price_expression_list[$entry->id] = $entry->title;
175}
176print $form->selectarray('expression_selection', $price_expression_list, $eid);
177print '</td></tr>';
178
179// Title input
180print '<tr><td class="fieldrequired">'.$langs->trans("Name").'</td><td>';
181print '<input class="flat" name="expression_title" size="15" value="'.(GETPOSTISSET('expression_title') ? GETPOST('expression_title') : ($price_expression->title ? $price_expression->title : '')).'">';
182print '</td></tr>';
183
184//Help text
185$help_text = $langs->trans("PriceExpressionEditorHelp1");
186$help_text .= '<br><br>'.$langs->trans("PriceExpressionEditorHelp2");
187$help_text .= '<br><br>'.$langs->trans("PriceExpressionEditorHelp3");
188$help_text .= '<br><br>'.$langs->trans("PriceExpressionEditorHelp4");
189$help_text .= '<br><br>'.$langs->trans("PriceExpressionEditorHelp5");
190foreach ($price_globals->listGlobalVariables() as $entry) {
191 $help_text .= '<br><b>#global_'.$entry->code.'#</b> '.$entry->description.' = '.$entry->value;
192}
193
194//Price expression editor
195print '<tr><td class="fieldrequired">'.$form->textwithpicto($langs->trans("PriceExpressionEditor"), $help_text, 1).'</td><td>';
196require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
197$doleditor = new DolEditor('expression', isset($price_expression->expression) ? $price_expression->expression : '', '', 300, '', '', false, false, false, ROWS_4, '90%');
198$doleditor->Create();
199print '</td></tr>';
200print '</table>';
201
202print dol_get_fiche_end();
203
204//Buttons
205print '<div class="center">';
206print '<input type="submit" class="butAction button-save" value="'.$langs->trans("Save").'">';
207print '<span id="back" class="butAction">'.$langs->trans("Back").'</span>';
208if ($eid == 0) {
209 print '<div class="inline-block divButAction"><span id="action-delete" class="butActionRefused classfortooltip">'.$langs->trans('Delete').'</span></div>'."\n";
210} else {
211 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>';
212}
213print '</div>';
214
215print '</form>';
216
217// This code reloads the page depending of selected option, goes to page selected by tab when back is pressed
218print '<script type="text/javascript">
219 jQuery(document).ready(run);
220 function run() {
221 jQuery("#back").click(on_click);
222 jQuery("#expression_selection").change(on_change);
223 }
224 function on_click() {
225 window.location = "'.str_replace('dynamic_price/editor.php', $tab.'.php', $_SERVER["PHP_SELF"]).'?id='.$id.($tab == 'price' ? '&action=edit_price&token='.newToken() : '').'";
226 }
227 function on_change() {
228 window.location = "'.$_SERVER["PHP_SELF"].'?id='.$id.'&tab='.$tab.'&eid=" + $("#expression_selection").val();
229 }
230</script>';
231
232// End of page
233llxFooter();
234$db->close();
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:71
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.
const TYPE_PRODUCT
Regular product.
const TYPE_SERVICE
Service.
llxFooter()
Footer empty.
Definition document.php:107
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
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.
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.