dolibarr 24.0.1
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}
40
41// Load Dolibarr environment
42$res = 0;
43// Try main.inc.php into web root known defined into CONTEXT_DOCUMENT_ROOT (not always defined)
44if (!$res && !empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) {
45 $res = @include $_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php";
46}
47// Try main.inc.php into web root detected using web root calculated from SCRIPT_FILENAME
48$tmp = empty($_SERVER['SCRIPT_FILENAME']) ? '' : $_SERVER['SCRIPT_FILENAME'];
49$tmp2 = realpath(__FILE__);
50$i = strlen($tmp) - 1;
51$j = strlen($tmp2) - 1;
52while ($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i] == $tmp2[$j]) {
53 $i--;
54 $j--;
55}
56if (!$res && $i > 0 && file_exists(substr($tmp, 0, ($i + 1))."/main.inc.php")) {
57 $res = @include substr($tmp, 0, ($i + 1))."/main.inc.php";
58}
59if (!$res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php")) {
60 $res = @include dirname(substr($tmp, 0, ($i + 1)))."/main.inc.php";
61}
62// Try main.inc.php using relative path
63if (!$res && file_exists("../../main.inc.php")) {
64 $res = @include "../../main.inc.php";
65}
66if (!$res && file_exists("../../../main.inc.php")) {
67 $res = @include "../../../main.inc.php";
68}
69if (!$res) {
70 http_response_code(500);
71 die("Include of main fails");
72}
80dol_include_once('/mymodule/class/myobject.class.php');
81
82
83$mode = GETPOST('mode', 'aZ09');
84$objectId = GETPOSTINT('objectId');
85$field = GETPOST('field', 'aZ09');
86$value = GETPOST('value', 'aZ09');
87
88// @phan-suppress-next-line PhanUndeclaredClass
89$object = new MyObject($db);
90
91// Security check
92if (!$user->hasRight('mymodule', 'myobject', 'write')) {
94}
95
96/*
97 * View
98 */
99
100dol_syslog("Call ajax mymodule/ajax/myobject.php");
101
103
104// Update the object field with the new value
105if ($objectId && $field && isset($value)) {
106 $object->fetch($objectId);
107 if ($object->id > 0) {
108 $object->$field = $value;
109 }
110 $result = $object->update($user);
111
112 if ($result < 0) {
113 print json_encode(['status' => 'error', 'message' => 'Error updating '. $field]);
114 } else {
115 print json_encode(['status' => 'success', 'message' => $field . ' updated successfully']);
116 }
117}
118
119$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.
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, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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.