dolibarr 21.0.0-alpha
objectonoff.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015-2023 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
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
25if (!defined('NOTOKENRENEWAL')) {
26 define('NOTOKENRENEWAL', '1'); // Disables token renewal
27}
28if (!defined('NOREQUIREMENU')) {
29 define('NOREQUIREMENU', '1');
30}
31if (!defined('NOREQUIREHTML')) {
32 define('NOREQUIREHTML', '1');
33}
34if (!defined('NOREQUIREAJAX')) {
35 define('NOREQUIREAJAX', '1');
36}
37if (!defined('NOREQUIRESOC')) {
38 define('NOREQUIRESOC', '1');
39}
40
41// Load Dolibarr environment
42require '../../main.inc.php';
43require_once DOL_DOCUMENT_ROOT.'/core/class/genericobject.class.php';
44
45$action = GETPOST('action', 'aZ09');
46$backtopage = GETPOST('backtopage');
47
48$id = GETPOSTINT('id');
49$element = GETPOST('element', 'alpha'); // 'myobject' (myobject=mymodule) or 'myobject@mymodule' or 'myobject_mysubobject' (myobject=mymodule)
50$field = GETPOST('field', 'alpha');
51$value = GETPOSTINT('value');
52$format = 'int';
53
54// Load object according to $id and $element
55$object = fetchObjectByElement($id, $element);
56if (!is_object($object)) {
57 httponly_accessforbidden("Bad value for combination of parameters element/field: Object not found."); // This includes the exit.
58}
59'@phan-var-force CommonObject $object';
60
61$object->fields[$field] = array('type' => $format, 'enabled' => 1);
62
63$module = $object->module;
64$element = $object->element;
65$usesublevelpermission = ($module != $element ? $element : '');
66if ($usesublevelpermission && !$user->hasRight($module, $element)) { // There is no permission on object defined, we will check permission on module directly
67 $usesublevelpermission = '';
68}
69
70//print $object->id.' - '.$object->module.' - '.$object->element.' - '.$object->table_element.' - '.$usesublevelpermission."\n";
71
72// Security check
73if (!empty($user->socid)) {
74 $socid = $user->socid;
75 if (!empty($object->socid) && $socid != $object->socid) {
76 httponly_accessforbidden("Access on object not allowed for this external user."); // This includes the exit.
77 }
78}
79
80// We check permission.
81// Check is done on $user->rights->element->create or $user->rights->element->subelement->create (because $action = 'set')
82if (preg_match('/stat[u][st]$/', $field) || ($field == 'evenunsubscribe' && $object->table_element == 'mailing')) {
83 restrictedArea($user, $object->module, $object, $object->table_element, $usesublevelpermission);
84} elseif ($element == 'product' && in_array($field, array('tosell', 'tobuy', 'tobatch'))) { // Special case for products
85 restrictedArea($user, 'produit|service', $object, 'product&product', '', '', 'rowid');
86} else {
87 httponly_accessforbidden("Bad value for combination of parameters element/field: Field not supported."); // This includes the exit.
88}
89
90
91/*
92 * Actions
93 */
94
95// None
96
97
98/*
99 * View
100 */
101
103
104print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
105
106// Registering new values
107if (($action == 'set') && !empty($id)) { // Test on permission already done in header according to object and field.
108 $triggerkey = strtoupper(($module != $element ? $module.'_' : '').$element).'_UPDATE';
109 // Special case
110 if ($triggerkey == 'SOCIETE_UPDATE') {
111 $triggerkey = 'COMPANY_MODIFY';
112 }
113 if ($triggerkey == 'PRODUCT_UPDATE') {
114 $triggerkey = 'PRODUCT_MODIFY';
115 }
116
117 $result = $object->setValueFrom($field, $value, $object->table_element, $id, $format, '', $user, $triggerkey);
118
119 if ($result < 0) {
120 print $object->error;
121 http_response_code(500);
122 exit;
123 }
124
125 if ($backtopage) {
126 header('Location: '.$backtopage);
127 exit;
128 }
129}
$id
Definition account.php:39
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
fetchObjectByElement($element_id, $element_type, $element_ref='', $useCache=0, $maxCacheByType=10)
Fetch an object from its id and element_type Inclusion of classes is automatic.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
httponly_accessforbidden($message='1', $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.
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.