Skip to content

Commit

Permalink
Add a stub notifier logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
wadmiraal committed Sep 8, 2014
1 parent fd16944 commit a7e1aaa
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Tudu/Controller/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function inboxAction(Application $app, Request $request)

$todo->save();

Notifier::notify($todo);
Notifier::notify($todo, $app);

return new Response('Todo list created, [id:' . $todo->getID() . ']', 201);
break;
Expand Down Expand Up @@ -123,7 +123,7 @@ public function inboxAction(Application $app, Request $request)

$todo->save();

Notifier::notify($todo);
Notifier::notify($todo, $app);

return new Response('Todo list updated, [id:' . $todo->getID() . ']', 200);
break;
Expand Down
55 changes: 52 additions & 3 deletions src/Tudu/Util/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,63 @@ class Notifier
*
* @param Tudu\Model\Todo $todo
* The todo list model.
* @param \Silex\Application $app
* The application.
*/
public static function notify($todo)
public static function notify(\Tudu\Model\Todo $todo, \Silex\Application $app)
{
// $my_html = Markdown::defaultTransform($my_text);
$tasks = array();
$doneTasks = array();
$conf = $app['conf'];

foreach ($todo->getTasks() as $task) {
$taskDescription = preg_replace('/\[.*(assigned to|due)\s*:\s*([^\]]+)\]/', '', $task->getTask());
$taskDescription = Markdown::defaultTransform($taskDescription);

if ($task->getDone()) {
$doneTasks[$task->getNum()] = $taskDescription;
} else {
$tasks[$task->getNum()] = $taskDescription;
}
}

$body = $todo->getDescription();

$body .= "\n\n";

foreach ($tasks as $num => $task) {
$body .= "[$num] $task\n";
}

foreach ($tasks as $num => $task) {
$body .= "--[$num] $task--\n";
}

$from = $conf['tudu.emails.update'];
if (!empty($conf['tudu.emails.names'])) {
$name = array_rand($conf['tudu.emails.names']);
$from = "$name <$from>";
}

if (!empty($conf['tudu.emails.signatures.html'])) {
$body .= "\n\n" . array_rand($conf['tudu.emails.signatures.html']);
}

// Skip sending email if we're in the development environment.
if (!(defined('APP_ENV') && APP_ENV === 'development')) {
mail();
foreach ($todo->getParticipants() as $participant) {
mail(
$participant->getEmail(),
$todo->getTitle() . '[id:' . $todo->getID() . ']',
$body,
implode("\r\n", array(
'From: ' . $from,
'In-Reply-To: ' . $participant->getLastMessageID(),
))
);
}
}

return $body;
}
}

0 comments on commit a7e1aaa

Please sign in to comment.