115 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
120 $tmpobject =
new MyObject($this->db);
122 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'read')) {
123 throw new RestException(403);
126 $socid = DolibarrApiAccess::$user->socid ?: 0;
128 $restrictonsocid = 0;
132 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
133 $search_sale = DolibarrApiAccess::$user->id;
139 $sql =
"SELECT t.rowid";
140 $sql .=
" FROM ".$this->db->prefix().$tmpobject->table_element.
" AS t";
141 if (!empty($tmpobject->isextrafieldmanaged) && (
int) $tmpobject->isextrafieldmanaged == 1) {
142 $sql .=
" LEFT JOIN ".$this->db->prefix().$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
144 if (!empty($tmpobject->ismultientitymanaged) && (
int) $tmpobject->ismultientitymanaged == 1) {
145 $sql .=
" WHERE t.entity IN (".getEntity($tmpobject->element).
")";
146 } elseif (preg_match(
'/^\w+@\w+$/', (
string) $tmpobject->ismultientitymanaged)) {
147 $tmparray = explode(
'@', (
string) $tmpobject->ismultientitymanaged);
148 $sql .=
" LEFT JOIN ".$this->db->prefix().$tmparray[1].
" as pt ON t.".$tmparray[0].
" = pt.rowid";
149 $sql .=
" WHERE pt.entity IN (".getEntity($tmpobject->element).
")";
151 $sql .=
" WHERE 1 = 1";
153 if ($restrictonsocid && $socid) {
154 $sql .=
" AND t.fk_soc = ".((int) $socid);
157 if ($search_sale && $search_sale !=
'-1') {
158 if ($search_sale == -2) {
159 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".$this->db->prefix().
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
160 } elseif ($search_sale > 0) {
161 $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).
")";
165 $parameters = array(
'sqlfilters' => $sqlfilters,
'apiroute' =>
'myobject',
'apimethod' => __METHOD__);
167 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters, $tmpobject, $action);
169 $sql = $hookmanager->resPrint;
170 } elseif ($reshook == 0) {
171 $sql .= $hookmanager->resPrint;
178 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
182 $sql .= $this->db->order($sortfield, $sortorder);
187 $offset = $limit * $page;
189 $sql .= $this->db->plimit($limit + 1, $offset);
192 $result = $this->db->query($sql);
195 $num = $this->db->num_rows($result);
196 $min = min($num, ($limit <= 0 ? $num : $limit));
198 $obj = $this->db->fetch_object($result);
199 $tmp_object =
new MyObject($this->db);
200 if ($tmp_object->fetch($obj->rowid)) {
206 throw new RestException(503,
'Error when retrieving myobject list: '.$this->db->lasterror());
225 public function post($request_data =
null)
227 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'write')) {
228 throw new RestException(403);
234 foreach ($request_data as $field => $value) {
235 if ($field ===
'caller') {
237 $this->myobject->context[
'caller'] =
sanitizeVal((
string) $request_data[
'caller'],
'aZ09');
241 if ($field ==
'array_options' && is_array($value)) {
242 foreach ($value as $index => $val) {
243 $this->myobject->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->myobject);
248 $this->myobject->$field = $this->
_checkValForAPI((
string) $field, $value, $this->myobject);
254 if ($this->myobject->create(DolibarrApiAccess::$user) < 0) {
255 throw new RestException(500,
"Error creating MyObject", array_merge(array($this->myobject->error), $this->myobject->errors));
257 return $this->myobject->id;
277 public function put(
$id, $request_data =
null)
279 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'write')) {
280 throw new RestException(403);
283 throw new RestException(403,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
286 $result = $this->myobject->fetch(
$id);
288 throw new RestException(404,
'MyObject not found');
291 foreach ($request_data as $field => $value) {
292 if ($field ==
'id') {
295 if ($field ===
'caller') {
297 $this->myobject->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
301 if ($field ==
'array_options' && is_array($value)) {
302 foreach ($value as $index => $val) {
303 $this->myobject->array_options[$index] = $this->
_checkValForAPI(
'extrafields', $val, $this->myobject);
308 if ($field ==
'array_options' && is_array($value)) {
309 foreach ($value as $index => $val) {
310 $this->myobject->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->myobject);
315 $this->myobject->$field = $this->
_checkValForAPI($field, $value, $this->myobject);
321 if ($this->myobject->update(DolibarrApiAccess::$user, 0) > 0) {
322 return $this->
get(
$id);
324 throw new RestException(500, $this->myobject->error);
343 public function delete(
$id)
345 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'delete')) {
346 throw new RestException(403);
349 throw new RestException(403,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
352 $result = $this->myobject->fetch(
$id);
354 throw new RestException(404,
'MyObject not found');
357 if ($this->myobject->delete(DolibarrApiAccess::$user) == 0) {
358 throw new RestException(409,
'Error when deleting MyObject : '.$this->myobject->error);
359 } elseif ($this->myobject->delete(DolibarrApiAccess::$user) < 0) {
360 throw new RestException(500,
'Error when deleting MyObject : '.$this->myobject->error);
366 'message' =>
'MyObject deleted'