Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

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

/**
 * Display field
 *
 * @var string
 */
        public $displayField = 'name';

/**
 * Validation rules
 *
 * @var array
 */
        public $validate = array(
                'name' => array(
                        'notEmpty' => array(
                                'rule' => array('notEmpty'),
                                //'message' => 'Your custom message here',
                                //'allowEmpty' => false,
                                //'required' => false,
                                //'last' => false, // Stop validation after this rule
                                //'on' => 'create', // Limit validation to 'create' or 'update' operations
                        ),
                ),
                'sql' => array(
                        'notEmpty' => array(
                                'rule' => array('notEmpty'),
                                //'message' => 'Your custom message here',
                                //'allowEmpty' => false,
                                //'required' => false,
                                //'last' => false, // Stop validation after this rule
                                //'on' => 'create', // Limit validation to 'create' or 'update' operations
                        ),
                ),
                'message' => array(
                        'notEmpty' => array(
                                'rule' => array('notEmpty'),
                                //'message' => 'Your custom message here',
                                //'allowEmpty' => false,
                                //'required' => false,
                                //'last' => false, // Stop validation after this rule
                                //'on' => 'create', // Limit validation to 'create' or 'update' operations
                        ),
                ),
                /*'url' => array(
                        'url' => array(
                                'rule' => array('url'),
                                //'message' => 'Your custom message here',
                                //'allowEmpty' => false,
                                //'required' => false,
                                //'last' => false, // Stop validation after this rule
                                //'on' => 'create', // Limit validation to 'create' or 'update' operations
                        ),
                        'notEmpty' => array(
                                'rule' => array('notEmpty'),
                                //'message' => 'Your custom message here',
                                //'allowEmpty' => false,
                                //'required' => false,
                                //'last' => false, // Stop validation after this rule
                                //'on' => 'create', // Limit validation to 'create' or 'update' operations
                        ),
                ),*/
        );

        function getActiveRules() {
                $options = array('conditions' => array('status'=>'active','starttime <=' => date('Y-m-d H:i:s',time()),'endtime >='  => date('Y-m-d H:i:s',time())));
                return $this->find('all',$options);
        }

        function getNotification($user_id) {
                $activeNotifications = array();
                $activeNotificationRules = $this->getActiveRules();
                if(!empty($activeNotificationRules)){
                        foreach ($activeNotificationRules as $key => $rule) {
                                $sql = $rule['NotificationRule']['sql'];
                                if(strpos($sql, '{uid}')!==false){
                                        $sql = str_replace('{uid}', $user_id, $sql);
                                }
                                $this->log($sql,'webnotifications');
                                $valid = $this->query($sql);
                                if(!empty($valid)){
                                        $activeNotifications[] = $rule;
                                }
                        }
                }
                if(!empty($activeNotifications)){
                        $randkey = array_rand($activeNotifications);
                        $this->log(print_r($activeNotifications,1),'webnotifications');
                        return $activeNotifications[$randkey];
                }else{
                        return null;
                }
        }
}