|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gewaer\Api\Controllers; |
|
6
|
|
|
|
|
7
|
|
|
use Gewaer\Models\EmailTemplates; |
|
8
|
|
|
use Gewaer\Exception\NotFoundHttpException; |
|
9
|
|
|
use Gewaer\Exception\UnprocessableEntityHttpException; |
|
10
|
|
|
use Phalcon\Security\Random; |
|
11
|
|
|
use Phalcon\Http\Response; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class LanguagesController |
|
15
|
|
|
* |
|
16
|
|
|
* @package Gewaer\Api\Controllers |
|
17
|
|
|
* |
|
18
|
|
|
* @property Users $userData |
|
19
|
|
|
* @property Request $request |
|
20
|
|
|
* @property Config $config |
|
21
|
|
|
* @property \Baka\Mail\Message $mail |
|
22
|
|
|
* @property Apps $app |
|
23
|
|
|
* |
|
24
|
|
|
*/ |
|
25
|
|
|
class EmailTemplatesController extends BaseController |
|
26
|
|
|
{ |
|
27
|
|
|
/* |
|
28
|
|
|
* fields we accept to create |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $createFields = ['users_id', 'companies_id', 'apps_id', 'name', 'template']; |
|
33
|
|
|
|
|
34
|
|
|
/* |
|
35
|
|
|
* fields we accept to create |
|
36
|
|
|
* |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $updateFields = ['users_id', 'companies_id', 'apps_id', 'name', 'template']; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* set objects |
|
43
|
|
|
* |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function onConstruct() |
|
47
|
|
|
{ |
|
48
|
2 |
|
$this->model = new EmailTemplates(); |
|
49
|
2 |
|
$this->additionalSearchFields = [ |
|
50
|
2 |
|
['is_deleted', ':', '0'], |
|
51
|
2 |
|
['companies_id', ':', '0|' . $this->userData->currentCompanyId()], |
|
52
|
|
|
]; |
|
53
|
2 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Add a new by copying a specific email template based on |
|
57
|
|
|
* |
|
58
|
|
|
* @method POST |
|
59
|
|
|
* @url /v1/data |
|
60
|
|
|
* @param integer $id |
|
61
|
|
|
* @return \Phalcon\Http\Response |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function copy(int $id): Response |
|
64
|
|
|
{ |
|
65
|
1 |
|
$request = $this->request->getPost(); |
|
66
|
|
|
|
|
67
|
1 |
|
if (empty($request)) { |
|
68
|
|
|
$request = $this->request->getJsonRawBody(true); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
//Find email template based on the basic parameters |
|
72
|
1 |
|
$existingEmailTemplate = $this->model::findFirst([ |
|
73
|
1 |
|
'conditions' => 'id = ?0 and companies_id = ?1 and apps_id = ?2 and is_deleted = 0', |
|
74
|
1 |
|
'bind' => [$id, $this->userData->default_company, $this->app->getId()] |
|
75
|
|
|
]); |
|
76
|
|
|
|
|
77
|
1 |
|
if (!is_object($existingEmailTemplate)) { |
|
78
|
|
|
throw new NotFoundHttpException('Email Template not found'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
$random = new Random(); |
|
82
|
1 |
|
$randomInstance = $random->base58(); |
|
83
|
|
|
|
|
84
|
1 |
|
$request['users_id'] = $existingEmailTemplate->users_id; |
|
85
|
1 |
|
$request['companies_id'] = $existingEmailTemplate->companies_id; |
|
86
|
1 |
|
$request['apps_id'] = $existingEmailTemplate->apps_id; |
|
87
|
1 |
|
$request['name'] = $existingEmailTemplate->name . '-' . $randomInstance; |
|
88
|
1 |
|
$request['template'] = $existingEmailTemplate->template; |
|
89
|
|
|
|
|
90
|
|
|
//try to save all the fields we allow |
|
91
|
1 |
|
if ($this->model->save($request, $this->createFields)) { |
|
92
|
1 |
|
return $this->response($this->model->toArray()); |
|
93
|
|
|
} else { |
|
94
|
|
|
//if not thorw exception |
|
95
|
|
|
throw new UnprocessableEntityHttpException((string) current($this->model->getMessages())); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|