108 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
113 $tmpobject =
new MyObject($this->db);
115 if (!DolibarrApiAccess::$user->rights->mymodule->myobject->read) {
116 throw new RestException(401);
119 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
121 $restrictonsocid = 0;
125 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
126 $search_sale = DolibarrApiAccess::$user->id;
129 $sql =
"SELECT t.rowid";
130 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
131 $sql .=
", sc.fk_soc, sc.fk_user";
133 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" AS t LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
135 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
136 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
138 $sql .=
" WHERE 1 = 1";
144 if ($tmpobject->ismultientitymanaged) {
145 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
147 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
148 $sql .=
" AND t.fk_soc = sc.fk_soc";
150 if ($restrictonsocid && $socid) {
151 $sql .=
" AND t.fk_soc = ".((int) $socid);
153 if ($restrictonsocid && $search_sale > 0) {
154 $sql .=
" AND t.rowid = sc.fk_soc";
157 if ($restrictonsocid && $search_sale > 0) {
158 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
164 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
168 $sql .= $this->db->order($sortfield, $sortorder);
173 $offset = $limit * $page;
175 $sql .= $this->db->plimit($limit + 1, $offset);
178 $result = $this->db->query($sql);
181 $num = $this->db->num_rows($result);
183 $obj = $this->db->fetch_object($result);
184 $tmp_object =
new MyObject($this->db);
185 if ($tmp_object->fetch($obj->rowid)) {
191 throw new RestException(503,
'Error when retrieving myobject list: '.$this->db->lasterror());
207 public function post($request_data =
null)
209 if (!DolibarrApiAccess::$user->rights->mymodule->myobject->write) {
210 throw new RestException(401);
214 $result = $this->
_validate($request_data);
216 foreach ($request_data as $field => $value) {
217 if ($field ===
'caller') {
219 $this->myobject->context[
'caller'] = $request_data[
'caller'];
223 $this->myobject->$field = $this->
_checkValForAPI($field, $value, $this->myobject);
229 if ($this->myobject->create(DolibarrApiAccess::$user)<0) {
230 throw new RestException(500,
"Error creating MyObject", array_merge(array($this->myobject->error), $this->myobject->errors));
232 return $this->myobject->id;
246 public function put($id, $request_data =
null)
248 if (!DolibarrApiAccess::$user->rights->mymodule->myobject->write) {
249 throw new RestException(401);
252 $result = $this->myobject->fetch($id);
254 throw new RestException(404,
'MyObject not found');
258 throw new RestException(401,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
261 foreach ($request_data as $field => $value) {
262 if ($field ==
'id') {
265 if ($field ===
'caller') {
267 $this->myobject->context[
'caller'] = $request_data[
'caller'];
271 $this->myobject->$field = $this->
_checkValForAPI($field, $value, $this->myobject);
277 if ($this->myobject->update(DolibarrApiAccess::$user,
false) > 0) {
278 return $this->
get($id);
280 throw new RestException(500, $this->myobject->error);
294 public function delete($id)
296 if (!DolibarrApiAccess::$user->rights->mymodule->myobject->delete) {
297 throw new RestException(401);
299 $result = $this->myobject->fetch($id);
301 throw new RestException(404,
'MyObject not found');
305 throw new RestException(401,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
308 if ($this->myobject->delete(DolibarrApiAccess::$user) == 0) {
309 throw new RestException(409,
'Error when deleting MyObject : '.$this->myobject->error);
310 } elseif ($this->myobject->delete(DolibarrApiAccess::$user) < 0) {
311 throw new RestException(500,
'Error when deleting MyObject : '.$this->myobject->error);
317 'message' =>
'MyObject deleted'
358 $object = parent::_cleanObjectDatas($object);
360 unset($object->rowid);
361 unset($object->canvas);
398 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
399 $nboflines = count($object->lines);
400 for ($i = 0; $i < $nboflines; $i++) {
403 unset($object->lines[$i]->lines);
404 unset($object->lines[$i]->note);