Skip to content

Commit

Permalink
Unscheduled notification sending is working
Browse files Browse the repository at this point in the history
Updates #151
  • Loading branch information
inghamn committed Jun 27, 2018
1 parent 02b1757 commit 1cb25c2
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 25 deletions.
9 changes: 9 additions & 0 deletions blocks/html/events/info.inc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use Application\Models\Event;
use Application\Models\EventType;
use Application\Auth;

use Domain\Notifications\Metadata as Notification;

$title = parent::escape($this->event->getTitle());
$primaryContact = parent::escape($this->event->getPrimaryContact());
$description = parent::escape($this->event->getDescription());
Expand Down Expand Up @@ -44,6 +46,13 @@ if ($t) {
'history'
);
}
if (Auth::isAllowed('notifications', 'send')) {
echo $helper->buttonLink(
BASE_URI."/notifications/send?event_id=$id;type=".Notification::TYPE_EMERGENCY,
$this->_('notify'),
'notify'
);
}
if (Auth::isAllowed('events', 'delete')) {
$uri = BASE_URI.'/events/delete?id='.$id;
echo "
Expand Down
30 changes: 30 additions & 0 deletions blocks/html/notifications/sendForm.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* @copyright 2018 City of Bloomington, Indiana
* @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt
* @param Event $this->event
* @param string $this->type
* @param string $this->comments
*/
declare (strict_types=1);
use Blossom\Classes\Block;
use Blossom\Classes\Template;
?>
<section>
<form method="post">
<fieldset>
<pre>
<?php
$template = new Template('default', 'txt');
$block = new Block("notifications/{$this->type}.inc", ['event'=>$this->event]);
echo $block->render('txt', $template);
?>
</pre>
<input name="event_id" type="hidden" value="<?= $this->event->getId(); ?>" />
<input name="type" type="hidden" value="<?= $this->type; ?>" />

<button type="submit" class="notify"><?= $this->_('send'); ?></button>
<a class="cancel button" href="<?= BASE_URI; ?>/events/view?id=<?= $this->event->getId(); ?>"><?= $this->_('cancel'); ?></a>
</fieldset>
</form>
</section>
File renamed without changes.
9 changes: 9 additions & 0 deletions blocks/txt/notifications/updates.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
/**
* @copyright 2018 City of Bloomington, Indiana
* @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt
* @param Event $this->event
*/
declare (strict_types=1);
$this->_include('events/partials/emailHeader.inc');
$this->_include('events/info.inc');
1 change: 1 addition & 0 deletions public/css/images/fa-svg/white/envelope.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/css/sass-modules/_buttons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ button, .button {
&.submit { background-image:url('images/fa-svg/white/check.svg' ); padding-left:$imageSpace; }
&.save { background-image:url('images/fa-svg/white/floppy-o.svg' ); padding-left:$imageSpace; }
&.search { background-image:url('images/fa-svg/white/search.svg' ); padding-left:$imageSpace; }
&.notify { background-image:url('images/fa-svg/white/envelope.svg' ); padding-left:$imageSpace; }
&.back { background-image:url('images/fa-svg/white/chevron-left.svg'); padding-left:$imageSpace; }
&.history { background-image:url('images/fa-svg/white/history.svg' ); padding-left:$imageSpace; }
&.calendar { background-image:url('images/fa-svg/white/calendar.svg' ); padding-left:$imageSpace; }
Expand Down
40 changes: 16 additions & 24 deletions src/Application/Controllers/EventsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
use Blossom\Classes\Database;
use Blossom\Classes\Template;

use Domain\Notifications\DataStorage\ZendDbNotificationsRepository;
use Domain\Notifications\UseCases\Find\Find;
use Domain\Notifications\Metadata as Notification;

class EventsController extends Controller
Expand Down Expand Up @@ -141,6 +139,21 @@ public function view(): Template
: new \Application\Views\NotFoundView();
}

public function notify(): Template
{
if (!empty($_GET['id'])) {
try { $event = new Event($_GET['id']); }
catch (\Exception $e) { }
}
if (isset($event)) {
self::sendNotifications($event, Notification::TYPE_EMERGENCY);
$_SESSION['errorMessages'] = ['notification/sent'];
header('Location: '.BASE_URL.'/events/view?id='.$event->getId());
exit();
}
else { return new \Application\Views\NotFoundView(); }
}

public function update(): Template
{
if (!empty($_REQUEST['id'])) {
Expand All @@ -165,7 +178,7 @@ public function update(): Template
$event->handleUpdate($_POST);

if (defined('NOTIFICATIONS_ENABLED') && NOTIFICATIONS_ENABLED) {
self::sendNotifications($event);
NotificationsController::sendNotifications($event, Notification::TYPE_UPDATES);
}

$url = $existingEventId
Expand Down Expand Up @@ -208,25 +221,4 @@ public function history(): Template
? new \Application\Views\Events\HistoryView($event->getHistory())
: new \Application\Views\NotFoundView();
}

public static function sendNotifications(Event $event)
{
$template = new Template('default', 'txt');
$block = new Block('events/notification.inc', ['event'=>$event]);

$message = $block->render('txt', $template);
$subject = sprintf($template->_('notification_subject %s', 'messages'), APPLICATION_NAME);
$name = preg_replace('/[^a-zA-Z0-9]+/','_',APPLICATION_NAME);
$fromEmail = "$name@". BASE_HOST;
$fromFullname = APPLICATION_NAME;

$repo = new ZendDbNotificationsRepository(Database::getConnection());
$find = new Find($repo);
$res = $find(Notification::TYPE_UPDATES);
foreach ($res->notifications as $n) {
$to = $n->email;
$from = "From: $fromFullname <$fromEmail>\r\nReply-to: ".ADMINISTRATOR_EMAIL;
mail($to, $subject, $message, $from, '-f'.ADMINISTRATOR_EMAIL);
}
}
}
46 changes: 46 additions & 0 deletions src/Application/Controllers/NotificationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
declare (strict_types=1);
namespace Application\Controllers;

use Application\Models\Event;
use Application\Views\Notifications\ListView;
use Application\Views\Notifications\Preview;
use Application\Views\Notifications\UpdateView;

use Blossom\Classes\Block;
use Blossom\Classes\Controller;
use Blossom\Classes\Database;
use Blossom\Classes\Template;

use Domain\Notifications\DataStorage\ZendDbNotificationsRepository;
use Domain\Notifications\Metadata as Notification;
use Domain\Notifications\UseCases\Delete\Delete;
Expand Down Expand Up @@ -83,4 +89,44 @@ public function delete()
}
return new \Application\Views\NotFoundView();
}

