107 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
112 $tmpobject =
new MyObject($this->db);
114 if (!DolibarrApiAccess::$user->rights->mymodule->myobject->read) {
115 throw new RestException(401);
118 $socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
120 $restrictonsocid = 0;
124 if ($restrictonsocid && !DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
125 $search_sale = DolibarrApiAccess::$user->id;
128 $sql =
"SELECT t.rowid";
129 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
130 $sql .=
", sc.fk_soc, sc.fk_user";
132 $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)";
134 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
135 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
137 $sql .=
" WHERE 1 = 1";
143 if ($tmpobject->ismultientitymanaged) {
144 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
146 if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
147 $sql .=
" AND t.fk_soc = sc.fk_soc";
149 if ($restrictonsocid && $socid) {
150 $sql .=
" AND t.fk_soc = ".((int) $socid);
152 if ($restrictonsocid && $search_sale > 0) {
153 $sql .=
" AND t.rowid = sc.fk_soc";
156 if ($restrictonsocid && $search_sale > 0) {
157 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
163 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
167 $sql .= $this->db->order($sortfield, $sortorder);
172 $offset = $limit * $page;
174 $sql .= $this->db->plimit($limit + 1, $offset);
177 $result = $this->db->query($sql);
180 $num = $this->db->num_rows($result);
182 $obj = $this->db->fetch_object($result);
183 $tmp_object =
new MyObject($this->db);
184 if ($tmp_object->fetch($obj->rowid)) {
190 throw new RestException(503,
'Error when retrieving myobject list: '.$this->db->lasterror());
192 if (!count($obj_ret)) {
193 throw new RestException(404,
'No myobject found');
208 public function post($request_data =
null)
210 if (!DolibarrApiAccess::$user->rights->mymodule->myobject->write) {
211 throw new RestException(401);
215 $result = $this->
_validate($request_data);
217 foreach ($request_data as $field => $value) {
218 $this->myobject->$field = $this->
_checkValForAPI($field, $value, $this->myobject);
224 if ($this->myobject->create(DolibarrApiAccess::$user)<0) {
225 throw new RestException(500,
"Error creating MyObject", array_merge(array($this->myobject->error), $this->myobject->errors));
227 return $this->myobject->id;
241 public function put($id, $request_data =
null)
243 if (!DolibarrApiAccess::$user->rights->mymodule->myobject->write) {
244 throw new RestException(401);
247 $result = $this->myobject->fetch($id);
249 throw new RestException(404,
'MyObject not found');
253 throw new RestException(401,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
256 foreach ($request_data as $field => $value) {
257 if ($field ==
'id') {
260 $this->myobject->$field = $this->
_checkValForAPI($field, $value, $this->myobject);
266 if ($this->myobject->update(DolibarrApiAccess::$user,
false) > 0) {
267 return $this->
get($id);
269 throw new RestException(500, $this->myobject->error);
283 public function delete($id)
285 if (!DolibarrApiAccess::$user->rights->mymodule->myobject->delete) {
286 throw new RestException(401);
288 $result = $this->myobject->fetch($id);
290 throw new RestException(404,
'MyObject not found');
294 throw new RestException(401,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
297 if ($this->myobject->delete(DolibarrApiAccess::$user) == 0) {
298 throw new RestException(409,
'Error when deleting MyObject : '.$this->myobject->error);
299 } elseif ($this->myobject->delete(DolibarrApiAccess::$user) < 0) {
300 throw new RestException(500,
'Error when deleting MyObject : '.$this->myobject->error);
306 'message' =>
'MyObject deleted'
347 $object = parent::_cleanObjectDatas($object);
349 unset($object->rowid);
350 unset($object->canvas);
387 if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
388 $nboflines = count($object->lines);
389 for ($i = 0; $i < $nboflines; $i++) {
392 unset($object->lines[$i]->lines);
393 unset($object->lines[$i]->note);