Skip to content

Commit

Permalink
Applications now send email notices on save
Browse files Browse the repository at this point in the history
Liaisons should receive an email each time their board
receives a new application.

Fixes #196
  • Loading branch information
inghamn committed Dec 8, 2017
1 parent 40eb34c commit eb3a3ac
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 3 deletions.
16 changes: 16 additions & 0 deletions Controllers/ApplicantsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public function apply()
}
$zend_db->getDriver()->getConnection()->commit();

$this->notifyLiaisons($applicant);
$this->template->blocks[] = new Block('applicants/success.inc', ['applicant'=>$applicant]);

return;
Expand Down Expand Up @@ -163,4 +164,19 @@ public function delete()
$this->template->blocks[] = new Block('404.inc');
}
}

private function notifyLiaisons(Applicant $applicant)
{
foreach ($applicant->getApplications() as $a) {
$people = $a->getPeopleToNotify();
if (count($people)) {
$b = new Block('liaisons/applicationNotification.inc', ['application'=>$a]);
$message = $b->render('txt', $this->template);
$subject = sprintf($this->template->_('board_application_subject', 'messages'), $a->getCommittee()->getName());
foreach ($people as $p) {
$p->sendNotification($message, $subject);
}
}
}
}
}
8 changes: 6 additions & 2 deletions Models/Applicant.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* @copyright 2016 City of Bloomington, Indiana
* @copyright 2016-2017 City of Bloomington, Indiana
* @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt
*/
namespace Application\Models;
Expand Down Expand Up @@ -140,6 +140,11 @@ public function handleUpdate(array $post)
//----------------------------------------------------------------
// Custom Functions
//----------------------------------------------------------------
public function getFullname() : string
{
return "{$this->getFirstname()} {$this->getLastname()}";
}

/**
* Applications for this applicant
*
Expand Down Expand Up @@ -193,5 +198,4 @@ public function getFiles()
}
return $files;
}

}
18 changes: 17 additions & 1 deletion Models/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,20 @@ public function getExpires($format=null)
$expires->add(new \DateInterval("P{$d}D"));
return $expires->format($format);
}
}

public function getPeopleToNotify() : array
{
$people = [];

$sql = "select p.*
from people p
join liaisons l on p.id=l.person_id
join applications a on l.committee_id=a.committee_id
where a.id=?";
$zend_db = Database::getConnection();
$result = $zend_db->createStatement($sql)->execute([$this->getId()]);
foreach ($result as $p) { $people[] = new Person($p); }

return $people;
}
}
18 changes: 18 additions & 0 deletions Models/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,22 @@ public function isSafeToDelete()
$result = $zend_db->query($sql)->execute();
return count($result) ? false : true;
}

public function sendNotification(string $message, string $subject=null, string $replyTo=null)
{
$to = $this->getEmail();
if ($to) {
if (!$subject) {
$subject = APPLICATION_NAME.' Notification';
}
$name = preg_replace('/[^a-zA-Z0-9]+/','_',APPLICATION_NAME);
$fromEmail = "$name@".BASE_HOST;
$fromFullname = APPLICATION_NAME;

$message = mb_convert_encoding($message, 'ISO-8859-1', 'UTF-8');
$from = "From: $fromFullname <$fromEmail>";
if ($replyTo) { $from.="\r\nReply-to: $replyTo"; }
mail($to, $subject, $message, $from, '-f'.ADMINISTRATOR_EMAIL);
}
}
}
1 change: 1 addition & 0 deletions ansible/group_vars/all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ onboard_google:

onboard_directory_service: ""
onboard_cas_host: ""
onboard_admin_email: ""
...
1 change: 1 addition & 0 deletions ansible/templates/site_config.j2
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ define('LOCALE', 'en_US');

define('TERM_END_WARNING_DEFAULT', 60);
define('APPLICATION_LIFETIME_DEFAULT', 90);
define('ADMINISTRATOR_EMAIL', '{{ onboard_admin_email }}');
19 changes: 19 additions & 0 deletions blocks/txt/liaisons/applicationNotification.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* @copyright 2017 City of Bloomington, Indiana
* @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt
* @param Application $this->application
*/
declare (strict_types=1);

$committee = $this->application->getCommittee()->getName();
$committee_id = $this->application->getCommittee_id();
$person = $this->application->getApplicant()->getFullname();
$message = sprintf($this->template->_('board_application_message', 'messages'), $person, $committee);
$url = BASE_URL.'/committees/applications?committee_id='.$committee_id;

echo "
$message
$url
";
1 change: 1 addition & 0 deletions data/site_config.inc.example
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,4 @@ define('LOCALE', 'en_US');

define('TERM_END_WARNING_DEFAULT', 60);
define('APPLICATION_LIFETIME_DEFAULT', 90);
define('ADMINISTRATOR_EMAIL', 'someone@localhost');
6 changes: 6 additions & 0 deletions language/en_US/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,9 @@ msgstr "Add %s"

msgid "edit_something"
msgstr "Edit %s"

msgid "board_application_subject"
msgstr "%s application received"

msgid "board_application_message"
msgstr "%s has applied for %s"

0 comments on commit eb3a3ac

Please sign in to comment.