22
33namespace PanicHD \PanicHD \Models ;
44
5+ use DB ;
56use Illuminate \Database \Eloquent \Collection ;
67use Illuminate \Database \Eloquent \Model ;
78use PanicHD \PanicHD \Traits \ContentEllipse ;
@@ -11,7 +12,7 @@ class Comment extends Model
1112 use ContentEllipse;
1213
1314 protected $ table = 'panichd_comments ' ;
14-
15+
1516 /**
1617 * All of the relationships to be touched.
1718 *
@@ -25,18 +26,21 @@ class Comment extends Model
2526 public function delete ()
2627 {
2728 $ a_errors = [];
28- foreach ($ this ->attachments ()->get () as $ att ){
29+ // Delete attachments
30+ foreach ($ this ->attachments ()->get () as $ att ){
2931 $ error = $ att ->delete ();
3032 if ($ error ) $ a_errors [] = $ error ;
3133 }
32-
34+
35+ // Delete notifications
36+ \DB ::table ('panichd_comment_email ' )->where ('comment_id ' , $ this ->id )->delete ();
37+
3338 $ error = $ a_errors ? implode ('. ' , $ a_errors ) : null ;
3439 if ($ error != "" ) return $ error ;
35-
40+
3641 parent ::delete ();
3742 }
38-
39-
43+
4044 /**
4145 * Get related ticket.
4246 *
@@ -57,7 +61,7 @@ public function user()
5761 {
5862 return $ this ->belongsTo ('\PanicHDMember ' , 'user_id ' );
5963 }
60-
64+
6165 /**
6266 * Get Comment owner as PanicHDMember model
6367 *
@@ -72,24 +76,34 @@ public function attachments()
7276 {
7377 return $ this ->hasMany (Attachment::class, 'comment_id ' )->orderByRaw ('CASE when mimetype LIKE "image/%" then 1 else 2 end ' );
7478 }
75-
79+
7680 /**
7781 * Filter visible comments depending on member level
7882 */
7983 public function scopeForLevel ($ query , $ level )
8084 {
8185 // User level
8286 if ($ level < 2 ) return $ query ->whereIN ('type ' , ['reply ' , 'complete ' , 'completetx ' , 'reopen ' ]);
83-
87+
8488 // For agent or admin
8589 return $ query ;
8690 }
87-
91+
8892 /**
8993 * Filter comment entries that are countable as real comments (complete and reopen comments are excluded)
9094 */
9195 public function scopeCountable ($ query )
9296 {
9397 return $ query ->whereIN ('type ' , ['reply ' , 'note ' , 'completetx ' ]);
9498 }
99+
100+ /**
101+ * Get emails | members whom notifications have been sent.
102+ *
103+ * Return @collection
104+ */
105+ public function getNotifications ()
106+ {
107+ return \DB ::table ('panichd_comment_email ' )->where ('comment_id ' , $ this ->id )->orderBy ('name ' )->get ();
108+ }
95109}
0 commit comments