dolibarr 22.0.5
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
27if (!defined('NOTOKENRENEWAL')) {
28 define('NOTOKENRENEWAL', '1'); // Disables token renewal
29}
30if (!defined('NOREQUIREMENU')) {
31 define('NOREQUIREMENU', '1');
32}
33if (!defined('NOREQUIREHTML')) {
34 define('NOREQUIREHTML', '1');
35}
36if (!defined('NOREQUIREAJAX')) {
37 define('NOREQUIREAJAX', '1');
38}
39if (!defined('NOREQUIRESOC')) {
40 define('NOREQUIRESOC', '1');
41}
42if (!defined('CSRFCHECK_WITH_TOKEN')) {
43 define('CSRFCHECK_WITH_TOKEN', '1'); // Token is required even in GET mode
44}
45
46// Load Dolibarr environment
47require '../../main.inc.php';
48require_once DOL_DOCUMENT_ROOT.'/core/class/genericobject.class.php';
49
58$action = GETPOST('action', 'aZ09');
59$backtopage = GETPOST('backtopage');
60
61$id = GETPOSTINT('id');
62$element = GETPOST('element', 'alpha'); // 'myobject' (myobject=mymodule) or 'myobject@mymodule' or 'myobject_mysubobject' (myobject=mymodule)
63$field = GETPOST('field', 'alpha');
64$value = GETPOSTINT('value');
65$format = 'int';
66
67// Load object according to $id and $element
68$object = fetchObjectByElement($id, $element);
69if (!is_object($object)) {
70 httponly_accessforbidden("Bad value for combination of parameters element/field: Object not found."); // This includes the exit.
71}
72'@phan-var-force CommonObject $object';
73
74$object->fields[$field] = array('type' => $format, 'enabled' => 1);
75
76$module = $object->module;
77$element = $object->element;
78$usesublevelpermission = ($module != $element ? $element : '');
79if ($usesublevelpermission && !$user->hasRight($module, $element)) { // There is no permission on object defined, we will check permission on module directly
80 $usesublevelpermission = '';
81}
82
83//print $object->id.' - '.$object->module.' - '.$object->element.' - '.$object->table_element.' - '.$usesublevelpermission."\n";
84
85// Security check
86if (!empty($user->socid)) {
87 $socid = $user->socid;
88 if (!empty($object->socid) && $socid != $object->socid) {
89 httponly_accessforbidden("Access on object not allowed for this external user."); // This includes the exit.
90 }
91}
92
93// We check permission.
94// Check is done on $user->rights->element->create or $user->rights->element->subelement->create (because $action = 'set')
95if (preg_match('/stat[u][st]$/', $field) || ($field == 'evenunsubscribe' && $object->table_element == 'mailing')) {
96 restrictedArea($user, $object->module, $object, $object->table_element, $usesublevelpermission);
97} elseif ($element == 'product' && in_array($field, array('tosell', 'tobuy', 'tobatch'))) { // Special case for products
98 restrictedArea($user, 'produit|service', $object, 'product&product', '', '', 'rowid');
99} else {
100 httponly_accessforbidden("Bad value for combination of parameters element/field: Field not supported."); // This includes the exit.
101}
102
103
104/*
105 * Actions
106 */
107
108// None
109
110
111/*
112 * View
113 */
114
116
117print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
118
119// Registering new values
120if (($action == 'set') && !empty($id)) { // Test on permission already done in header according to object and field.
121 $triggerkey = strtoupper(($module != $element ? $module.'_' : '').$element).'_UPDATE';
122 // Special case
123 if ($triggerkey == 'SOCIETE_UPDATE') {
124 $triggerkey = 'COMPANY_MODIFY';
125 }
126 if ($triggerkey == 'PRODUCT_UPDATE') {
127 $triggerkey = 'PRODUCT_MODIFY';
128 }
129
130 $result = $object->setValueFrom($field, $value, $object->table_element, $id, $format, '', $user, $triggerkey);
131
132 if ($result < 0) {
133 print $object->error;
134 http_response_code(500);
135 exit;
136 }
137
138 if ($backtopage) {
139 header('Location: '.$backtopage);
140 exit;
141 }
142}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
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.