| 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) {
|
| 15797 |
anikendra |
78 |
$cachekey = 'webnotifications-'.$user_id;
|
|
|
79 |
$activeNotifications = Cache::read($cachekey,'hour');
|
|
|
80 |
if(empty($activeNotifications)) {
|
|
|
81 |
// $activeNotifications = array();
|
|
|
82 |
$activeNotificationRules = $this->getActiveRules();
|
|
|
83 |
if(!empty($activeNotificationRules)){
|
|
|
84 |
foreach ($activeNotificationRules as $key => $rule) {
|
|
|
85 |
$sql = $rule['NotificationRule']['sql'];
|
|
|
86 |
if(strpos($sql, '{uid}')!==false){
|
|
|
87 |
$sql = str_replace('{uid}', $user_id, $sql);
|
|
|
88 |
}
|
|
|
89 |
$this->log($sql,'webnotifications');
|
|
|
90 |
$valid = $this->query($sql);
|
|
|
91 |
if(!empty($valid)){
|
|
|
92 |
$activeNotifications[] = $rule;
|
|
|
93 |
}
|
| 14579 |
anikendra |
94 |
}
|
|
|
95 |
}
|
| 15797 |
anikendra |
96 |
Cache::write($cachekey,$activeNotifications,'hour');
|
| 14579 |
anikendra |
97 |
}
|
|
|
98 |
if(!empty($activeNotifications)){
|
|
|
99 |
$randkey = array_rand($activeNotifications);
|
| 14849 |
anikendra |
100 |
$this->log(print_r($activeNotifications,1),'webnotifications');
|
| 14579 |
anikendra |
101 |
return $activeNotifications[$randkey];
|
|
|
102 |
}else{
|
|
|
103 |
return null;
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
}
|