Subversion Repositories SmartDukaan

Rev

Rev 15410 | Rev 16243 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 15410 Rev 15667
Line 15... Line 15...
15
 */
15
 */
16
	public $components = array('Paginator');
16
	public $components = array('Paginator');
17
 
17
 
18
	public function beforeFilter() {
18
	public function beforeFilter() {
19
		parent::beforeFilter();
19
		parent::beforeFilter();
20
		$this->Auth->allow('add');
20
		$this->Auth->allow('add','generateAffiliateUrl','sendPushNotification');
21
	}
21
	}
22
/**
22
/**
23
 * index method
23
 * index method
24
 *
24
 *
25
 * @return void
25
 * @return void
Line 218... Line 218...
218
			$this->Session->setFlash(__('The pushnotification has been deleted.'));
218
			$this->Session->setFlash(__('The pushnotification has been deleted.'));
219
		} else {
219
		} else {
220
			$this->Session->setFlash(__('The pushnotification could not be deleted. Please, try again.'));
220
			$this->Session->setFlash(__('The pushnotification could not be deleted. Please, try again.'));
221
		}
221
		}
222
		return $this->redirect(array('action' => 'index'));
222
		return $this->redirect(array('action' => 'index'));
-
 
223
	}
-
 
224
 
-
 
225
	public function generateAffiliateUrl(){
-
 
226
		$url = $this->request->data['url'];
-
 
227
		$user_id = $this->request->data['userId'];
-
 
228
		$storeId = $this->request->data['storeId'];
-
 
229
		//Get StoreProduct Info		
-
 
230
		$cachekey = 'store-'.$storeId;
-
 
231
		$store = Cache::read($cachekey,'month');
-
 
232
		if(empty($store)) {
-
 
233
			$this->loadModel('Store');
-
 
234
			$store = $this->Store->find('first',array('recursive'=>-1,'conditions'=>array('id'=>$storeId)));
-
 
235
			Cache::write($cachekey,$store,'month');			
-
 
236
		}
-
 
237
		$prefix = "SHA".$storeId;
-
 
238
		$tag = $prefix.time();
-
 
239
		if($storeId == 2){				
-
 
240
			$url = str_replace('www','m',$url);
-
 
241
		} elseif($storeId == 3) {
-
 
242
			$url_parts = parse_url($url);
-
 
243
			$url_parts['path'] = str_replace('viewAllSellers/','',$url_parts['path']);//quickfix for snapdeal
-
 
244
			if(isset($url_parts['query'])) {
-
 
245
				$url = "http://m.snapdeal.com".$url_parts['path'].'?'.$url_parts['query']."&utm_source=aff_prog&utm_campaign=afts&offer_id=17";
-
 
246
			}else{
-
 
247
				$url = "http://m.snapdeal.com".$url_parts['path'].'?utm_source=aff_prog&utm_campaign=afts&offer_id=17';
-
 
248
			}
-
 
249
		} elseif($storeId == 4){
-
 
250
			$next = str_replace('www','m',$url);	
-
 
251
			$url = $this->getAutoLoginUrl($userId,$next);
-
 
252
			$url .= '?utm_source=profitmandi';
-
 
253
		}
-
 
254
		if( strpos($url, '?') === false ) {
-
 
255
			$firstChar = '?';
-
 
256
		} else {
-
 
257
			$firstChar = '&';
-
 
258
		}
-
 
259
		$url .= $firstChar.$store['Store']['affid_param'].'='.$store['Store']['affiliate_id'];
-
 
260
		if(!empty($store['Store']['sub_tag_param'])){
-
 
261
			$url .= '&'.$store['Store']['sub_tag_param'].'='.$tag;
-
 
262
		}
-
 
263
		// $extras = array('store'=>$store['Store']['name'],'source'=>'notification');
-
 
264
		// $data = array('user_id' => $user_id,'store_product_id'=>0,'tag'=>$tag,'url'=>$url,'price'=>0,'extras'=>json_encode($extras));
-
 
265
		// $this->loadModel('Click');
-
 
266
		// $this->Click->create();
-
 
267
		// $this->Click->save($data);
-
 
268
		// return $url;
-
 
269
		$result = array('url' => $url );
-
 
270
		$this->layout = "ajax";
-
 
271
        $this->response->type('json');
-
 
272
        $this->set(array(
-
 
273
            'result' => $result,
-
 
274
            '_serialize' => array('result')
-
 
275
        ));
-
 
276
        $this->render('/Elements/json');
-
 
277
	}
-
 
278
 
-
 
279
	public function sendPushNotification() {
-
 
280
		$gcm_id = $this->request->data['gcmId'];
-
 
281
	 	$user_id =  $this->request->data['userId'];	 	
-
 
282
	 	$cid =  $this->request->data['cid'];
-
 
283
	 	$title =  $this->request->data['title'];
-
 
284
	 	$message =  $this->request->data['message'];
-
 
285
	 	$type =  $this->request->data['type'];
-
 
286
	 	$url =  $this->request->data['url'];
-
 
287
		session_write_close();
-
 
288
    	Configure::load('live');    	
-
 
289
		$msg = array(
-
 
290
		    'message'       => $message,
-
 
291
		    'cid'       	=> $cid,
-
 
292
		    'title'         => $title,
-
 
293
		    'type'      	=> $type,
-
 
294
		    'url'		    => $url,
-
 
295
		    'vibrate'       => 1,
-
 
296
		    'sound'         => 1,
-
 
297
		    'largeIcon'     => 'large_icon',
-
 
298
		    'smallIcon'     => 'small_icon'
-
 
299
		);
-
 
300
		// Set POST variables
-
 
301
		$url = 'https://android.googleapis.com/gcm/send';
-
 
302
 
-
 
303
		$registration_ids = array($gcm_id);
-
 
304
		$fields = array(
-
 
305
		    'registration_ids' => $registration_ids,
-
 
306
		    'data' => $msg,
-
 
307
		);
-
 
308
		$headers = array(
-
 
309
		    'Authorization: key=' . Configure::read('googleapikey'),
-
 
310
		    'Content-Type: application/json'
-
 
311
		);
-
 
312
		$this->log(print_r($msg,1),'pushnotifications');
-
 
313
		// Open connection
-
 
314
		$ch = curl_init();
-
 
315
 
-
 
316
		// Set the url, number of POST vars, POST data
-
 
317
		curl_setopt($ch, CURLOPT_URL, $url);
-
 
318
		curl_setopt($ch, CURLOPT_POST, true);
-
 
319
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-
 
320
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
 
321
 
-
 
322
		// Disabling SSL Certificate support temporarly
-
 
323
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
-
 
324
 
-
 
325
		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
-
 
326
 
-
 
327
		// Execute post
-
 
328
		$result = curl_exec($ch);
-
 
329
		if ($result === FALSE) {
-
 
330
		    die('Curl failed: ' . curl_error($ch));
-
 
331
		}
-
 
332
		$result = json_decode($result,1);
-
 
333
 
-
 
334
		// Close connection
-
 
335
		curl_close($ch);
-
 
336
		$this->log("For $user_id ".$registatoin_ids[0]." ".print_r($result,1),'pushnotifications');
-
 
337
		if($res['success']==1){
-
 
338
			$data = array('id'=>$cid,'type'=>'sent','status'=>$res['success'],'message'=>'success');
-
 
339
			$this->Pushnotification->save($data);
-
 
340
		}else if($res['success']==0){
-
 
341
			$message=$res['results'][0]['error'];
-
 
342
			$data = array('id'=>$cid,'type'=>'sent','status'=>$res['success'],'message'=>$message);
-
 
343
			$sqlQuery="update gcm_users set failurecount=failurecount+1 where gcm_regid='".$registration_ids[0]."'";
-
 
344
			$resultData=$this->GcmUser->query($sqlQuery);
-
 
345
			$this->Pushnotification->save($data);
223
	}}
346
		}
-
 
347
		$this->layout = "ajax";
-
 
348
        $this->response->type('json');
-
 
349
        $this->set(array(
-
 
350
            'result' => $result,
-
 
351
            '_serialize' => array('result')
-
 
352
        ));
-
 
353
        $this->render('/Elements/json');
-
 
354
	}
-
 
355
}
224
356