PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/Tasks/Model/Task.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 260 lines | 145 code | 24 blank | 91 comment | 14 complexity | 88f705d5d05d7a4cf384755e7480c1d6 MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0
  4. *
  5. * @package Tasks
  6. * @subpackage Model
  7. * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
  8. * @author Cornelius Weiss <c.weiss@metaways.de>
  9. * @copyright Copyright (c) 2007-2012 Metaways Infosystems GmbH (http://www.metaways.de)
  10. */
  11. /**
  12. * Task-Record Class
  13. *
  14. * @package Tasks
  15. * @subpackage Model
  16. */
  17. class Tasks_Model_Task extends Tinebase_Record_Abstract
  18. {
  19. const CLASS_PUBLIC = 'PUBLIC';
  20. const CLASS_PRIVATE = 'PRIVATE';
  21. //const CLASS_CONFIDENTIAL = 'CONFIDENTIAL';
  22. /**
  23. * key in $_validators/$_properties array for the filed which
  24. * represents the identifier
  25. *
  26. * @var string
  27. */
  28. protected $_identifier = 'id';
  29. /**
  30. * if foreign Id fields should be resolved on search and get from json
  31. * should have this format:
  32. * array('Calendar_Model_Contact' => 'contact_id', ...)
  33. * or for more fields:
  34. * array('Calendar_Model_Contact' => array('contact_id', 'customer_id), ...)
  35. * (e.g. resolves contact_id with the corresponding Model)
  36. *
  37. * @var array
  38. */
  39. protected static $_resolveForeignIdFields = array(
  40. 'Tinebase_Model_User' => array('created_by', 'last_modified_by', 'organizer'),
  41. 'recursive' => array('attachments' => 'Tinebase_Model_Tree_Node'),
  42. );
  43. /**
  44. * application the record belongs to
  45. *
  46. * @var string
  47. */
  48. protected $_application = 'Tasks';
  49. /**
  50. * validators
  51. *
  52. * @var array
  53. */
  54. protected $_validators = array(
  55. // tine record fields
  56. 'container_id' => array(Zend_Filter_Input::ALLOW_EMPTY => true, 'Int' ),
  57. 'created_by' => array(Zend_Filter_Input::ALLOW_EMPTY => true, ),
  58. 'creation_time' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  59. 'last_modified_by' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  60. 'last_modified_time' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  61. 'is_deleted' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  62. 'deleted_time' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  63. 'deleted_by' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  64. 'seq' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  65. // task only fields
  66. //'id' => array(Zend_Filter_Input::ALLOW_EMPTY => true, 'Alnum'),
  67. 'id' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  68. 'percent' => array(Zend_Filter_Input::ALLOW_EMPTY => true, 'default' => 0),
  69. 'completed' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  70. 'due' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  71. // ical common fields
  72. 'class' => array(
  73. Zend_Filter_Input::ALLOW_EMPTY => true,
  74. array('InArray', array(self::CLASS_PUBLIC, self::CLASS_PRIVATE, /*self::CLASS_CONFIDENTIAL*/)),
  75. ),
  76. 'description' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  77. 'geo' => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => NULL),
  78. 'location' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  79. 'organizer' => array(Zend_Filter_Input::ALLOW_EMPTY => true, ),
  80. 'originator_tz' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  81. 'priority' => array(Zend_Filter_Input::ALLOW_EMPTY => true, 'default' => 1),
  82. 'status' => array(Zend_Filter_Input::ALLOW_EMPTY => false ),
  83. 'send' => array(Zend_Filter_Input::ALLOW_EMPTY => true, 'default' => 0),
  84. 'summary' => array(Zend_Filter_Input::PRESENCE => 'required' ),
  85. 'url' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  86. 'start_time' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  87. 'uid' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  88. 'etag' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  89. // ical common fields with multiple appearance
  90. 'attach' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  91. 'attendee' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  92. 'tags' => array(Zend_Filter_Input::ALLOW_EMPTY => true ), //originally categories
  93. 'comment' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  94. 'contact' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  95. 'related' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  96. 'resources' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  97. 'rstatus' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  98. // scheduleable interface fields
  99. 'dtstart' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  100. 'duration' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  101. 'recurid' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  102. // scheduleable interface fields with multiple appearance
  103. 'exdate' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  104. 'exrule' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  105. 'rdate' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  106. 'rrule' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  107. // tine 2.0 notes, alarms and relations
  108. 'notes' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  109. 'alarms' => array(Zend_Filter_Input::ALLOW_EMPTY => true ), // RecordSet of Tinebase_Model_Alarm
  110. 'relations' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  111. 'attachments' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  112. 'feedback' => array(Zend_Filter_Input::ALLOW_EMPTY => true ),
  113. );
  114. /**
  115. * datetime fields
  116. *
  117. * @var array
  118. */
  119. protected $_datetimeFields = array(
  120. 'creation_time',
  121. 'last_modified_time',
  122. 'deleted_time',
  123. 'completed',
  124. 'start_time',
  125. 'dtstart',
  126. 'due',
  127. 'exdate',
  128. 'rdate'
  129. );
  130. /**
  131. * the constructor
  132. * it is needed because we have more validation fields in Tasks
  133. *
  134. * @param mixed $_data
  135. * @param bool $bypassFilters sets {@see this->bypassFilters}
  136. * @param bool $convertDates sets {@see $this->convertDates}
  137. */
  138. public function __construct($_data = NULL, $_bypassFilters = false, $_convertDates = true)
  139. {
  140. $this->_filters['organizer'] = new Zend_Filter_Empty(NULL);
  141. parent::__construct($_data, $_bypassFilters, $_convertDates);
  142. }
  143. /**
  144. * sets the record related properties from user generated input.
  145. *
  146. * @param array $_data
  147. * @return void
  148. */
  149. public function setFromArray(array $_data)
  150. {
  151. //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($_data, true));
  152. if (empty($_data['geo'])) {
  153. $_data['geo'] = NULL;
  154. }
  155. if (empty($_data['class'])) {
  156. $_data['class'] = self::CLASS_PUBLIC;
  157. }
  158. if (isset($_data['organizer']) && is_array($_data['organizer'])) {
  159. $_data['organizer'] = $_data['organizer']['accountId'];
  160. }
  161. if (isset($_data['alarms']) && is_array($_data['alarms'])) {
  162. $_data['alarms'] = new Tinebase_Record_RecordSet('Tinebase_Model_Alarm', $_data['alarms'], TRUE);
  163. }
  164. //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($_data, true));
  165. parent::setFromArray($_data);
  166. }
  167. /**
  168. * create notification message for task alarm
  169. *
  170. * @return string
  171. *
  172. * @todo should we get the locale pref for each single user here instead of the default?
  173. * @todo move lead stuff to Crm(_Model_Lead)?
  174. * @todo add getSummary to Addressbook_Model_Contact for linked contacts?
  175. */
  176. public function getNotificationMessage()
  177. {
  178. // get locale from prefs
  179. $localePref = Tinebase_Core::getPreference()->getValue(Tinebase_Preference::LOCALE);
  180. $locale = Tinebase_Translation::getLocale($localePref);
  181. $translate = Tinebase_Translation::getTranslation($this->_application, $locale);
  182. // get date strings
  183. $timezone = ($this->originator_tz) ? $this->originator_tz : Tinebase_Core::getUserTimezone();
  184. if($this->due){
  185. $dueDateString = Tinebase_Translation::dateToStringInTzAndLocaleFormat($this->due, $timezone, $locale);
  186. }else {
  187. $dueDateString = "";
  188. }
  189. // resolve values
  190. Tinebase_User::getInstance()->resolveUsers($this, 'organizer', true);
  191. $status = Tasks_Config::getInstance()->get(Tasks_Config::TASK_STATUS)->records->getById($this->status);
  192. $organizerName = ($this->organizer) ? $this->organizer->accountDisplayName : '';
  193. //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($this->toArray(), TRUE));
  194. $text = $this->summary . "\n\n"
  195. . $translate->_('Due') . ': ' . $dueDateString . "\n"
  196. . $translate->_('Organizer') . ': ' . $organizerName . "\n"
  197. . $translate->_('Description') . ': ' . $this->description . "\n"
  198. . $translate->_('Priority') . ': ' . $this->priority . "\n"
  199. . $translate->_('Status') . ': ' . $translate->_($status['value']) . "\n"
  200. . $translate->_('Percent') . ': ' . $this->percent . "%\n\n";
  201. // add relations (get with ignore acl)
  202. $relations = Tinebase_Relations::getInstance()->getRelations(get_class($this), 'Sql', $this->getId(), NULL, array('TASK'), TRUE);
  203. foreach ($relations as $relation) {
  204. if ($relation->related_model == 'Crm_Model_Lead') {
  205. $lead = $relation->related_record;
  206. $text .= $translate->_('Lead') . ': ' . $lead->lead_name . "\n";
  207. $leadRelations = Tinebase_Relations::getInstance()->getRelations(get_class($lead), 'Sql', $lead->getId());
  208. foreach ($leadRelations as $leadRelation) {
  209. if ($leadRelation->related_model == 'Addressbook_Model_Contact') {
  210. $contact = $leadRelation->related_record;
  211. $text .= $leadRelation->type . ': ' . $contact->n_fn . ' (' . $contact->org_name . ')' . "\n"
  212. . ((! empty($contact->tel_work)) ? "\t" . $translate->_('Telephone') . ': ' . $contact->tel_work . "\n" : '')
  213. . ((! empty($contact->email)) ? "\t" . $translate->_('Email') . ': ' . $contact->email . "\n" : '');
  214. }
  215. }
  216. }
  217. }
  218. //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . $text);
  219. return $text;
  220. }
  221. /**
  222. * sets and returns the addressbook entry of the organizer
  223. *
  224. * @return Addressbook_Model_Contact
  225. */
  226. public function resolveOrganizer()
  227. {
  228. Tinebase_User::getInstance()->resolveUsers($this, 'organizer', true);
  229. if (! empty($this->organizer) && $this->organizer instanceof Tinebase_Model_User) {
  230. $contacts = Addressbook_Controller_Contact::getInstance()->getMultiple($this->organizer->contact_id, TRUE);
  231. if ($contacts) {
  232. return $contacts->getFirstRecord();
  233. }
  234. }
  235. }
  236. }