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      if ($field == 
'array_options' && is_array($value)) {
 
  272        foreach ($value as $index => $val) {
 
  273          $this->myobject->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->myobject);
 
  278      $this->myobject->$field = $this->
_checkValForAPI($field, $value, $this->myobject);
 
  284    if ($this->myobject->update(DolibarrApiAccess::$user, 
false) > 0) {
 
  285      return $this->
get($id);
 
  287      throw new RestException(500, $this->myobject->error);
 
 
  301  public function delete($id)
 
  303    if (!DolibarrApiAccess::$user->rights->mymodule->myobject->delete) {
 
  304      throw new RestException(401);
 
  306    $result = $this->myobject->fetch($id);
 
  308      throw new RestException(404, 
'MyObject not found');
 
  312      throw new RestException(401, 
'Access to instance id='.$this->myobject->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
 
  315    if ($this->myobject->delete(DolibarrApiAccess::$user) == 0) {
 
  316      throw new RestException(409, 
'Error when deleting MyObject : '.$this->myobject->error);
 
  317    } elseif ($this->myobject->delete(DolibarrApiAccess::$user) < 0) {
 
  318      throw new RestException(500, 
'Error when deleting MyObject : '.$this->myobject->error);
 
  324        'message' => 
'MyObject deleted' 
 
  365    $object = parent::_cleanObjectDatas($object);
 
  367    unset($object->rowid);
 
  368    unset($object->canvas);
 
  405    if (isset($object->lines) && is_array($object->lines) && count($object->lines) > 0) {
 
  406      $nboflines = count($object->lines);
 
  407      for ($i = 0; $i < $nboflines; $i++) {
 
  410        unset($object->lines[$i]->lines);
 
  411        unset($object->lines[$i]->note);