20use Luracast\Restler\RestException;
55 $this->myobject =
new MyObject($this->db);
78 public function get(
$id)
80 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'read')) {
81 throw new RestException(403);
84 throw new RestException(403,
'Access to instance id='.
$id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
87 $result = $this->myobject->fetch(
$id);
89 throw new RestException(404,
'MyObject not found');
116 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
119 $tmpobject =
new MyObject($this->db);
121 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'read')) {
122 throw new RestException(403);
125 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
127 $restrictonsocid = 0;
131 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
132 $search_sale = DolibarrApiAccess::$user->id;
134 if (!isModEnabled(
'societe')) {
138 $sql =
"SELECT t.rowid";
139 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" AS t";
140 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
141 $sql .=
" WHERE 1 = 1";
142 if ($tmpobject->ismultientitymanaged) {
143 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
145 if ($restrictonsocid && $socid) {
146 $sql .=
" AND t.fk_soc = ".((int) $socid);
149 if ($search_sale && $search_sale !=
'-1') {
150 if ($search_sale == -2) {
151 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
152 } elseif ($search_sale > 0) {
153 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
160 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
164 $sql .= $this->db->order($sortfield, $sortorder);
169 $offset = $limit * $page;
171 $sql .= $this->db->plimit($limit + 1, $offset);
174 $result = $this->db->query($sql);
177 $num = $this->db->num_rows($result);
179 $obj = $this->db->fetch_object($result);
180 $tmp_object =
new MyObject($this->db);
181 if ($tmp_object->fetch($obj->rowid)) {
187 throw new RestException(503,
'Error when retrieving myobject list: '.$this->db->lasterror());
206 public function post($request_data =
null)
208 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'write')) {
209 throw new RestException(403);
215 foreach ($request_data as $field => $value) {
216 if ($field ===
'caller') {
218 $this->myobject->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
222 if ($field ==
'array_options' && is_array($value)) {
223 foreach ($value as $index => $val) {
224 $this->myobject->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->myobject);
229 $this->myobject->$field = $this->
_checkValForAPI((
string) $field, $value, $this->myobject);
235 if ($this->myobject->create(DolibarrApiAccess::$user) < 0) {
236 throw new RestException(500,
"Error creating MyObject", array_merge(array($this->myobject->error), $this->myobject->errors));
238 return $this->myobject->id;
260 public function put(
$id, $request_data =
null)
262 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'write')) {
263 throw new RestException(403);
266 throw new RestException(403,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
269 $result = $this->myobject->fetch(
$id);
271 throw new RestException(404,
'MyObject not found');
274 foreach ($request_data as $field => $value) {
275 if ($field ==
'id') {
278 if ($field ===
'caller') {
280 $this->myobject->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
284 if ($field ==
'array_options' && is_array($value)) {
285 foreach ($value as $index => $val) {
286 $this->myobject->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->myobject);
291 $this->myobject->$field = $this->
_checkValForAPI($field, $value, $this->myobject);
297 if ($this->myobject->update(DolibarrApiAccess::$user,
false) > 0) {
298 return $this->
get(
$id);
300 throw new RestException(500, $this->myobject->error);
319 public function delete(
$id)
321 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'delete')) {
322 throw new RestException(403);
325 throw new RestException(403,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
328 $result = $this->myobject->fetch(
$id);
330 throw new RestException(404,
'MyObject not found');
333 if ($this->myobject->delete(DolibarrApiAccess::$user) == 0) {
334 throw new RestException(409,
'Error when deleting MyObject : '.$this->myobject->error);
335 } elseif ($this->myobject->delete(DolibarrApiAccess::$user) < 0) {
336 throw new RestException(500,
'Error when deleting MyObject : '.$this->myobject->error);
342 'message' =>
'MyObject deleted'
363 foreach ($this->myobject->fields as $field => $propfield) {
364 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || $propfield[
'notnull'] != 1) {
367 if (!isset($data[$field])) {
368 throw new RestException(400,
"$field field missing");
370 $myobject[$field] = $data[$field];
400 $nboflines = count(
$object->lines);
401 for ($i = 0; $i < $nboflines; $i++) {
404 unset(
$object->lines[$i]->lines);
405 unset(
$object->lines[$i]->note);
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_validateMyObject($data)
Validate fields before creating or updating object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List myobjects.
_cleanObjectDatas($object)
Clean sensitive object data fields @phpstan-template T of Object.
__construct()
Constructor.
post($request_data=null)
Create myobject object.
put($id, $request_data=null)
Update myobject.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.