<?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 LgArea extends AppModel {
	
	public $belongsTo  = array(		
		'Region' => array('className' => 'Region', 'foreignKey' => 'region_id'),
		'Depot' => array('className' => 'Depot', 'foreignKey' => 'depot_id'),
	);
	
	public function getlist($region_id = 0, $depot_id = 0){
		$conditions = array();
		if (!empty($region_id)){
			$conditions['region_id'] = $region_id;
		}
		
		if (!empty($depot_id)){
			$conditions['depot_id'] = $depot_id;
		}
		
		return $this->find('list', array( 'fields'=>array('id', 'name'),'conditions'=>$conditions, 'order'=>array('name'=>'asc')));
	}
	
	/* public function getlistbyrIds($rIds = NULL){
        $conditions = array();
        if (!empty($rIds)){
            $conditions = array('region_id IN'=>$rIds);
        }
		//pr($conditions);
        return $this->find('list', array( 'fields'=>array('id', 'title'),'conditions'=>$conditions, 'order'=>array('title'=>'asc')));
    } */

	function getLgaListByRole($user, $query){
		$conditions = array();
		switch($user['Role']['grade']){
			case 3:
				$conditions['LgArea.region_id'] = $user['region_id'];
				break;
			case 4:
				$conditions['LgArea.depot_id'] = $user['depot_id'];
				break;
			case 5:
			case 6:				
				$conditions['LgArea.id'] = $user['lg_area_id'];
			break;
		}
		
		
		if (!empty($query['rg'])){
			$conditions['LgArea.region_id'] = $query['rg'];
		}
		
		if (!empty($query['dp'])){
			$conditions['LgArea.id'] = $query['dp'];
		}
		
		if (!empty($query['lg'])){
			$conditions['LgArea.id'] = $query['lg'];
		}
		
		$lgAreas = $this->find('list', array('fields'=>array('id','name'), 'conditions' => $conditions,'order'=>array('name'=>'asc')));
		
		return $lgAreas;
	}
}