/
subscriptions.class.php
231 lines (211 loc) · 7.63 KB
/
subscriptions.class.php
1
<?php// to do - // initialize subscription table from User table// add entry to subscription table when User table entry created// Subscriptions is the client advertising code that are placed on the sitedefine ("SUBSCRIPTIONS_FEATURE_INTERVAL",3);require_once(PATH_CORE .'/classes/dbRowObject.class.php');class Subscriptions extends dbRowObject{}class SubscriptionsTable{ /////////////////////////////////////////////////////////////////////////////////////////////////////// // standard table fields var $db; static $tablename="Subscriptions"; static $idname = "id"; static $idtype = "INT(11) unsigned NOT NULL auto_increment"; static $dbRowObjectClass = "Subscriptions"; static $fields = array( "userid" => "BIGINT(20) default 0", "rxFeatures" => "TINYINT(1) default 0", "rxMode" => "ENUM('notification','sms','email') default 'notification'", "lastFeatureSent" => "DATETIME" ); static $keydefinitions = array(); /////////////////////////////////////////////////////////////////////////////////////////////////////// // standard table functions function __construct(&$db=NULL) { if (is_null($db)) { require_once('db.class.php'); $this->db=new cloudDatabase(); } else $this->db=$db; } // although many functions will be duplicated between table subclasses, having a parent class gets too messy function getRowObject() { $classname = self::$dbRowObjectClass; return new $classname($this->db, self::$tablename, array_keys(self::$fields), self::$idname); } // generic table creation routine, same for all *Table classes static function createTable($manageObj) { $manageObj->addTable(self::$tablename,self::$idname,self::$idtype,"MyISAM"); foreach (array_keys(self::$fields) as $key) { $manageObj->updateAddColumn(self::$tablename,$key,self::$fields[$key]); } foreach (self::$keydefinitions as $keydef) { $manageObj->updateAddKey(self::$tablename,$keydef[0], $keydef[1], $keydef[2], $keydef[3]); } } function checkExists($userid) { $chkDup=$this->db->queryC("SELECT ".self::$idname." FROM ".self::$tablename." WHERE userid=$userid;"); return $chkDup; } }class SubscriptionsManager { var $facebook; var $cObj; function __construct(&$db=NULL) { if (is_null($db)) { require_once(PATH_CORE.'/classes/db.class.php'); $this->db=new cloudDatabase(); } else $this->db=$db; require_once(PATH_CORE.'/classes/content.class.php'); $this->cObj=new content($this->db); } function loadFacebook(&$facebook){ $this->facebook =&$facebook; } function populateSubscriptions() { $subTable = new SubscriptionsTable($this->db); $sub = $subTable->getRowObject(); $sub->rxFeatures=1; $sub->rxMode='notification'; // query gets all user table entries that aren't in subscriptions table - never should happen any more now that create is hooked into session class $q=$this->db->query("SELECT User.userid FROM User LEFT JOIN Subscriptions ON User.userid=Subscriptions.userid WHERE Subscriptions.userid IS NULL"); while ($data=$this->db->readQ($q)) { $sub->userid=$data->userid; $qDup=$subTable->checkExists($data->userid); if (!$qDup) { $sub->insert(); } } } function deliverFeatures() { // deliver via notification $msg=$this->buildFeatureUpdate('notification'); $this->db->log('Start: sending feature notifications'); $this->db->log($msg); try { $page=0; while ($idList=$this->getRecipientListByMode('facebook','notification',$page)) { $this->db->log('notifications_send: '.$idList.' => '.$msg.' app_to_user'); $apiResult=$this->facebook->api_client->notifications_send($idList, $msg, 'app_to_user'); $page+=100; } // set lastFeatureSent for this group $this->getRecipientListByMode('update','notification'); } catch (Exception $e) { $this->db->log($e); } $this->db->log('Done: sending feature notifications'); // deliver via sms $this->db->log('Start: sending feature SMS notifications'); $msg=$this->buildFeatureUpdate('sms'); $this->db->log($msg); try { $page=0; while ($idList=$this->getRecipientListByMode('facebook','sms',$page)) { $this->db->log('send sms:'.$idList); $apiResult=$this->facebook->api_client->sms_send($idList, $msg); $page+=100; } // set lastFeatureSent for this group $this->getRecipientListByMode('update','sms'); } catch (Exception $e) { $this->db->log($e); } $this->db->log('Done: sending SMS notifications'); // deliver via email $this->db->log('Start: sending feature EMAIL notifications'); $fbml=$this->buildFeatureUpdate('email'); try { $page=0; while ($idList=$this->getRecipientListByMode('facebook','email',$page)) { $subject='Breaking features from '.SITE_TITLE; $this->db->log('notifications_sendEmail: '.$idList.' => '.$subject.' '.$fbml); $apiResult=$this->facebook->api_client->notifications_sendEmail($idList, $subject,strip_tags($fbml),$fbml); $page+=100; } // set lastFeatureSent for this group $this->getRecipientListByMode('update','email'); } catch (Exception $e) { $this->db->log($e); } $this->db->log('Done: sending EMAIL notifications'); } function buildFeatureUpdate($rxMode='notification') { $stories=array(); $q=$this->db->query("SELECT story_1_id,story_2_id,story_3_id FROM FeaturedTemplate;"); $data=$this->db->readQ($q); if ($data->story_1_id<>0) { $stories[]=$this->cObj->getById($data->story_1_id); } if ($data->story_2_id<>0) { $stories[]=$this->cObj->getById($data->story_2_id); } if ($data->story_3_id<>0) { $stories[]=$this->cObj->getById($data->story_3_id); } $this->db->log($stories); $code=''; $i=1; switch ($rxMode) { default: foreach ($stories as $story) { $code.='<a href="'.URL_CANVAS.'?p=read&cid='.$story->siteContentId.'">'.$story->title.'</a>'; if ($i<>count($stories)) $code.=', '; $i+=1; } $code='Breaking features from <a href="'.URL_CANVAS.'">'.SITE_TITLE.'</a>. '.$code; $code.='...<a href="'.URL_CANVAS.'?p=account&o=subscribe">Change settings</a>'; break; case 'email': foreach ($stories as $story) { $code.='<a href="'.URL_CANVAS.'?p=read&cid='.$story->siteContentId.'">'.$story->title.'</a>'; if ($i<>count($stories)) $code.=', '; $i+=1; } $code.='...<a href="'.URL_CANVAS.'?p=account&o=subscribe">Change settings</a>'; break; case 'sms': foreach ($stories as $story) { $code.=$story->title; if ($i<>count($stories)) $code.=', '; $i+=1; } $code=SITE_TITLE.' Update: '.$code; break; } return $code; } function getRecipientListByMode($listMode='facebook',$rxMode='notification',$start=0,$limit=100) { // build list of people subscribed to this mode who haven't received an update in SUBSCRIPTIONS_FEATURE_INTERVAL hours // get next list of recipients up to limit $startRow=$start*$limit; if ($listMode=='facebook') { switch ($rxMode) { default: // sms or notification $idList=$this->db->buildIdList("SELECT UserInfo.fbId as id FROM Subscriptions LEFT JOIN UserInfo ON Subscriptions.userid=UserInfo.userid WHERE rxFeatures=1 AND rxMode='$rxMode' AND (lastFeatureSent<date_sub(NOW(), INTERVAL ".SUBSCRIPTIONS_FEATURE_INTERVAL." HOUR) OR lastFeatureSent IS NULL) LIMIT $startRow,$limit;"); break; } } else { // set lastFeatureSent for recipient list by userid $this->db->update("Subscriptions","lastFeatureSent=NOW()","rxFeatures=1 AND rxMode='$rxMode' AND (lastFeatureSent<date_sub(NOW(), INTERVAL ".SUBSCRIPTIONS_FEATURE_INTERVAL." HOUR) OR lastFeatureSent IS NULL)"); } if ($idList=='') return false; return $idList; } }?>