This repository has been archived on 2024-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
Sequenzia/app/models/Dmail.php

88 lines
2.2 KiB
PHP
Raw Normal View History

2013-10-27 01:06:58 +02:00
<?php
class Dmail extends Rails\ActiveRecord\Base
{
public function to_name()
{
if (!$this->to_id)
return;
return $this->to->name;
}
public function from_name()
{
return $this->from->name;
}
public function mark_as_read()
{
$this->updateAttribute('has_seen', true);
if (!Dmail::where("to_id = ? AND has_seen = false", current_user()->id)->exists())
current_user()->updateAttribute('has_mail', false);
}
# iTODO:
public function send_dmail()
{
// if ($this->to->receive_dmails && is_int(strpos($this->to->email, '@')))
// UserMailer::deliver_dmail($this->to, $this->from, $this->title(), $this->body);
}
public function update_recipient()
{
$this->to->updateAttribute('has_mail', true);
}
protected function validations()
{
return array(
'to_id' => array('presence' => true),
'from_id' => array('presence' => true),
'title' => array('format' => ['with' => '/\S/']),
'body' => array('format' => ['with' => '/\S/'])
);
}
protected function associations()
{
return array(
'belongs_to' => array(
'to' => array('class_name' => 'User', 'foreign_key' => 'to_id'),
'from' => array('class_name' => 'User', 'foreign_key' => 'from_id')
)
);
}
protected function callbacks()
{
return [
'after_create' => ['update_recipient', 'send_dmail']
];
}
public function title()
{
$title = $this->getAttribute('title');
if ($this->parent_id && strpos($title, 'Re: ') !== 0) {
return "Re: " . $title;
} else {
return $title;
}
}
2013-11-08 17:37:29 +01:00
public function setToName($name)
2013-10-27 01:06:58 +02:00
{
if (!$user = User::where(['name' => $name])->first())
return;
$this->to_id = $user->id;
}
2013-11-08 17:37:29 +01:00
public function setFromName($name)
2013-10-27 01:06:58 +02:00
{
if (!$user = User::where(['name' => $name])->first())
return;
$this->from_id = $user->id;
}
2013-11-08 17:37:29 +01:00
}