20use Luracast\Restler\RestException;
70 public function get(
$id)
72 if (!DolibarrApiAccess::$user->hasRight(
'partnership',
'read')) {
73 throw new RestException(403);
76 $result = $this->partnership->fetch(
$id);
78 throw new RestException(404,
'Partnership not found');
82 throw new RestException(403,
'Access to instance id='.$this->partnership->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
108 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
113 if (!DolibarrApiAccess::$user->hasRight(
'partnership',
'read')) {
114 throw new RestException(403);
117 $socid = DolibarrApiAccess::$user->socid ?: 0;
119 $restrictonsocid = 0;
123 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
124 $search_sale = DolibarrApiAccess::$user->id;
127 $sql =
"SELECT t.rowid";
128 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" AS t";
129 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
130 $sql .=
" WHERE 1 = 1";
131 if ($tmpobject->ismultientitymanaged) {
132 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
134 if ($restrictonsocid && $socid) {
135 $sql .=
" AND t.fk_soc = ".((int) $socid);
138 if ($search_sale && $search_sale !=
'-1') {
139 if ($search_sale == -2) {
140 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
141 } elseif ($search_sale > 0) {
142 $sql .=
" AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).
")";
149 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
153 $sql .= $this->db->order($sortfield, $sortorder);
158 $offset = $limit * $page;
160 $sql .= $this->db->plimit($limit + 1, $offset);
163 $result = $this->db->query($sql);
166 $num = $this->db->num_rows($result);
167 $min = min($num, ($limit <= 0 ? $num : $limit));
169 $obj = $this->db->fetch_object($result);
171 if ($tmp_object->fetch($obj->rowid)) {
177 throw new RestException(503,
'Error when retrieving partnership list: '.$this->db->lasterror());
195 public function post($request_data =
null)
197 if (!DolibarrApiAccess::$user->hasRight(
'partnership',
'write')) {
198 throw new RestException(403);
202 $result = $this->
_validate($request_data);
204 foreach ($request_data as $field => $value) {
205 if ($field ===
'caller') {
207 $this->partnership->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
211 $this->partnership->$field = $this->
_checkValForAPI($field, $value, $this->partnership);
217 if ($this->partnership->create(DolibarrApiAccess::$user) < 0) {
218 throw new RestException(500,
"Error creating Partnership", array_merge(array($this->partnership->error), $this->partnership->errors));
220 return $this->partnership->id;
236 public function put(
$id, $request_data =
null)
238 if (!DolibarrApiAccess::$user->hasRight(
'partnership',
'write')) {
239 throw new RestException(403);
242 $result = $this->partnership->fetch(
$id);
244 throw new RestException(404,
'Partnership not found');
248 throw new RestException(403,
'Access to instance id='.$this->partnership->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
251 foreach ($request_data as $field => $value) {
252 if ($field ==
'id') {
255 if ($field ===
'caller') {
257 $this->partnership->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
260 if ($field ==
'array_options' && is_array($value)) {
261 foreach ($value as $index => $val) {
262 $this->partnership->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->partnership);
267 $this->partnership->$field = $this->
_checkValForAPI($field, $value, $this->partnership);
273 if ($this->partnership->update(DolibarrApiAccess::$user, 0) > 0) {
274 return $this->
get(
$id);
276 throw new RestException(500, $this->partnership->error);
292 public function delete(
$id)
294 if (!DolibarrApiAccess::$user->hasRight(
'partnership',
'delete')) {
295 throw new RestException(403);
297 $result = $this->partnership->fetch(
$id);
299 throw new RestException(404,
'Partnership not found');
303 throw new RestException(403,
'Access to instance id='.$this->partnership->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
306 if (!$this->partnership->delete(DolibarrApiAccess::$user)) {
307 throw new RestException(500,
'Error when deleting Partnership : '.$this->partnership->error);
313 'message' =>
'Partnership deleted'
374 $nboflines = count(
$object->lines);
375 for ($i = 0; $i < $nboflines; $i++) {
378 unset(
$object->lines[$i]->lines);
379 unset(
$object->lines[$i]->note);
396 if ($data ===
null) {
399 $partnership = array();
400 foreach ($this->partnership->fields as $field => $propfield) {
401 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || empty($propfield[
'notnull']) || $propfield[
'notnull'] != 1) {
404 if (!isset($data[$field])) {
405 throw new RestException(400,
"$field field missing");
407 $partnership[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List partnerships.
__construct()
Constructor.
post($request_data=null)
Create partnership object.
_validate($data)
Validate fields before create or update object.
put($id, $request_data=null)
Update partnership.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.