20use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
71 public function get(
$id)
73 if (!DolibarrApiAccess::$user->hasRight(
'partnership',
'read')) {
74 throw new RestException(403);
77 $result = $this->partnership->fetch(
$id);
79 throw new RestException(404,
'Partnership not found');
83 throw new RestException(403,
'Access to instance id='.$this->partnership->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
109 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
114 if (!DolibarrApiAccess::$user->hasRight(
'partnership',
'read')) {
115 throw new RestException(403);
118 $socid = DolibarrApiAccess::$user->socid ?: 0;
120 $restrictonsocid = 0;
124 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
125 $search_sale = DolibarrApiAccess::$user->id;
128 $sql =
"SELECT t.rowid";
129 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" AS t";
130 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
131 $sql .=
" WHERE 1 = 1";
132 if ($tmpobject->ismultientitymanaged) {
133 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
135 if ($restrictonsocid && $socid) {
136 $sql .=
" AND t.fk_soc = ".((int) $socid);
139 if ($search_sale && $search_sale !=
'-1') {
140 if ($search_sale == -2) {
141 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
142 } elseif ($search_sale > 0) {
143 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
150 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
154 $sql .= $this->db->order($sortfield, $sortorder);
159 $offset = $limit * $page;
161 $sql .= $this->db->plimit($limit + 1, $offset);
164 $result = $this->db->query($sql);
167 $num = $this->db->num_rows($result);
168 $min = min($num, ($limit <= 0 ? $num : $limit));
170 $obj = $this->db->fetch_object($result);
172 if ($tmp_object->fetch($obj->rowid)) {
178 throw new RestException(503,
'Error when retrieving partnership list: '.$this->db->lasterror());
196 public function post($request_data =
null)
198 if (!DolibarrApiAccess::$user->hasRight(
'partnership',
'write')) {
199 throw new RestException(403);
203 $result = $this->
_validate($request_data);
205 foreach ($request_data as $field => $value) {
206 if ($field ===
'caller') {
208 $this->partnership->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
212 $this->partnership->$field = $this->
_checkValForAPI($field, $value, $this->partnership);
218 if ($this->partnership->create(DolibarrApiAccess::$user) < 0) {
219 throw new RestException(500,
"Error creating Partnership", array_merge(array($this->partnership->error), $this->partnership->errors));
221 return $this->partnership->id;
237 public function put(
$id, $request_data =
null)
239 if (!DolibarrApiAccess::$user->hasRight(
'partnership',
'write')) {
240 throw new RestException(403);
243 $result = $this->partnership->fetch(
$id);
245 throw new RestException(404,
'Partnership not found');
249 throw new RestException(403,
'Access to instance id='.$this->partnership->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
252 foreach ($request_data as $field => $value) {
253 if ($field ==
'id') {
256 if ($field ===
'caller') {
258 $this->partnership->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
261 if ($field ==
'array_options' && is_array($value)) {
262 foreach ($value as $index => $val) {
268 $this->partnership->$field = $this->
_checkValForAPI($field, $value, $this->partnership);
274 if ($this->partnership->update(DolibarrApiAccess::$user, 0) > 0) {
275 return $this->
get(
$id);
277 throw new RestException(500, $this->partnership->error);
293 public function delete(
$id)
295 if (!DolibarrApiAccess::$user->hasRight(
'partnership',
'delete')) {
296 throw new RestException(403);
298 $result = $this->partnership->fetch(
$id);
300 throw new RestException(404,
'Partnership not found');
304 throw new RestException(403,
'Access to instance id='.$this->partnership->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
307 if (!$this->partnership->delete(DolibarrApiAccess::$user)) {
308 throw new RestException(500,
'Error when deleting Partnership : '.$this->partnership->error);
314 'message' =>
'Partnership deleted'
375 $nboflines = count(
$object->lines);
376 for ($i = 0; $i < $nboflines; $i++) {
379 unset(
$object->lines[$i]->lines);
380 unset(
$object->lines[$i]->note);
397 if ($data ===
null) {
400 $partnership = array();
401 foreach ($this->partnership->fields as $field => $propfield) {
402 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || empty($propfield[
'notnull']) || $propfield[
'notnull'] != 1) {
405 if (!isset($data[$field])) {
406 throw new RestException(400,
"$field field missing");
408 $partnership[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
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.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.