Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email dev #67

Merged
merged 4 commits into from
Oct 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@
[submodule "KO3-Event"]
path = modules/KO3-Event
url = git://github.com/thinkclay/KO3-Event.git
[submodule "modules/email"]
path = modules/email
url = git://github.com/shadowhand/email.git
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ To install the platform on your computer/server, the target system must meet the

1. Open a terminal
2. Clone the project ```git clone https://github.com/ushahidi/pingapp.git```
3. Update the submodules ```git submodule update --init```
3. Update the submodules ```git submodule update --init --recursive```
4. Install the Twilio packages using [Composer](http://getcomposer.org) by running ```composer install```.


Expand Down
93 changes: 65 additions & 28 deletions application/classes/Controller/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ class Controller_Messages extends Controller_PingApp {
* @var array
*/
private $_errors = array();

private $_post = NULL;

/**
* Creates a new message
* @todo move sending to helper functions
*/
public function action_new()
{
$this->template->content = View::factory('pages/send')
$this->template->content = View::factory('pages/messages/new')
->bind('user', $this->user)
->bind('post', $post)
->bind('post', $this->_post)
->bind('errors', $this->_errors)
->bind('done', $done);

$this->template->footer->js = View::factory('pages/js/send');
$this->template->footer->js = View::factory('pages/messages/js/new');

if ($this->request->method() === 'POST')
{
Expand All @@ -45,6 +47,8 @@ public function action_new()
*/
private function _broadcast_message()
{
$this->_post = $this->request->post();

// Get the SMS provider to use
try
{
Expand All @@ -64,6 +68,20 @@ private function _broadcast_message()
$this->_errors[] = 'No recipients selected';
return FALSE;
}

// Send Type Included?
if ( ! $this->request->post('type') OR ! is_array($this->request->post('type')) )
{
$this->_errors[] = 'No message type selected';
return FALSE;
}

// Type is Email or SMS?
if ( ! in_array('email', $this->request->post('type')) AND ! in_array('sms', $this->request->post('type')))
{
$this->_errors[] = 'Message type must be SMS or Email';
return FALSE;
}

// If EVERYONE is selected, ignore the others
$operator = 'IN';
Expand All @@ -73,7 +91,6 @@ private function _broadcast_message()
$recipients = 0;
}
$contacts = ORM::factory('Contact')
->where('type', '=', 'phone')
->join('contacts_people')->on('contact.id', '=', 'contacts_people.contact_id')
->join('people')->on('people.id', '=', 'contacts_people.person_id')
->where('people.user_id', '=', $this->user->id)
Expand All @@ -82,37 +99,57 @@ private function _broadcast_message()

if ( ! $contacts->count() )
{
$this->_errors[] = 'None of your recipients have phone numbers to send to';
$this->_errors[] = 'No recipients to send to';
return FALSE;
}

// Create the message
$message = ORM::factory('Message');
try
{
// Set values and save
$message->values(array(
'message' => $this->request->post('message'),
'user_id' => $this->user->id,
'type' => 'sms'
));
$message->save();

// Save Ping
foreach ($contacts as $contact)
foreach ($this->request->post('type') as $type)
{
$ping = ORM::factory('Ping');
$ping->values(array(
'message_id' => $message->id,
'tracking_id' => '0',
'type' => 'sms',
'contact_id' => $contact->id,
'provider' => strtolower(PingApp_SMS_Provider::$sms_provider),
'status' => 'pending',
'sent' => 0
// Create the message
$$type = ORM::factory('Message');

// Set values and save
${$type}->values(array(
'type' => $type,
'message' => $this->request->post('message'),
'title' => $this->request->post('title'),
'user_id' => $this->user->id
));
$ping->save();
}
${$type}->check();
}

foreach ($this->request->post('type') as $type)
{
${$type}->save();

// Save Ping
foreach ($contacts as $contact)
{
$ping = ORM::factory('Ping');
$ping->values(array(
'message_id' => ${$type}->id,
'tracking_id' => '0',
'contact_id' => $contact->id,
'provider' => 0,
'status' => 'pending',
'sent' => 0
));

if ($type == 'sms' AND $contact->type == 'phone')
{
$ping->type = $type;
$ping->save();
}

if ($type == 'email' AND $contact->type == 'email')
{
$ping->type = $type;
$ping->save();
}
}
}
}
catch (ORM_Validation_Exception $e)
{
Expand Down
10 changes: 10 additions & 0 deletions application/classes/Controller/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public function action_email()
$this->_action('email');
}

/**
* Customize Messages
*
* @return void
*/
public function action_customize()
{
$this->_action('customize');
}

/**
* Edit TOS Settings
*
Expand Down
41 changes: 39 additions & 2 deletions application/classes/Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,34 @@ class Model_Message extends ORM {
'pings' => array(),
);

/**
* A message belongs to a user
*/
protected $_belongs_to = array(
'user' => array()
);

// Insert/Update Timestamps
protected $_created_column = array('column' => 'created', 'format' => 'Y-m-d H:i:s');
protected $_updated_column = array('column' => 'updated', 'format' => 'Y-m-d H:i:s');

public function rules()
{
return array(
'title' => array(
array('min_length', array(':value', 2)),
array('max_length', array(':value', 120)),
array(array($this, 'valid_title'), array(':validation', ':field')),
),
'message' => array(
array('not_empty'),
array('min_length', array(':value', 2)),
array('max_length', array(':value', 140)),
)
array('max_length', array(':value', 120)),
),
'type' => array(
array('not_empty'),
array('in_array', array(':value', array('sms', 'email', 'twitter')) ),
),
);
}

Expand All @@ -40,9 +56,30 @@ public function rules()
public function filters()
{
return array(
'title' => array(
array('trim'),
),
'message' => array(
array('trim'),
),
);
}

/**
* Validate Message Against Message Type
*
* @param array $validation
* @param string $field field name
* @param [type] [varname] [description]
* @return void
*/
public function valid_title($validation, $field)
{
// Valid Email?
if ( isset($validation['type']) AND
$validation['type'] == 'email' AND ! $validation[$field] )
{
$validation->error($field, 'invalid_title');
}
}
}
80 changes: 78 additions & 2 deletions application/classes/PingApp/Ping.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ private static function _sms($ping, $contact)
Kohana::$log->add(Log::ERROR, $e->getMessage());
}

if (($tracking_id = $provider->send($contact->contact, $ping->message->message)) !== FALSE)
$tagline = PingApp_Settings::get('message_sms');

if (($tracking_id = $provider->send($contact->contact, $ping->message->message.' '.$tagline)) !== FALSE)
{
$ping->tracking_id = $tracking_id;
$ping->provider = $provider::$sms_provider; // Update the provider in case its changed
Expand All @@ -170,6 +172,80 @@ private static function _sms($ping, $contact)

private static function _email($ping, $contact)
{

$driver = PingApp_Settings::get('email_outgoing_type');
$options = array(
'hostname' => PingApp_Settings::get('email_outgoing_host'),
'port' => PingApp_Settings::get('email_outgoing_port'),
'encryption' => (PingApp_Settings::get('email_outgoing_security') != 'none')
? PingApp_Settings::get('email_outgoing_security') : '',
'username' => PingApp_Settings::get('email_outgoing_username'),
'password' => PingApp_Settings::get('email_outgoing_password')
);

$tracking_id = self::_tracking_id();

$config = Kohana::$config->load('email');
$config->set('driver', $driver);
$config->set('options', $options);

$title = $ping->message->title;

$person = $contact->people->order_by('created', 'ASC')->find();

$sender_name = $ping->message->user->first_name.' '.$ping->message->user->last_name;
$sender_email = $ping->message->user->email;
$sender = ($sender_name) ? $sender_name : $sender_email;

$prepend = 'Ping requested by: '.$sender."\n\n";

$body = PingApp_Settings::get('message_email');
$body = str_replace('{{name}}', $person->name, $body);
$body = str_replace('{{message}}', $ping->message->message, $body);
$body = $prepend.$body."\n\n\n".' [{'.$tracking_id.'}]';$body."\n\n\n".' [{'.$tracking_id.'}]';

$from = PingApp_Settings::get('email_from');
$from_name = PingApp_Settings::get('email_from_name');

try
{
$result = Email::factory($title, $body)
->to($contact->contact)
->from($from, $sender)
->send();

$ping->provider = (PingApp_Settings::get('email_outgoing_host')) ? PingApp_Settings::get('email_outgoing_host') : 'email';
$ping->tracking_id = $tracking_id;
$ping->sent = 1;
$ping->save();
}
catch (Exception $e)
{
// Failed
$ping->status = 'failed';
$ping->save();

Kohana::$log->add(Log::ERROR, $e->getMessage());
}
}

private static function _tracking_id()
{
$unique = FALSE;
$code = NULL;
while ( ! $unique)
{
$code = Text::random('alnum', 32);
$ping = ORM::factory('Ping')
->where('type', '=', 'email')
->where('tracking_id', '=', $code)
->find();

if ( ! $ping->loaded() )
{
$unique = TRUE;
}
}

return $code;
}
}
1 change: 1 addition & 0 deletions application/config/modules.template
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ return array(
'minion' => MODPATH.'minion', // CLI Tasks
'migrations' => MODPATH.'migrations', // Minion Migrations
'KO3-Event' => MODPATH.'KO3-Event', // Event Module
'email' => MODPATH.'email', // Email Module
'crowdmapid' => MODPATH.'crowdmapid', // CrowdmapID Authentication
);
15 changes: 15 additions & 0 deletions application/messages/models/message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Messages for Messages
*
* @author Ushahidi Team <[email protected]>
* @package Ushahidi\Application\Messages
* @copyright Ushahidi - http://www.ushahidi.com
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License Version 3 (GPLv3)
*/
return array
(
'title' => array(
'invalid_title' => 'a title is required for email pings',
),
);
25 changes: 25 additions & 0 deletions application/migrations/1/20131008180256.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php defined('SYSPATH') OR die('No direct script access.');

class Migration_1_20131008180256 extends Minion_Migration_Base {

/**
* Run queries needed to apply this migration
*
* @param Kohana_Database $db Database connection
*/
public function up(Kohana_Database $db)
{
$db->query(NULL, "ALTER TABLE `messages` ADD `title` VARCHAR(255) NULL DEFAULT NULL AFTER `user_id`;");
}

/**
* Run queries needed to remove this migration
*
* @param Kohana_Database $db Database connection
*/
public function down(Kohana_Database $db)
{
$db->query(NULL, "ALTER TABLE `messages` DROP `title`;");
}

}
Loading