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;
121 $restrictonsocid = (
getDolGlobalString(
'PARTNERSHIP_IS_MANAGED_FOR') ==
'member') ? 0 : 1;
125 if ($restrictonsocid && !DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socid) {
126 $search_sale = DolibarrApiAccess::$user->id;
129 $sql =
"SELECT t.rowid";
130 $sql .=
" FROM ".MAIN_DB_PREFIX.$tmpobject->table_element.
" AS t";
131 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.$tmpobject->table_element.
"_extrafields AS ef ON (ef.fk_object = t.rowid)";
132 $sql .=
" WHERE 1 = 1";
133 if ($tmpobject->ismultientitymanaged) {
134 $sql .=
' AND t.entity IN ('.getEntity($tmpobject->element).
')';
136 if ($restrictonsocid && $socid) {
137 $sql .=
" AND t.fk_soc = ".((int) $socid);
140 if ($search_sale && $search_sale !=
'-1') {
141 if ($search_sale == -2) {
142 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', 0, 1);
143 } elseif ($search_sale > 0) {
144 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
't.fk_soc', (
int) $search_sale);
151 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
155 $sql .= $this->db->order($sortfield, $sortorder);
160 $offset = $limit * $page;
162 $sql .= $this->db->plimit($limit + 1, $offset);
165 $result = $this->db->query($sql);
168 $num = $this->db->num_rows($result);
169 $min = min($num, ($limit <= 0 ? $num : $limit));
171 $obj = $this->db->fetch_object($result);
173 if ($tmp_object->fetch($obj->rowid)) {
179 throw new RestException(503,
'Error when retrieving partnership list: '.$this->db->lasterror());
197 public function post($request_data =
null)
199 if (!DolibarrApiAccess::$user->hasRight(
'partnership',
'write')) {
200 throw new RestException(403);
204 $result = $this->
_validate($request_data);
206 foreach ($request_data as $field => $value) {
207 if ($field ===
'caller') {
209 $this->partnership->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
213 $this->partnership->$field = $this->
_checkValForAPI($field, $value, $this->partnership);
219 if ($this->partnership->create(DolibarrApiAccess::$user) < 0) {
220 throw new RestException(500,
"Error creating Partnership", array_merge(array($this->partnership->error), $this->partnership->errors));
222 return $this->partnership->id;
238 public function put(
$id, $request_data =
null)
240 if (!DolibarrApiAccess::$user->hasRight(
'partnership',
'write')) {
241 throw new RestException(403);
244 $result = $this->partnership->fetch(
$id);
246 throw new RestException(404,
'Partnership not found');
250 throw new RestException(403,
'Access to instance id='.$this->partnership->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
253 foreach ($request_data as $field => $value) {
254 if ($field ==
'id') {
257 if ($field ===
'caller') {
259 $this->partnership->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
262 if ($field ==
'array_options' && is_array($value)) {
263 foreach ($value as $index => $val) {
269 $this->partnership->$field = $this->
_checkValForAPI($field, $value, $this->partnership);
275 if ($this->partnership->update(DolibarrApiAccess::$user, 0) > 0) {
276 return $this->
get(
$id);
278 throw new RestException(500, $this->partnership->errorsToString());
294 public function delete(
$id)
296 if (!DolibarrApiAccess::$user->hasRight(
'partnership',
'delete')) {
297 throw new RestException(403);
299 $result = $this->partnership->fetch(
$id);
301 throw new RestException(404,
'Partnership not found');
305 throw new RestException(403,
'Access to instance id='.$this->partnership->id.
' of object not allowed for login '.DolibarrApiAccess::$user->login);
308 if (!$this->partnership->delete(DolibarrApiAccess::$user)) {
309 throw new RestException(500,
'Error when deleting Partnership : '.$this->partnership->errorsToString());
315 'message' =>
'Partnership deleted'
376 $nboflines = count(
$object->lines);
377 for ($i = 0; $i < $nboflines; $i++) {
380 unset(
$object->lines[$i]->lines);
381 unset(
$object->lines[$i]->note);
398 if ($data ===
null) {
401 $partnership = array();
402 foreach ($this->partnership->fields as $field => $propfield) {
403 if (in_array($field, array(
'rowid',
'entity',
'date_creation',
'tms',
'fk_user_creat')) || empty($propfield[
'notnull']) || $propfield[
'notnull'] != 1) {
406 if (!isset($data[$field])) {
407 throw new RestException(400,
"$field field missing");
409 $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
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.