20use Luracast\Restler\RestException;
54 $this->myobject =
new MyObject($this->db);
77 public function get(
$id)
79 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'read')) {
80 throw new RestException(403);
83 throw new RestException(403,
'Access to instance id='.
$id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
86 $result = $this->myobject->fetch(
$id);
88 throw new RestException(404,
'MyObject not found');
115 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
118 $tmpobject =
new MyObject($this->db);
120 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'read')) {
121 throw new RestException(403);
124 $socid = DolibarrApiAccess::$user->socid ?: 0;
126 $restrictonsocid = 0;
130 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
131 $search_sale = DolibarrApiAccess::$user->id;
133 if (!isModEnabled(
'societe')) {
137 $sql =
"SELECT t.rowid";
138 $sql .=
" FROM ".$this->db->prefix().$tmpobject->table_element.
" AS t";
139 $sql .=
" LEFT JOIN ".$this->db->prefix().$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
140 $sql .=
" WHERE 1 = 1";
141 if ($tmpobject->ismultientitymanaged) {
142 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
144 if ($restrictonsocid && $socid) {
145 $sql .=
" AND t.fk_soc = ".((int) $socid);
148 if ($search_sale && $search_sale !=
'-1') {
149 if ($search_sale == -2) {
150 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".$this->db->prefix().
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
151 } elseif ($search_sale > 0) {
152 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".$this->db->prefix().
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
159 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
163 $sql .= $this->db->order($sortfield, $sortorder);
168 $offset = $limit * $page;
170 $sql .= $this->db->plimit($limit + 1, $offset);
173 $result = $this->db->query($sql);
176 $num = $this->db->num_rows($result);
178 $obj = $this->db->fetch_object($result);
179 $tmp_object =
new MyObject($this->db);
180 if ($tmp_object->fetch($obj->rowid)) {
186 throw new RestException(503,
'Error when retrieving myobject list: '.$this->db->lasterror());
205 public function post($request_data =
null)
207 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'write')) {
208 throw new RestException(403);
214 foreach ($request_data as $field => $value) {
215 if ($field ===
'caller') {
217 $this->myobject->context[
'caller'] =
sanitizeVal((
string) $request_data[
'caller'],
'aZ09');
221 if ($field ==
'array_options' && is_array($value)) {
222 foreach ($value as $index => $val) {
223 $this->myobject->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->myobject);
228 $this->myobject->$field = $this->
_checkValForAPI((
string) $field, $value, $this->myobject);
234 if ($this->myobject->create(DolibarrApiAccess::$user) < 0) {
235 throw new RestException(500,
"Error creating MyObject", array_merge(array($this->myobject->error), $this->myobject->errors));
237 return $this->myobject->id;
257 public function put(
$id, $request_data =
null)
259 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'write')) {
260 throw new RestException(403);
263 throw new RestException(403,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
266 $result = $this->myobject->fetch(
$id);
268 throw new RestException(404,
'MyObject not found');
271 foreach ($request_data as $field => $value) {
272 if ($field ==
'id') {
275 if ($field ===
'caller') {
277 $this->myobject->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
281 if ($field ==
'array_options' && is_array($value)) {
282 foreach ($value as $index => $val) {
283 $this->myobject->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->myobject);
288 if ($field ==
'array_options' && is_array($value)) {
289 foreach ($value as $index => $val) {
290 $this->myobject->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->myobject);
295 $this->myobject->$field = $this->
_checkValForAPI($field, $value, $this->myobject);
301 if ($this->myobject->update(DolibarrApiAccess::$user, 0) > 0) {
302 return $this->
get(
$id);
304 throw new RestException(500, $this->myobject->error);
323 public function delete(
$id)
325 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'delete')) {
326 throw new RestException(403);
329 throw new RestException(403,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
332 $result = $this->myobject->fetch(
$id);
334 throw new RestException(404,
'MyObject not found');
337 if ($this->myobject->delete(DolibarrApiAccess::$user) == 0) {
338 throw new RestException(409,
'Error when deleting MyObject : '.$this->myobject->error);
339 } elseif ($this->myobject->delete(DolibarrApiAccess::$user) < 0) {
340 throw new RestException(500,
'Error when deleting MyObject : '.$this->myobject->error);
346 'message' =>
'MyObject deleted'
366 if (!is_array($data)) {
370 foreach ($this->myobject->fields as $field => $propfield) {
371 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || $propfield[
'notnull'] != 1) {
374 if (!isset($data[$field])) {
375 throw new RestException(400,
"$field field missing");
377 $myobject[$field] = $data[$field];
407 $nboflines = count(
$object->lines);
408 for ($i = 0; $i < $nboflines; $i++) {
411 unset(
$object->lines[$i]->lines);
412 unset(
$object->lines[$i]->note);
$id
Support class for third parties, contacts, members, users or resources.
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.