Skip to content

Commit

Permalink
Event update notifications now get sent on update
Browse files Browse the repository at this point in the history
Fixes #144
  • Loading branch information
inghamn committed May 3, 2018
1 parent 3d242b3 commit 83eb271
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
24 changes: 2 additions & 22 deletions src/Application/Controllers/EventsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Application\Models\Event;
use Application\Models\EventType;
use Application\Models\GoogleGateway;
use Application\Models\Notifications;
use Application\Models\Person;
use Application\Models\PeopleTable;
use Application\Template\Helpers\ButtonLink;
Expand Down Expand Up @@ -148,7 +149,7 @@ public function update()
$event->save();

if (defined('NOTIFICATIONS_ENABLED') && NOTIFICATIONS_ENABLED) {
self::sendNotifications($event, self::getNotificationEmailAddresses());
self::sendNotifications($event, Notifications::emailAddresses(Notifications::TYPE_UPDATES));
}

$url = $existingEventId
Expand Down Expand Up @@ -186,27 +187,6 @@ public function delete()
exit();
}

/**
* Returns an array of email address strings
*
* @return array
*/
public static function getNotificationEmailAddresses(): array
{
global $NOTIFICATIONS_ADDITIONAL_ADDRESSES;

$emailAddresses = isset($NOTIFICATIONS_ADDITIONAL_ADDRESSES)
? $NOTIFICATIONS_ADDITIONAL_ADDRESSES
: [];

$table = new PeopleTable();
$list = $table->find(['notifications'=>true]);
foreach ($list as $p) {
$email = $p->getEmail();
if ($email) { $emailAddresses[] = $email; }
}
}

public static function sendNotifications(Event $event, array $emailAddresses)
{
$template = new Template('default', 'txt');
Expand Down
46 changes: 46 additions & 0 deletions src/Application/Models/Notifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\Models;

class Notifications
{
// These are the flags in the database to look for.
const TYPE_UPDATES = 'notify_updates';
const TYPE_EMERGENCY = 'notify_emergency';

public static $TYPES = [
self::TYPE_UPDATES,
self::TYPE_EMERGENCY
];

/**
* Returns an array of email address strings
*
* @param string $notificationType The notification type
* @return array
*/
public static function emailAddresses(string $notificationType): array
{
global $NOTIFICATIONS_ADDITIONAL_ADDRESSES;

if (!in_array($notificationType, self::$TYPES)) {
throw new \Exception('notifications/unknownType');
}

$emailAddresses = isset($NOTIFICATIONS_ADDITIONAL_ADDRESSES)
? $NOTIFICATIONS_ADDITIONAL_ADDRESSES
: [];

$table = new PeopleTable();
$list = $table->find([$notificationType=>true]);
foreach ($list as $p) {
$email = $p->getEmail();
if ($email) { $emailAddresses[] = $email; }
}
return $emailAddresses;
}
}

1 comment on commit 83eb271

@inghamn
Copy link
Member Author

@inghamn inghamn commented on 83eb271 May 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes #164

Please sign in to comment.