api_agendaevents.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. $min = min($num, ($limit <= 0 ? $num : $limit));
  130. while ($i < $min)
  131. {
  132. $obj = $db->fetch_object($result);
  133. $actioncomm_static = new ActionComm($db);
  134. if($actioncomm_static->fetch($obj->rowid)) {
  135. $obj_ret[] = $this->_cleanObjectDatas($actioncomm_static);
  136. }
  137. $i++;
  138. }
  139. }
  140. else {
  141. throw new RestException(503, 'Error when retrieve Agenda Event list : '.$db->lasterror());
  142. }
  143. if( ! count($obj_ret)) {
  144. throw new RestException(404, 'No Agenda Event found');
  145. }
  146. return $obj_ret;
  147. }
  148. /**
  149. * Create Agenda Event object
  150. *
  151. * @param array $request_data Request data
  152. * @return int ID of Agenda Event
  153. */
  154. function post($request_data = NULL)
  155. {
  156. if(! DolibarrApiAccess::$user->rights->agenda->myactions->create) {
  157. throw new RestException(401, "Insuffisant rights to create your Agenda Event");
  158. }
  159. if(! DolibarrApiAccess::$user->rights->agenda->allactions->create && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
  160. throw new RestException(401, "Insuffisant rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
  161. }
  162. // Check mandatory fields
  163. $result = $this->_validate($request_data);
  164. foreach($request_data as $field => $value) {
  165. $this->actioncomm->$field = $value;
  166. }
  167. /*if (isset($request_data["lines"])) {
  168. $lines = array();
  169. foreach ($request_data["lines"] as $line) {
  170. array_push($lines, (object) $line);
  171. }
  172. $this->expensereport->lines = $lines;
  173. }*/
  174. if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) {
  175. throw new RestException(500, "Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors));
  176. }
  177. return $this->actioncomm->id;
  178. }
  179. /**
  180. * Update Agenda Event general fields (won't touch lines of expensereport)
  181. *
  182. * @param int $id Id of Agenda Event to update
  183. * @param array $request_data Datas
  184. *
  185. * @return int
  186. */
  187. /*
  188. function put($id, $request_data = NULL) {
  189. if(! DolibarrApiAccess::$user->rights->agenda->myactions->create) {
  190. throw new RestException(401, "Insuffisant rights to create your Agenda Event");
  191. }
  192. if(! DolibarrApiAccess::$user->rights->agenda->allactions->create && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
  193. throw new RestException(401, "Insuffisant rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
  194. }
  195. $result = $this->expensereport->fetch($id);
  196. if( ! $result ) {
  197. throw new RestException(404, 'expensereport not found');
  198. }
  199. if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
  200. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  201. }
  202. foreach($request_data as $field => $value) {
  203. if ($field == 'id') continue;
  204. $this->expensereport->$field = $value;
  205. }
  206. if($this->expensereport->update($id, DolibarrApiAccess::$user,1,'','','update'))
  207. return $this->get($id);
  208. return false;
  209. }
  210. */
  211. /**
  212. * Delete Agenda Event
  213. *
  214. * @param int $id Agenda Event ID
  215. *
  216. * @return array
  217. */
  218. function delete($id)
  219. {
  220. if(! DolibarrApiAccess::$user->rights->agenda->myactions->delete) {
  221. throw new RestException(401, "Insuffisant rights to delete your Agenda Event");
  222. }
  223. $result = $this->actioncomm->fetch($id);
  224. if(! DolibarrApiAccess::$user->rights->agenda->allactions->delete && DolibarrApiAccess::$user->id != $this->actioncomm->userownerid) {
  225. throw new RestException(401, "Insuffisant rights to delete an Agenda Event of owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
  226. }
  227. if( ! $result ) {
  228. throw new RestException(404, 'Agenda Event not found');
  229. }
  230. if( ! DolibarrApi::_checkAccessToResource('actioncomm',$this->actioncomm->id)) {
  231. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  232. }
  233. if( ! $this->actioncomm->delete(DolibarrApiAccess::$user)) {
  234. throw new RestException(500, 'Error when delete Agenda Event : '.$this->actioncomm->error);
  235. }
  236. return array(
  237. 'success' => array(
  238. 'code' => 200,
  239. 'message' => 'Agenda Event deleted'
  240. )
  241. );
  242. }
  243. /**
  244. * Validate fields before create or update object
  245. *
  246. * @param array $data Array with data to verify
  247. * @return array
  248. * @throws RestException
  249. */
  250. function _validate($data)
  251. {
  252. $event = array();
  253. foreach (AgendaEvents::$FIELDS as $field) {
  254. if (!isset($data[$field]))
  255. throw new RestException(400, "$field field missing");
  256. $event[$field] = $data[$field];
  257. }
  258. return $event;
  259. }
  260. }