-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.php
More file actions
134 lines (117 loc) · 3.96 KB
/
Message.php
File metadata and controls
134 lines (117 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace Syntax\Core;
use Auth;
class Message extends \BaseModel
{
/********************************************************************
* Declarations
*******************************************************************/
protected $table = 'messages';
protected $primaryKey = 'uniqueId';
public $incrementing = false;
/**
* Soft Delete users instead of completely removing them
*
* @var bool $softDelete Whether to delete or soft delete
*/
protected $softDelete = true;
const STANDARD = 1;
const EXPERIENCE = 2;
const ACTION_APPROVAL = 4;
const CHARACTER_APPROVAL = 5;
const MODERATION = 3;
const PERMISSION = 6;
/********************************************************************
* Aware validation rules
*******************************************************************/
public static $rules = array(
'sender_id' => 'required|exists:users,uniqueId',
'receiver_id' => 'required|exists:users,uniqueId',
'title' => 'required|max:200',
'content' => 'required',
'message_type_id' => 'required|exists:message_types,id',
'child_id' => 'exists:messages,uniqueId',
'parent_id' => 'exists:messages,uniqueId',
);
/********************************************************************
* Relationships
*******************************************************************/
public static $relationsData = array(
'sender' => array('belongsTo', 'User', 'foreignKey' => 'sender_id'),
'receiver' => array('belongsTo', 'User', 'foreignKey' => 'receiver_id'),
'child' => array('belongsTo', 'Message', 'foreignKey' => 'child_id'),
'parent' => array('belongsTo', 'Message', 'foreignKey' => 'parent_id'),
'type' => array('belongsTo', 'Message_Type', 'foreignKey' => 'message_type_id'),
'deletes' => array('hasMany', 'Message_User_Delete', 'foreignKey' => 'message_id'),
'reads' => array('hasMany', 'Message_User_Read', 'foreignKey' => 'message_id'),
'folders' => array('hasMany', 'Message_Folder_Message', 'foreignKey' => 'message_id'),
);
/********************************************************************
* Model events
*******************************************************************/
/********************************************************************
* Getter and Setter methods
*******************************************************************/
/**
* See if this message has been deleted by the user
*
* @return boolean
*/
public function getDeletedAttribute()
{
$deleted = Message_User_Delete::where('message_id', $this->id)->where('user_id', Auth::user()->id)->first();
return ($deleted == null ? 0 : 1);
}
public function getReadIconAttribute()
{
$read = $this->userRead(Auth::user()->id);
if ($read == 1) {
return '<i class="fa fa-circle text-info"></i>';
} else {
return '<i class="fa fa-circle-o text-info"></i>';
}
}
public function getReadAttribute()
{
return $this->userRead(Auth::user()->id);
}
/********************************************************************
* Extra Methods
*******************************************************************/
public function inFolder($userId)
{
$folders = $this->folders();
if (count($folders) > 0) {
$folders = $folders->filter(function ($folder) {
if ($folder->user_id == $userId) {
return true;
}
});
if (count($folders) > 0) {
return true;
}
} else {
return false;
}
}
/**
* See if this message has been read by the user
*
* @return boolean
*/
public function userRead($userId)
{
$read = Message_User_Read::where('message_id', $this->id)->where('user_id', $userId)->first();
return ($read == null ? 0 : 1);
}
/**
* See if this message has been deleted by the user
*
* @return boolean
*/
public function userDeleted($userId)
{
$delete = Message_User_Delete::where('message_id', $this->id)->where('user_id', $userId)->first();
return ($delete == null ? 0 : 1);
}
}