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

/app/protected/modules/notifications/models/Notification.php

https://bitbucket.org/zurmo/zurmo/
PHP | 246 lines | 170 code | 12 blank | 64 comment | 8 complexity | 039bef859b2d625ba2e2c6c809de360a MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-3.0, LGPL-2.1, BSD-2-Clause
  1. <?php
  2. /*********************************************************************************
  3. * Zurmo is a customer relationship management program developed by
  4. * Zurmo, Inc. Copyright (C) 2015 Zurmo Inc.
  5. *
  6. * Zurmo is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * Zurmo is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
  24. * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
  25. *
  26. * The interactive user interfaces in original and modified versions
  27. * of this program must display Appropriate Legal Notices, as required under
  28. * Section 5 of the GNU Affero General Public License version 3.
  29. *
  30. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  31. * these Appropriate Legal Notices must retain the display of the Zurmo
  32. * logo and Zurmo copyright notice. If the display of the logo is not reasonably
  33. * feasible for technical reasons, the Appropriate Legal Notices must display the words
  34. * "Copyright Zurmo Inc. 2015. All rights reserved".
  35. ********************************************************************************/
  36. /**
  37. * A class for creating notification models.
  38. */
  39. class Notification extends Item implements MashableInboxInterface
  40. {
  41. public static function getMashableInboxRulesType()
  42. {
  43. return 'Notification';
  44. }
  45. public function __toString()
  46. {
  47. if ($this->type == null)
  48. {
  49. return null;
  50. }
  51. $notificationRulesClassName = $this->type . 'NotificationRules';
  52. if (@class_exists($notificationRulesClassName))
  53. {
  54. $rule = NotificationRulesFactory::createNotificationRulesByType($this->type);
  55. return $rule->getDisplayName();
  56. }
  57. else
  58. {
  59. return Zurmo::t('Core', '(Unnamed)');
  60. }
  61. }
  62. /**
  63. * Given a type and a user, find out how many existing notifications exist for that user
  64. * and that type.
  65. * @param string $type
  66. * @param User $user
  67. * @return int
  68. */
  69. public static function getCountByTypeAndUser($type, User $user)
  70. {
  71. $models = self::getByTypeAndUser($type, $user);
  72. return count($models);
  73. }
  74. /**
  75. * @param $type
  76. * @param User $user
  77. * @return Array of models
  78. */
  79. public static function getByTypeAndUser($type, User $user)
  80. {
  81. assert('is_string($type) && $type != ""');
  82. assert('$user->id > 0');
  83. $searchAttributeData = array();
  84. $searchAttributeData['clauses'] = array(
  85. 1 => array(
  86. 'attributeName' => 'type',
  87. 'operatorType' => 'equals',
  88. 'value' => $type,
  89. ),
  90. 2 => array(
  91. 'attributeName' => 'owner',
  92. 'relatedAttributeName' => 'id',
  93. 'operatorType' => 'equals',
  94. 'value' => $user->id,
  95. ),
  96. );
  97. $searchAttributeData['structure'] = '1 and 2';
  98. $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('Notification');
  99. $where = RedBeanModelDataProvider::makeWhere('Notification', $searchAttributeData, $joinTablesAdapter);
  100. $models = self::getSubset($joinTablesAdapter, null, null, $where, null);
  101. return $models;
  102. }
  103. /**
  104. * Get all notifications based on notificationMessage id
  105. * @param $notificationMessageId
  106. * @return Array of models
  107. */
  108. public static function getByNotificationMessageId($notificationMessageId)
  109. {
  110. assert('is_int($notificationMessageId)');
  111. $searchAttributeData = array();
  112. $searchAttributeData['clauses'] = array(
  113. 1 => array(
  114. 'attributeName' => 'notificationMessage',
  115. 'relatedAttributeName' => 'id',
  116. 'operatorType' => 'equals',
  117. 'value' => $notificationMessageId,
  118. ),
  119. );
  120. $searchAttributeData['structure'] = '1';
  121. $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('Notification');
  122. $where = RedBeanModelDataProvider::makeWhere('Notification', $searchAttributeData, $joinTablesAdapter);
  123. $models = self::getSubset($joinTablesAdapter, null, null, $where, null);
  124. return $models;
  125. }
  126. /**
  127. * Delete all notifications and related NotificationMessages by type and user
  128. * @param $type
  129. * @param User $user
  130. */
  131. public static function deleteByTypeAndUser($type, User $user)
  132. {
  133. $notifications = static::getByTypeAndUser($type, $user);
  134. if (!empty($notifications))
  135. {
  136. foreach ($notifications as $notification)
  137. {
  138. static::deleteNotificationAndRelatedNotificationMessage($notification);
  139. }
  140. }
  141. }
  142. /**
  143. * Delete notification and related notificationMessage, if there are no relations from other notifications to
  144. * this notificationMessage
  145. * @param Notification $notification
  146. */
  147. public static function deleteNotificationAndRelatedNotificationMessage(Notification $notification)
  148. {
  149. try
  150. {
  151. if (isset($notification->notificationMessage) && $notification->notificationMessage instanceOf NotificationMessage)
  152. {
  153. $notificationMessageNotifications = Notification::getByNotificationMessageId($notification->notificationMessage->id);
  154. if (count($notificationMessageNotifications) == 1)
  155. {
  156. $notification->notificationMessage->delete();
  157. }
  158. }
  159. }
  160. catch (NotFoundException $e)
  161. {
  162. }
  163. $notification->delete();
  164. }
  165. public static function getCountByUser(User $user)
  166. {
  167. assert('$user->id > 0');
  168. $searchAttributeData = array();
  169. $searchAttributeData['clauses'] = array(
  170. 1 => array(
  171. 'attributeName' => 'owner',
  172. 'relatedAttributeName' => 'id',
  173. 'operatorType' => 'equals',
  174. 'value' => $user->id,
  175. ),
  176. );
  177. $searchAttributeData['structure'] = '1';
  178. $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('Notification');
  179. $where = RedBeanModelDataProvider::makeWhere('Notification', $searchAttributeData, $joinTablesAdapter);
  180. return self::getCount($joinTablesAdapter, $where, null, true);
  181. }
  182. public static function getDefaultMetadata()
  183. {
  184. $metadata = parent::getDefaultMetadata();
  185. $metadata[__CLASS__] = array(
  186. 'members' => array(
  187. 'type',
  188. 'ownerHasReadLatest',
  189. ),
  190. 'relations' => array(
  191. 'notificationMessage' => array(static::HAS_ONE, 'NotificationMessage', static::NOT_OWNED),
  192. 'owner' => array(static::HAS_ONE, 'User', static::NOT_OWNED,
  193. static::LINK_TYPE_SPECIFIC, 'owner'),
  194. ),
  195. 'rules' => array(
  196. array('owner', 'required'),
  197. array('type', 'required'),
  198. array('type', 'type', 'type' => 'string'),
  199. array('type', 'length', 'min' => 1, 'max' => 64),
  200. array('ownerHasReadLatest', 'boolean'),
  201. ),
  202. 'elements' => array(
  203. 'owner' => 'User',
  204. ),
  205. 'defaultSortAttribute' => null,
  206. 'noAudit' => array(
  207. 'owner',
  208. 'type',
  209. 'ownerHasReadLatest',
  210. )
  211. );
  212. return $metadata;
  213. }
  214. public static function isTypeDeletable()
  215. {
  216. return true;
  217. }
  218. public static function getModuleClassName()
  219. {
  220. return 'NotificationsModule';
  221. }
  222. protected static function translatedAttributeLabels($language)
  223. {
  224. return array_merge(parent::translatedAttributeLabels($language),
  225. array(
  226. 'ownerHasReadLatest' => Zurmo::t('NotificationsModule', 'Owner Has Read Latest', array(), null, $language),
  227. 'notificationMessage' => Zurmo::t('Core', 'Notification Message', array(), null, $language),
  228. 'owner' => Zurmo::t('ZurmoModule', 'Owner', array(), null, $language),
  229. 'type' => Zurmo::t('Core', 'Type', array(), null, $language),
  230. )
  231. );
  232. }
  233. }
  234. ?>