Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Message model is used for internal messaging in the system.
- *
- * @package citylance
- */
- class Model_Message extends Model_Abstract {
- public static function initialize(Jelly_Meta $meta)
- {
- $meta->sorting(array('received_at' => 'DESC'))
- ->fields(array(
- 'id' => new Field_Primary,
- 'subject' => new Field_String(array(
- 'rules' => array(
- 'not_empty' => array(TRUE),
- ),
- )),
- 'content' => new Field_Text(array(
- 'rules' => array(
- 'not_empty' => array(TRUE),
- ),
- 'filters' => array(
- 'text::auto_p' => array(TRUE),
- ),
- )),
- 'sender' => new Field_BelongsTo(array(
- 'foreign' => 'user',
- 'column' => 'sender_id',
- )),
- 'recipient' => new Field_BelongsTo(array(
- 'foreign' => 'user',
- 'column' => 'recipient_id',
- 'rules' => array(
- 'not_empty' => array(TRUE),
- ),
- 'callbacks' => array(
- 'contacts' => array('Model_User', '_check_contact')
- ),
- )),
- 'unread' => new Field_Boolean(array(
- 'default' => TRUE,
- )),
- 'inbox' => new Field_Boolean(array(
- 'default' => TRUE,
- )),
- 'outbox' => new Field_Boolean(array(
- 'default' => TRUE,
- )),
- 'draft' => new Field_Boolean(array(
- 'default' => TRUE,
- )),
- 'created_at' => new Field_Timestamp(array(
- 'auto_now_create' => TRUE,
- 'format' => 'Y-m-d H:i:s',
- )),
- 'updated_at' => new Field_Timestamp(array(
- 'auto_now_update' => TRUE,
- 'format' => 'Y-m-d H:i:s',
- )),
- 'received_at' => new Field_Timestamp(array(
- 'format' => 'Y-m-d H:i:s',
- )),
- ));
- }
- /**
- * Creates the message based on input array and the author
- *
- * @param array $input
- * @param object $author
- * @return object $this
- */
- public function create(array $input, Model_User $author)
- {
- $this->set(arr::extract($input, array('subject', 'content', 'recipient')))
- ->set(array(
- 'sender' => $author,
- 'inbox' => FALSE,
- 'outbox' => FALSE,
- 'draft' => TRUE,
- ))
- ->save();
- return $this;
- }
- /**
- * Truncates the message for the user
- *
- * @param object $user
- * @return object $this
- */
- public function truncate(Model_User $user)
- {
- $status = array('draft' => FALSE);
- if ($user->id == $this->recipient->id) {
- $status['inbox'] = FALSE;
- }
- if ($user->id == $this->sender->id) {
- $status['outbox'] = FALSE;
- }
- $this->set($status)->save();
- }
- /**
- * Marks message as sent and places it to inbox and outbox
- *
- * @throws Kohana_Exception
- */
- public function send()
- {
- $status = array(
- 'unread' => TRUE,
- 'inbox' => TRUE,
- 'outbox' => TRUE,
- 'draft' => FALSE,
- );
- if ($this->recipient->has('blacklist', $this->sender)) {
- $status['inbox'] = FALSE;
- }
- $this->set($status)->save();
- }
- /**
- * Marks message as read
- *
- * @return object $this
- */
- public function mark_read()
- {
- $this->set('unread', FALSE)->save();
- return $this;
- }
- /**
- * Checks if user can view the message
- *
- * @param object $user
- * @return boolean
- */
- public function can_view(Model_User $user)
- {
- if (
- ($this->recipient->id == $user->id AND $this->inbox)
- OR ($this->sender->id == $user->id
- AND ($this->outbox OR $this->draft))
- ) {
- return TRUE;
- }
- return FALSE;
- }
- /**
- * Checks if the message can be edited by the user.
- *
- * @param object $user
- * @return boolean
- */
- public function cat_edit(Model_User $user)
- {
- if ($this->sender->id == $user->id AND $this->draft) {
- return TRUE;
- }
- return FALSE;
- }
- }
Add Comment
Please, Sign In to add comment