19use Luracast\Restler\RestException;
54 $this->myobject =
new MyObject($this->db);
73 public function get($id)
75 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'read')) {
76 throw new RestException(403);
79 throw new RestException(403,
'Access to instance id='.$id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
82 $result = $this->myobject->fetch($id);
84 throw new RestException(404,
'MyObject not found');
109 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
112 $tmpobject =
new MyObject($this->db);
114 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'read')) {
115 throw new RestException(403);
118 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
120 $restrictonsocid = 0;
124 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
125 $search_sale = DolibarrApiAccess::$user->id;
127 if (!isModEnabled(
'societe')) {
131 $sql =
"SELECT t.rowid";
132 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" AS t";
133 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
134 $sql .=
" WHERE 1 = 1";
135 if ($tmpobject->ismultientitymanaged) {
136 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
138 if ($restrictonsocid && $socid) {
139 $sql .=
" AND t.fk_soc = ".((int) $socid);
142 if ($search_sale && $search_sale !=
'-1') {
143 if ($search_sale == -2) {
144 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
145 } elseif ($search_sale > 0) {
146 $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).
")";
153 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
157 $sql .= $this->db->order($sortfield, $sortorder);
162 $offset = $limit * $page;
164 $sql .= $this->db->plimit($limit + 1, $offset);
167 $result = $this->db->query($sql);
170 $num = $this->db->num_rows($result);
172 $obj = $this->db->fetch_object($result);
173 $tmp_object =
new MyObject($this->db);
174 if ($tmp_object->fetch($obj->rowid)) {
180 throw new RestException(503,
'Error when retrieving myobject list: '.$this->db->lasterror());
197 public function post($request_data =
null)
199 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'write')) {
200 throw new RestException(403);
206 foreach ($request_data as $field => $value) {
207 if ($field ===
'caller') {
209 $this->myobject->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
213 if ($field ==
'array_options' && is_array($value)) {
214 foreach ($value as $index => $val) {
215 $this->myobject->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->myobject);
220 $this->myobject->$field = $this->
_checkValForAPI($field, $value, $this->myobject);
226 if ($this->myobject->create(DolibarrApiAccess::$user)<0) {
227 throw new RestException(500,
"Error creating MyObject", array_merge(array($this->myobject->error), $this->myobject->errors));
229 return $this->myobject->id;
245 public function put($id, $request_data =
null)
247 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'write')) {
248 throw new RestException(403);
251 throw new RestException(403,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
254 $result = $this->myobject->fetch($id);
256 throw new RestException(404,
'MyObject not found');
259 foreach ($request_data as $field => $value) {
260 if ($field ==
'id') {
263 if ($field ===
'caller') {
265 $this->myobject->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
269 if ($field ==
'array_options' && is_array($value)) {
270 foreach ($value as $index => $val) {
271 $this->myobject->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->myobject);
276 $this->myobject->$field = $this->
_checkValForAPI($field, $value, $this->myobject);
282 if ($this->myobject->update(DolibarrApiAccess::$user,
false) > 0) {
283 return $this->
get($id);
285 throw new RestException(500, $this->myobject->error);
302 public function delete($id)
304 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'delete')) {
305 throw new RestException(403);
308 throw new RestException(403,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
311 $result = $this->myobject->fetch($id);
313 throw new RestException(404,
'MyObject not found');
316 if ($this->myobject->delete(DolibarrApiAccess::$user) == 0) {
317 throw new RestException(409,
'Error when deleting MyObject : '.$this->myobject->error);
318 } elseif ($this->myobject->delete(DolibarrApiAccess::$user) < 0) {
319 throw new RestException(500,
'Error when deleting MyObject : '.$this->myobject->error);
325 'message' =>
'MyObject deleted'
342 foreach ($this->myobject->fields as $field => $propfield) {
343 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || $propfield[
'notnull'] != 1) {
346 if (!isset($data[$field])) {
347 throw new RestException(400,
"$field field missing");
349 $myobject[$field] = $data[$field];
375 $nboflines = count(
$object->lines);
376 for ($i = 0; $i < $nboflines; $i++) {
379 unset(
$object->lines[$i]->lines);
380 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 create or update object.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List myobjects.
_cleanObjectDatas($object)
Clean sensible object datas.
__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.