1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use CMSFactory\assetManager; |
4
|
|
|
|
5
|
|
|
if (!defined('BASEPATH')) { |
6
|
|
|
exit('No direct script access allowed'); |
7
|
|
|
} |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Image CMS |
11
|
|
|
* |
12
|
|
|
* Mailer Admin |
13
|
|
|
*/ |
14
|
|
|
class Admin extends BaseAdminController |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
public function __construct() { |
18
|
|
|
parent::__construct(); |
19
|
|
|
|
20
|
|
|
$this->load->library('DX_Auth'); |
21
|
|
|
$lang = new MY_Lang(); |
22
|
|
|
$lang->load('mailer'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @TODO: Переписать на ассет менеджер |
27
|
|
|
*/ |
28
|
|
|
public function index() { |
29
|
|
|
$roles = $this->db->get('shop_rbac_roles')->result_array(); |
30
|
|
|
|
31
|
|
|
/** @var CI_DB_result $query */ |
32
|
|
|
$query = $this->db->get('mail'); |
33
|
|
|
|
34
|
|
|
$all_subscribers = $query->num_rows() > 0 ? $query->result_array() : []; |
35
|
|
|
|
36
|
|
|
assetManager::create() |
37
|
|
|
->setData( |
38
|
|
|
[ |
39
|
|
|
'roles' => $roles, |
40
|
|
|
'admin_mail' => $this->dx_auth->get_user_email(), |
41
|
|
|
'all' => $all_subscribers, |
42
|
|
|
'site_settings' => $this->cms_base->get_settings(), |
43
|
|
|
] |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
if (!$this->ajaxRequest) { |
47
|
|
|
assetManager::create() |
48
|
|
|
->renderAdmin('form', true); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function send_email() { |
53
|
|
|
|
54
|
|
|
// Load form validation class |
55
|
|
|
$this->load->library('form_validation'); |
56
|
|
|
|
57
|
|
|
$this->form_validation->set_rules('subject', lang('Theme', 'mailer'), 'required|trim'); |
58
|
|
|
$this->form_validation->set_rules('name', lang('Your name', 'mailer'), 'required|trim'); |
59
|
|
|
$this->form_validation->set_rules('email', lang('Your e-mail', 'mailer'), 'required|trim|valid_email'); |
60
|
|
|
$this->form_validation->set_rules('message', lang('Message', 'mailer'), 'required|trim'); |
61
|
|
|
|
62
|
|
|
if ($this->form_validation->run($this) == FALSE) { |
63
|
|
|
showMessage(validation_errors(), false, 'r'); |
64
|
|
|
} else { |
65
|
|
|
$this->load->helper('typography'); |
66
|
|
|
$this->load->library('email'); |
67
|
|
|
|
68
|
|
|
// Init email config |
69
|
|
|
$config['wordwrap'] = TRUE; |
|
|
|
|
70
|
|
|
$config['charset'] = 'UTF-8'; |
71
|
|
|
$config['mailtype'] = $this->input->post('mailtype'); |
72
|
|
|
$this->email->initialize($config); |
73
|
|
|
|
74
|
|
|
// Get users array |
75
|
|
|
$users = $this->db->get('mail'); |
76
|
|
|
|
77
|
|
|
if ($users->num_rows() > 0) { |
78
|
|
|
$message = $this->input->post('message'); |
79
|
|
|
|
80
|
|
|
if ($this->input->post('mailtype') == 'html') { |
81
|
|
|
$message = '<html><body>' . nl2br_except_pre($message) . '</body></html>'; |
82
|
|
|
} |
83
|
|
|
$counter = [ |
84
|
|
|
'true' => 0, |
85
|
|
|
'all' => 0, |
86
|
|
|
]; |
87
|
|
|
foreach ($users->result_array() as $user) { |
88
|
|
|
// Replace {username} |
89
|
|
|
$tmp_msg = str_replace('%username%', $user['username'], $message); |
90
|
|
|
|
91
|
|
|
$this->email->from($this->input->post('email'), $this->input->post('name')); |
92
|
|
|
$this->email->to($user['email']); |
93
|
|
|
$this->email->reply_to($this->input->post('email'), $this->input->post('name')); |
94
|
|
|
$this->email->subject($this->input->post('subject')); |
95
|
|
|
$this->email->message($tmp_msg); |
96
|
|
|
$counter['all'] ++; |
97
|
|
|
if ($this->email->send()) { |
98
|
|
|
$counter['true'] ++; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->load->library('lib_admin'); |
103
|
|
|
$this->lib_admin->log(lang('Send', 'mailer') . '(' . $counter['true'] . '/' . $counter['all'] . ')' . lang('users e-mail with a subject', 'mailer') . ')' . $this->input->post('subject')); |
104
|
|
|
$class = ''; |
105
|
|
|
if ($counter['true'] == $counter['all']) { |
106
|
|
|
$class = ''; |
107
|
|
|
} else if ($counter['true'] == 0) { |
108
|
|
|
$class = 'r'; |
109
|
|
|
} |
110
|
|
|
if ($class !== 'r') { |
111
|
|
|
showMessage(lang('message has been sent', 'mailer') . ': ' . $counter['true'] . lang('Number of e-mails sent', 'mailer') . $counter['all'] . lang('pcs.', 'mailer') . $class); |
112
|
|
|
} else { |
113
|
|
|
showMessage(lang('none of the messages', 'mailer') . $counter['all'] . lang('Number not', 'mailer'), $class); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function deleteUsers() { |
120
|
|
|
|
121
|
|
View Code Duplication |
if ($this->input->post('ids')) { |
122
|
|
|
|
123
|
|
|
foreach ($this->input->post('ids') as $id) { |
124
|
|
|
$this->db->delete('mail', ['id' => $id]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
showMessage(lang('Subscribers removal', 'mailer')); |
128
|
|
|
} else { |
129
|
|
|
showMessage(lang('There is not ID', 'mailer'), '', 'r'); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/* End of file admin.php */ |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.