PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/tine20/Timetracker/Frontend/Json.php

https://github.com/testruby/Tine-2.0-Open-Source-Groupware-and-CRM
PHP | 303 lines | 144 code | 38 blank | 121 comment | 11 complexity | 7d73deabd7e9dcb96be4e41341aa4a9d MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0
  4. * @package Timetracker
  5. * @subpackage Frontend
  6. * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
  7. * @author Philipp Schüle <p.schuele@metaways.de>
  8. * @copyright Copyright (c) 2007-2011 Metaways Infosystems GmbH (http://www.metaways.de)
  9. *
  10. */
  11. /**
  12. *
  13. * This class handles all Json requests for the Timetracker application
  14. *
  15. * @package Timetracker
  16. * @subpackage Frontend
  17. */
  18. class Timetracker_Frontend_Json extends Tinebase_Frontend_Json_Abstract
  19. {
  20. /**
  21. * timesheet controller
  22. *
  23. * @var Timetracker_Controller_Timesheet
  24. */
  25. protected $_timesheetController = NULL;
  26. /**
  27. * timesheet controller
  28. *
  29. * @var Timetracker_Controller_Timeaccount
  30. */
  31. protected $_timeaccountController = NULL;
  32. /**
  33. * the constructor
  34. *
  35. */
  36. public function __construct()
  37. {
  38. $this->_applicationName = 'Timetracker';
  39. $this->_timesheetController = Timetracker_Controller_Timesheet::getInstance();
  40. $this->_timeaccountController = Timetracker_Controller_Timeaccount::getInstance();
  41. }
  42. /************************************** protected helper functions **************************************/
  43. /**
  44. * returns record prepared for json transport
  45. *
  46. * @param Tinebase_Record_Interface $_record
  47. * @return array record data
  48. */
  49. protected function _recordToJson($_record)
  50. {
  51. switch (get_class($_record)) {
  52. case 'Timetracker_Model_Timesheet':
  53. $_record['timeaccount_id'] = $_record['timeaccount_id'] ? $this->_timeaccountController->get($_record['timeaccount_id']) : $_record['timeaccount_id'];
  54. $_record['timeaccount_id']['account_grants'] = Timetracker_Model_TimeaccountGrants::getGrantsOfAccount(Tinebase_Core::get('currentAccount'), $_record['timeaccount_id']);
  55. $_record['timeaccount_id']['account_grants'] = $this->_resolveTimesheetGrantsByTimeaccountGrants($_record['timeaccount_id']['account_grants'], $_record['account_id']);
  56. Tinebase_User::getInstance()->resolveUsers($_record, 'account_id');
  57. $recordArray = parent::_recordToJson($_record);
  58. break;
  59. case 'Timetracker_Model_Timeaccount':
  60. $recordArray = parent::_recordToJson($_record);
  61. // When editing a single TA we send _ALL_ grants to the client
  62. $recordArray['grants'] = Timetracker_Model_TimeaccountGrants::getTimeaccountGrants($_record)->toArray();
  63. foreach($recordArray['grants'] as &$value) {
  64. switch($value['account_type']) {
  65. case Tinebase_Acl_Rights::ACCOUNT_TYPE_USER:
  66. $value['account_name'] = Tinebase_User::getInstance()->getUserById($value['account_id'])->toArray();
  67. break;
  68. case Tinebase_Acl_Rights::ACCOUNT_TYPE_GROUP:
  69. $value['account_name'] = Tinebase_Group::getInstance()->getGroupById($value['account_id'])->toArray();
  70. break;
  71. case Tinebase_Acl_Rights::ACCOUNT_TYPE_ANYONE:
  72. $value['account_name'] = array('accountDisplayName' => 'Anyone');
  73. break;
  74. default:
  75. throw new Tinebase_Exception_InvalidArgument('Unsupported accountType.');
  76. break;
  77. }
  78. }
  79. break;
  80. }
  81. return $recordArray;
  82. }
  83. /**
  84. * returns multiple records prepared for json transport
  85. *
  86. * NOTE: we can't use parent::_multipleRecordsToJson here because of the different container handling
  87. *
  88. * @param Tinebase_Record_RecordSet $_leads Crm_Model_Lead
  89. * @return array data
  90. */
  91. protected function _multipleRecordsToJson(Tinebase_Record_RecordSet $_records, $_filter=NULL)
  92. {
  93. if (count($_records) == 0) {
  94. return array();
  95. }
  96. switch ($_records->getRecordClassName()) {
  97. case 'Timetracker_Model_Timesheet':
  98. // resolve timeaccounts
  99. $timeaccountIds = $_records->timeaccount_id;
  100. $timeaccounts = $this->_timeaccountController->getMultiple(array_unique(array_values($timeaccountIds)));
  101. Timetracker_Model_TimeaccountGrants::getGrantsOfRecords($timeaccounts, Tinebase_Core::get('currentAccount'));
  102. foreach ($_records as $record) {
  103. $idx = $timeaccounts->getIndexById($record->timeaccount_id);
  104. if ($idx !== FALSE) {
  105. $record->timeaccount_id = $timeaccounts[$idx];
  106. $record->timeaccount_id->account_grants = $this->_resolveTimesheetGrantsByTimeaccountGrants($record->timeaccount_id->account_grants, $record->account_id);
  107. } else {
  108. Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not resolve timeaccount (id: ' . $record->timeaccount_id . '). No permission?');
  109. }
  110. }
  111. // resolve user afterwards because we compare ids in _resolveTimesheetGrantsByTimeaccountGrants()
  112. Tinebase_User::getInstance()->resolveMultipleUsers($_records, array('account_id', 'created_by', 'last_modified_by'), true);
  113. break;
  114. case 'Timetracker_Model_Timeaccount':
  115. // resolve timeaccounts grants
  116. Timetracker_Model_TimeaccountGrants::getGrantsOfRecords($_records, Tinebase_Core::get('currentAccount'));
  117. $this->_resolveTimeaccountGrants($_records);
  118. break;
  119. }
  120. Tinebase_Tags::getInstance()->getMultipleTagsOfRecords($_records);
  121. $_records->setTimezone(Tinebase_Core::get('userTimeZone'));
  122. $_records->convertDates = true;
  123. $result = $_records->toArray();
  124. return $result;
  125. }
  126. /**
  127. * calculate effective ts grants so the client doesn't need to calculate them
  128. *
  129. * @param array $TimeaccountGrantsArray
  130. * @param int $timesheetOwnerId
  131. * @return array
  132. */
  133. protected function _resolveTimesheetGrantsByTimeaccountGrants($timeaccountGrantsArray, $timesheetOwnerId)
  134. {
  135. $manageAllRight = Timetracker_Controller_Timeaccount::getInstance()->checkRight(Timetracker_Acl_Rights::MANAGE_TIMEACCOUNTS, FALSE);
  136. $currentUserId = Tinebase_Core::getUser()->getId();
  137. $modifyGrant = $manageAllRight || ($timeaccountGrantsArray[Timetracker_Model_TimeaccountGrants::BOOK_OWN]
  138. && $timesheetOwnerId == $currentUserId) || $timeaccountGrantsArray[Timetracker_Model_TimeaccountGrants::BOOK_ALL];
  139. $timeaccountGrantsArray[Tinebase_Model_Grants::GRANT_READ] = true;
  140. $timeaccountGrantsArray[Tinebase_Model_Grants::GRANT_EDIT] = $modifyGrant;
  141. $timeaccountGrantsArray[Tinebase_Model_Grants::GRANT_DELETE] = $modifyGrant;
  142. return $timeaccountGrantsArray;
  143. }
  144. /**
  145. * calculate effective ta grants so the client doesn't need to calculate them
  146. *
  147. * @param array $_timesaccounts
  148. */
  149. protected function _resolveTimeaccountGrants(Tinebase_Record_RecordSet $_timesaccounts)
  150. {
  151. $manageAllRight = Timetracker_Controller_Timeaccount::getInstance()->checkRight(Timetracker_Acl_Rights::MANAGE_TIMEACCOUNTS, FALSE);
  152. foreach ($_timesaccounts as $timeaccount) {
  153. $timeaccountGrantsArray = $timeaccount->account_grants;
  154. $modifyGrant = $manageAllRight || $timeaccountGrantsArray[Timetracker_Model_TimeaccountGrants::GRANT_ADMIN];
  155. $timeaccountGrantsArray[Tinebase_Model_Grants::GRANT_READ] = true;
  156. $timeaccountGrantsArray[Tinebase_Model_Grants::GRANT_EDIT] = $modifyGrant;
  157. $timeaccountGrantsArray[Tinebase_Model_Grants::GRANT_DELETE] = $modifyGrant;
  158. $timeaccount->account_grants = $timeaccountGrantsArray;
  159. // also move the grants into the container_id property, as the clients expects records to
  160. // be contained in some kind of container where it searches the grants in
  161. $timeaccount->container_id = array(
  162. 'account_grants' => $timeaccountGrantsArray
  163. );
  164. }
  165. }
  166. /************************************** public API **************************************/
  167. /**
  168. * Search for records matching given arguments
  169. *
  170. * @param array $filter
  171. * @param array $paging
  172. * @return array
  173. */
  174. public function searchTimesheets($filter, $paging)
  175. {
  176. $result = $this->_search($filter, $paging, $this->_timesheetController, 'Timetracker_Model_TimesheetFilter');
  177. $result['totalcountbillable'] = $result['totalcount']['countBillable'];
  178. $result['totalsum'] = $result['totalcount']['sum'];
  179. $result['totalsumbillable'] = $result['totalcount']['sumBillable'];
  180. $result['totalcount'] = $result['totalcount']['count'];
  181. return $result;
  182. }
  183. /**
  184. * Return a single record
  185. *
  186. * @param string $id
  187. * @return array record data
  188. */
  189. public function getTimesheet($id)
  190. {
  191. return $this->_get($id, $this->_timesheetController);
  192. }
  193. /**
  194. * creates/updates a record
  195. *
  196. * @param array $recordData
  197. * @return array created/updated record
  198. */
  199. public function saveTimesheet($recordData)
  200. {
  201. return $this->_save($recordData, $this->_timesheetController, 'Timesheet');
  202. }
  203. /**
  204. * update some fields of multiple records
  205. *
  206. * @param array $filter
  207. * @param array $values
  208. * @return array with number of updated records
  209. */
  210. public function updateMultipleTimesheets($filter, $values)
  211. {
  212. return $this->_updateMultiple($filter, $values, $this->_timesheetController, 'Timetracker_Model_TimesheetFilter');
  213. }
  214. /**
  215. * deletes existing records
  216. *
  217. * @param array $ids
  218. * @return string
  219. */
  220. public function deleteTimesheets($ids)
  221. {
  222. return $this->_delete($ids, $this->_timesheetController);
  223. }
  224. /**
  225. * Search for records matching given arguments
  226. *
  227. * @param array $filter
  228. * @param array $paging
  229. * @return array
  230. */
  231. public function searchTimeaccounts($filter, $paging)
  232. {
  233. return $this->_search($filter, $paging, $this->_timeaccountController, 'Timetracker_Model_TimeaccountFilter');
  234. }
  235. /**
  236. * Return a single record
  237. *
  238. * @param string $id
  239. * @return array record data
  240. */
  241. public function getTimeaccount($id)
  242. {
  243. return $this->_get($id, $this->_timeaccountController);
  244. }
  245. /**
  246. * creates/updates a record
  247. *
  248. * @param array $recordData
  249. * @return array created/updated record
  250. */
  251. public function saveTimeaccount($recordData)
  252. {
  253. return $this->_save($recordData, $this->_timeaccountController, 'Timeaccount');
  254. }
  255. /**
  256. * deletes existing records
  257. *
  258. * @param array $ids
  259. * @return string
  260. */
  261. public function deleteTimeaccounts($ids)
  262. {
  263. return $this->_delete($ids, $this->_timeaccountController);
  264. }
  265. }