<?php
App::uses('Model', 'Model');

/**
 * Application model for Cake.
 *
 * Add your application-wide methods in the class below, your models
 * will inherit them.
 *
 * @package       app.Model
 */

class BikesupervisorChecklist extends AppModel {
	
	public $actsAs = array('Containable');
	
	public $belongsTo = array(
			'User' => array('className' => 'User', 'foreignKey' => 'user_id', 'fields' => array('id', 'name'), 'type'=>'inner'), 
			'Customer' => array('className' => 'Customer', 'foreignKey' => 'customer_id', 'fields' => array('first_name', 'last_name'), 'type'=>'left'), 
			'Region' => array('className' => 'Region', 'foreignKey' => 'region_id', 'type'=>'left', 'fields' => array('id', 'title')),
			'Depot' => array('className' => 'Depot', 'foreignKey' => 'depot_id', 'type'=>'left', 'fields' => array('id', 'title','region_id')),
			
		);
	 
	/* public function visibleConditions(&$conditions, $user){
		switch($user['Role']['grade']){
			case 6:
				$conditions['CustomerVisit.user_id'] = $user['id'];
			break;
			case 5:
				$conditions['OR'] = array('CustomerVisit.user_id'=> $user['id'], 'CustomerVisit.user_id IN (SELECT uid2 FROM user_relations WHERE user_id ='. $user['id'].' )' );
			break;
			case 4:
				$conditions['CustomerVisit.depot_id'] = $user['depot_id'];
			break;
			case 3:
				$conditions['OR'] = array('CustomerVisit.user_id'=> $user['id'], 'CustomerVisit.user_id IN (SELECT id FROM users WHERE region_id ='. $user['region_id'].' )' );
			break;
		}
	} */
	
	public function visibleroleConditions(&$conditions, $user, &$paginate){
		switch($user['Role']['grade']){
			case 6:
				$conditions['BikesupervisorChecklist.user_id'] = $user['id'];
			break;
			case 5:
				$paginate['joins'][] = array('table' => '(SELECT uid2 FROM user_relations WHERE user_id = '.$user['id'].')', 'type'=>'left', 'alias'=>'UserRelation', 'conditions'=>array('BikesupervisorChecklist.user_id = UserRelation.uid2'));
				
				
				$conditions['OR'] = array('BikesupervisorChecklist.user_id'=> $user['id'], 'UserRelation.uid2 IS NOT NULL' );
			break;
			case 4:
				$conditions['BikesupervisorChecklist.depot_id'] = $user['depot_id'];
			break;
			case 3:
				$paginate['joins'][] = array('table' => '(SELECT id FROM users WHERE region_id = '.$user['region_id'].')', 'type'=>'left', 'alias'=>'User2', 'conditions'=>array('BikesupervisorChecklist.user_id = User2.id'));
				
				
				$conditions['OR'] = array('BikesupervisorChecklist.user_id'=> $user['id'], 'User2.id IS NOT NULL' );
								
			break;
		}
	}
}