Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
App::uses('AppModel', 'Model');
App::uses('Xml', 'Utility');
/**
 * Api Model
 *
 */
class Api extends AppModel {

/**
 * Primary key field
 *
 * @var string
 */     

        public $primaryKey = '0';
        public $useTable = false;

        private $icubesurl;
        private $siteId;
        private $password;

        public function __construct() {
                $this->icubesurl = Configure::read('icubesurl');
                $this->siteId = Configure::read('siteId');
                $this->password = Configure::read('password');
        }

        public function getLists() {
                $input = '<DATASET><SITE_ID>'.$this->siteId.'</SITE_ID><DATA TYPE="extra" id="password">'.$this->password.'</DATA></DATASET>';
                $input =  urlencode($input);
                $fields = array('input' => $input,'type'=>'list','activity'=>'query-listdata');         
                return $this->makeApiCall($fields);
        }

        private function makeApiCall($fields){
                foreach($fields as $key=>$value) {
                        $fields_string .= $key.'='.$value.'&';
                }
                rtrim($fields_string, '&');
                $ch = curl_init($this->icubesurl);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch,CURLOPT_POST, count($fields));
                curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
                $output = curl_exec($ch);       
                $xmlArray = Xml::toArray(Xml::build($output));
                return $xmlArray;
        }

        public function getProducts($url,$params) {
                $newurl = $url.'1';
                $result = $this->makeSaholicCall($newurl,$params);
                if(!empty($result)) {
                        foreach ($result['response']['items'] as $key => $item) {
                                $allProducts[] = $item;
                        }                       
                        $count = $result['response']['count'];
                        $pages = ceil($count/$params['perpage']);
                        for($i=2;$i<=$pages;$i++){
                                $newurl = $url.$i;
                                $result = $this->makeSaholicCall($newurl,$params);
                                if(!empty($result)) {
                                        foreach ($result['response']['items'] as $key => $item) {
                                                $allProducts[] = $item;
                                        }
                                }                       
                        }
                }
                return($allProducts);
        }

        private function makeSaholicCall($url,$fields) {
                $fields_string = '';
                foreach($fields as $key=>$value) {
                        $fields_string .= $key.'='.$value.'&';
                }
                $fields_string = substr($fields_string, 0, -1);
                $apiUrl = $url.'/?'.$fields_string;
                echo $apiUrl;
                $ch = curl_init($apiUrl);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $output = curl_exec($ch);
                return json_decode($output,true);
        }

        public function addmessage($mlid,$text,$message,$subject){
                $message = preg_replace( '/\s+/', ' ', $message);
                $message = htmlspecialchars($message); 
                // $text = str_replace('<br>', '\n', $text);
                // $text = preg_replace( '/\s+/', ' ', $text);
                $input = '<DATASET><SITE_ID>'.$this->siteId.'</SITE_ID><DATA TYPE="extra" id="password">'.$this->password.'</DATA><MLID>'.$mlid.'</MLID><DATA type="subject">'.$subject.'</DATA><DATA type="from-email">alerts@esaholic.com</DATA><DATA type="from-name">Saholic Alerts</DATA><DATA type="message-format">HTML</DATA><DATA type="message-html">'.$message.'</DATA><DATA type="message-text">'.$text.'</DATA><DATA type="category">Newsletter</DATA></DATASET>'; 
                $input =  urlencode($input);
                $fields = array('input' => $input,'type'=>'message','activity'=>'add');
                return $this->makeApiCall($fields);
        }

        public function sendmessage($mlid,$mid){
                $input = '<DATASET><SITE_ID>'.$this->siteId.'</SITE_ID><DATA TYPE="extra" id="password">'.$this->password.'</DATA><MLID>'.$mlid.'</MLID><MID>'.$mid.'</MID><DATA type="action">schedule</DATA><DATA type="delivery_monitor">on</DATA><DATA type="inbox_snapshot">on</DATA></DATASET>';
                $input =  urlencode($input);
                $fields = array('type'=>'message','activity'=>'schedule','input'=>$input);
                // return $this->makeApiCall($fields);
        }

        public function newmember($mlid,$email,$fname='',$lname='',$salutation='',$address=''){
                $input = '<DATASET><SITE_ID>'.$this->siteId.'</SITE_ID><DATA TYPE="extra" id="password">'.$this->password.'</DATA><MLID>'.$mlid.'</MLID><DATA type="email">'.$email.'</DATA><DATA type="demographic" id="1">'.$fname.'</DATA><DATA type="demographic" id="2">'.$lname.'</DATA></DATASET>';
                //<DATA type="demographic" id="3">'.$salutation.'</DATA><DATA type="demographic" id="4">'.$address.'</DATA>
                $input =  urlencode($input);
                $fields = array('input'=>$input,'type'=>'record','activity'=>'add');
                return $this->makeApiCall($fields);     
        }
}