Subversion Repositories SmartDukaan

Rev

Rev 14849 | Rev 15085 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
14579 anikendra 1
<?php
2
App::uses('AppModel', 'Model');
3
/**
4
 * NotificationRule Model
5
 *
6
 */
7
class NotificationRule extends AppModel {
8
 
9
/**
10
 * Display field
11
 *
12
 * @var string
13
 */
14
	public $displayField = 'name';
15
 
16
/**
17
 * Validation rules
18
 *
19
 * @var array
20
 */
21
	public $validate = array(
22
		'name' => array(
23
			'notEmpty' => array(
24
				'rule' => array('notEmpty'),
25
				//'message' => 'Your custom message here',
26
				//'allowEmpty' => false,
27
				//'required' => false,
28
				//'last' => false, // Stop validation after this rule
29
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
30
			),
31
		),
32
		'sql' => array(
33
			'notEmpty' => array(
34
				'rule' => array('notEmpty'),
35
				//'message' => 'Your custom message here',
36
				//'allowEmpty' => false,
37
				//'required' => false,
38
				//'last' => false, // Stop validation after this rule
39
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
40
			),
41
		),
42
		'message' => array(
43
			'notEmpty' => array(
44
				'rule' => array('notEmpty'),
45
				//'message' => 'Your custom message here',
46
				//'allowEmpty' => false,
47
				//'required' => false,
48
				//'last' => false, // Stop validation after this rule
49
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
50
			),
51
		),
14855 anikendra 52
		/*'url' => array(
14579 anikendra 53
			'url' => array(
54
				'rule' => array('url'),
55
				//'message' => 'Your custom message here',
56
				//'allowEmpty' => false,
57
				//'required' => false,
58
				//'last' => false, // Stop validation after this rule
59
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
60
			),
61
			'notEmpty' => array(
62
				'rule' => array('notEmpty'),
63
				//'message' => 'Your custom message here',
64
				//'allowEmpty' => false,
65
				//'required' => false,
66
				//'last' => false, // Stop validation after this rule
67
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
68
			),
14855 anikendra 69
		),*/
14579 anikendra 70
	);
71
 
72
	function getActiveRules() {
73
		$options = array('conditions' => array('status'=>'active','starttime <=' => date('Y-m-d H:i:s',time()),'endtime >='  => date('Y-m-d H:i:s',time())));
74
		return $this->find('all',$options);
75
	}
76
 
77
	function getNotification($user_id) {
78
		$activeNotifications = array();
79
		$activeNotificationRules = $this->getActiveRules();
80
		if(!empty($activeNotificationRules)){
81
			foreach ($activeNotificationRules as $key => $rule) {
82
				$sql = $rule['NotificationRule']['sql'];
83
				if(strpos($sql, '{uid}')!==false){
84
					$sql = str_replace('{uid}', $user_id, $sql);
85
				}
14849 anikendra 86
				$this->log($sql,'webnotifications');
87
				$valid = $this->query($sql,'webnotifications');
14579 anikendra 88
				if(!empty($valid)){
89
					$activeNotifications[] = $rule;
90
				}
91
			}
92
		}
93
		if(!empty($activeNotifications)){
94
			$randkey = array_rand($activeNotifications);
14849 anikendra 95
			$this->log(print_r($activeNotifications,1),'webnotifications');
14579 anikendra 96
			return $activeNotifications[$randkey];
97
		}else{
98
			return null;
99
		}
100
	}
101
}