PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / trunk
Fluent Support – Helpdesk & Customer Support Ticket System vtrunk
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Models / MailBox.php

MailBox.php in Fluent Support – Helpdesk & Customer Support Ticket System trunk, at app/Models/MailBox.php

234 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\App\Models;
4
5 use Exception;
6 use FluentSupport\App\Modules\PermissionManager;
7 use FluentSupport\App\Services\EmailNotification\Settings;
8 use FluentSupport\App\Services\Helper;
9 use FluentSupport\Framework\Support\Arr;
10
11 class MailBox extends Model
12 {
13 protected $table = 'fs_mail_boxes';
14
15 protected $appends = ['settings'];
16
17 /**
18 * The attributes that are mass assignable.
19 *
20 * @var array
21 */
22 protected $fillable = [
23 'name',
24 'slug',
25 'email',
26 'box_type',
27 'mapped_email',
28 'email_footer',
29 'settings',
30 'avatar',
31 'created_by',
32 'is_default'
33 ];
34
35 public static function boot()
36 {
37 parent::boot();
38
39 static::creating(function ($model) {
40 $model->slug = static::slugify($model->name);
41 $model->settings = $model->settings ?: [
42 'admin_email_address' => ''
43 ];
44 });
45 }
46
47 public function setSettingsAttribute($settings)
48 {
49 $this->attributes['settings'] = \maybe_serialize($settings);
50 }
51
52 public function getSettingsAttribute($value)
53 {
54 if (array_key_exists('settings', $this->attributes)) {
55 return Helper::safeUnserialize($this->attributes['settings']);
56 }
57
58 return [];
59 }
60
61 /**
62 * Returns the mailboxes the current agent is allowed to know about.
63 *
64 * Restricted inboxes are filtered out, so the `settings` blob only ever describes
65 * an inbox the agent already works in. Note that the restriction list is the only
66 * gate here: an agent with no restrictions receives `settings` — including
67 * `admin_email_address` — for every inbox, regardless of their other permissions.
68 *
69 * @param bool $withEmail Include the inbox address. Only pass true once the caller
70 * has confirmed the current user holds `fst_sensitive_data`.
71 * @return \FluentSupport\Framework\Database\Orm\Collection
72 */
73 public static function getAccessibleBoxes($withEmail = false)
74 {
75 $columns = ['id', 'name', 'settings'];
76
77 if ($withEmail) {
78 $columns[] = 'email';
79 }
80
81 $query = MailBox::select($columns);
82
83 $restrictedBoxes = PermissionManager::getRestrictedMailboxIds();
84
85 if ($restrictedBoxes) {
86 $query->whereNotIn('id', $restrictedBoxes);
87 }
88
89 return $query->get();
90 }
91
92 public static function slugify($title)
93 {
94 $slug = sanitize_title($title, 'business', 'display');
95 if (MailBox::where('slug', $slug)->first()) {
96 $slug .= '-' . time();
97 }
98 return $slug;
99 }
100
101 public function getMailerHeader()
102 {
103 $headers = [];
104 $fromString = $this->email; //$this->name;
105 if($this->name) {
106 $fromString = $this->name.' <'.$fromString.'>';
107 }
108
109 if ($fromString) {
110 $headers[] = 'From: '. $fromString;
111 }
112
113 // Set Reply-To Header
114 $headers[] = 'Reply-To: '. $fromString;
115 $headers[] = 'In-Reply-To: '. $fromString;
116
117 return $headers;
118 }
119
120 public function getMeta($key, $default = '')
121 {
122 $class = __NAMESPACE__ . '\MailBox';
123
124 $meta = Meta::where('object_type', $class)
125 ->where('object_id', $this->id)
126 ->where('key', $key)
127 ->first();
128
129 if($meta) {
130 return Helper::safeUnserialize($meta->value);
131 }
132
133 return $default;
134 }
135
136 public function saveMeta($key, $value)
137 {
138 $class = __NAMESPACE__ . '\MailBox';
139
140 $meta = Meta::where('object_type', $class)
141 ->where('object_id', $this->id)
142 ->where('key', $key)
143 ->first();
144
145 if($meta) {
146 $meta->value = maybe_serialize($value);
147 $meta->save();
148 return true;
149 }
150
151 Meta::insert([
152 'object_type' => $class,
153 'object_id' => $this->id,
154 'key' => $key,
155 'value' => maybe_serialize($value)
156 ]);
157 return true;
158 }
159
160 public function deleteMeta($key)
161 {
162 $class = __NAMESPACE__ . '\MailBox';
163
164 $meta = Meta::where('object_type', $class)
165 ->where('object_id', $this->id)
166 ->where('key', $key)
167 ->delete();
168 }
169
170 public function deleteAllMeta()
171 {
172 $class = __NAMESPACE__ . '\MailBox';
173
174 $meta = Meta::where('object_type', $class)
175 ->where('object_id', $this->id)
176 ->delete();
177 }
178 /**
179 * This `getMailBox` method is used to get a mailbox by id.
180 * @param int $id
181 * @return MailBox
182 */
183 public function getMailBox ($id )
184 {
185 return MailBox::findOrFail($id);
186 }
187
188 /**
189 * This `createMailBox` method is used to create a new MailBox.
190 * @param array $data
191 * @return MailBox
192 */
193 public function createMailBox ( $data )
194 {
195 if ($data['box_type'] == 'email') {
196 $data['settings'] = [
197 'admin_email_address' => ''
198 ];
199 } else {
200 $data['settings'] = [
201 'admin_email_address' => $data['email']
202 ];
203 }
204
205 if (!MailBox::first()) {
206 $data['is_default'] = 'yes';
207 }
208
209 return MailBox::create($data);
210 }
211
212
213 /**
214 * This `updateMailBox` method is used to update a mailbox.
215 * @param array $data
216 * @param int $mailBoxId
217 * @return object $mailbox
218 */
219 public function updateMailBox ($data, $mailBoxId )
220 {
221
222 $mailbox = MailBox::findOrFail($mailBoxId);
223
224 if ($data['box_type'] == 'email' && empty($data['mapped_email'])) {
225 throw new \Exception('Mapped Email Address is required');
226 }
227
228 $mailbox->fill($data);
229 $mailbox->save();
230
231 return $mailbox;
232 }
233 }
234