Guest User

Untitled

a guest
May 27th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. <?php
  2. /**
  3. * Message model is used for internal messaging in the system.
  4. *
  5. * @package citylance
  6. */
  7. class Model_Message extends Model_Abstract {
  8. public static function initialize(Jelly_Meta $meta)
  9. {
  10. $meta->sorting(array('received_at' => 'DESC'))
  11. ->fields(array(
  12. 'id' => new Field_Primary,
  13. 'subject' => new Field_String(array(
  14. 'rules' => array(
  15. 'not_empty' => array(TRUE),
  16. ),
  17. )),
  18. 'content' => new Field_Text(array(
  19. 'rules' => array(
  20. 'not_empty' => array(TRUE),
  21. ),
  22. 'filters' => array(
  23. 'text::auto_p' => array(TRUE),
  24. ),
  25. )),
  26. 'sender' => new Field_BelongsTo(array(
  27. 'foreign' => 'user',
  28. 'column' => 'sender_id',
  29. )),
  30. 'recipient' => new Field_BelongsTo(array(
  31. 'foreign' => 'user',
  32. 'column' => 'recipient_id',
  33. 'rules' => array(
  34. 'not_empty' => array(TRUE),
  35. ),
  36. 'callbacks' => array(
  37. 'contacts' => array('Model_User', '_check_contact')
  38. ),
  39. )),
  40. 'unread' => new Field_Boolean(array(
  41. 'default' => TRUE,
  42. )),
  43. 'inbox' => new Field_Boolean(array(
  44. 'default' => TRUE,
  45. )),
  46. 'outbox' => new Field_Boolean(array(
  47. 'default' => TRUE,
  48. )),
  49. 'draft' => new Field_Boolean(array(
  50. 'default' => TRUE,
  51. )),
  52. 'created_at' => new Field_Timestamp(array(
  53. 'auto_now_create' => TRUE,
  54. 'format' => 'Y-m-d H:i:s',
  55. )),
  56. 'updated_at' => new Field_Timestamp(array(
  57. 'auto_now_update' => TRUE,
  58. 'format' => 'Y-m-d H:i:s',
  59. )),
  60. 'received_at' => new Field_Timestamp(array(
  61. 'format' => 'Y-m-d H:i:s',
  62. )),
  63. ));
  64. }
  65.  
  66. /**
  67. * Creates the message based on input array and the author
  68. *
  69. * @param array $input
  70. * @param object $author
  71. * @return object $this
  72. */
  73. public function create(array $input, Model_User $author)
  74. {
  75. $this->set(arr::extract($input, array('subject', 'content', 'recipient')))
  76. ->set(array(
  77. 'sender' => $author,
  78. 'inbox' => FALSE,
  79. 'outbox' => FALSE,
  80. 'draft' => TRUE,
  81. ))
  82. ->save();
  83. return $this;
  84. }
  85.  
  86. /**
  87. * Truncates the message for the user
  88. *
  89. * @param object $user
  90. * @return object $this
  91. */
  92. public function truncate(Model_User $user)
  93. {
  94. $status = array('draft' => FALSE);
  95. if ($user->id == $this->recipient->id) {
  96. $status['inbox'] = FALSE;
  97. }
  98.  
  99. if ($user->id == $this->sender->id) {
  100. $status['outbox'] = FALSE;
  101. }
  102. $this->set($status)->save();
  103. }
  104.  
  105. /**
  106. * Marks message as sent and places it to inbox and outbox
  107. *
  108. * @throws Kohana_Exception
  109. */
  110. public function send()
  111. {
  112. $status = array(
  113. 'unread' => TRUE,
  114. 'inbox' => TRUE,
  115. 'outbox' => TRUE,
  116. 'draft' => FALSE,
  117. );
  118. if ($this->recipient->has('blacklist', $this->sender)) {
  119. $status['inbox'] = FALSE;
  120. }
  121. $this->set($status)->save();
  122. }
  123.  
  124. /**
  125. * Marks message as read
  126. *
  127. * @return object $this
  128. */
  129. public function mark_read()
  130. {
  131. $this->set('unread', FALSE)->save();
  132. return $this;
  133. }
  134.  
  135. /**
  136. * Checks if user can view the message
  137. *
  138. * @param object $user
  139. * @return boolean
  140. */
  141. public function can_view(Model_User $user)
  142. {
  143. if (
  144. ($this->recipient->id == $user->id AND $this->inbox)
  145. OR ($this->sender->id == $user->id
  146. AND ($this->outbox OR $this->draft))
  147. ) {
  148. return TRUE;
  149. }
  150. return FALSE;
  151. }
  152.  
  153. /**
  154. * Checks if the message can be edited by the user.
  155. *
  156. * @param object $user
  157. * @return boolean
  158. */
  159. public function cat_edit(Model_User $user)
  160. {
  161. if ($this->sender->id == $user->id AND $this->draft) {
  162. return TRUE;
  163. }
  164. return FALSE;
  165. }
  166. }
Add Comment
Please, Sign In to add comment