public function send()
{
if (!empty($_REQUEST['event_id'])) {
try { $event = new \Application\Models\Event($_REQUEST['event_id']); }
catch (\Exception $e) { }
}

if (isset($event) && !empty($_REQUEST['type'])) {
if (!empty($_POST['event_id']) && !empty($_POST['type'])) {
self::sendNotifications($event, $_POST['type']);

header('Location: '.BASE_URL.'/events/view?id='.$event->getId());
exit();
}
return new Preview($event, $_REQUEST['type']);
}
else { return new \Application\Views\NotFoundView(); }
}

public static function sendNotifications(Event $event, string $type)
{
$template = new Template('default', 'txt');
$block = new Block("notifications/$type.inc", ['event'=>$event]);

$message = $block->render('txt', $template);
$subject = sprintf($template->_('notification_subject %s', 'messages'), APPLICATION_NAME);
$name = preg_replace('/[^a-zA-Z0-9]+/','_',APPLICATION_NAME);
$fromEmail = "$name@". BASE_HOST;
$fromFullname = APPLICATION_NAME;

$repo = new ZendDbNotificationsRepository(Database::getConnection());
$find = new Find($repo);
$res = $find($type);
foreach ($res->notifications as $n) {
$to = $n->email;
$from = "From: $fromFullname <$fromEmail>\r\nReply-to: ".ADMINISTRATOR_EMAIL;
mail($to, $subject, $message, $from, '-f'.ADMINISTRATOR_EMAIL);
}
}
}
24 changes: 24 additions & 0 deletions src/Application/Views/Notifications/Preview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @copyright 2018 City of Bloomington, Indiana
* @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt
*/
declare (strict_types=1);
namespace Application\Views\Notifications;

use Application\Models\Event;
use Blossom\Classes\Block;
use Blossom\Classes\Template;

class Preview extends Template
{
public function __construct(Event $event, string $type)
{
parent::__construct('admin', 'html');

$this->blocks[] = new Block('notifications/sendForm.inc', [
'event' => $event,
'type' => $type
]);
}
}
21 changes: 21 additions & 0 deletions src/Application/Views/ValidationErrorView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* @copyright 2018 City of Bloomington, Indiana
* @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt
*/
declare (strict_types=1);
namespace Application\Views;

use Blossom\Classes\Block;
use Blossom\Classes\Template;

class ValidationErrorView extends Template
{
public function __construct(array $errors)
{
header('HTTP/1.1 422 Unprocessable Entity', true, 422);

parent::__construct('admin', 'html', $vars);
$this->blocks[] = new Block('errorMessages.inc', ['errorMessages' => $errors]);
}
}
3 changes: 2 additions & 1 deletion templates/html/partials/panel-widgets/Administrator_menu.inc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ $routes = [
'departments' => 'department',
'eventTypes' => 'eventType',
'people' => 'person',
'users' => 'user'
'users' => 'user',
'notifications' => 'notification'
];
foreach ($routes as $plural=>$singular) {
$requiredAction = 'index';
Expand Down

0 comments on commit 1cb25c2

Please sign in to comment.