From 485942b696f2d17bf43443aa1ffbb064256c646a Mon Sep 17 00:00:00 2001 From: Abhiroop Bhatnagar Date: Tue, 25 Jun 2013 17:50:04 +0530 Subject: [PATCH] Set up InteractionMode0Controller to handle InteractionMode1 --- configuration.inc.default | 2 +- controllers/InteractionMode0Controller.php | 59 +++++++++++++++++++--- controllers/InteractionMode1Controller.php | 4 +- models/IncomingSMS.php | 15 +++--- models/ServiceList.php | 12 +++-- 5 files changed, 70 insertions(+), 22 deletions(-) diff --git a/configuration.inc.default b/configuration.inc.default index 4c11a01..42e003d 100644 --- a/configuration.inc.default +++ b/configuration.inc.default @@ -86,7 +86,7 @@ define('DB_USER',APPLICATION_NAME); define('DB_PASS','password'); /** - * Response to get_service_list + * Response to get_service_codes * GROUP - Return List of Groups * SERVICES - Return list of Services */ diff --git a/controllers/InteractionMode0Controller.php b/controllers/InteractionMode0Controller.php index 310880f..a499a4f 100644 --- a/controllers/InteractionMode0Controller.php +++ b/controllers/InteractionMode0Controller.php @@ -18,24 +18,67 @@ public function generateResponse($endpoint) { $incomingSMS=new IncomingSMS; $previousQuery=QueryRecord::getRecord($incomingSMS->getFrom()); - if(($incomingSMS->getSubKeyword()==SUB_KEYWORD_MORE)&&$previousQuery) + + if(!($previousQuery)) + { + //return error; + } + if($previousQuery['interaction_mode']==1) { - if($previousQuery['interaction_mode']==1) + if( ($incomingSMS->getSubKeyword()==SUB_KEYWORD_MORE)&& + ( ($previousQuery['additional_info']=='GROUPS')|| + ($previousQuery['additional_info']=='SERVICES') ) ) { $page=$previousQuery['previous_page']+1; $type=$previousQuery['additional_info']; $controller=new InteractionMode1Controller($this->template); - $controller->generateResponse($endpoint,$page,$type); + $controller->generateResponse($endpoint,$type,$page); } - else + + else { - //error + $validQuery=FALSE; + if(self::findGroupNumber($incomingSMS->getQueryText())) + { + $groupNumber=self::findGroupNumber($incomingSMS->getQueryText()); + $page=1; + $validQuery=TRUE; + } + else if (($incomingSMS->getSubKeyword()==SUB_KEYWORD_MORE)&&(self::findGroupNumber($previousQuery['additional_info']))) + { + $groupNumber=self::findGroupNumber($previousQuery['additional_info']); + $page=$previousQuery['previous_page']+1; + $validQuery=TRUE; + } + if($validQuery) + { + $xmlurl = file_get_contents($endpoint.'/services.xml'); + $xml = simplexml_load_string($xmlurl, null, LIBXML_NOCDATA); + $groupList=ServiceList::getGroupList($xml); + $group=$groupList[$groupNumber]; + $serviceList=ServiceList::getServiceList($xml,$group); + $pages=InteractionMode1Controller::constructPages($serviceList,'SERVICES'); + QueryRecord::save(1,$page,GROUP_OPTIONS_PREFIX.$groupNumber); + $this->template->smsBlocks=$pages['page'.$page]; + } + else + { + //error + } } } + + + } + private static function findGroupNumber($string) + { + if(preg_match('/^'.GROUP_OPTIONS_PREFIX.'([0-9]*$)/', $string,$matches)) + { + return $matches[1]; + } else { - //error + return 0; } - - } + } } diff --git a/controllers/InteractionMode1Controller.php b/controllers/InteractionMode1Controller.php index df44cb3..a6267ee 100644 --- a/controllers/InteractionMode1Controller.php +++ b/controllers/InteractionMode1Controller.php @@ -16,7 +16,7 @@ public function __construct(Template $template) parent::__construct($template); } - public function generateResponse($endpoint,$page=1,$type=GET_SERVICE_LIST_RESPONSE) + public function generateResponse($endpoint,$type=GET_SERVICE_LIST_RESPONSE,$page=1) { $list=array(); $xmlurl = file_get_contents($endpoint.'/services.xml'); @@ -41,7 +41,7 @@ public function generateResponse($endpoint,$page=1,$type=GET_SERVICE_LIST_RESPON //error } } - private static function constructPages(array $list,$type) + public static function constructPages(array $list,$type) { $prefix=($type=='SERVICES')?SERVICE_OPTIONS_PREFIX:GROUP_OPTIONS_PREFIX; $pages=array(); diff --git a/models/IncomingSMS.php b/models/IncomingSMS.php index c766af0..ee199fa 100644 --- a/models/IncomingSMS.php +++ b/models/IncomingSMS.php @@ -15,14 +15,16 @@ public function __construct() { $this->smsFrom = trim(self::fetchValuefromKey(SMS_FROM)); $this->smsBody = trim(self::fetchValuefromKey(SMS_BODY)); - $this->smsAPIKey= trim(self::fetchValuefromKey(SMS_API_KEY_PARAM)); + $this->smsAPIKey= defined('SMS_API_KEY_PARAM')?trim(self::fetchValuefromKey(SMS_API_KEY_PARAM)):NULL; $this->smsBodyPieces=explode(" ",$this->smsBody,3); } public function isAPIKeyValid() { + if(isset($this->smsAPIKey)) { + if(($this->smsAPIKey)==SMS_API_KEY) return TRUE; else @@ -30,7 +32,7 @@ public function isAPIKeyValid() throw new Exception('Invalid Key'); } } - + return TRUE; } public function isKeywordMatched() @@ -99,11 +101,12 @@ public function getQueryText() *All the pieces after Keyword and subKeyword are QueryText */ $QueryTextIndex=$potentialQueryTextIndex; - $QueryText=$this->smsBodyPieces[$QueryTextIndex]; - for($i=$QueryTextIndex+1;$i<=2;$i++) + $QueryText=isset($this->smsBodyPieces[$QueryTextIndex])?$this->smsBodyPieces[$QueryTextIndex]:NULL; + foreach($this->smsBodyPieces as $key => $value) { - $QueryText=$QueryText." ".$this->smsBodyPieces[$i]; - } + if($key>$QueryTextIndex) + $QueryText=$QueryText." ".$value; + } return $QueryText; } diff --git a/models/ServiceList.php b/models/ServiceList.php index 5906797..45edeb4 100644 --- a/models/ServiceList.php +++ b/models/ServiceList.php @@ -6,14 +6,16 @@ */ class ServiceList { - public static function getServiceList($xml) + public static function getServiceList($xml,$group='All') { foreach ($xml->service as $service) - { - $serviceCode=(int)$service->service_code; - $serviceList[$serviceCode]=(string)$service->service_name; - + { + if(((string)$service->group==$group)||($group=='All')) + { + $serviceCode=(int)$service->service_code; + $serviceList[$serviceCode]=(string)$service->service_name; + } } uasort($serviceList, 'self::cmp'); return $serviceList;