Skip to content

Commit

Permalink
SMS or e-mail notice option per-listenee
Browse files Browse the repository at this point in the history
  • Loading branch information
voitto committed Oct 22, 2008
1 parent 5cba70f commit 2943642
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 123 deletions.
2 changes: 1 addition & 1 deletion app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ env:

zeepAccessKey:
zeepSecretKey:
zeepUrl:
zeepUrl: https://api.zeepmobile.com/messaging/2008-07-14/send_message

ldap_server:
ldap_context:
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ function index( &$vars ) {
);
}

function put( &$vars ) {
extract( $vars );
$resource->update_from_post( $request );
header_status( '200 OK' );
redirect_to( $request->resource );
}

function delete( &$vars ) {
extract( $vars );
Expand Down
1 change: 1 addition & 0 deletions app/models/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function Subscription() {
$this->has_one( 'person' );

$this->let_read( 'all:remove' );
$this->let_modify( 'all:members' );
$this->let_delete( 'all:members' );

$this->set_hidden();
Expand Down
140 changes: 26 additions & 114 deletions app/plugins/email_notice.php
Original file line number Diff line number Diff line change
@@ -1,136 +1,48 @@
<?php

// send an e-mail when content changes
after_filter( 'send_email_notice', 'insert_from_post' );
after_filter( 'send_email_notice', 'update_from_post' );
after_filter( 'send_email_notice', 'delete_from_post' );
after_filter( 'broadcast_email_notice', 'insert_from_post' );


function send_email_notice( &$model, &$rec ) {
function broadcast_email_notice( &$model, &$rec ) {

global $db;
global $request;
if (!(isset($rec->title)))
return;

// get data modesl for 3 tables
$Entry =& $db->get_table('entries');
$Group =& $db->get_table( 'groups' );
$Person =& $db->get_table( 'people' );
global $request, $db;

// load the first 20 records from the groups table
$Group->find();
$i = get_profile();

// keep a list of people we have notified
$sent_to = array();

// get the name of the table from the data model reference we received
$notify_table = $model->table;
$Subscription = $db->model('Subscription');

// get the primary key value of the record reference we received
$notify_id = $rec->id;
$Subscription->has_one( 'subscriber:identity' );

// if the table that was modified is a metadata table (comments, reviews)
// notify about the "target" table being modified
if (array_key_exists( 'target_id', $model->field_array )) {
$e = $Entry->find($rec->attributes['target_id']);
if ($e) {
$notify_table = $e->resource;
$notify_id = $e->record_id;
}
}
$where = array(
'subscriptions.subscribed'=>$i->id,
);

// get the data model we are notifying about
$datamodel =& $db->get_table($notify_table);
$Subscription->set_param( 'find_by', $where );

// get the profile data for the current user
$profile = get_profile();
$Subscription->find();

// loop over each group
while ($g = $Group->MoveNext()) {
if (in_array($g->name,array('administrators','everyone','members'))) continue;
while ($sub = $Subscription->MoveNext()) {

// if the GROUP has READ or CREATE then do notify its members
if ($rec->id && (in_array($g->name,$datamodel->access_list['read']['id']) ||
in_array($g->name,$datamodel->access_list['create'][$notify_table]))) {
$sid = $sub->FirstChild('identities');

if (!in_array($sid->id,$sent_to) && $sub->email) {

$html = false;
// this is the body of the e-mail if ($html == false)
$text = $rec->title;

$subject = $i->nickname . " posted a notice";

// loop over each member in the group
while ( $m = $g->NextChild( 'memberships' ) ) {

// get a person activerecord object for the member's person_id
$p = $Person->find( $m->person_id );

if ($p) {

$action = $request->action;

$notify = "notify_".$action;

// get an identities activerecord object for the person's first identity
// this is an example of traversing the result dataset without re-querying
$i = $p->FirstChild( 'identities' );

// if we haven't already sent this person a message
if (isset($m->$notify) && ($m->$notify) && is_email($i->email_value) && !(in_array($i->email_value, $sent_to))) {

// a token may be set to allow the notify-ee to "EXPRESS" register as a new site user
// it fills in some of the "new user" form info such as e-mail address for them
if (isset($i->token) && strlen($i->token) > 0)
$addr = $request->url_for(array('resource'=>$notify_table,'id'=>$notify_id,'ident'=>$i->token));
else
$addr = $request->url_for(array('resource'=>$notify_table,'id'=>$notify_id));

// this is the HTML content of the e-mail
$html = '
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
<html>
<body>
<br>
<b><u><i>Click on this link:</i></u></b><br>
<br>
<font color="red"><a href="'
.$addr.
'">'.$addr.'</a></font>
</body>
</html>';

// oh wait, we are not going to send the HTML it is just wasting space for now
// comment this out to try the HTML yourself
$html = false;

// this is the body of the e-mail if ($html == false)
$text = 'Content was updated at the following location:'."\r\n\r\n".$addr."\r\n\r\n";

// change the e-mail subject line depending on what action took place
if ($action == 'post')
$actionmessage = " created a new ";
elseif ($action == 'put')
$actionmessage = " updated a ";
elseif ($action == 'delete')
$actionmessage = " deleted a ";

// set the e-mail subject to the current user's first name
// classify() converts a table name "nerds" to "Nerd"
// the converse is tableize()
$subject = $profile->given_name.$actionmessage.classify($request->resource);

// this sends e-mail using the xpertmailer package
// the environment() function reads a value from the config.yml file
send_email( $i->email_value, $subject, $text, environment('email_from'), environment('email_name'), $html );

// add a new entry to the list of successful (more like woeful) recipients
$sent_to[] = $i->email_value;

}

}

}
send_email( $sid->email_value, $subject, $text, environment('email_from'), environment('email_name'), $html );

$sent_to[] = $sid->id;

}

}

}



?>
28 changes: 27 additions & 1 deletion app/plugins/sms_notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,32 @@

function mobile_event( &$vars ) {

$response = "Welcome to ".environment('site_title');
if (!($_SERVER['REMOTE_ADDR'] == '204.244.102.2'))
trigger_error('Sorry but your IP address is unlike the Zeep server',E_USER_ERROR);

extract($vars);

$Post =& $db->model('Post');
$Identity =& $db->model('Identity');

if ($_POST['event'] == 'MO') {

// Array ( [sms_prefix] => omb__ [short_code] => 88147 [uid] => 243132
// [body] => test from deep [min] => +15035554444 [event] => MO )

$p = $Post->base();
$p->set_value( 'profile_id', $_POST['uid'] );
$p->set_value( 'parent_id', 0 );
$p->set_value( 'title', $_POST['body'] );
$p->save_changes();
$i = $Identity->find($_POST['uid']);
if ($i)
$p->set_etag($i->person_id);
$response = "";

} else {
$response = "Welcome to ".environment('site_title');
}

$header = array(
"Status: 200 OK",
Expand Down Expand Up @@ -111,6 +136,7 @@ function broadcast_sms_notice( &$model, &$rec ) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters );
$response = curl_exec($ch);
echo $response; exit;
curl_close($ch);

}
Expand Down
2 changes: 1 addition & 1 deletion app/plugins/wp.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ function add_management_page( $page,$menu,$access,$file,$func='',$url='' ) {

function get_bloginfo( $var ) {
global $blogdata;
if (in_array($var,array('wpurl','url')))
if (in_array($var,array('wpurl')))
if (isset($blogdata[$var]))
if ("/" == substr($blogdata[$var],-1))
return substr($blogdata[$var],0,-1);
Expand Down
59 changes: 53 additions & 6 deletions app/views/identities/_subs.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@
?>


<h2>Friends</h2>




<h2>Subscriptions:</h2>
<h4>(click on a subscription to remove it)</h4>
<br />

<?php $Subscription->find_by('subscriber',$Member->id); ?>
Expand All @@ -22,8 +18,59 @@ <h4>(click on a subscription to remove it)</h4>
<?php if (empty($subentry->etag)) $subscriber->set_etag(); ?>
<?php if ($subscriber->subscriber == $Member->id) : ?>
<?php $si = $Identity->find( $subscriber->subscribed ); ?>
<a href="<?php echo $request->url_for(array('resource'=>'subscriptions','id'=>$subscriber->id,'action'=>'remove')); ?>"><img style="border:none;width:48px;height:48px;" src="<?php echo $si->avatar; ?>"></a>

<h3><?php echo $si->nickname; ?>
<?php if (!empty($si->fullname)) : ?>
&nbsp;(<?php echo $si->fullname; ?>)
<?php endif; ?></h3>

<p><a style="hover:none;" href="<?php echo $request->url_for(array('resource'=>$si->nickname)); ?>"><img style="border:none;width:48px;height:48px;" src="<?php echo $si->avatar; ?>"></a>&nbsp;&nbsp;&nbsp;&nbsp;SMS&nbsp;<input name="do_sms<?php echo $subscriber->id; ?>" id="do_sms<?php echo $subscriber->id; ?>" type="checkbox" <?php if ($subscriber->sms) echo "checked"; ?>>&nbsp;&nbsp;&nbsp;&nbsp;E-mail&nbsp;<input name="do_email<?php echo $subscriber->id; ?>" id="do_email<?php echo $subscriber->id; ?>" type="checkbox" <?php if ($subscriber->email) echo "checked"; ?>>&nbsp;&nbsp;&nbsp;&nbsp;<a style="hover:none;" href="<?php echo $request->url_for(array('resource'=>'subscriptions','id'=>$subscriber->id,'action'=>'remove')); ?>"><img style="border:none;width:16px;height:16px;" src="resource/block.gif"></a></p>


<script type="text/javascript">
$(document).ready(function(){

$("#do_sms<?php echo $subscriber->id; ?>").click(function(e){
var submitdata = {};
if (!$("#do_sms<?php echo $subscriber->id; ?>").attr('checked')) {
submitdata['subscription[sms]'] = 'false';
} else {
submitdata['subscription[sms]'] = 'true';
}
submitdata['entry[etag]'] = '<?php echo $subentry->etag; ?>';
submitdata['method'] = 'put';
var someurl = "<?php url_for(array(
'resource'=>'subscriptions'
)); ?>";
$.post(someurl, submitdata, function(str) {
//alert(str);
});
});

$("#do_email<?php echo $subscriber->id; ?>").click(function(e){
var submitdata = {};
if (!$("#do_email<?php echo $subscriber->id; ?>").attr('checked')) {
submitdata['subscription[email]'] = 'false';
} else {
submitdata['subscription[email]'] = 'true';
}
submitdata['entry[etag]'] = '<?php echo $subentry->etag; ?>';
submitdata['method'] = 'put';
var someurl = "<?php url_for(array(
'resource'=>'subscriptions'
)); ?>";
$.post(someurl, submitdata, function(str) {
//alert(str);
});
});

});
</script>

<br />

<?php endif; ?>

<?php endwhile; ?>


0 comments on commit 2943642

Please sign in to comment.