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;
176 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
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 $this->myobject->fetch_optionals();
244 foreach ($value as $index => $val) {
250 $this->myobject->$field = $this->
_checkValForAPI((
string) $field, $value, $this->myobject);
256 if ($this->myobject->create(DolibarrApiAccess::$user) < 0) {
257 throw new RestException(500,
"Error creating MyObject", array_merge(array($this->myobject->error), $this->myobject->errors));
259 return $this->myobject->id;
279 public function put(
$id, $request_data =
null)
281 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'write')) {
282 throw new RestException(403);
285 throw new RestException(403,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
288 $result = $this->myobject->fetch(
$id);
290 throw new RestException(404,
'MyObject not found');
293 foreach ($request_data as $field => $value) {
294 if ($field ==
'id') {
297 if ($field ===
'caller') {
299 $this->myobject->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
303 if ($field ==
'array_options' && is_array($value)) {
304 foreach ($value as $index => $val) {
310 $this->myobject->$field = $this->
_checkValForAPI((
string) $field, $value, $this->myobject);
316 if ($this->myobject->update(DolibarrApiAccess::$user, 0) > 0) {
317 return $this->
get(
$id);
319 throw new RestException(500, $this->myobject->error);
338 public function delete(
$id)
340 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'delete')) {
341 throw new RestException(403);
344 throw new RestException(403,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
347 $result = $this->myobject->fetch(
$id);
349 throw new RestException(404,
'MyObject not found');
352 if ($this->myobject->delete(DolibarrApiAccess::$user) == 0) {
353 throw new RestException(409,
'Error when deleting MyObject : '.$this->myobject->error);
354 } elseif ($this->myobject->delete(DolibarrApiAccess::$user) < 0) {
355 throw new RestException(500,
'Error when deleting MyObject : '.$this->myobject->error);
361 'message' =>
'MyObject deleted'