|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Canvas\Api\Controllers; |
|
6
|
|
|
|
|
7
|
|
|
use Canvas\Models\EmailTemplates; |
|
8
|
|
|
use Canvas\Models\Users; |
|
9
|
|
|
use Canvas\Http\Exception\NotFoundException; |
|
10
|
|
|
use Phalcon\Security\Random; |
|
|
|
|
|
|
11
|
|
|
use Phalcon\Http\Response; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class LanguagesController. |
|
17
|
|
|
* |
|
18
|
|
|
* @package Canvas\Api\Controllers |
|
19
|
|
|
* |
|
20
|
|
|
* @property Users $userData |
|
21
|
|
|
* @property Request $request |
|
22
|
|
|
* @property Config $config |
|
23
|
|
|
* @property \Baka\Mail\Message $mail |
|
24
|
|
|
* @property Apps $app |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
class EmailTemplatesController extends BaseController |
|
28
|
|
|
{ |
|
29
|
|
|
/* |
|
30
|
|
|
* fields we accept to create |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $createFields = [ |
|
35
|
|
|
'users_id', |
|
36
|
|
|
'companies_id', |
|
37
|
|
|
'apps_id', |
|
38
|
|
|
'name', |
|
39
|
|
|
'template' |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
/* |
|
43
|
|
|
* fields we accept to create |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $updateFields = [ |
|
48
|
|
|
'users_id', |
|
49
|
|
|
'companies_id', |
|
50
|
|
|
'apps_id', |
|
51
|
|
|
'name', |
|
52
|
|
|
'template' |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* set objects. |
|
57
|
|
|
* |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
|
public function onConstruct() |
|
61
|
|
|
{ |
|
62
|
|
|
$this->model = new EmailTemplates(); |
|
|
|
|
|
|
63
|
|
|
$this->additionalSearchFields = [ |
|
|
|
|
|
|
64
|
|
|
['is_deleted', ':', '0'], |
|
65
|
|
|
['companies_id', ':', '0|' . $this->userData->currentCompanyId()], |
|
66
|
|
|
]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Add a new by copying a specific email template based on. |
|
71
|
|
|
* |
|
72
|
|
|
* @method POST |
|
73
|
|
|
* @url /v1/data |
|
74
|
|
|
* @param integer $id |
|
75
|
|
|
* @return \Phalcon\Http\Response |
|
76
|
|
|
*/ |
|
77
|
|
|
public function copy(int $id): Response |
|
78
|
|
|
{ |
|
79
|
|
|
$request = $this->request->getPostData(); |
|
80
|
|
|
|
|
81
|
|
|
//Find email template based on the basic parameters |
|
82
|
|
|
$existingEmailTemplate = $this->model::findFirst([ |
|
83
|
|
|
'conditions' => 'id = ?0 and companies_id in (?1,?2) and apps_id in (?3,?4) and is_deleted = 0', |
|
84
|
|
|
'bind' => [$id, $this->userData->currentCompanyId(), 0, $this->app->getId(), 0] |
|
85
|
|
|
]); |
|
86
|
|
|
|
|
87
|
|
|
if (!is_object($existingEmailTemplate)) { |
|
88
|
|
|
throw new NotFoundException('Email Template not found'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$random = new Random(); |
|
92
|
|
|
$randomInstance = $random->base58(); |
|
93
|
|
|
|
|
94
|
|
|
$request['users_id'] = $existingEmailTemplate->users_id; |
|
95
|
|
|
$request['companies_id'] = $this->userData->currentCompanyId(); |
|
96
|
|
|
$request['apps_id'] = $this->app->getId(); |
|
97
|
|
|
$request['name'] = $existingEmailTemplate->name . '-' . $randomInstance; |
|
98
|
|
|
$request['template'] = $existingEmailTemplate->template; |
|
99
|
|
|
|
|
100
|
|
|
$this->model->saveOrFail($request, $this->createFields); |
|
101
|
|
|
|
|
102
|
|
|
return $this->response($this->model->toArray()); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Send test email to specific recipient. |
|
107
|
|
|
* @param string $email |
|
108
|
|
|
* @return Response |
|
109
|
|
|
*/ |
|
110
|
|
|
public function sendTestEmail(): Response |
|
111
|
|
|
{ |
|
112
|
|
|
$request = $this->request->getPost(); |
|
113
|
|
|
|
|
114
|
|
|
if (empty($request)) { |
|
115
|
|
|
$request = $this->request->getJsonRawBody(true); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$emailRecipients = explode(',', $request['emails']); |
|
119
|
|
|
|
|
120
|
|
|
foreach ($emailRecipients as $emailRecipient) { |
|
121
|
|
|
$userExists = Users::findFirst([ |
|
122
|
|
|
'conditions' => 'email = ?0 and is_deleted = 0', |
|
123
|
|
|
'bind' => [$emailRecipient] |
|
124
|
|
|
]); |
|
125
|
|
|
|
|
126
|
|
|
if (!is_object($userExists)) { |
|
127
|
|
|
throw new NotFoundException('Email recipient not found'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
$subject = _('Test Email Template'); |
|
131
|
|
|
$this->mail |
|
132
|
|
|
->to((string)$userExists->email) |
|
133
|
|
|
->subject($subject) |
|
134
|
|
|
->content($request['template']) |
|
135
|
|
|
->sendNow(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $this->response('Test email sent'); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths