19 use Luracast\Restler\RestException;
70 public function get($id)
72 if (!DolibarrApiAccess::$user->rights->mymodule->read) {
73 throw new RestException(401);
76 $result = $this->myobject->fetch($id);
78 throw new RestException(404,
'MyObject not found');
82 throw new RestException(401,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
105 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
112 if (!DolibarrApiAccess::$user->rights->mymodule->myobject->read) {
113 throw new RestException(401);
116 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
118 $restrictonsocid = 0;
122 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id;
124 $sql =
"SELECT t.rowid";
125 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .=
", sc.fk_soc, sc.fk_user";
126 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" as t";
128 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
129 $sql .=
" WHERE 1 = 1";
135 if ($tmpobject->ismultientitymanaged) $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
136 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .=
" AND t.fk_soc = sc.fk_soc";
137 if ($restrictonsocid && $socid) $sql .=
" AND t.fk_soc = ".$socid;
138 if ($restrictonsocid && $search_sale > 0) $sql .=
" AND t.rowid = sc.fk_soc";
140 if ($restrictonsocid && $search_sale > 0) {
141 $sql .=
" AND sc.fk_user = ".$search_sale;
146 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
148 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
149 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
152 $sql .= $this->
db->order($sortfield, $sortorder);
157 $offset = $limit * $page;
159 $sql .= $this->
db->plimit($limit + 1, $offset);
162 $result = $this->
db->query($sql);
166 $num = $this->
db->num_rows($result);
169 $obj = $this->
db->fetch_object($result);
171 if ($tmp_object->fetch($obj->rowid)) {
177 throw new RestException(503,
'Error when retrieving myobject list: '.$this->
db->lasterror());
179 if (!count($obj_ret)) {
180 throw new RestException(404,
'No myobject found');
195 public function post($request_data = null)
197 if (!DolibarrApiAccess::$user->rights->mymodule->write) {
198 throw new RestException(401);
201 $result = $this->
_validate($request_data);
203 foreach ($request_data as $field => $value) {
204 $this->myobject->$field = $value;
206 if ($this->myobject->create(DolibarrApiAccess::$user)<0) {
207 throw new RestException(500,
"Error creating MyObject", array_merge(array($this->myobject->error), $this->myobject->errors));
209 return $this->myobject->id;
223 public function put($id, $request_data = null)
225 if (!DolibarrApiAccess::$user->rights->mymodule->write) {
226 throw new RestException(401);
229 $result = $this->myobject->fetch($id);
231 throw new RestException(404,
'MyObject not found');
235 throw new RestException(401,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
238 foreach ($request_data as $field => $value) {
239 if ($field ==
'id')
continue;
240 $this->myobject->$field = $value;
243 if ($this->myobject->update(DolibarrApiAccess::$user,
false) > 0)
245 return $this->
get($id);
247 throw new RestException(500, $this->myobject->error);
261 public function delete($id)
263 if (!DolibarrApiAccess::$user->rights->mymodule->delete) {
264 throw new RestException(401);
266 $result = $this->myobject->fetch($id);
268 throw new RestException(404,
'MyObject not found');
272 throw new RestException(401,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
275 if (!$this->myobject->delete(DolibarrApiAccess::$user))
277 throw new RestException(500,
'Error when deleting MyObject : '.$this->myobject->error);
283 'message' =>
'MyObject deleted'
299 $object = parent::_cleanObjectDatas($object);
301 unset($object->rowid);
302 unset($object->canvas);
339 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
340 $nboflines = count($object->lines);
341 for ($i = 0; $i < $nboflines; $i++)
345 unset($object->lines[$i]->lines);
346 unset($object->lines[$i]->note);
364 foreach ($this->myobject->fields as $field => $propfield) {
365 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || $propfield[
'notnull'] != 1)
continue;
366 if (!isset($data[$field]))
367 throw new RestException(400,
"$field field missing");
368 $myobject[$field] = $data[$field];
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname= '')
Make an include_once using default root and alternate root if it fails.
put($id, $request_data=null)
Update myobject.
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $sqlfilters= '')
List myobjects.
post($request_data=null)
Create myobject object.
_cleanObjectDatas($object)
Clean sensible object datas.
_validate($data)
Validate fields before create or update object.
__construct()
Constructor.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.