Diff #4 - trunk/src/Opus/Forms/NotificationCondition.php
3,136 bytes
|
|
January 20, 2025 at 08:20
|
Diff
Index: NotificationCondition.php
--- NotificationCondition.php (nonexistent) +++ NotificationCondition.php (revision 4) @@ -0,0 +1,116 @@ +<?php +declare(strict_types = 1); + +namespace App\Webapp\Opus\Forms; + +use Apex\Svc{Container, View}; +use App\Webapp\Models\EmailNotification; +use App\Webapp\Notifications\NotificationsConfig; +use Apex\App\Base\Web\Utils\FormBuilder; +use Apex\App\Interfaces\Opus\FormInterface; +use App\Webapp\Exceptions\WebappNotificationException; + +/** + * Form - NotificationCondition + */ +class NotificationCondition implements FormInterface +{ + + /** + * Whether or not to pre-populate form fields with POSTed data. + */ + public bool $allow_post_values = true; + + #[Inject(Container::class)] + private Container $cntr; + + #[Inject(View::class)] + private View $view; + + #[Inject(NotificationsConfig::class)] + private NotificationsConfig $config; + + #[Inject(FormBuilder::class)] + private FormBuilder $creator; + + /** + * Get form fields. + */ + public function getFields(array $attr = []):array + { + + // Initialize + $builder = $this->cntr->make(FormBuilder::class); + list($sender, $recipient, $condition) = ['', '', []]; + $controller = $attr['controller'] ?? ''; + $notification_id = $attr['notification_id'] ?? 0; + + // Check for notification + if ($notification_id > 0 && $email = EmailNotification::whereId($notification_id)) { + $sender = $email->sender; + $recipient = $email->recipient; + $condition = json_decode($email->condition_vars, true); + $controller = $email->controller; + } + + // Load controller class + if (!class_exists($controller)) { + throw new WebappNotificationException("The e-mail notification controller does not exist at, $controller"); + } + $obj = $this->cntr->make($controller); + + // Get sender and recipient options + $sender_options = $this->config->getContactOptions($obj, 'sender', $sender); + $recipient_options = $this->config->getContactOptions($obj, 'recipient', $recipient); + + // Get condition fields + $condition_fields = $obj->getConditionFormFields($builder); + + // Create form fields + $fields = [ + 'sender' => $builder->select()->required()->options($sender_options), + 'recipient' => $builder->select()->required()->options($recipient_options) + ]; + + // Return, if no condition fields + if (count($condition_fields) == 0) { + return $fields; + } + + // Add condition fields, if needed + $fields['sep_condition'] = $builder->seperator('Condition'); + foreach ($condition_fields as $alias => $vars) { + $fields['cond_' . $alias] = $vars; + } + + // Return + return $fields; + } + + /** + * Get record + */ + public function getRecord(string $record_id):array + { + + // Get row + if (!$row = $this->db->getIdRow('notification_condition', $record_id)) { + $row = []; + } + + // Return + return $row; + } + + /** + * Validate + */ + public function validate(array $attr = []):bool + { + + // Return + return true; + } + +} +
Full Code
<?php declare(strict_types = 1);
namespace App\Webapp\Opus\Forms;
use Apex\Svc{Container, View}; use App\Webapp\Models\EmailNotification; use App\Webapp\Notifications\NotificationsConfig; use Apex\App\Base\Web\Utils\FormBuilder; use Apex\App\Interfaces\Opus\FormInterface; use App\Webapp\Exceptions\WebappNotificationException;
/** * Form - NotificationCondition */ class NotificationCondition implements FormInterface {
/**
* Whether or not to pre-populate form fields with POSTed data.
*/
public bool $allow_post_values = true;
#[Inject(Container::class)]
private Container $cntr;
#[Inject(View::class)]
private View $view;
#[Inject(NotificationsConfig::class)]
private NotificationsConfig $config;
#[Inject(FormBuilder::class)]
private FormBuilder $creator;
/**
* Get form fields.
*/
public function getFields(array $attr = []):array
{
// Initialize
$builder = $this->cntr->make(FormBuilder::class);
list($sender, $recipient, $condition) = ['', '', []];
$controller = $attr['controller'] ?? '';
$notification_id = $attr['notification_id'] ?? 0;
// Check for notification
if ($notification_id > 0 && $email = EmailNotification::whereId($notification_id)) {
$sender = $email->sender;
$recipient = $email->recipient;
$condition = json_decode($email->condition_vars, true);
$controller = $email->controller;
}
// Load controller class
if (!class_exists($controller)) {
throw new WebappNotificationException("The e-mail notification controller does not exist at, $controller");
}
$obj = $this->cntr->make($controller);
// Get sender and recipient options
$sender_options = $this->config->getContactOptions($obj, 'sender', $sender);
$recipient_options = $this->config->getContactOptions($obj, 'recipient', $recipient);
// Get condition fields
$condition_fields = $obj->getConditionFormFields($builder);
// Create form fields
$fields = [
'sender' => $builder->select()->required()->options($sender_options),
'recipient' => $builder->select()->required()->options($recipient_options)
];
// Return, if no condition fields
if (count($condition_fields) == 0) {
return $fields;
}
// Add condition fields, if needed
$fields['sep_condition'] = $builder->seperator('Condition');
foreach ($condition_fields as $alias => $vars) {
$fields['cond_' . $alias] = $vars;
}
// Return
return $fields;
}
/**
* Get record
*/
public function getRecord(string $record_id):array
{
// Get row
if (!$row = $this->db->getIdRow('notification_condition', $record_id)) {
$row = [];
}
// Return
return $row;
}
/**
* Validate
*/
public function validate(array $attr = []):bool
{
// Return
return true;
}
}