api_agendaevents.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. 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 for owner id ".$request_data['userownerid'].' 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. * @return array Array of Agenda Events objects
  93. */
  94. public function index($sortfield = "t.id", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = 0, $sqlfilters = '')
  95. {
  96. global $db, $conf;
  97. $obj_ret = array();
  98. if (!DolibarrApiAccess::$user->rights->agenda->myactions->read) {
  99. throw new RestException(401, "Insufficient rights to read events");
  100. }
  101. // case of external user
  102. $socid = 0;
  103. if (!empty(DolibarrApiAccess::$user->socid)) {
  104. $socid = DolibarrApiAccess::$user->socid;
  105. }
  106. // If the internal user must only see his customers, force searching by him
  107. $search_sale = 0;
  108. if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) {
  109. $search_sale = DolibarrApiAccess::$user->id;
  110. }
  111. if (!isModEnabled('societe')) {
  112. $search_sale = 0; // If module thirdparty not enabled, sale representative is something that does not exists
  113. }
  114. $sql = "SELECT t.id as rowid";
  115. if (isModEnabled("societe")) {
  116. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  117. $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)
  118. }
  119. }
  120. $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
  121. if (isModEnabled("societe")) {
  122. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  123. $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
  124. }
  125. }
  126. $sql .= ' WHERE t.entity IN ('.getEntity('agenda').')';
  127. if (isModEnabled("societe")) {
  128. if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) {
  129. $sql .= " AND t.fk_soc = sc.fk_soc";
  130. }
  131. }
  132. if ($user_ids) {
  133. $sql .= " AND t.fk_user_action IN (".$this->db->sanitize($user_ids).")";
  134. }
  135. if ($socid > 0) {
  136. $sql .= " AND t.fk_soc = ".((int) $socid);
  137. }
  138. // Insert sale filter
  139. if ($search_sale > 0) {
  140. $sql .= " AND sc.fk_user = ".((int) $search_sale);
  141. }
  142. // Add sql filters
  143. if ($sqlfilters) {
  144. $errormessage = '';
  145. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  146. if ($errormessage) {
  147. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  148. }
  149. }
  150. $sql .= $this->db->order($sortfield, $sortorder);
  151. if ($limit) {
  152. if ($page < 0) {
  153. $page = 0;
  154. }
  155. $offset = $limit * $page;
  156. $sql .= $this->db->plimit($limit + 1, $offset);
  157. }
  158. $result = $this->db->query($sql);
  159. if ($result) {
  160. $i = 0;
  161. $num = $this->db->num_rows($result);
  162. $min = min($num, ($limit <= 0 ? $num : $limit));
  163. while ($i < $min) {
  164. $obj = $this->db->fetch_object($result);
  165. $actioncomm_static = new ActionComm($this->db);
  166. if ($actioncomm_static->fetch($obj->rowid)) {
  167. $obj_ret[] = $this->_cleanObjectDatas($actioncomm_static);
  168. }
  169. $i++;
  170. }
  171. } else {
  172. throw new RestException(503, 'Error when retrieve Agenda Event list : '.$this->db->lasterror());
  173. }
  174. if (!count($obj_ret)) {
  175. throw new RestException(404, 'No Agenda Event found');
  176. }
  177. return $obj_ret;
  178. }
  179. /**
  180. * Create Agenda Event object
  181. *
  182. * @param array $request_data Request data
  183. * @return int ID of Agenda Event
  184. */
  185. public function post($request_data = null)
  186. {
  187. if (!DolibarrApiAccess::$user->rights->agenda->myactions->create) {
  188. throw new RestException(401, "Insufficient rights to create your Agenda Event");
  189. }
  190. if (!DolibarrApiAccess::$user->hasRight('agenda', 'allactions', 'create') && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
  191. throw new RestException(401, "Insufficient rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
  192. }
  193. // Check mandatory fields
  194. $result = $this->_validate($request_data);
  195. foreach ($request_data as $field => $value) {
  196. $this->actioncomm->$field = $this->_checkValForAPI($field, $value, $this->actioncomm);
  197. }
  198. /*if (isset($request_data["lines"])) {
  199. $lines = array();
  200. foreach ($request_data["lines"] as $line) {
  201. array_push($lines, (object) $line);
  202. }
  203. $this->expensereport->lines = $lines;
  204. }*/
  205. if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) {
  206. throw new RestException(500, "Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors));
  207. }
  208. return $this->actioncomm->id;
  209. }
  210. /**
  211. * Update Agenda Event general fields
  212. *
  213. * @param int $id Id of Agenda Event to update
  214. * @param array $request_data Datas
  215. *
  216. * @return int
  217. */
  218. public function put($id, $request_data = null)
  219. {
  220. if (!DolibarrApiAccess::$user->rights->agenda->myactions->create) {
  221. throw new RestException(401, "Insufficient rights to create your Agenda Event");
  222. }
  223. if (!DolibarrApiAccess::$user->hasRight('agenda', 'allactions', 'create') && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
  224. throw new RestException(401, "Insufficient rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
  225. }
  226. $result = $this->actioncomm->fetch($id);
  227. if ($result) {
  228. $this->actioncomm->fetch_optionals();
  229. $this->actioncomm->fetch_userassigned();
  230. $this->actioncomm->oldcopy = clone $this->actioncomm;
  231. }
  232. if (!$result) {
  233. throw new RestException(404, 'actioncomm not found');
  234. }
  235. if (!DolibarrApi::_checkAccessToResource('actioncomm', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) {
  236. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  237. }
  238. foreach ($request_data as $field => $value) {
  239. if ($field == 'id') {
  240. continue;
  241. }
  242. $this->actioncomm->$field = $this->_checkValForAPI($field, $value, $this->actioncomm);
  243. }
  244. if ($this->actioncomm->update(DolibarrApiAccess::$user, 1) > 0) {
  245. return $this->get($id);
  246. }
  247. return false;
  248. }
  249. /**
  250. * Delete Agenda Event
  251. *
  252. * @param int $id Agenda Event ID
  253. *
  254. * @return array
  255. */
  256. public function delete($id)
  257. {
  258. if (!DolibarrApiAccess::$user->hasRight('agenda', 'myactions', 'delete')) {
  259. throw new RestException(401, "Insufficient rights to delete your Agenda Event");
  260. }
  261. $result = $this->actioncomm->fetch($id);
  262. if ($result) {
  263. $this->actioncomm->fetch_optionals();
  264. $this->actioncomm->fetch_userassigned();
  265. $this->actioncomm->oldcopy = clone $this->actioncomm;
  266. }
  267. if (!DolibarrApiAccess::$user->rights->agenda->allactions->delete && DolibarrApiAccess::$user->id != $this->actioncomm->userownerid) {
  268. throw new RestException(401, "Insufficient rights to delete an Agenda Event of owner id ".$this->actioncomm->userownerid.' Your id is '.DolibarrApiAccess::$user->id);
  269. }
  270. if (!$result) {
  271. throw new RestException(404, 'Agenda Event not found');
  272. }
  273. if (!DolibarrApi::_checkAccessToResource('actioncomm', $this->actioncomm->id, 'actioncomm', '', 'fk_soc', 'id')) {
  274. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  275. }
  276. if (!$this->actioncomm->delete(DolibarrApiAccess::$user)) {
  277. throw new RestException(500, 'Error when delete Agenda Event : '.$this->actioncomm->error);
  278. }
  279. return array(
  280. 'success' => array(
  281. 'code' => 200,
  282. 'message' => 'Agenda Event deleted'
  283. )
  284. );
  285. }
  286. /**
  287. * Validate fields before create or update object
  288. *
  289. * @param array $data Array with data to verify
  290. * @return array
  291. * @throws RestException
  292. */
  293. private function _validate($data)
  294. {
  295. $event = array();
  296. foreach (AgendaEvents::$FIELDS as $field) {
  297. if (!isset($data[$field])) {
  298. throw new RestException(400, "$field field missing");
  299. }
  300. $event[$field] = $data[$field];
  301. }
  302. return $event;
  303. }
  304. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  305. /**
  306. * Clean sensible object datas
  307. *
  308. * @param Object $object Object to clean
  309. * @return Object Object with cleaned properties
  310. */
  311. protected function _cleanObjectDatas($object)
  312. {
  313. // phpcs:enable
  314. $object = parent::_cleanObjectDatas($object);
  315. unset($object->note); // alreaydy into note_private
  316. unset($object->usermod);
  317. unset($object->libelle);
  318. unset($object->context);
  319. unset($object->canvas);
  320. unset($object->contact);
  321. unset($object->contact_id);
  322. unset($object->thirdparty);
  323. unset($object->user);
  324. unset($object->origin);
  325. unset($object->origin_id);
  326. unset($object->ref_ext);
  327. unset($object->statut);
  328. unset($object->state_code);
  329. unset($object->state_id);
  330. unset($object->state);
  331. unset($object->region);
  332. unset($object->region_code);
  333. unset($object->country);
  334. unset($object->country_id);
  335. unset($object->country_code);
  336. unset($object->barcode_type);
  337. unset($object->barcode_type_code);
  338. unset($object->barcode_type_label);
  339. unset($object->barcode_type_coder);
  340. unset($object->mode_reglement_id);
  341. unset($object->cond_reglement_id);
  342. unset($object->cond_reglement);
  343. unset($object->fk_delivery_address);
  344. unset($object->shipping_method_id);
  345. unset($object->fk_account);
  346. unset($object->total_ht);
  347. unset($object->total_tva);
  348. unset($object->total_localtax1);
  349. unset($object->total_localtax2);
  350. unset($object->total_ttc);
  351. unset($object->fk_incoterms);
  352. unset($object->label_incoterms);
  353. unset($object->location_incoterms);
  354. unset($object->name);
  355. unset($object->lastname);
  356. unset($object->firstname);
  357. unset($object->civility_id);
  358. unset($object->contact);
  359. unset($object->societe);
  360. unset($object->demand_reason_id);
  361. unset($object->transport_mode_id);
  362. unset($object->region_id);
  363. unset($object->actions);
  364. unset($object->lines);
  365. unset($object->modelpdf);
  366. return $object;
  367. }
  368. }