116 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
121 $tmpobject =
new MyObject($this->db);
123 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'read')) {
124 throw new RestException(403);
127 $socid = DolibarrApiAccess::$user->socid ?: 0;
129 $restrictonsocid = 0;
133 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
134 $search_sale = DolibarrApiAccess::$user->id;
140 $sql =
"SELECT t.rowid";
141 $sql .=
" FROM ".$this->db->prefix().$tmpobject->table_element.
" AS t";
142 if (!empty($tmpobject->isextrafieldmanaged) && (
int) $tmpobject->isextrafieldmanaged == 1) {
143 $sql .=
" LEFT JOIN ".$this->db->prefix().$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
145 if (!empty($tmpobject->ismultientitymanaged) && (
int) $tmpobject->ismultientitymanaged == 1) {
146 $sql .=
" WHERE t.entity IN (".getEntity($tmpobject->element).
")";
147 } elseif (preg_match(
'/^\w+@\w+$/', (
string) $tmpobject->ismultientitymanaged)) {
148 $tmparray = explode(
'@', (
string) $tmpobject->ismultientitymanaged);
149 $sql .=
" LEFT JOIN ".$this->db->prefix().$this->db->sanitize($tmparray[1]).
" as pt ON t.".$this->db->sanitize($tmparray[0]).
" = pt.rowid";
150 $sql .=
" WHERE pt.entity IN (".getEntity($tmpobject->element).
")";
152 $sql .=
" WHERE 1 = 1";
154 if ($restrictonsocid && $socid) {
155 $sql .=
" AND t.fk_soc = ".((int) $socid);
158 if ($search_sale && $search_sale !=
'-1') {
159 if ($search_sale == -2) {
160 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
161 } elseif ($search_sale > 0) {
162 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
166 $parameters = array(
'sqlfilters' => $sqlfilters,
'apiroute' =>
'myobject',
'apimethod' => __METHOD__);
168 $reshook = $hookmanager->executeHooks(
'printFieldListWhere', $parameters, $tmpobject, $action);
170 $sql = $hookmanager->resPrint;
171 } elseif ($reshook == 0) {
172 $sql .= $hookmanager->resPrint;
179 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
183 $sql .= $this->db->order($sortfield, $sortorder);
188 $offset = $limit * $page;
190 $sql .= $this->db->plimit($limit + 1, $offset);
193 $result = $this->db->query($sql);
196 $num = $this->db->num_rows($result);
197 $min = min($num, ($limit <= 0 ? $num : $limit));
199 $obj = $this->db->fetch_object($result);
200 $tmp_object =
new MyObject($this->db);
201 if ($tmp_object->fetch($obj->rowid)) {
207 throw new RestException(503,
'Error when retrieving myobject list: '.$this->db->lasterror());
226 public function post($request_data =
null)
228 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'write')) {
229 throw new RestException(403);
235 foreach ($request_data as $field => $value) {
236 if ($field ===
'caller') {
238 $this->myobject->context[
'caller'] =
sanitizeVal((
string) $request_data[
'caller'],
'aZ09');
242 if ($field ==
'array_options' && is_array($value)) {
243 $this->myobject->fetch_optionals();
245 foreach ($value as $index => $val) {
251 $this->myobject->$field = $this->
_checkValForAPI((
string) $field, $value, $this->myobject);
257 if ($this->myobject->create(DolibarrApiAccess::$user) < 0) {
258 throw new RestException(500,
"Error creating MyObject", array_merge(array($this->myobject->error), $this->myobject->errors));
260 return $this->myobject->id;
280 public function put(
$id, $request_data =
null)
282 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'write')) {
283 throw new RestException(403);
286 throw new RestException(403,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
289 $result = $this->myobject->fetch(
$id);
291 throw new RestException(404,
'MyObject not found');
294 foreach ($request_data as $field => $value) {
295 if ($field ==
'id') {
298 if ($field ===
'caller') {
300 $this->myobject->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
304 if ($field ==
'array_options' && is_array($value)) {
305 foreach ($value as $index => $val) {
311 $this->myobject->$field = $this->
_checkValForAPI((
string) $field, $value, $this->myobject);
317 if ($this->myobject->update(DolibarrApiAccess::$user, 0) > 0) {
318 return $this->
get(
$id);
320 throw new RestException(500, $this->myobject->error);
339 public function delete(
$id)
341 if (!DolibarrApiAccess::$user->hasRight(
'mymodule',
'myobject',
'delete')) {
342 throw new RestException(403);
345 throw new RestException(403,
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
348 $result = $this->myobject->fetch(
$id);
350 throw new RestException(404,
'MyObject not found');
353 if ($this->myobject->delete(DolibarrApiAccess::$user) == 0) {
354 throw new RestException(409,
'Error when deleting MyObject : '.$this->myobject->error);
355 } elseif ($this->myobject->delete(DolibarrApiAccess::$user) < 0) {
356 throw new RestException(500,
'Error when deleting MyObject : '.$this->myobject->error);
362 'message' =>
'MyObject deleted'