Subversion Repositories SmartDukaan

Rev

Rev 12345 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
App::uses('AppModel', 'Model');
3
App::uses('Xml', 'Utility');
4
/**
5
 * Api Model
6
 *
7
 */
8
class Api extends AppModel {
9
 
10
/**
11
 * Primary key field
12
 *
13
 * @var string
14
 */	
15
 
16
	public $primaryKey = '0';
17
	public $useTable = false;
18
 
19
	private $icubesurl;
20
	private $siteId;
21
	private $password;
22
 
23
	public function __construct() {
24
		$this->icubesurl = Configure::read('icubesurl');
25
		$this->siteId = Configure::read('siteId');
26
		$this->password = Configure::read('password');
27
	}
28
 
29
	public function getLists() {
30
		$input = '<DATASET><SITE_ID>'.$this->siteId.'</SITE_ID><DATA TYPE="extra" id="password">'.$this->password.'</DATA></DATASET>';
31
		$input =  urlencode($input);
32
		$fields = array('input' => $input,'type'=>'list','activity'=>'query-listdata');		
33
		return $this->makeApiCall($fields);
34
	}
35
 
36
	private function makeApiCall($fields){
37
		foreach($fields as $key=>$value) {
38
		        $fields_string .= $key.'='.$value.'&';
39
		}
40
		rtrim($fields_string, '&');
41
		$ch = curl_init($this->icubesurl);
42
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
43
		curl_setopt($ch,CURLOPT_POST, count($fields));
44
		curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
45
		$output = curl_exec($ch);	
46
		$xmlArray = Xml::toArray(Xml::build($output));
47
		return $xmlArray;
48
	}
49
 
50
	public function getProducts($url,$params) {
51
		$newurl = $url.'1';
52
		$result = $this->makeSaholicCall($newurl,$params);
53
		if(!empty($result)) {
54
			foreach ($result['response']['items'] as $key => $item) {
55
				$allProducts[] = $item;
56
			}			
57
			$count = $result['response']['count'];
58
			$pages = ceil($count/$params['perpage']);
59
			for($i=2;$i<=$pages;$i++){
60
				$newurl = $url.$i;
61
				$result = $this->makeSaholicCall($newurl,$params);
62
				if(!empty($result)) {
63
					foreach ($result['response']['items'] as $key => $item) {
64
						$allProducts[] = $item;
65
					}
66
				}			
67
			}
68
		}
69
		return($allProducts);
70
	}
71
 
72
	private function makeSaholicCall($url,$fields) {
73
		$fields_string = '';
74
		foreach($fields as $key=>$value) {
75
		        $fields_string .= $key.'='.$value.'&';
76
		}
77
		$fields_string = substr($fields_string, 0, -1);
78
		$apiUrl = $url.'/?'.$fields_string;
79
		echo $apiUrl;
80
		$ch = curl_init($apiUrl);
81
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
82
		$output = curl_exec($ch);
83
		return json_decode($output,true);
84
	}
85
 
86
	public function addmessage($mlid,$text,$message,$subject){
87
		$message = preg_replace( '/\s+/', ' ', $message);
88
		$message = htmlspecialchars($message); 
89
		// $text = str_replace('<br>', '\n', $text);
90
		// $text = preg_replace( '/\s+/', ' ', $text);
91
		$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>'; 
92
		$input =  urlencode($input);
93
		$fields = array('input' => $input,'type'=>'message','activity'=>'add');
94
		return $this->makeApiCall($fields);
95
	}
96
 
97
	public function sendmessage($mlid,$mid){
98
		$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>';
99
		$input =  urlencode($input);
100
		$fields = array('type'=>'message','activity'=>'schedule','input'=>$input);
12347 anikendra 101
		return $this->makeApiCall($fields);
12345 anikendra 102
	}
103
 
104
	public function newmember($mlid,$email,$fname='',$lname='',$salutation='',$address=''){
105
		$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>';
106
		//<DATA type="demographic" id="3">'.$salutation.'</DATA><DATA type="demographic" id="4">'.$address.'</DATA>
107
		$input =  urlencode($input);
108
		$fields = array('input'=>$input,'type'=>'record','activity'=>'add');
109
		return $this->makeApiCall($fields);	
110
	}
111
}