b43072e0 by Marty Penner

Make notifications translatable

1 parent a1a85c5a
...@@ -26,9 +26,6 @@ function display_page() ...@@ -26,9 +26,6 @@ function display_page()
26 { 26 {
27 27
28 if (isset($_GET['action']) && $_GET['action'] == "edit") { 28 if (isset($_GET['action']) && $_GET['action'] == "edit") {
29 /** @var \wpdb $wpdb */
30 global $wpdb;
31
32 /** @var \StdClass $entry */ 29 /** @var \StdClass $entry */
33 $entry = get_post($_GET['page_id']); 30 $entry = get_post($_GET['page_id']);
34 31
...@@ -42,26 +39,26 @@ function display_page() ...@@ -42,26 +39,26 @@ function display_page()
42 $entry->email = $email; 39 $entry->email = $email;
43 $entry->system = $system; 40 $entry->system = $system;
44 41
45 // here
46 $validation = new Notifications\Validation; 42 $validation = new Notifications\Validation;
47 43
48 $validation->set_rules('type', 'Notification Type', 'required'); 44 $validation->set_rules('type', 'Notification Type', 'required');
49 $validation->set_rules('title', 'Description', 'trim|required|min_length[4]'); 45 $validation->set_rules('title', 'Description', 'trim|required|min_length[4]');
50 $validation->set_rules('type', 'Notification Type', 'required');
51 $validation->set_rules('sendto', 'Send To', 'required'); 46 $validation->set_rules('sendto', 'Send To', 'required');
52 47
53 $type_val = ($_POST && $_POST['type'] == "scheduled") ? 'required' : ''; 48 $type_val = ($_POST && $_POST['type'] == 'scheduled') ? 'required' : '';
54 $validation->set_rules('execute_date', 'Execute Date', $type_val); 49 $validation->set_rules('execute_date', 'Execute Date', $type_val);
55 50
56 $trigger_val = ($_POST && $_POST['type'] == "triggered") ? 'required' : ''; 51 $trigger_val = ($_POST && $_POST['type'] == 'triggered') ? 'required' : '';
57 $validation->set_rules('trigger', 'Trigger', $trigger_val); 52 $validation->set_rules('trigger', 'Trigger', $trigger_val);
58 53
59 $validation->set_rules('subject', 'Subject', 'trim');
60 $validation->set_rules('text', 'Text Version', 'trim|min_length[16]');
61 $validation->set_rules('html', 'HTML Version', 'trim|min_length[16]');
62
63 $validation->set_rules('system_message_type', 'System Message Type', 'trim'); 54 $validation->set_rules('system_message_type', 'System Message Type', 'trim');
64 $validation->set_rules('system', 'System Message', 'trim|min_length[16]'); 55
56 foreach (['en', 'fr'] as $lang) {
57 $validation->set_rules($lang.'_subject', 'Subject', 'trim');
58 $validation->set_rules($lang.'_text', 'Text Version', 'trim|min_length[16]');
59 $validation->set_rules($lang.'_html', 'HTML Version', 'trim|min_length[16]');
60 $validation->set_rules($lang.'_system', 'System Message', 'trim|min_length[16]');
61 }
65 62
66 //details 63 //details
67 if ($validation->run() == true) { 64 if ($validation->run() == true) {
...@@ -69,73 +66,77 @@ function display_page() ...@@ -69,73 +66,77 @@ function display_page()
69 // Clean up data before saving 66 // Clean up data before saving
70 Tools\tzClean($_POST); 67 Tools\tzClean($_POST);
71 68
72 $type = $_POST['type']; 69 $type = $_POST['type'];
73 $title = $_POST['title']; 70 $title = $_POST['title'];
74 $sendto = $_POST['sendto']; 71 $sendto = $_POST['sendto'];
75 $execute_date = ($type == "scheduled") ? $_POST['execute_date'] : "0000-00-00 00:00:00"; 72 $executeDate = ($type == 'scheduled') ? $_POST['execute_date'] : '0000-00-00 00:00:00';
76 $trigger = ($type == "scheduled") ? "scheduled-cron-job" : $_POST['trigger']; 73 $trigger = ($type == 'scheduled') ? 'scheduled-cron-job' : $_POST['trigger'];
77 74
78 // email 75 $emailData = [];
79 $subject = $_POST['subject']; 76 $systemMessageData = [];
80 $text = $_POST['text']; 77
81 $html = $_POST['html']; 78 foreach (['en', 'fr'] as $lang) {
82 $attachments = []; 79 // email
83 $upload_dir = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'notifications'.DIRECTORY_SEPARATOR; 80 $subject = $_POST[$lang.'_subject'];
84 81 $text = $_POST[$lang.'_text'];
85 fixFilesArray($_FILES['attachment']); 82 $html = $_POST[$lang.'_html'];
86 83 $attachments = [];
87 foreach ($_FILES['attachment'] as $position => $file) { 84 $uploadDir = __DIR__.'/../../../uploads/notifications/';
88 // should output array with indices name, type, tmp_name, error, size 85
89 if ($file['name'] != "") { 86 fixFilesArray($_FILES[$lang.'_attachment']);
90 move_uploaded_file($file['tmp_name'], $upload_dir.$file['name']); 87
91 $attachments[] = $file['name']; 88 foreach ($_FILES[$lang.'_attachment'] as $position => $file) {
89 // should output array with indices name, type, tmp_name, error, size
90 if ($file['name'] != '') {
91 move_uploaded_file($file['tmp_name'], $uploadDir.$file['name']);
92 $attachments[] = $file['name'];
93 }
92 } 94 }
93 }
94 95
95 // system 96 // system
96 $system_message_type = $_POST['system_message_type']; 97 $systemMessageType = $_POST['system_message_type'];
97 $system = $_POST['system']; 98 $system = $_POST[$lang.'_system'];
98 99
99 update_post_meta($id, 'notif_type', $type); 100 if (count($entry->email[$lang.'_attachments']) > 0) {
100 update_post_meta($id, 'execute_date', mysqldatetime_to_timestamp($execute_date)); 101 $attachments = array_merge($entry->email[$lang.'_attachments'], $attachments);
101 update_post_meta($id, 'trigger', $trigger); 102 }
103
104 $emailData = array_merge(
105 $emailData,
106 [
107 $lang.'_subject' => $subject,
108 $lang.'_text' => $text,
109 $lang.'_html' => $html,
110 $lang.'_attachments' => $attachments
111 ]
112 );
113 $systemMessageData = array_merge(
114 $systemMessageData,
115 [
116 'system_message_type' => $systemMessageType,
117 $lang.'_message' => $system
118 ]
119 );
120 }
102 121
103 update_post_meta( 122 update_post_meta(
104 $id, 123 $id,
105 "details", 124 'details',
106 [ 125 [
107 'type' => $type, 126 'type' => $type,
108 'sendto' => $sendto, 127 'sendto' => $sendto,
109 'status' => $entry->details['status'], 128 'status' => $entry->details['status'],
110 'trigger' => $trigger, 129 'trigger' => $trigger,
111 'execute_date' => $execute_date 130 'execute_date' => $executeDate
112 ] 131 ]
113 ); 132 );
114
115 update_post_meta($id, 'send_status', $entry->details['status']); 133 update_post_meta($id, 'send_status', $entry->details['status']);
134 update_post_meta($id, 'notif_type', $type);
135 update_post_meta($id, 'execute_date', mysqldatetime_to_timestamp($executeDate));
136 update_post_meta($id, 'trigger', $trigger);
116 137
117 if (count($entry->email['attachments']) > 0) { 138 update_post_meta($id, 'email', $emailData);
118 $attachments = array_merge($entry->email['attachments'], $attachments); 139 update_post_meta($id, 'system', $systemMessageData);
119 }
120
121 update_post_meta(
122 $id,
123 'email',
124 [
125 'subject' => $subject,
126 'text' => $text,
127 'html' => $html,
128 'attachments' => $attachments
129 ]
130 );
131 update_post_meta(
132 $id,
133 'system',
134 [
135 'system_message_type' => $system_message_type,
136 'message' => $system
137 ]
138 );
139 140
140 $update = []; 141 $update = [];
141 $update['ID'] = $id; 142 $update['ID'] = $id;
...@@ -152,17 +153,15 @@ function display_page() ...@@ -152,17 +153,15 @@ function display_page()
152 $entry->email = $email; 153 $entry->email = $email;
153 $entry->system = $system; 154 $entry->system = $system;
154 155
155 $flash = "<strong>Notification Saved Successfully!</strong><br /><a href='/wp-admin/admin.php?page=notifications'>Click here</a> to view all notifications.</a>"; 156 $flash = '<strong>Notification Saved Successfully!</strong><br /><a href=""/wp-admin/admin.php?page=notifications">Click here</a> to view all notifications.</a>';
156 require_once(__DIR__.'/views/form.php'); 157 require_once(__DIR__.'/views/form.php');
157 } else { 158 } else {
158 require_once(__DIR__.'/views/form.php'); 159 require_once(__DIR__.'/views/form.php');
159 } 160 }
160 } else { 161 } else {
161 162 if (isset($_GET['action']) && $_GET['action'] == 'delete') {
162 if (isset($_GET['action']) && $_GET['action'] == "delete") {
163 wp_delete_post($_GET['page_id'], true); 163 wp_delete_post($_GET['page_id'], true);
164 } elseif (isset($_GET['action']) && $_GET['action'] == "archive") { 164 } elseif (isset($_GET['action']) && $_GET['action'] == 'archive') {
165
166 $id = $_GET['page_id']; 165 $id = $_GET['page_id'];
167 166
168 $postdata = get_post_meta($id, 'details', true); 167 $postdata = get_post_meta($id, 'details', true);
...@@ -197,12 +196,12 @@ function display_page() ...@@ -197,12 +196,12 @@ function display_page()
197 $system = get_post_meta($id, 'system', true); 196 $system = get_post_meta($id, 'system', true);
198 197
199 $entry->trigger = $details['trigger']; 198 $entry->trigger = $details['trigger'];
200 $entry->status = isset($details['status']) ? $details['status'] : "active"; 199 $entry->status = isset($details['status']) ? $details['status'] : 'active';
201 $entry->type = $details['type']; 200 $entry->type = $details['type'];
202 $entry->sendto = $details['sendto']; 201 $entry->sendto = $details['sendto'];
203 202
204 $entry->is_email = (($email['text'] != '' || $email['html'] != '')) ? true : false; 203 $entry->is_email = (($email['en_text'] != '' || $email['en_html'] != '')) ? true : false;
205 $entry->is_system = (isset($system['message']) && $system['message'] != '') ? true : false; 204 $entry->is_system = (isset($system['en_message']) && $system['en_message'] != '') ? true : false;
206 205
207 $entry->execute_date = $details['execute_date']; 206 $entry->execute_date = $details['execute_date'];
208 207
...@@ -273,28 +272,26 @@ function notification_settings() ...@@ -273,28 +272,26 @@ function notification_settings()
273 272
274 function create_notification() 273 function create_notification()
275 { 274 {
276 /** @var \wpdb $wpdb */
277 global $wpdb;
278
279 // here
280 $validation = new Notifications\Validation; 275 $validation = new Notifications\Validation;
281 276
282 $validation->set_rules('type', 'Notification Type', 'required'); 277 $validation->set_rules('type', 'Notification Type', 'required');
283 $validation->set_rules('title', 'Description', 'trim|required|min_length[16]'); 278 $validation->set_rules('title', 'Description', 'trim|required|min_length[4]');
284 $validation->set_rules('type', 'Notification Type', 'required');
285 $validation->set_rules('sendto', 'Send To', 'required'); 279 $validation->set_rules('sendto', 'Send To', 'required');
286 280
287 $type_val = ($_POST && $_POST['type'] == "scheduled") ? 'required' : ''; 281 $type_val = ($_POST && $_POST['type'] == 'scheduled') ? 'required' : '';
288 $validation->set_rules('execute_date', 'Execute Date', $type_val); 282 $validation->set_rules('execute_date', 'Execute Date', $type_val);
289 283
290 $trigger_val = ($_POST && $_POST['type'] == "triggered") ? 'required' : ''; 284 $trigger_val = ($_POST && $_POST['type'] == 'triggered') ? 'required' : '';
291 $validation->set_rules('trigger', 'Trigger', $trigger_val); 285 $validation->set_rules('trigger', 'Trigger', $trigger_val);
292 286
293 $validation->set_rules('subject', 'Subject', 'trim'); 287 $validation->set_rules('system_message_type', 'System Message Type', 'trim');
294 $validation->set_rules('text', 'Text Version', 'trim|min_length[16]');
295 $validation->set_rules('html', 'HTML Version', 'trim|min_length[16]');
296 288
297 $validation->set_rules('system', 'System Message', 'trim|min_length[16]'); 289 foreach (['en', 'fr'] as $lang) {
290 $validation->set_rules($lang.'_subject', 'Subject', 'trim');
291 $validation->set_rules($lang.'_text', 'Text Version', 'trim|min_length[16]');
292 $validation->set_rules($lang.'_html', 'HTML Version', 'trim|min_length[16]');
293 $validation->set_rules($lang.'_system', 'System Message', 'trim|min_length[16]');
294 }
298 295
299 if ($_POST && ($_POST['subject'] == '' && $_POST['system'] == '')) { 296 if ($_POST && ($_POST['subject'] == '' && $_POST['system'] == '')) {
300 $form_error = true; 297 $form_error = true;
...@@ -306,7 +303,7 @@ function create_notification() ...@@ -306,7 +303,7 @@ function create_notification()
306 Tools\tzClean($_POST); 303 Tools\tzClean($_POST);
307 304
308 // ok, so now we need to create the notification. 305 // ok, so now we need to create the notification.
309 class postTemplate 306 class PostTemplate
310 { 307 {
311 var $post_title = ''; 308 var $post_title = '';
312 var $post_content = ''; 309 var $post_content = '';
...@@ -315,79 +312,84 @@ function create_notification() ...@@ -315,79 +312,84 @@ function create_notification()
315 var $comment_status = 'closed'; 312 var $comment_status = 'closed';
316 } 313 }
317 314
318 //details 315 // details
319 $type = $_POST['type']; 316 $type = $_POST['type'];
320 $title = $_POST['title']; 317 $title = $_POST['title'];
321 $sendto = $_POST['sendto']; 318 $sendto = $_POST['sendto'];
322 $execute_date = ($type == "scheduled") ? $_POST['execute_date'] : "0000-00-00 00:00:00"; 319 $executeDate = ($type == 'scheduled') ? $_POST['execute_date'] : '0000-00-00 00:00:00';
323 $trigger = ($type == "scheduled") ? "scheduled-cron-job" : $_POST['trigger']; 320 $trigger = ($type == 'scheduled') ? 'scheduled-cron-job' : $_POST['trigger'];
324 321
325 // email 322 $emailData = [];
326 $subject = $_POST['subject']; 323 $systemMessageData = [];
327 $text = $_POST['text']; 324
328 $html = $_POST['html']; 325 foreach (['en', 'fr'] as $lang) {
329 $attachments = []; 326 // email
330 $upload_dir = __DIR__.'/uploads/'; 327 $subject = $_POST[$lang.'_subject'];
331 fixFilesArray($_FILES['attachment']); 328 $text = $_POST[$lang.'_text'];
332 foreach ($_FILES['attachment'] as $position => $file) { 329 $html = $_POST[$lang.'_html'];
333 // should output array with indices name, type, tmp_name, error, size 330 $attachments = [];
334 if ($file['name'] != "") { 331 $uploadDir = __DIR__.'/../../../uploads/notifications/';
335 move_uploaded_file($file['tmp_name'], $upload_dir.$file['name']); 332
336 $attachments[] = $file['name']; 333 fixFilesArray($_FILES[$lang.'_attachment']);
334
335 foreach ($_FILES[$lang.'_attachment'] as $position => $file) {
336 // should output array with indices name, type, tmp_name, error, size
337 if ($file['name'] != '') {
338 move_uploaded_file($file['tmp_name'], $uploadDir.$file['name']);
339 $attachments[] = $file['name'];
340 }
337 } 341 }
338 }
339 342
340 // system 343 // system
341 $system_message_type = $_POST['system_message_type']; 344 $systemMessageType = $_POST['system_message_type'];
342 $system = $_POST['system']; 345 $system = $_POST[$lang.'_system'];
346
347 $emailData = array_merge(
348 $emailData,
349 [
350 $lang.'_subject' => $subject,
351 $lang.'_text' => $text,
352 $lang.'_html' => $html,
353 $lang.'_attachments' => $attachments
354 ]
355 );
356 $systemMessageData = array_merge(
357 $systemMessageData,
358 [
359 'system_message_type' => $systemMessageType,
360 $lang.'_message' => $system
361 ]
362 );
363 }
343 364
344 // make post... 365 // make post...
345 366 $notification = new PostTemplate();
346 $notification = new postTemplate();
347 $notification->post_title = $title; 367 $notification->post_title = $title;
348 $notification->post_content = "Notification created ".date( 368 $notification->post_content = 'Notification created '.date(
349 'Y-m-d H:i:s' 369 'Y-m-d H:i:s'
350 )." --- to be sent on $execute_date"; 370 )." --- to be sent on $executeDate";
351 $notification->post_date_gmt = date("Y-m-d H:i:s", time()); 371 $notification->post_date_gmt = date('Y-m-d H:i:s', time());
352
353 $id = wp_insert_post($notification);
354 372
355 update_post_meta($id, 'notif_type', $type); 373 $id = wp_insert_post((array)$notification);
356 update_post_meta($id, 'execute_date', mysqldatetime_to_timestamp($execute_date));
357 update_post_meta($id, 'trigger', $trigger);
358 374
359 add_post_meta( 375 update_post_meta(
360 $id, 376 $id,
361 "details", 377 'details',
362 [ 378 [
363 'type' => $type, 379 'type' => $type,
364 'sendto' => $sendto, 380 'sendto' => $sendto,
365 'status' => 'pending', 381 'status' => 'pending',
366 'trigger' => $trigger, 382 'trigger' => $trigger,
367 'execute_date' => $execute_date 383 'execute_date' => $executeDate
368 ] 384 ]
369 ); 385 );
370
371 update_post_meta($id, 'send_status', 'pending'); 386 update_post_meta($id, 'send_status', 'pending');
387 update_post_meta($id, 'notif_type', $type);
388 update_post_meta($id, 'execute_date', mysqldatetime_to_timestamp($executeDate));
389 update_post_meta($id, 'trigger', $trigger);
372 390
373 add_post_meta( 391 update_post_meta($id, 'email', $emailData);
374 $id, 392 update_post_meta($id, 'system', $systemMessageData);
375 "email",
376 [
377 'subject' => $subject,
378 'text' => $text,
379 'html' => $html,
380 'attachments' => $attachments
381 ]
382 );
383 update_post_meta(
384 $id,
385 "system",
386 [
387 'system_message_type' => $system_message_type,
388 'message' => $system
389 ]
390 );
391 393
392 $flash = "<strong>Notification Saved Successfully!</strong><br /><a href='/wp-admin/admin.php?page=notifications'>Click here</a> to view all notifications.</a>"; 394 $flash = "<strong>Notification Saved Successfully!</strong><br /><a href='/wp-admin/admin.php?page=notifications'>Click here</a> to view all notifications.</a>";
393 require_once(__DIR__.'/views/create.php'); 395 require_once(__DIR__.'/views/create.php');
......
...@@ -79,7 +79,9 @@ function print_user_notices($showOnlyUnread = false) ...@@ -79,7 +79,9 @@ function print_user_notices($showOnlyUnread = false)
79 } 79 }
80 80
81 $system = get_post_meta($notice['notification_id'], 'system', true); 81 $system = get_post_meta($notice['notification_id'], 'system', true);
82 $content = $system['message']; 82 $content = isset($system[ICL_LANGUAGE_CODE.'_message'])
83 ? $system[ICL_LANGUAGE_CODE.'_message']
84 : $system['en_message'];
83 85
84 if (isset($notice['args']) && count($notice['args']) > 0) { 86 if (isset($notice['args']) && count($notice['args']) > 0) {
85 foreach ($notice['args'] as $key => $val) { 87 foreach ($notice['args'] as $key => $val) {
...@@ -214,6 +216,9 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args = ...@@ -214,6 +216,9 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
214 /** @var \wpdb $wpdb */ 216 /** @var \wpdb $wpdb */
215 global $wpdb; 217 global $wpdb;
216 218
219 // LANGTODO: get this from the user's preference. As it stands, if an admin triggers this, the notification will be in whatever language the admin is viewing the page as
220 $lang = ICL_LANGUAGE_CODE;
221
217 /** @var \StdClass $notification */ 222 /** @var \StdClass $notification */
218 $notification = get_notification_by_trigger($trigger); 223 $notification = get_notification_by_trigger($trigger);
219 224
...@@ -228,10 +233,8 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args = ...@@ -228,10 +233,8 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
228 $notification->status = isset($details['status']) ? $details['status'] : 'active'; 233 $notification->status = isset($details['status']) ? $details['status'] : 'active';
229 $notification->type = $details['type']; 234 $notification->type = $details['type'];
230 $notification->sendto = @$details['sendto']; 235 $notification->sendto = @$details['sendto'];
231 $notification->is_email = (($email['text'] != '' || $email['html'] != '') && $email['subject'] != '') 236 $notification->is_email = (($email[$lang.'_text'] != '' || $email[$lang.'_html'] != '') && $email[$lang.'_subject'] != '');
232 ? true 237 $notification->is_system = (isset($system[$lang.'_message']) && $system[$lang.'_message'] != '');
233 : false;
234 $notification->is_system = (isset($system['message']) && $system['message'] != '') ? true : false;
235 238
236 // if is_system ========================================== 239 // if is_system ==========================================
237 if ($notification->is_system && $uid != 0) { 240 if ($notification->is_system && $uid != 0) {
...@@ -257,52 +260,59 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args = ...@@ -257,52 +260,59 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
257 260
258 // if is_email =========================================== 261 // if is_email ===========================================
259 if ($notification->is_email) { 262 if ($notification->is_email) {
260
261 if ($uid == 0 && !isset($args['email'])) { 263 if ($uid == 0 && !isset($args['email'])) {
262 return; 264 return;
263 } elseif ($uid == 0 && isset($args['email'])) { 265 } elseif ($uid == 0 && isset($args['email'])) {
264 $to_email = $args['email']; 266 $toEmail = $args['email'];
265 } else { 267 } else {
266 268
267 $user = new User\Account($uid); 269 $user = new User\Account($uid);
268 $email_address_preference = get_user_meta($user->ID, 'email_address_preference', true); 270 $email_address_preference = get_user_meta($user->ID, 'email_address_preference', true);
269 if (empty($email_address_preference)) { 271 if (empty($email_address_preference)) {
270 $to_email = $user->user_email; 272 $toEmail = $user->user_email;
271 } else { 273 } else {
274 $pp = strtolower($email_address_preference).'_';
275 $toEmail = get_user_meta($user->ID, $pp.'email', true);
272 276
273 $pp = strtolower($email_address_preference)."_"; 277 if (empty($toEmail)) {
274 $to_email = get_user_meta($user->ID, $pp.'email', true); 278 $toEmail = $user->user_email;
275
276 if (empty($to_email)) {
277 $to_email = $user->user_email;
278 } 279 }
279 } 280 }
280 } 281 }
281 282
282 $contents = $email; 283 $contents = $email;
283 284
284 $from_email = get_bloginfo('admin_email'); 285 $fromEmail = get_bloginfo('admin_email');
285 $subject = strip_tags($contents['subject']); 286 $subject = strip_tags(
286 $html = @$contents['html']; 287 isset($contents[$lang.'_subject']) && !empty($contents[$lang.'_subject'])
287 $alttext = strip_tags($contents['text']); 288 ? $contents[$lang.'_subject']
289 : $contents['en_subject']
290 );
291 $html = isset($contents[$lang.'_html']) && !empty($contents[$lang.'_html'])
292 ? $contents[$lang.'_html']
293 : @$contents['en_html'];
294 $altText = strip_tags(
295 isset($contents[$lang.'_text']) && !empty($contents[$lang.'_text'])
296 ? $contents[$lang.'_text']
297 : $contents['en_text']
298 );
288 299
289 foreach ($args as $key => $val) { 300 foreach ($args as $key => $val) {
290 if ((filter_var($val, FILTER_VALIDATE_URL) !== false) && !empty($html)) { 301 if ((filter_var($val, FILTER_VALIDATE_URL) !== false) && !empty($html)) {
291 $html = str_replace("{".$key."}", "<a href='".$val."'>".$val."</a>", $html); 302 $html = str_replace('{'.$key.'}', '<a href="'.$val.'">'.$val.'</a>', $html);
292 } else { 303 } else {
293 $html = str_replace("{".$key."}", $val, $html); 304 $html = str_replace('{'.$key.'}', $val, $html);
294 $alttext = str_replace("{".$key."}", $val, $alttext); 305 $altText = str_replace('{'.$key.'}', $val, $altText);
295 $subject = str_replace("{".$key."}", $val, $subject); 306 $subject = str_replace('{'.$key.'}', $val, $subject);
296 } 307 }
297 } 308 }
298 309
299 $attachments = []; 310 $attachments = isset($contents[$lang.'_attachments'])
300 if (isset($contents['attachments'])) { 311 ? $contents[$lang.'_attachments']
301 $attachments = $contents['attachments']; 312 : $contents['en_attachments'];
302 } 313 $att1 = isset($attachments[0]) ? $attachments[0] : '';
303 $att1 = isset($attachments[0]) ? $attachments[0] : ''; 314 $att2 = isset($attachments[1]) ? $attachments[1] : '';
304 $att2 = isset($attachments[1]) ? $attachments[1] : ''; 315 $att3 = isset($attachments[2]) ? $attachments[2] : '';
305 $att3 = isset($attachments[2]) ? $attachments[2] : '';
306 316
307 $attachments = array_map( 317 $attachments = array_map(
308 function ($attachmentPath) { 318 function ($attachmentPath) {
...@@ -313,7 +323,7 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args = ...@@ -313,7 +323,7 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
313 323
314 $sentSuccessfully = false; 324 $sentSuccessfully = false;
315 if (CBV\system_can_send_emails()) { 325 if (CBV\system_can_send_emails()) {
316 $response = \wpMandrill::mail($to_email, $subject, !empty($html) ? $html : $alttext, [], $attachments); 326 $response = \wpMandrill::mail($toEmail, $subject, !empty($html) ? $html : $altText, [], $attachments);
317 if ( 327 if (
318 is_array($response) 328 is_array($response)
319 && isset($response[0]['status']) 329 && isset($response[0]['status'])
...@@ -328,10 +338,10 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args = ...@@ -328,10 +338,10 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
328 'wp_mail_daemon', 338 'wp_mail_daemon',
329 [ 339 [
330 'notification_id' => $nid, 340 'notification_id' => $nid,
331 'from_email' => $from_email, 341 'from_email' => $fromEmail,
332 'to_email' => $to_email, 342 'to_email' => $toEmail,
333 'subject' => $subject, 343 'subject' => $subject,
334 'text' => $alttext, 344 'text' => $altText,
335 'html' => $html, 345 'html' => $html,
336 'attachment1' => $att1, 346 'attachment1' => $att1,
337 'attachment2' => $att2, 347 'attachment2' => $att2,
......
...@@ -9,7 +9,7 @@ use Tz\WordPress\Tools\Notifications\Settings; ...@@ -9,7 +9,7 @@ use Tz\WordPress\Tools\Notifications\Settings;
9 <div class="wrap"> 9 <div class="wrap">
10 <h2>Notifications - Create New</h2> 10 <h2>Notifications - Create New</h2>
11 11
12 <?php if (isset($flash) && $flash != ""): ?> 12 <?php if (isset($flash) && $flash != ''): ?>
13 <div class="post-success"> 13 <div class="post-success">
14 <?php echo $flash; ?> 14 <?php echo $flash; ?>
15 </div> 15 </div>
...@@ -231,4 +231,4 @@ use Tz\WordPress\Tools\Notifications\Settings; ...@@ -231,4 +231,4 @@ use Tz\WordPress\Tools\Notifications\Settings;
231 jQuery('.scheduled_sendto').show(); 231 jQuery('.scheduled_sendto').show();
232 } 232 }
233 } 233 }
234 </script>
...\ No newline at end of file ...\ No newline at end of file
234 </script>
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
2 use Tz\WordPress\Tools; 2 use Tz\WordPress\Tools;
3 use Tz\WordPress\Tools\Notifications; 3 use Tz\WordPress\Tools\Notifications;
4 use Tz\WordPress\Tools\Notifications\Settings; 4 use Tz\WordPress\Tools\Notifications\Settings;
5
5 ?> 6 ?>
6 7
7 <link rel="stylesheet" href="<?php echo Tools\url('assets/css/notifications.css', __FILE__) ?>"/> 8 <link rel="stylesheet" href="<?php echo Tools\url('assets/css/notifications.css', __FILE__) ?>"/>
...@@ -14,11 +15,15 @@ use Tz\WordPress\Tools\Notifications\Settings; ...@@ -14,11 +15,15 @@ use Tz\WordPress\Tools\Notifications\Settings;
14 <?php echo $flash; ?> 15 <?php echo $flash; ?>
15 </div> 16 </div>
16 <?php endif; ?> 17 <?php endif; ?>
17 <?php if ($validation->validation_errors() != "" || isset($form_error)): ?> 18 <?php if ($validation->validation_errors() != '' || isset($form_error)): ?>
18 <div class="post-errors"> 19 <div class="post-errors">
19 <div class="post-errors-title"><strong>Oops.</strong> There was an error saving your notification.</div> 20 <div class="post-errors-title">
21 <strong>Oops.</strong> There was an error saving your notification.
22 </div>
20 <?php if (isset($form_error)): ?> 23 <?php if (isset($form_error)): ?>
21 <p class="post-errors-content">You must include either an Email, System or SMS message.</p> 24 <p class="post-errors-content">
25 You must include either an Email, System or SMS message.
26 </p>
22 <?php endif; ?> 27 <?php endif; ?>
23 </div> 28 </div>
24 <?php endif; ?> 29 <?php endif; ?>
...@@ -27,7 +32,7 @@ use Tz\WordPress\Tools\Notifications\Settings; ...@@ -27,7 +32,7 @@ use Tz\WordPress\Tools\Notifications\Settings;
27 action="/wp-admin/admin.php?page=notifications&action=edit&page_id=<?php echo $_GET['page_id'] ?>"> 32 action="/wp-admin/admin.php?page=notifications&action=edit&page_id=<?php echo $_GET['page_id'] ?>">
28 <input type="hidden" name="_POSTED_" value="yes"/> 33 <input type="hidden" name="_POSTED_" value="yes"/>
29 34
30 <table cellspacing="0" class="widefat post fixed" style="margin-top:15px;"> 35 <table cellspacing="0" class="widefat post fixed" style="margin-top: 15px;">
31 <thead> 36 <thead>
32 <tr> 37 <tr>
33 <th width="150">Notification Details</th> 38 <th width="150">Notification Details</th>
...@@ -43,7 +48,8 @@ use Tz\WordPress\Tools\Notifications\Settings; ...@@ -43,7 +48,8 @@ use Tz\WordPress\Tools\Notifications\Settings;
43 <option value="scheduled" <?php echo ($validation->set_value( 48 <option value="scheduled" <?php echo ($validation->set_value(
44 'type', 49 'type',
45 $entry->details['type'] 50 $entry->details['type']
46 ) == 'scheduled') ? 'selected="selected"' : ''; ?>>Scheduled Notification 51 ) == 'scheduled') ? 'selected="selected"' : ''; ?>>
52 Scheduled Notification
47 </option> 53 </option>
48 <?php if (current_user_can( 54 <?php if (current_user_can(
49 Settings\MANAGE_SYSTEM_NOTIFICATIONS 55 Settings\MANAGE_SYSTEM_NOTIFICATIONS
...@@ -52,7 +58,8 @@ use Tz\WordPress\Tools\Notifications\Settings; ...@@ -52,7 +58,8 @@ use Tz\WordPress\Tools\Notifications\Settings;
52 <option value="triggered" <?php echo ($validation->set_value( 58 <option value="triggered" <?php echo ($validation->set_value(
53 'type', 59 'type',
54 $entry->details['type'] 60 $entry->details['type']
55 ) == 'triggered') ? 'selected="selected"' : ''; ?>>System Triggered Notification 61 ) == 'triggered') ? 'selected="selected"' : ''; ?>>
62 System Triggered Notification
56 </option> 63 </option>
57 <?php endif; ?> 64 <?php endif; ?>
58 </select> 65 </select>
...@@ -61,11 +68,13 @@ use Tz\WordPress\Tools\Notifications\Settings; ...@@ -61,11 +68,13 @@ use Tz\WordPress\Tools\Notifications\Settings;
61 </tr> 68 </tr>
62 <tr> 69 <tr>
63 <td width="150">Notification Description</td> 70 <td width="150">Notification Description</td>
64 <td><input type="text" name="title" class="wide-input-field" 71 <td>
72 <input type="text" name="title" class="wide-input-field"
65 value="<?php echo $validation->set_value( 73 value="<?php echo $validation->set_value(
66 'title', 74 'title',
67 $entry->post_title 75 $entry->post_title
68 ); ?>"/><?php echo $validation->form_error('title'); ?></td> 76 ); ?>"/><?php echo $validation->form_error('title'); ?>
77 </td>
69 </tr> 78 </tr>
70 <tr class="scheduled_sendto"> 79 <tr class="scheduled_sendto">
71 <td>Sent To:</td> 80 <td>Sent To:</td>
...@@ -80,7 +89,8 @@ use Tz\WordPress\Tools\Notifications\Settings; ...@@ -80,7 +89,8 @@ use Tz\WordPress\Tools\Notifications\Settings;
80 <option value="allusers" <?php echo ($validation->set_value( 89 <option value="allusers" <?php echo ($validation->set_value(
81 'sendto', 90 'sendto',
82 $entry->details['sendto'] 91 $entry->details['sendto']
83 ) == 'allusers') ? 'selected="selected"' : ''; ?>>All Users 92 ) == 'allusers') ? 'selected="selected"' : ''; ?>>
93 All Users
84 </option> 94 </option>
85 <optgroup label="By Group:"> 95 <optgroup label="By Group:">
86 <?php foreach (Notifications\getGroups() as $group_id => $group_name): ?> 96 <?php foreach (Notifications\getGroups() as $group_id => $group_name): ?>
...@@ -101,146 +111,194 @@ use Tz\WordPress\Tools\Notifications\Settings; ...@@ -101,146 +111,194 @@ use Tz\WordPress\Tools\Notifications\Settings;
101 111
102 <tr class="scheduled-extended"> 112 <tr class="scheduled-extended">
103 <td>Execute Date / Time</td> 113 <td>Execute Date / Time</td>
104 <td><input type="text" name="execute_date" id="execute_date" class="wide-input-field date-pick" 114 <td>
115 <input type="text" name="execute_date" id="execute_date" class="wide-input-field date-pick"
105 readonly="readonly" value="<?php echo $validation->set_value( 116 readonly="readonly" value="<?php echo $validation->set_value(
106 'execute_date', 117 'execute_date',
107 $entry->details['execute_date'] 118 $entry->details['execute_date']
108 ); ?>"/><?php echo $validation->form_error('execute_date'); ?></td> 119 ); ?>"/><?php echo $validation->form_error('execute_date'); ?>
120 </td>
109 </tr> 121 </tr>
110 122
111 <tr class="trigger-extended"> 123 <tr class="trigger-extended">
112 <td>Trigger</td> 124 <td>Trigger</td>
113 <td><input type="text" name="trigger" id="trigger" class="wide-input-field" 125 <td>
126 <input type="text" name="trigger" id="trigger" class="wide-input-field"
114 value="<?php echo $validation->set_value( 127 value="<?php echo $validation->set_value(
115 'trigger', 128 'trigger',
116 $entry->details['trigger'] 129 $entry->details['trigger']
117 ); ?>"/><?php echo $validation->form_error('trigger'); ?></td> 130 ); ?>"/><?php echo $validation->form_error('trigger'); ?>
118 </tr>
119
120 </tbody>
121 </table>
122
123 <table cellspacing="0" class="widefat post fixed expandable" style="margin-top:15px;">
124 <thead>
125 <tr>
126 <th width="150" class="toggle"><h6>Email</h6></th>
127 <th class="action-bar">&nbsp;</th>
128 </tr>
129 </thead>
130 <tbody style="<?php echo ($validation->set_value(
131 'subject',
132 $entry->email['subject']
133 ) != '' || $validation->set_value('text', $entry->email['text']) != '' || $validation->set_value(
134 'html',
135 $entry->email['html']
136 ) != '') ? '' : 'display: none;'; ?>;">
137 <tr>
138 <td width="150">Subject Line</td>
139 <td><input type="text" name="subject" class="wide-input-field" style="width:100%;"
140 value="<?php echo $validation->set_value(
141 'subject',
142 $entry->email['subject']
143 ); ?>"/><?php echo $validation->form_error('subject'); ?></td>
144 </tr>
145 <tr>
146 <td>Text Version</td>
147 <td><textarea name="text" class="wide-input-field" rows="10"
148 style="width:100%;"><?php echo $validation->set_value(
149 'text',
150 $entry->email['text']
151 ); ?></textarea><?php echo $validation->form_error('text'); ?></td>
152 </tr>
153 <tr>
154 <td>HTML Version (optional)</td>
155 <td><textarea name="html" id="htmlversion" class="wide-input-field" rows="10"
156 style="width:100%;"><?php echo $validation->set_value(
157 'html',
158 $entry->email['html']
159 ); ?></textarea><?php echo $validation->form_error('html'); ?></td>
160 </tr>
161
162 <tr>
163 <td width="150">Attachments</td>
164 <td>&nbsp;</td>
165 </tr>
166
167 <?php
168 $attachements = $entry->email['attachments'];
169 $allowed_attachments = 3;
170 foreach ($attachements as $attachment): ?>
171 <tr>
172 <td>&nbsp;</td>
173 <td><?php echo $attachment; ?> &nbsp; (<a href="#" entry_id="<?php echo $_GET['page_id'] ?>"
174 class="attachment" rel="<?php echo $attachment; ?>">remove</a>)
175 </td>
176 </tr>
177 <?php $allowed_attachments--; endforeach; ?>
178
179 <?php for ($a = 1; $a <= $allowed_attachments; $a++): ?>
180
181 <tr>
182 <td>&nbsp;</td>
183 <td><input type="file" name="attachment[]"/></td>
184 </tr>
185 <?php endfor; ?>
186
187 </tbody>
188 </table>
189
190 <table cellspacing="0" class="widefat post fixed expandable" style="margin-top:15px;">
191 <thead>
192 <tr>
193 <th width="150" class="toggle"><h6>System Message</h6></th>
194 <th class="action-bar">&nbsp;</th>
195 </tr>
196 </thead>
197 <tbody
198 style="<?php echo ($validation->set_value('system', $entry->system['message']) == "") ? "display:none"
199 : ""; ?>;">
200 <tr>
201 <td>Message Type</td>
202 <td>
203 <select name="system_message_type" class="wide-input-field">
204 <option value="none" <?php echo ($validation->set_value(
205 'system_message_type',
206 $entry->system['system_message_type']
207 ) == "none") ? 'selected="selected"' : ""; ?>>General Message
208 </option>
209 <option value="action_required" <?php echo ($validation->set_value(
210 'system_message_type',
211 $entry->system['system_message_type']
212 ) == "action_required") ? 'selected="selected"' : ""; ?>>Action Required
213 </option>
214 <option value="e-flash" <?php echo ($validation->set_value(
215 'system_message_type',
216 $entry->system['system_message_type']
217 ) == "e-flash") ? 'selected="selected"' : ""; ?>>E-Flash
218 </option>
219 <option value="new_event" <?php echo ($validation->set_value(
220 'system_message_type',
221 $entry->system['system_message_type']
222 ) == "new_event") ? 'selected="selected"' : ""; ?>>New Event
223 </option>
224 </select>
225 </td> 131 </td>
226 </tr> 132 </tr>
227 <tr>
228 <td>Message (Text/HTML)</td>
229 <td><textarea name="system" class="wide-input-field" rows="4"
230 style="width:100%;"><?php echo $validation->set_value(
231 'system',
232 isset($entry->system['message'])
233 ? $entry->system['message']
234 : ""
235 ); ?></textarea><?php echo $validation->form_error('system'); ?></td>
236 </tr>
237 </tbody> 133 </tbody>
238 </table> 134 </table>
239 135
136 <div id="tabs">
137 <ul>
138 <li>
139 <a href="#tab-1">English</a>
140 </li>
141 <li>
142 <a href="#tab-2">French</a>
143 </li>
144 </ul>
145
146 <?php foreach (['en', 'fr'] as $index => $lang): ?>
147 <div id="tab-<?php echo $index + 1; ?>">
148 <table cellspacing="0" class="widefat post fixed expandable" style="margin-top: 15px;">
149 <thead>
150 <tr>
151 <th width="150" class="toggle"><h6>Email</h6></th>
152 <th class="action-bar">&nbsp;</th>
153 </tr>
154 </thead>
155 <tbody style="<?php echo ($validation->set_value(
156 $lang.'_subject',
157 $entry->email[$lang.'_subject']
158 ) != '' || $validation->set_value(
159 $lang.'_text',
160 $entry->email[$lang.'_text']
161 ) != '' || $validation->set_value(
162 $lang.'_html',
163 $entry->email[$lang.'_html']
164 ) != '') ? '' : 'display: none;'; ?>;">
165 <tr>
166 <td width="150">Subject Line</td>
167 <td>
168 <input type="text" name="<?php echo $lang; ?>_subject"
169 class="wide-input-field" style="width: 100%;"
170 value="<?php echo $validation->set_value(
171 $lang.'_subject',
172 $entry->email[$lang.'_subject']
173 ); ?>"/><?php echo $validation->form_error($lang.'_subject'); ?>
174 </td>
175 </tr>
176 <tr>
177 <td>Text Version</td>
178 <td>
179 <textarea name="<?php echo $lang; ?>_text"
180 class="wide-input-field" rows="10"
181 style="width: 100%;"><?php echo $validation->set_value(
182 $lang.'_text',
183 $entry->email[$lang.'_text']
184 ); ?></textarea><?php echo $validation->form_error($lang.'_text'); ?>
185 </td>
186 </tr>
187 <tr>
188 <td>HTML Version (optional)</td>
189 <td>
190 <textarea name="<?php echo $lang; ?>_html"
191 id="htmlversion" class="wide-input-field" rows="10"
192 style="width: 100%;"><?php echo $validation->set_value(
193 $lang.'_html',
194 $entry->email[$lang.'_html']
195 ); ?></textarea><?php echo $validation->form_error($lang.'_html'); ?>
196 </td>
197 </tr>
198
199 <tr>
200 <td width="150">Attachments</td>
201 <td>&nbsp;</td>
202 </tr>
203
204 <?php
205 $attachments = $entry->email[$lang.'_attachments'];
206 $allowedAttachments = 3;
207 foreach ($attachments as $attachment): ?>
208 <tr>
209 <td>&nbsp;</td>
210 <td>
211 <?php echo $attachment; ?> &nbsp; (<a href="#"
212 entry_id="<?php echo $_GET['page_id'] ?>"
213 class="attachment"
214 rel="<?php echo $attachment; ?>">remove</a>)
215 </td>
216 </tr>
217 <?php
218 $allowedAttachments--;
219 endforeach;
220 ?>
221
222 <?php for ($a = 1; $a <= $allowedAttachments; $a++): ?>
223 <tr>
224 <td>&nbsp;</td>
225 <td><input type="file" name="<?php echo $lang; ?>_attachment[]"/></td>
226 </tr>
227 <?php endfor; ?>
228
229 </tbody>
230 </table>
231
232 <table cellspacing="0" class="widefat post fixed expandable" style="margin-top: 15px;">
233 <thead>
234 <tr>
235 <th width="150" class="toggle"><h6>System Message</h6></th>
236 <th class="action-bar">&nbsp;</th>
237 </tr>
238 </thead>
239 <tbody style="<?php echo ($validation->set_value(
240 $lang.'_system',
241 $entry->system[$lang.'_message']
242 ) == '')
243 ? 'display: none'
244 : ''; ?>;">
245 <tr>
246 <td>Message Type</td>
247 <td>
248 <?php if ($lang == 'en'): ?>
249 <select name="system_message_type" class="wide-input-field">
250 <option value="none" <?php echo ($validation->set_value(
251 'system_message_type',
252 $entry->system['system_message_type']
253 ) == 'none') ? 'selected="selected"' : ''; ?>>
254 General Message
255 </option>
256 <option value="action_required" <?php echo ($validation->set_value(
257 'system_message_type',
258 $entry->system['system_message_type']
259 ) == 'action_required') ? 'selected="selected"' : ''; ?>>
260 Action Required
261 </option>
262 <option value="e-flash" <?php echo ($validation->set_value(
263 'system_message_type',
264 $entry->system['system_message_type']
265 ) == 'e-flash') ? 'selected="selected"' : ''; ?>>
266 E-Flash
267 </option>
268 <option value="new_event" <?php echo ($validation->set_value(
269 'system_message_type',
270 $entry->system['system_message_type']
271 ) == 'new_event') ? 'selected="selected"' : ''; ?>>
272 New Event
273 </option>
274 </select>
275 <?php else: ?>
276 <span class="system_message_type_value"></span>
277 <?php endif; ?>
278 </td>
279 </tr>
280 <tr>
281 <td>Message (Text/HTML)</td>
282 <td>
283 <textarea name="<?php echo $lang; ?>_system" class="wide-input-field" rows="4"
284 style="width:100%;"><?php echo $validation->set_value(
285 $lang.'_system',
286 isset($entry->system[$lang.'_message'])
287 ? $entry->system[$lang.'_message']
288 : ''
289 ); ?></textarea><?php echo $validation->form_error($lang.'_system'); ?>
290 </td>
291 </tr>
292 </tbody>
293 </table>
294 </div>
295 <?php endforeach; ?>
296 </div>
240 297
241 <p> 298 <p>
242 <input type="submit" value=" Update "/><input type="button" value=" Cancel " 299 <input type="submit" value=" Update "/>
243 onclick="document.location.href='/wp-admin/admin.php?page=notifications';"/> 300 <input type="button" value=" Cancel "
301 onclick="document.location.href='/wp-admin/admin.php?page=notifications';"/>
244 </p> 302 </p>
245 303
246 </form> 304 </form>
...@@ -249,6 +307,13 @@ use Tz\WordPress\Tools\Notifications\Settings; ...@@ -249,6 +307,13 @@ use Tz\WordPress\Tools\Notifications\Settings;
249 307
250 <script> 308 <script>
251 jQuery(document).ready(function ($) { 309 jQuery(document).ready(function ($) {
310 $('#tabs').tabs();
311
312 // Update system message type clone when changing the message type
313 $('[name="system_message_type"]').change(function () {
314 $('.system_message_type_value').text($(this).find(':selected').text());
315 }).change();
316
252 updateNotificationType(); 317 updateNotificationType();
253 318
254 $('.attachment').live('click', function (e) { 319 $('.attachment').live('click', function (e) {
...@@ -292,7 +357,6 @@ use Tz\WordPress\Tools\Notifications\Settings; ...@@ -292,7 +357,6 @@ use Tz\WordPress\Tools\Notifications\Settings;
292 jQuery('tbody', $table).fadeIn(); 357 jQuery('tbody', $table).fadeIn();
293 } 358 }
294 }); 359 });
295
296 }); 360 });
297 361
298 function updateNotificationType() { 362 function updateNotificationType() {
...@@ -313,4 +377,4 @@ use Tz\WordPress\Tools\Notifications\Settings; ...@@ -313,4 +377,4 @@ use Tz\WordPress\Tools\Notifications\Settings;
313 }); 377 });
314 } 378 }
315 } 379 }
316 </script>
...\ No newline at end of file ...\ No newline at end of file
380 </script>
......