dolibarr 21.0.0-alpha
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$object = $product;
62
63$usercanread = (($object->type == Product::TYPE_PRODUCT && $user->hasRight('produit', 'lire')) || ($object->type == Product::TYPE_SERVICE && $user->hasRight('service', 'lire')));
64$usercancreate = (($object->type == Product::TYPE_PRODUCT && $user->hasRight('produit', 'creer')) || ($object->type == Product::TYPE_SERVICE && $user->hasRight('service', 'creer')));
65$usercandelete = (($object->type == Product::TYPE_PRODUCT && $user->hasRight('produit', 'supprimer')) || ($object->type == Product::TYPE_SERVICE && $user->hasRight('service', 'supprimer')));
66
67
68/*
69 * Actions
70 */
71
72if ($action == 'add' && $usercancreate) {
73 if ($eid == 0) {
74 $result = $price_expression->find_title($title);
75 if ($result == 0) { //No existing entry found with title, ok
76 // Check the expression validity by parsing it
77 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
78 $priceparser = new PriceParser($db);
79 $price_result = $priceparser->testExpression($id, $expression);
80 if ($price_result < 0) { //Expression is not valid
81 setEventMessages($priceparser->translatedError(), null, 'errors');
82 } else {
83 $price_expression->title = $title;
84 $price_expression->expression = $expression;
85 $result = $price_expression->create($user);
86 if ($result > 0) { //created successfully, set the eid to newly created entry
87 $eid = $price_expression->id;
88 setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
89 } else {
90 setEventMessages("add: ".$price_expression->error, $price_expression->errors, 'errors');
91 }
92 }
93 } elseif ($result < 0) {
94 setEventMessages("add find: ".$price_expression->error, $price_expression->errors, 'errors');
95 } else {
96 setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
97 }
98 }
99}
100
101if ($action == 'update' && $usercancreate) {
102 if ($eid != 0) {
103 $result = $price_expression->find_title($title);
104 if ($result == 0 || $result == $eid) { //No existing entry found with title or existing one is the current one, ok
105 // Check the expression validity by parsing it
106 require_once DOL_DOCUMENT_ROOT.'/product/dynamic_price/class/price_parser.class.php';
107 $priceparser = new PriceParser($db);
108 $price_result = $priceparser->testExpression($id, $expression);
109 if ($price_result < 0) { //Expression is not valid
110 setEventMessages($priceparser->translatedError(), null, 'errors');
111 } else {
112 $price_expression->id = $eid;
113 $price_expression->title = $title;
114 $price_expression->expression = $expression;
115 $result = $price_expression->update($user);
116 if ($result < 0) {
117 setEventMessages("update: ".$price_expression->error, $price_expression->errors, 'errors');
118 } else {
119 setEventMessages($langs->trans("RecordSaved"), null, 'mesgs');
120 }
121 }
122 } elseif ($result < 0) {
123 setEventMessages("update find: ".$price_expression->error, $price_expression->errors, 'errors');
124 } else {
125 setEventMessages($langs->trans("ErrorRecordAlreadyExists"), null, 'errors');
126 }
127 }
128}
129
130if ($action == 'delete' && $usercancreate) {
131 if ($eid != 0) {
132 $price_expression->fetch($eid);
133 $result = $price_expression->delete($user);
134 if ($result < 0) {
135 setEventMessages("delete: ".$price_expression->error, $price_expression->errors, 'errors');
136 }
137 $eid = 0;
138 }
139}
140
141
142/*
143 * View
144 */
145
146$form = new Form($db);
147
148llxHeader("", "", $langs->trans("CardProduct".$product->type), '', 0, 0, '', '', '', 'mod-product page-dynamic_price_editor');
149
150print load_fiche_titre($langs->trans("PriceExpressionEditor"));
151
152//Form/Table
153print '<form action="'.$_SERVER['PHP_SELF'].'?id='.$id.'&amp;tab='.$tab.'&amp;eid='.$eid.'" method="POST">';
154print '<input type="hidden" name="token" value="'.newToken().'">';
155print '<input type="hidden" name="action" value='.($eid == 0 ? 'add' : 'update').'>';
156
157print dol_get_fiche_head();
158
159print '<table class="border centpercent">';
160
161// Price expression selector
162print '<tr><td class="titlefield fieldrequired">'.$langs->trans("PriceExpressionSelected").'</td><td>';
163$price_expression_list = array(0 => $langs->trans("New")); //Put the new as first option
164foreach ($price_expression->list_price_expression() as $entry) {
165 $price_expression_list[$entry->id] = $entry->title;
166}
167print $form->selectarray('expression_selection', $price_expression_list, $eid);
168print '</td></tr>';
169
170// Title input
171print '<tr><td class="fieldrequired">'.$langs->trans("Name").'</td><td>';
172print '<input class="flat" name="expression_title" size="15" value="'.(GETPOSTISSET('expression_title') ? GETPOST('expression_title') : ($price_expression->title ? $price_expression->title : '')).'">';
173print '</td></tr>';
174
175//Help text
176$help_text = $langs->trans("PriceExpressionEditorHelp1");
177$help_text .= '<br><br>'.$langs->trans("PriceExpressionEditorHelp2");
178$help_text .= '<br><br>'.$langs->trans("PriceExpressionEditorHelp3");
179$help_text .= '<br><br>'.$langs->trans("PriceExpressionEditorHelp4");
180$help_text .= '<br><br>'.$langs->trans("PriceExpressionEditorHelp5");
181foreach ($price_globals->listGlobalVariables() as $entry) {
182 $help_text .= '<br><b>#global_'.$entry->code.'#</b> '.$entry->description.' = '.$entry->value;
183}
184
185//Price expression editor
186print '<tr><td class="fieldrequired">'.$form->textwithpicto($langs->trans("PriceExpressionEditor"), $help_text, 1).'</td><td>';
187require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
188$doleditor = new DolEditor('expression', isset($price_expression->expression) ? $price_expression->expression : '', '', 300, '', '', false, false, false, ROWS_4, '90%');
189$doleditor->Create();
190print '</td></tr>';
191print '</table>';
192
193print dol_get_fiche_end();
194
195//Buttons
196print '<div class="center">';
197print '<input type="submit" class="butAction button-save" value="'.$langs->trans("Save").'">';
198print '<span id="back" class="butAction">'.$langs->trans("Back").'</span>';
199if ($eid == 0) {
200 print '<div class="inline-block divButAction"><span id="action-delete" class="butActionRefused classfortooltip">'.$langs->trans('Delete').'</span></div>'."\n";
201} else {
202 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>';
203}
204print '</div>';
205
206print '</form>';
207
208// This code reloads the page depending of selected option, goes to page selected by tab when back is pressed
209print '<script type="text/javascript">
210 jQuery(document).ready(run);
211 function run() {
212 jQuery("#back").click(on_click);
213 jQuery("#expression_selection").change(on_change);
214 }
215 function on_click() {
216 window.location = "'.str_replace('dynamic_price/editor.php', $tab.'.php', $_SERVER["PHP_SELF"]).'?id='.$id.($tab == 'price' ? '&action=edit_price&token='.newToken() : '').'";
217 }
218 function on_change() {
219 window.location = "'.$_SERVER["PHP_SELF"].'?id='.$id.'&tab='.$tab.'&eid=" + $("#expression_selection").val();
220 }
221</script>';
222
223// End of page
224llxFooter();
225$db->close();
$id
Definition account.php:39
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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:70
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.