api_agendaevents.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 <http://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 Event $actioncomm {@type ActionComm}
  35. */
  36. public $actioncomm;
  37. /**
  38. * Constructor
  39. */
  40. 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 array|mixed Data without useless information
  53. *
  54. * @throws RestException
  55. */
  56. function get($id)
  57. {
  58. if(! DolibarrApiAccess::$user->rights->agenda->myactions->read) {
  59. throw new RestException(401, "Insuffisant rights to read an event");
  60. }
  61. $result = $this->actioncomm->fetch($id);
  62. if( ! $result ) {
  63. throw new RestException(404, 'Agenda Events not found');
  64. }
  65. if(! DolibarrApiAccess::$user->rights->agenda->allactions->read && $this->actioncomm->ownerid != DolibarrApiAccess::$user->id) {
  66. throw new RestException(401, "Insuffisant rights to read event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
  67. }
  68. if( ! DolibarrApi::_checkAccessToResource('agenda',$this->actioncomm->id)) {
  69. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  70. }
  71. $this->actioncomm->fetchObjectLinked();
  72. return $this->_cleanObjectDatas($this->actioncomm);
  73. }
  74. /**
  75. * List Agenda Events
  76. *
  77. * Get a list of Agenda Events
  78. *
  79. * @param string $sortfield Sort field
  80. * @param string $sortorder Sort order
  81. * @param int $limit Limit for list
  82. * @param int $page Page number
  83. * @param string $user_ids User ids filter field (owners of event). Example: '1' or '1,2,3' {@pattern /^[0-9,]*$/i}
  84. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.label:like:'%dol%') and (t.date_creation:<:'20160101')"
  85. * @return array Array of Agenda Events objects
  86. */
  87. function index($sortfield = "t.id", $sortorder = 'ASC', $limit = 0, $page = 0, $user_ids = 0, $sqlfilters = '') {
  88. global $db, $conf;
  89. $obj_ret = array();
  90. // case of external user
  91. $socid = 0;
  92. if (! empty(DolibarrApiAccess::$user->societe_id)) $socid = DolibarrApiAccess::$user->societe_id;
  93. // If the internal user must only see his customers, force searching by him
  94. $search_sale = 0;
  95. if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id;
  96. $sql = "SELECT t.id as rowid";
  97. $sql.= " FROM ".MAIN_DB_PREFIX."actioncomm as t";
  98. $sql.= ' WHERE t.entity IN ('.getEntity('agenda', 1).')';
  99. if ($user_ids) $sql.=" AND t.fk_user_action IN (".$user_ids.")";
  100. if ($socid > 0) $sql.= " AND t.fk_soc = ".$socid;
  101. // Insert sale filter
  102. if ($search_sale > 0)
  103. {
  104. $sql .= " AND sc.fk_user = ".$search_sale;
  105. }
  106. // Add sql filters
  107. if ($sqlfilters)
  108. {
  109. if (! DolibarrApi::_checkFilters($sqlfilters))
  110. {
  111. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  112. }
  113. $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  114. $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  115. }
  116. $sql.= $db->order($sortfield, $sortorder);
  117. if ($limit) {
  118. if ($page < 0)
  119. {
  120. $page = 0;
  121. }
  122. $offset = $limit * $page;
  123. $sql.= $db->plimit($limit + 1, $offset);
  124. }
  125. $result = $db->query($sql);
  126. if ($result)
  127. {
  128. $num = $db->num_rows($result);
  129. while ($i < min($num, ($limit <= 0 ? $num : $limit)))
  130. {
  131. $obj = $db->fetch_object($result);
  132. $actioncomm_static = new ActionComm($db);
  133. if($actioncomm_static->fetch($obj->rowid)) {
  134. $obj_ret[] = $this->_cleanObjectDatas($actioncomm_static);
  135. }
  136. $i++;
  137. }
  138. }
  139. else {
  140. throw new RestException(503, 'Error when retrieve Agenda Event list : '.$db->lasterror());
  141. }
  142. if( ! count($obj_ret)) {
  143. throw new RestException(404, 'No Agenda Event found');
  144. }
  145. return $obj_ret;
  146. }
  147. /**
  148. * Create Agenda Event object
  149. *
  150. * @param array $request_data Request data
  151. * @return int ID of Agenda Event
  152. */
  153. function post($request_data = NULL)
  154. {
  155. if(! DolibarrApiAccess::$user->rights->agenda->myactions->create) {
  156. throw new RestException(401, "Insuffisant rights to create your Agenda Event");
  157. }
  158. if(! DolibarrApiAccess::$user->rights->agenda->allactions->create && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
  159. throw new RestException(401, "Insuffisant rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
  160. }
  161. // Check mandatory fields
  162. $result = $this->_validate($request_data);
  163. foreach($request_data as $field => $value) {
  164. $this->actioncomm->$field = $value;
  165. }
  166. /*if (isset($request_data["lines"])) {
  167. $lines = array();
  168. foreach ($request_data["lines"] as $line) {
  169. array_push($lines, (object) $line);
  170. }
  171. $this->expensereport->lines = $lines;
  172. }*/
  173. if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) {
  174. throw new RestException(500, "Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors));
  175. }
  176. return $this->actioncomm->id;
  177. }
  178. /**
  179. * Update Agenda Event general fields (won't touch lines of expensereport)
  180. *
  181. * @param int $id Id of Agenda Event to update
  182. * @param array $request_data Datas
  183. *
  184. * @return int
  185. */
  186. /*
  187. function put($id, $request_data = NULL) {
  188. if(! DolibarrApiAccess::$user->rights->agenda->myactions->create) {
  189. throw new RestException(401, "Insuffisant rights to create your Agenda Event");
  190. }
  191. if(! DolibarrApiAccess::$user->rights->agenda->allactions->create && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
  192. throw new RestException(401, "Insuffisant rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
  193. }
  194. $result = $this->expensereport->fetch($id);
  195. if( ! $result ) {
  196. throw new RestException(404, 'expensereport not found');
  197. }
  198. if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
  199. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  200. }
  201. foreach($request_data as $field => $value) {
  202. if ($field == 'id') continue;
  203. $this->expensereport->$field = $value;
  204. }
  205. if($this->expensereport->update($id, DolibarrApiAccess::$user,1,'','','update'))
  206. return $this->get($id);
  207. return false;
  208. }
  209. */
  210. /**
  211. * Delete Agenda Event
  212. *
  213. * @param int $id Agenda Event ID
  214. *
  215. * @return array
  216. */
  217. function delete($id)
  218. {
  219. if(! DolibarrApiAccess::$user->rights->agenda->myactions->delete) {
  220. throw new RestException(401, "Insuffisant rights to delete your Agenda Event");
  221. }
  222. $result = $this->actioncomm->fetch($id);
  223. if(! DolibarrApiAccess::$user->rights->agenda->allactions->delete && DolibarrApiAccess::$user->id != $this->actioncomm->userownerid) {
  224. throw new RestException(401, "Insuffisant rights to delete an Agenda Event of owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
  225. }
  226. if( ! $result ) {
  227. throw new RestException(404, 'Agenda Event not found');
  228. }
  229. if( ! DolibarrApi::_checkAccessToResource('actioncomm',$this->actioncomm->id)) {
  230. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  231. }
  232. if( ! $this->actioncomm->delete(DolibarrApiAccess::$user)) {
  233. throw new RestException(500, 'Error when delete Agenda Event : '.$this->actioncomm->error);
  234. }
  235. return array(
  236. 'success' => array(
  237. 'code' => 200,
  238. 'message' => 'Agenda Event deleted'
  239. )
  240. );
  241. }
  242. /**
  243. * Validate fields before create or update object
  244. *
  245. * @param array $data Array with data to verify
  246. * @return array
  247. * @throws RestException
  248. */
  249. function _validate($data)
  250. {
  251. $event = array();
  252. foreach (AgendaEvents::$FIELDS as $field) {
  253. if (!isset($data[$field]))
  254. throw new RestException(400, "$field field missing");
  255. $event[$field] = $data[$field];
  256. }
  257. return $event;
  258. }
  259. }