Diff #4 - trunk/src/Notifications/NotificationsConfig.php
5,514 bytes
|
|
January 20, 2025 at 08:20
|
Diff
Index: NotificationsConfig.php
--- NotificationsConfig.php (nonexistent) +++ NotificationsConfig.php (revision 4) @@ -0,0 +1,186 @@ +<?php +declare(strict_types = 1); + +namespace App\Webapp\Notifications; + +use Apex\Svc\App; +use App\Webapp\AdminProfiles; +use App\Webapp\Models{EmailNotification, EmailAttachment}; +use Apex\App\Base\Web\Utils\FormValidator; +use Apex\App\Interfaces\EmailNotificationControllerInterface; + +/** + * Notifications config + */ +class NotificationsConfig +{ + + #[Inject(AdminProfiles::class)] + private AdminProfiles $admin_profiles; + + #[Inject(FormValidator::class)] + private FormValidator $validator; + + #[Inject(App::class)] + private App $app; + + /** + * Get merge field options + */ + public function getMergeFieldOptions(EmailNotificationControllerInterface $obj):string + { + + // Get merge fields + $fields = $this->getMergeFields($obj); + + // Create options + $html = ''; + foreach ($fields as $title => $vars) { + $html .= "\n"; + } + + // Return + return $html; + } + + /** + * Get merge fields + */ + public function getMergeFields(EmailNotificationControllerInterface $obj):array + { + + // Get system fields + $fields = []; + $fields['System'] = [ + 'site_name' => 'Site Name', + 'domain_name' => 'Domain Name', + 'install_url' => 'Install URL', + 'current_date' => 'Current Date', + 'current_time' => 'Current Time', + 'ip_address' => 'IP Address', + 'user_agent' => 'User Agent' + ]; + + // Profile fields + $fields['Profile'] = [ + 'uuid' => 'Uuid', + 'username' => '$username', + 'first_name' => 'First Name', + 'last_name' => 'Last Name', + 'full_name' => 'Full Name', + 'email' => 'E-Mail Address', + 'phone' => 'Phone Number', + 'group' => 'User Group', + 'language' => 'Language', + 'timezone' => 'Timezone', + 'currency' => 'Currency', + 'created_at' => 'Date Created', + 'reg_ip' => 'Registration IP', + 'reg_user_agent' => 'Registration User Agent', + 'reg_country' => 'Registration Country', + 'reg_province' => 'Registration Province', + 'reg_city' => 'Registration City' + ]; + + // Get fields + $obj_fields = $obj->getMergeFields(); + foreach ($obj_fields as $key => $value) { + $fields[$key] = $value; + } + + // Return + return $fields; + } + + /** + * Get sender options + */ + public function getContactOptions(EmailNotificationControllerInterface $obj, string $type, string $selected = ''):string + { + + // Get admin options + $options = $this->admin_profiles->createSelectOptions($selected, true); + + // Add user + $chk = $selected == 'user' ? 'selected="selected"' : ''; + $options .= ""; + + // Check controller senders + $method = $type == 'sender' ? 'getAvailableSenders' : 'getAvailableRecipients'; + if (!$obj_contacts = $obj->$method()) { + return $options; + } + + // Add controller contacts + foreach ($obj_contacts as $alias => $name) { + $chk = $selected == $alias ? 'selected="selected"' : ''; + $options .= ""; + } + + // Return + return $options; + } + + /** + 8 Create new notification + */ + public function create(array $post = [], int $notification_id = 0):?EmailNotification + { + + // Get post fields + if (count($post) == 0) { + $post = $this->app->getAllPost(); + + // Validate forms + if (!$this->validator->validateForm('webapp.notification_message', $post)) { + return null; + } elseif (!$this->validator->validateForm('webapp.notification_condition', $post)) { + return null; + } + } + + // Gather condition + $condition = []; + foreach ($post as $key => $value) { + if (!preg_match("/^cond_(.+)$/", $key, $m)) { + continue; + } + $condition[$m[1]] = $value; + } + + // Create notifiation + $email = EmailNotification::insertOrUpdate( + ['id' => $notification_id], + [ + 'controller' => $post['controller'], + 'sender' => $post['sender'], + 'recipient' => $post['recipient'], + 'reply_to' => $post['reply_to'] ?? '', + 'cc' => $post['cc'] ?? '', + 'bcc' => $post['bcc'], + 'subject' => $post['subject'], + 'text_contents' => $post['text_contents'], + 'html_contents' => $post['html_contents'] ?? '', + 'condition_vars' => json_encode($condition) + ]); + + // Add attachment, if needed + if ($this->app->hasFile('attachment')) { + EmailAttachment::insert([ + 'notification_id' => $email->id, + 'filename' => $this->app->fileName('attachment'), + 'filesize' => filesize($this->app->fileTmpName('attachment')), + 'contents' => base64_encode($this->app->fileContents('attachment')) + ]); + } + + // Return + return $email; + } + +} +
Full Code
<?php declare(strict_types = 1);
namespace App\Webapp\Notifications;
use Apex\Svc\App; use App\Webapp\AdminProfiles; use App\Webapp\Models{EmailNotification, EmailAttachment}; use Apex\App\Base\Web\Utils\FormValidator; use Apex\App\Interfaces\EmailNotificationControllerInterface;
/** * Notifications config */ class NotificationsConfig {
#[Inject(AdminProfiles::class)]
private AdminProfiles $admin_profiles;
#[Inject(FormValidator::class)]
private FormValidator $validator;
#[Inject(App::class)]
private App $app;
/**
* Get merge field options
*/
public function getMergeFieldOptions(EmailNotificationControllerInterface $obj):string
{
// Get merge fields
$fields = $this->getMergeFields($obj);
// Create options
$html = '';
foreach ($fields as $title => $vars) {
$html .= "<optgroup label=\"$title\">\n";
foreach ($vars as $key => $name) {
$html .= " <option value=\"$key\">$name</option>\n";
}
$html .= "</optgroup>\n";
}
// Return
return $html;
}
/**
* Get merge fields
*/
public function getMergeFields(EmailNotificationControllerInterface $obj):array
{
// Get system fields
$fields = [];
$fields['System'] = [
'site_name' => 'Site Name',
'domain_name' => 'Domain Name',
'install_url' => 'Install URL',
'current_date' => 'Current Date',
'current_time' => 'Current Time',
'ip_address' => 'IP Address',
'user_agent' => 'User Agent'
];
// Profile fields
$fields['Profile'] = [
'uuid' => 'Uuid',
'username' => '$username',
'first_name' => 'First Name',
'last_name' => 'Last Name',
'full_name' => 'Full Name',
'email' => 'E-Mail Address',
'phone' => 'Phone Number',
'group' => 'User Group',
'language' => 'Language',
'timezone' => 'Timezone',
'currency' => 'Currency',
'created_at' => 'Date Created',
'reg_ip' => 'Registration IP',
'reg_user_agent' => 'Registration User Agent',
'reg_country' => 'Registration Country',
'reg_province' => 'Registration Province',
'reg_city' => 'Registration City'
];
// Get fields
$obj_fields = $obj->getMergeFields();
foreach ($obj_fields as $key => $value) {
$fields[$key] = $value;
}
// Return
return $fields;
}
/**
* Get sender options
*/
public function getContactOptions(EmailNotificationControllerInterface $obj, string $type, string $selected = ''):string
{
// Get admin options
$options = $this->admin_profiles->createSelectOptions($selected, true);
// Add user
$chk = $selected == 'user' ? 'selected="selected"' : '';
$options .= "<option value=\"user\" $chk>User</option>";
// Check controller senders
$method = $type == 'sender' ? 'getAvailableSenders' : 'getAvailableRecipients';
if (!$obj_contacts = $obj->$method()) {
return $options;
}
// Add controller contacts
foreach ($obj_contacts as $alias => $name) {
$chk = $selected == $alias ? 'selected="selected"' : '';
$options .= "<option value=\"$alias\" $chk>$name</option>";
}
// Return
return $options;
}
/**
8 Create new notification
*/
public function create(array $post = [], int $notification_id = 0):?EmailNotification
{
// Get post fields
if (count($post) == 0) {
$post = $this->app->getAllPost();
// Validate forms
if (!$this->validator->validateForm('webapp.notification_message', $post)) {
return null;
} elseif (!$this->validator->validateForm('webapp.notification_condition', $post)) {
return null;
}
}
// Gather condition
$condition = [];
foreach ($post as $key => $value) {
if (!preg_match("/^cond_(.+)$/", $key, $m)) {
continue;
}
$condition[$m[1]] = $value;
}
// Create notifiation
$email = EmailNotification::insertOrUpdate(
['id' => $notification_id],
[
'controller' => $post['controller'],
'sender' => $post['sender'],
'recipient' => $post['recipient'],
'reply_to' => $post['reply_to'] ?? '',
'cc' => $post['cc'] ?? '',
'bcc' => $post['bcc'],
'subject' => $post['subject'],
'text_contents' => $post['text_contents'],
'html_contents' => $post['html_contents'] ?? '',
'condition_vars' => json_encode($condition)
]);
// Add attachment, if needed
if ($this->app->hasFile('attachment')) {
EmailAttachment::insert([
'notification_id' => $email->id,
'filename' => $this->app->fileName('attachment'),
'filesize' => filesize($this->app->fileTmpName('attachment')),
'contents' => base64_encode($this->app->fileContents('attachment'))
]);
}
// Return
return $email;
}
}