api_agendaevents.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use Luracast\Restler\RestException;
  19. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
  20. /**
  21. * API class for Agenda Events
  22. *
  23. * @access protected
  24. * @class DolibarrApiAccess {@requires user,external}
  25. */
  26. class AgendaEvents extends DolibarrApi
  27. {
  28. /**
  29. * @var array $FIELDS Mandatory fields, checked when create and update object
  30. */
  31. public static $FIELDS = array(
  32. );
  33. /**
  34. * @var ActionComm $actioncomm {@type ActionComm}
  35. */
  36. public $actioncomm;
  37. /**
  38. * Constructor
  39. */
  40. public function __construct()
  41. {
  42. global $db, $conf;
  43. $this->db = $db;
  44. $this->actioncomm = new ActionComm($this->db);
  45. }
  46. /**
  47. * Get properties of a Agenda Events object
  48. *
  49. * Return an array with Agenda Events informations
  50. *
  51. * @param int $id ID of Agenda Events
  52. * @return Object Object with cleaned properties
  53. *
  54. * @throws RestException
  55. */
  56. public function get($id)
  57. {
  58. if (!DolibarrApiAccess::$user->rights->agenda->myactions->read) {
  59. throw new RestException(401, "Insufficient rights to read an event");
  60. }
  61. if ($id === 0) {
  62. $result = $this->actioncomm->initAsSpecimen();
  63. } else {
  64. $result = $this->actioncomm->fetch($id);
  65. if ($result) {
  66. $this->actioncomm->fetch_optionals();
  67. $this->actioncomm->fetchObjectLinked();
  68. }
  69. }
  70. if (!$result) {
  71. throw new RestException(404, 'Agenda Events not found');
  72. }
  73. if (!DolibarrApiAccess::$user->rights->agenda->allactions->read && $this->actioncomm->userownerid != DolibarrApiAccess::$user->id) {
  74. throw new RestException(401, 'Insufficient rights to read event of this owner id. Your id is '.DolibarrApiAccess::$user->id);
  75. }
  76. if (!DolibarrApi::_checkAccessToResource('agenda', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) {
  77. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  78. }
  79. return $this->_cleanObjectDatas($this->actioncomm);
  80. }
  81. /**
  82. * List Agenda Events
  83. *
  84. * Get a list of Agenda Events
  85. *
  86. * @param string $sortfield Sort field
  87. * @param string $sortorder Sort order
  88. * @param int $limit Limit for list
  89. * @param int $page Page number
  90. * @param string $user_ids User ids filter field (owners of event). Example: '1' or '1,2,3' {@pattern /^[0-9,]*$/i}
  91. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.label:like:'%dol%') and (t.datec:<:'20160101')"
  92. * @param string $properties Restrict the data returned to theses properties. Ignored if empty. Comma separated list of properties names
  93. * @return array Array of Agenda Events objects
  94. */
  95. public function index($sortfield = "t.id", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = 0, $sqlfilters = '', $properties = '')
  96. {
  97. global $db, $conf;
  98. $obj_ret = array();
  99. if (!DolibarrApiAccess::$user->rights->agenda->myactions->read) {
  100. throw new RestException(401, "Insufficient rights to read events");
  101. }
  102. // case of external user
  103. $socid = 0;
  104. if (!empty(DolibarrApiAccess::$user->socid)) {
  105. $socid = DolibarrApiAccess::$user->socid;
  106. }
  107. // If the internal user must only see his customers, force searching by him
  108. $search_sale = 0;
  109. if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
  110. $search_sale = DolibarrApiAccess::$user->id;
  111. }
  112. if (!isModEnabled('societe')) {
  113. $search_sale = 0; // If module thirdparty not enabled, sale representative is something that does not exists
  114. }
  115. $sql = "SELECT t.id as rowid";
  116. if (isModEnabled("societe")) {
  117. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  118. $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
  119. }
  120. }
  121. $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm AS t LEFT JOIN ".MAIN_DB_PREFIX."actioncomm_extrafields AS ef ON (ef.fk_object = t.id)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
  122. if (isModEnabled("societe")) {
  123. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  124. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  125. }
  126. }
  127. $sql .= ' WHERE t.entity IN ('.getEntity('agenda').')';
  128. if (isModEnabled("societe")) {
  129. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  130. $sql .= " AND t.fk_soc = sc.fk_soc";
  131. }
  132. }
  133. if ($user_ids) {
  134. $sql .= " AND t.fk_user_action IN (".$this->db->sanitize($user_ids).")";
  135. }
  136. if ($socid > 0) {
  137. $sql .= " AND t.fk_soc = ".((int) $socid);
  138. }
  139. // Insert sale filter
  140. if ($search_sale > 0) {
  141. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  142. }
  143. // Add sql filters
  144. if ($sqlfilters) {
  145. $errormessage = '';
  146. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  147. if ($errormessage) {
  148. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  149. }
  150. }
  151. $sql .= $this->db->order($sortfield, $sortorder);
  152. if ($limit) {
  153. if ($page < 0) {
  154. $page = 0;
  155. }
  156. $offset = $limit * $page;
  157. $sql .= $this->db->plimit($limit + 1, $offset);
  158. }
  159. $result = $this->db->query($sql);
  160. if ($result) {
  161. $i = 0;
  162. $num = $this->db->num_rows($result);
  163. $min = min($num, ($limit <= 0 ? $num : $limit));
  164. while ($i < $min) {
  165. $obj = $this->db->fetch_object($result);
  166. $actioncomm_static = new ActionComm($this->db);
  167. if ($actioncomm_static->fetch($obj->rowid)) {
  168. $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($actioncomm_static), $properties);
  169. }
  170. $i++;
  171. }
  172. } else {
  173. throw new RestException(503, 'Error when retrieve Agenda Event list : '.$this->db->lasterror());
  174. }
  175. if (!count($obj_ret)) {
  176. throw new RestException(404, 'No Agenda Event found');
  177. }
  178. return $obj_ret;
  179. }
  180. /**
  181. * Create Agenda Event object
  182. *
  183. * @param array $request_data Request data
  184. * @return int ID of Agenda Event
  185. */
  186. public function post($request_data = null)
  187. {
  188. if (!DolibarrApiAccess::$user->rights->agenda->myactions->create) {
  189. throw new RestException(401, "Insufficient rights to create your Agenda Event");
  190. }
  191. if (!DolibarrApiAccess::$user->hasRight('agenda', 'allactions', 'create') && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
  192. throw new RestException(401, "Insufficient rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
  193. }
  194. // Check mandatory fields
  195. $result = $this->_validate($request_data);
  196. foreach ($request_data as $field => $value) {
  197. $this->actioncomm->$field = $this->_checkValForAPI($field, $value, $this->actioncomm);
  198. }
  199. /*if (isset($request_data["lines"])) {
  200. $lines = array();
  201. foreach ($request_data["lines"] as $line) {
  202. array_push($lines, (object) $line);
  203. }
  204. $this->expensereport->lines = $lines;
  205. }*/
  206. if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) {
  207. throw new RestException(500, "Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors));
  208. }
  209. return $this->actioncomm->id;
  210. }
  211. /**
  212. * Update Agenda Event general fields
  213. *
  214. * @param int $id Id of Agenda Event to update
  215. * @param array $request_data Datas
  216. *
  217. * @return int
  218. */
  219. public function put($id, $request_data = null)
  220. {
  221. if (!DolibarrApiAccess::$user->rights->agenda->myactions->create) {
  222. throw new RestException(401, "Insufficient rights to create your Agenda Event");
  223. }
  224. if (!DolibarrApiAccess::$user->hasRight('agenda', 'allactions', 'create') && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
  225. throw new RestException(401, "Insufficient rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
  226. }
  227. $result = $this->actioncomm->fetch($id);
  228. if ($result) {
  229. $this->actioncomm->fetch_optionals();
  230. $this->actioncomm->fetch_userassigned();
  231. $this->actioncomm->oldcopy = clone $this->actioncomm;
  232. }
  233. if (!$result) {
  234. throw new RestException(404, 'actioncomm not found');
  235. }
  236. if (!DolibarrApi::_checkAccessToResource('actioncomm', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) {
  237. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  238. }
  239. foreach ($request_data as $field => $value) {
  240. if ($field == 'id') {
  241. continue;
  242. }
  243. $this->actioncomm->$field = $this->_checkValForAPI($field, $value, $this->actioncomm);
  244. }
  245. if ($this->actioncomm->update(DolibarrApiAccess::$user, 1) > 0) {
  246. return $this->get($id);
  247. }
  248. return false;
  249. }
  250. /**
  251. * Delete Agenda Event
  252. *
  253. * @param int $id Agenda Event ID
  254. *
  255. * @return array
  256. */
  257. public function delete($id)
  258. {
  259. if (!DolibarrApiAccess::$user->hasRight('agenda', 'myactions', 'delete')) {
  260. throw new RestException(401, "Insufficient rights to delete your Agenda Event");
  261. }
  262. $result = $this->actioncomm->fetch($id);
  263. if ($result) {
  264. $this->actioncomm->fetch_optionals();
  265. $this->actioncomm->fetch_userassigned();
  266. $this->actioncomm->oldcopy = clone $this->actioncomm;
  267. }
  268. if (!DolibarrApiAccess::$user->rights->agenda->allactions->delete && DolibarrApiAccess::$user->id != $this->actioncomm->userownerid) {
  269. throw new RestException(401, "Insufficient rights to delete an Agenda Event of owner id ".$this->actioncomm->userownerid.' Your id is '.DolibarrApiAccess::$user->id);
  270. }
  271. if (!$result) {
  272. throw new RestException(404, 'Agenda Event not found');
  273. }
  274. if (!DolibarrApi::_checkAccessToResource('actioncomm', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) {
  275. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  276. }
  277. if (!$this->actioncomm->delete(DolibarrApiAccess::$user)) {
  278. throw new RestException(500, 'Error when delete Agenda Event : '.$this->actioncomm->error);
  279. }
  280. return array(
  281. 'success' => array(
  282. 'code' => 200,
  283. 'message' => 'Agenda Event deleted'
  284. )
  285. );
  286. }
  287. /**
  288. * Validate fields before create or update object
  289. *
  290. * @param array $data Array with data to verify
  291. * @return array
  292. * @throws RestException
  293. */
  294. private function _validate($data)
  295. {
  296. $event = array();
  297. foreach (AgendaEvents::$FIELDS as $field) {
  298. if (!isset($data[$field])) {
  299. throw new RestException(400, "$field field missing");
  300. }
  301. $event[$field] = $data[$field];
  302. }
  303. return $event;
  304. }
  305. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  306. /**
  307. * Clean sensible object datas
  308. *
  309. * @param Object $object Object to clean
  310. * @return Object Object with cleaned properties
  311. */
  312. protected function _cleanObjectDatas($object)
  313. {
  314. // phpcs:enable
  315. $object = parent::_cleanObjectDatas($object);
  316. unset($object->note); // alreaydy into note_private
  317. unset($object->usermod);
  318. unset($object->libelle);
  319. unset($object->context);
  320. unset($object->canvas);
  321. unset($object->contact);
  322. unset($object->contact_id);
  323. unset($object->thirdparty);
  324. unset($object->user);
  325. unset($object->origin);
  326. unset($object->origin_id);
  327. unset($object->ref_ext);
  328. unset($object->statut);
  329. unset($object->state_code);
  330. unset($object->state_id);
  331. unset($object->state);
  332. unset($object->region);
  333. unset($object->region_code);
  334. unset($object->country);
  335. unset($object->country_id);
  336. unset($object->country_code);
  337. unset($object->barcode_type);
  338. unset($object->barcode_type_code);
  339. unset($object->barcode_type_label);
  340. unset($object->barcode_type_coder);
  341. unset($object->mode_reglement_id);
  342. unset($object->cond_reglement_id);
  343. unset($object->cond_reglement);
  344. unset($object->fk_delivery_address);
  345. unset($object->shipping_method_id);
  346. unset($object->fk_account);
  347. unset($object->total_ht);
  348. unset($object->total_tva);
  349. unset($object->total_localtax1);
  350. unset($object->total_localtax2);
  351. unset($object->total_ttc);
  352. unset($object->fk_incoterms);
  353. unset($object->label_incoterms);
  354. unset($object->location_incoterms);
  355. unset($object->name);
  356. unset($object->lastname);
  357. unset($object->firstname);
  358. unset($object->civility_id);
  359. unset($object->contact);
  360. unset($object->societe);
  361. unset($object->demand_reason_id);
  362. unset($object->transport_mode_id);
  363. unset($object->region_id);
  364. unset($object->actions);
  365. unset($object->lines);
  366. return $object;
  367. }
  368. }