dolibarr 24.0.0-beta
myobject.php
1<?php
2/* Copyright (C) 2022 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) ---Replace with your own copyright and developer email---
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
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}
40if (!defined('NOCSRFCHECK')) {
41 define('NOCSRFCHECK', '1');
42}
43if (!defined('NOREQUIREHTML')) {
44 define('NOREQUIREHTML', '1');
45}
46
47// Load Dolibarr environment
48$res = 0;
49// Try main.inc.php into web root known defined into CONTEXT_DOCUMENT_ROOT (not always defined)
50if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) {
51 $res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php";
52}
53// Try main.inc.php into web root detected using web root calculated from SCRIPT_FILENAME
54$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME'];
55$tmp2 = realpath(__FILE__);
56$i = strlen($tmp) - 1;
57$j = strlen($tmp2) - 1;
58while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
59 $i--;
60 $j--;
61}
62if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/main.inc.php")) {
63 $res = @include substr($tmp, 0, ($i + 1))."/main.inc.php";
64}
65if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php")) {
66 $res = @include dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php";
67}
68// Try main.inc.php using relative path
69if (!$res && file_exists("../../main.inc.php")) {
70 $res = @include "../../main.inc.php";
71}
72if (!$res && file_exists("../../../main.inc.php")) {
73 $res = @include "../../../main.inc.php";
74}
75if (!$res) {
76 http_response_code(500);
77 die("Include of main fails");
78}
86dol_include_once('/mymodule/class/myobject.class.php');
87
88
89$mode = GETPOST('mode', 'aZ09');
90$objectId = GETPOSTINT('objectId');
91$field = GETPOST('field', 'aZ09');
92$value = GETPOST('value', 'aZ09');
93
94// @phan-suppress-next-line PhanUndeclaredClass
95$object = new MyObject($db);
96
97// Security check
98if (!$user->hasRight('mymodule', 'myobject', 'write')) {
100}
101
102/*
103 * View
104 */
105
106dol_syslog("Call ajax mymodule/ajax/myobject.php");
107
109
110// Update the object field with the new value
111if ($objectId && $field && isset($value)) {
112 $object->fetch($objectId);
113 if ($object->id > 0) {
114 $object->$field = $value;
115 }
116 $result = $object->update($user);
117
118 if ($result < 0) {
119 print json_encode(['status' => 'error', 'message' => 'Error updating '. $field]);
120 } else {
121 print json_encode(['status' => 'success', 'message' => $field . ' updated successfully']);
122 }
123}
124
125$db->close();
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for MyObject.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.