PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/Calendar/Frontend/Json.php

https://github.com/testruby/Tine-2.0-Open-Source-Groupware-and-CRM
PHP | 360 lines | 174 code | 48 blank | 138 comment | 16 complexity | 568ccc8dd8f64dfad7a74c316e09e033 MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0
  4. *
  5. * @package Calendar
  6. * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
  7. * @author Cornelius Weiss <c.weiss@metaways.de>
  8. * @copyright Copyright (c) 2007-2008 Metaways Infosystems GmbH (http://www.metaways.de)
  9. */
  10. /**
  11. * json interface for calendar
  12. * @package Calendar
  13. */
  14. class Calendar_Frontend_Json extends Tinebase_Frontend_Json_Abstract
  15. {
  16. protected $_applicationName = 'Calendar';
  17. /**
  18. * creates an exception instance of a recurring event
  19. *
  20. * NOTE: deleting persistent exceptions is done via a normal delte action
  21. * and handled in the controller
  22. *
  23. * @param array $recordData
  24. * @param bool $deleteInstance
  25. * @param bool $deleteAllFollowing
  26. * @param bool $checkBusyConficts
  27. * @return array exception Event | updated baseEvent
  28. */
  29. public function createRecurException($recordData, $deleteInstance, $deleteAllFollowing, $checkBusyConficts = FALSE)
  30. {
  31. $event = new Calendar_Model_Event(array(), TRUE);
  32. $event->setFromJsonInUsersTimezone($recordData);
  33. $returnEvent = Calendar_Controller_Event::getInstance()->createRecurException($event, $deleteInstance, $deleteAllFollowing, $checkBusyConficts);
  34. return $this->getEvent($returnEvent->getId());
  35. }
  36. /**
  37. * deletes existing events
  38. *
  39. * @param array $_ids
  40. * @return string
  41. */
  42. public function deleteEvents($ids)
  43. {
  44. return $this->_delete($ids, Calendar_Controller_Event::getInstance());
  45. }
  46. /**
  47. * deletes existing resources
  48. *
  49. * @param array $_ids
  50. * @return string
  51. */
  52. public function deleteResources($ids)
  53. {
  54. return $this->_delete($ids, Calendar_Controller_Resource::getInstance());
  55. }
  56. /**
  57. * deletes a recur series
  58. *
  59. * @param array $recordData
  60. * @return void
  61. */
  62. public function deleteRecurSeries($recordData)
  63. {
  64. $event = new Calendar_Model_Event(array(), TRUE);
  65. $event->setFromJsonInUsersTimezone($recordData);
  66. Calendar_Controller_Event::getInstance()->deleteRecurSeries($event);
  67. return array('success' => true);
  68. }
  69. /**
  70. * Return a single event
  71. *
  72. * @param string $id
  73. * @return array record data
  74. */
  75. public function getEvent($id)
  76. {
  77. return $this->_get($id, Calendar_Controller_Event::getInstance());
  78. }
  79. /**
  80. * Returns registry data of the calendar.
  81. *
  82. * @return mixed array 'variable name' => 'data'
  83. *
  84. * @todo move exception handling (no default calender found) to another place?
  85. */
  86. public function getRegistryData()
  87. {
  88. $defaultCalendarId = Tinebase_Core::getPreference('Calendar')->getValue(Calendar_Preference::DEFAULTCALENDAR);
  89. try {
  90. $defaultCalendarArray = Tinebase_Container::getInstance()->getContainerById($defaultCalendarId)->toArray();
  91. $defaultCalendarArray['account_grants'] = Tinebase_Container::getInstance()->getGrantsOfAccount(Tinebase_Core::getUser(), $defaultCalendarId)->toArray();
  92. } catch (Exception $e) {
  93. // remove default cal pref
  94. Tinebase_Core::getPreference('Calendar')->deleteUserPref(Calendar_Preference::DEFAULTCALENDAR);
  95. $defaultCalendarArray = array();
  96. }
  97. return array(
  98. // registry setting is called defaultContainer to be compatible to the other apps
  99. 'defaultContainer' => $defaultCalendarArray
  100. );
  101. }
  102. /**
  103. * Return a single resouece
  104. *
  105. * @param string $id
  106. * @return array record data
  107. */
  108. public function getResource($id)
  109. {
  110. return $this->_get($id, Calendar_Controller_Resource::getInstance());
  111. }
  112. /**
  113. * Search for events matching given arguments
  114. *
  115. * @param array $_filter
  116. * @param array $_paging
  117. * @return array
  118. */
  119. public function searchEvents($filter, $paging)
  120. {
  121. $controller = Calendar_Controller_Event::getInstance();
  122. $decodedPagination = is_array($paging) ? $paging : Zend_Json::decode($paging);
  123. $pagination = new Tinebase_Model_Pagination($decodedPagination);
  124. $clientFilter = $filter = $this->_decodeFilter($filter, 'Calendar_Model_EventFilter');
  125. // add fixed calendar on demand
  126. $fixedCalendars = Tinebase_Config::getInstance()->getConfigAsArray('fixedCalendars', 'Calendar');
  127. if (is_array($fixedCalendars) && ! empty($fixedCalendars)) {
  128. $fixed = new Calendar_Model_EventFilter(array(), 'AND');
  129. $fixed->addFilter( new Tinebase_Model_Filter_Text('container_id', 'in', $fixedCalendars));
  130. $periodFilter = $filter->getFilter('period');
  131. if ($periodFilter) {
  132. $fixed->addFilter($periodFilter);
  133. }
  134. $og = new Calendar_Model_EventFilter(array(), 'OR');
  135. $og->addFilterGroup($fixed);
  136. $og->addFilterGroup($clientFilter);
  137. $filter = new Calendar_Model_EventFilter(array(), 'AND');
  138. $filter->addFilterGroup($og);
  139. }
  140. $records = $controller->search($filter, $pagination, FALSE);
  141. $result = $this->_multipleRecordsToJson($records, $clientFilter);
  142. return array(
  143. 'results' => $result,
  144. 'totalcount' => count($result),
  145. 'filter' => $clientFilter->toArray(TRUE),
  146. );
  147. }
  148. /**
  149. * Search for resources matching given arguments
  150. *
  151. * @param array $_filter
  152. * @param array $_paging
  153. * @return array
  154. */
  155. public function searchResources($filter, $paging)
  156. {
  157. return $this->_search($filter, $paging, Calendar_Controller_Resource::getInstance(), 'Calendar_Model_ResourceFilter');
  158. }
  159. /**
  160. * creates/updates an event
  161. *
  162. * @param array $recordData
  163. * @param bool $checkBusyConficts
  164. * @return array created/updated event
  165. */
  166. public function saveEvent($recordData, $checkBusyConficts=FALSE)
  167. {
  168. return $this->_save($recordData, Calendar_Controller_Event::getInstance(), 'Event', 'id', array($checkBusyConficts));
  169. }
  170. /**
  171. * creates/updates a Resource
  172. *
  173. * @param array $recordData
  174. * @return array created/updated Resource
  175. */
  176. public function saveResource($recordData)
  177. {
  178. return $this->_save($recordData, Calendar_Controller_Resource::getInstance(), 'Resource');
  179. }
  180. /**
  181. * sets attendee status for an attender on the given event
  182. *
  183. * NOTE: for recur events we implicitly create an exceptions on demand
  184. *
  185. * @param array $eventData
  186. * @param array $attenderData
  187. * @param string $authKey
  188. * @return array complete event
  189. */
  190. public function setAttenderStatus($eventData, $attenderData, $authKey)
  191. {
  192. $event = new Calendar_Model_Event($eventData);
  193. $attender = new Calendar_Model_Attender($attenderData);
  194. Calendar_Controller_Event::getInstance()->attenderStatusUpdate($event, $attender, $authKey);
  195. return $this->getEvent($event->getId());
  196. }
  197. /**
  198. * updated a recur series
  199. *
  200. * @param array $recordData
  201. * @param bool $checkBusyConficts
  202. * @noparamyet JSONstring $returnPeriod NOTE IMPLMENTED YET
  203. * @return array
  204. */
  205. public function updateRecurSeries($recordData, $checkBusyConficts=FALSE /*, $returnPeriod*/)
  206. {
  207. $recurInstance = new Calendar_Model_Event(array(), TRUE);
  208. $recurInstance->setFromJsonInUsersTimezone($recordData);
  209. //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(print_r($recurInstance->toArray(), true));
  210. $baseEvent = Calendar_Controller_Event::getInstance()->updateRecurSeries($recurInstance, $checkBusyConficts);
  211. return $this->getEvent($baseEvent->getId());
  212. }
  213. /**
  214. * returns record prepared for json transport
  215. *
  216. * @param Tinebase_Record_Interface $_record
  217. * @return array record data
  218. */
  219. protected function _recordToJson($_record)
  220. {
  221. if ($_record instanceof Calendar_Model_Event) {
  222. Calendar_Model_Attender::resolveAttendee($_record->attendee);
  223. $this->_resolveRrule($_record);
  224. $this->_resolveOrganizer($_record);
  225. }
  226. $recordData = parent::_recordToJson($_record);
  227. return $recordData;
  228. }
  229. /**
  230. * returns multiple records prepared for json transport
  231. *
  232. * @param Tinebase_Record_RecordSet $_records Tinebase_Record_Abstract
  233. * @param Tinebase_Model_Filter_FilterGroup
  234. * @return array data
  235. */
  236. protected function _multipleRecordsToJson(Tinebase_Record_RecordSet $_records, $_filter=NULL)
  237. {
  238. if ($_records->getRecordClassName() == 'Calendar_Model_Event') {
  239. if (is_null($_filter)) {
  240. throw new Tinebase_Exception_InvalidArgument('Required argument $_filter is missing');
  241. }
  242. Tinebase_Notes::getInstance()->getMultipleNotesOfRecords($_records);
  243. Calendar_Model_Attender::resolveAttendee($_records->attendee);
  244. $this->_resolveOrganizer($_records);
  245. $this->_resolveRrule($_records);
  246. // get/resolve alarms
  247. Calendar_Controller_Event::getInstance()->getAlarms($_records);
  248. //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(print_r($_records->toArray(), true));
  249. // merge recurset and remove not matching records
  250. $period = $_filter->getFilter('period');
  251. if ($period) {
  252. Calendar_Model_Rrule::mergeRecuranceSet($_records, $period->getFrom(), $period->getUntil());
  253. foreach ($_records as $event) {
  254. if (! $event->isInPeriod($period)) {
  255. if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . ' (' . __LINE__
  256. . ') Removing not matching event ' . $event->summary);
  257. $_records->removeRecord($event);
  258. }
  259. }
  260. }
  261. // @todo sort (record set)
  262. $eventsData = parent::_multipleRecordsToJson($_records);
  263. foreach($eventsData as $eventData) {
  264. if (! array_key_exists(Tinebase_Model_Grants::GRANT_READ, $eventData) || ! $eventData[Tinebase_Model_Grants::GRANT_READ]) {
  265. $eventData['notes'] = array();
  266. $eventData['tags'] = array();
  267. }
  268. }
  269. return $eventsData;
  270. }
  271. //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(print_r($_records->toArray(), true));
  272. return parent::_multipleRecordsToJson($_records);
  273. }
  274. /**
  275. * resolves organizer of given event
  276. *
  277. * @param Tinebase_Record_RecordSet|Calendar_Model_Event $_events
  278. */
  279. protected function _resolveOrganizer($_events)
  280. {
  281. $events = $_events instanceof Tinebase_Record_RecordSet ? $_events : array($_events);
  282. $organizerIds = array();
  283. foreach ($events as $event) {
  284. if ($event->organizer) {
  285. $organizerIds[] = $event->organizer;
  286. }
  287. }
  288. $organizers = Addressbook_Controller_Contact::getInstance()->getMultiple(array_unique($organizerIds), TRUE);
  289. foreach ($events as $event) {
  290. if ($event->organizer) {
  291. $idx = $organizers->getIndexById($event->organizer);
  292. if ($idx !== FALSE) {
  293. $event->organizer = $organizers[$idx];
  294. }
  295. }
  296. }
  297. }
  298. /**
  299. * resolves rrule of given event
  300. *
  301. * @param Tinebase_Record_RecordSet|Calendar_Model_Event $_events
  302. */
  303. protected function _resolveRrule($_events)
  304. {
  305. $events = $_events instanceof Tinebase_Record_RecordSet ? $_events : array($_events);
  306. foreach ($events as $event) {
  307. if ($event->rrule) {
  308. $event->rrule = Calendar_Model_Rrule::getRruleFromString($event->rrule);
  309. }
  310. }
  311. }
  312. }