<?php
App::uses('AppController', 'Controller');
/**
 * Schulprofils Controller
 *
 * @property Schulprofil $Schulprofil
 * @property PaginatorComponent $Paginator
 * @property FlashComponent $Flash
 * @property SessionComponent $Session
 */
class SchulprofilsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator', 'Flash', 'Session');

/**
 * Helpers
 *
 * @var array
 */
	public $helpers = array('Js' => array('Jquery'), 'Paginator');

/**
 * Defining the query conditions pagination will use by default.
 *
 * @var array
 */
	public $paginate = array(
        'limit' => 5,
        'conditions' => array(
        	'Schule.id !=' => ''
        ),
        'order' => array('Status DESC')
    );

/**
 * finder method
 *
 * @return void
 */
	public function index() {
		$this->Schulprofil->recursive = 0;

		if ($this->request->is('post')) {
			array_push($this->paginate['conditions'], $this->__getFilterConditions($this->request->data));

			$this->Paginator->settings = $this->paginate;
			$this->Paginator->paginate();
			$schulprofils = $this->Paginator->paginate();
			$this->set(compact('schulprofils'));

			$this->Session->write('SchulprofilFinder.Options', $this->request->data);

			// Render view
			$this->render('/Elements/result');
		} else {
			if ($this->Session->check('SchulprofilFinder.Options')) {
				$this->request->data = $this->Session->read('SchulprofilFinder.Options');

				$schulprofilCounter = $this->__getCounter($this->request->data);
			} else {
				$this->request->data = array();

				$schulprofilCounter = $this->__getCounter($this->request->data);
			}
			$this->set(compact('schulprofilCounter'));

			// Flughafen Options
			$airportOptions = array();
			if (!empty($this->request->data['Schulprofil']['Area'])) {
				$airportConditions = array(
					array('Area' => $this->request->data['Schulprofil']['Area']),
					array('Flughafen NOT LIKE' => ''),
					array('Schule.id !=' => '')
				);
			} else {
				$airportConditions = array(
					array('OR' => array(
						array('Area' => 'gb'),
						array('Area' => 'usa')
					)),
					array('Flughafen NOT LIKE' => ''),
					array('Schule.id !=' => '')
				);
			}
			$airports = $this->Schulprofil->find('all', array(
				'conditions' => $airportConditions,
				'fields' => array('DISTINCT Flughafen'),
				'order' => array('Flughafen')
			));
			if (!empty($airports)) {
				foreach ($airports as $airport) {
					$airportOptions[$airport['Schulprofil']['Flughafen']] = $airport['Schulprofil']['Flughafen'];
				}
			}
			$this->set(compact('airportOptions'));

			// Schultage Options
			/*$educationDayOptions = array();
			$educationDays = $this->Schulprofil->find('all', array(
				'conditions' => array('Schultage >' => 0),
				'fields' => array('DISTINCT Schultage'),
				'order' => array('Schultage')
			));
			if (!empty($educationDays)) {
				foreach ($educationDays as $educationDay) {
					$educationDayOptions[$educationDay['Schulprofil']['Schultage']] = $educationDay['Schulprofil']['Schultage'];
				}
			}
			$this->set(compact('educationDayOptions'));*/

			// TermfeeVon Placeholder
			$termfeeVonPlaceholder = $this->Schulprofil->find('first', array(
				'conditions' => array(
					'Termfee !=' => 0,
					'Schule.id !=' => ''
				),
				'fields' => array('MIN(Termfee) AS TermfeeVon')
			));
			$termfeeVonPlaceholder = $termfeeVonPlaceholder[0]['TermfeeVon'];
			$this->set(compact('termfeeVonPlaceholder'));

			// TermfeeBis Placeholder
			$termfeeBisPlaceholder = $this->Schulprofil->find('first', array(
				'conditions' => array(
					'Schule.id !=' => ''
				),
				'fields' => array('MAX(Termfee) AS TermfeeBis')
			));
			$termfeeBisPlaceholder = $termfeeBisPlaceholder[0]['TermfeeBis'];
			$this->set(compact('termfeeBisPlaceholder'));

			// entranceYear Selected
			if (!empty($this->request->data['Schulprofil']['entranceYear'])) {
				$entranceYearSelected = $this->request->data['Schulprofil']['entranceYear'];
			} else {
				$entranceYearSelected = "";
			}
			$this->set(compact('entranceYearSelected'));

			// Verweildauer Selected
			if (!empty($this->request->data['Schulprofil']['Verweildauer'])) {
				$verweildauerSelected = $this->request->data['Schulprofil']['Verweildauer'];
			} else {
				$verweildauerSelected = "";
			}
			$this->set(compact('verweildauerSelected'));

			// Render view
			//$this->render('/Elements/search');
		}
		
	}

/**
 * search method
 *
 * @return void
 */
	public function search() {
		$this->Schulprofil->recursive = 0;

		if ($this->Session->check('SchulprofilFinder.Options')) {
			$this->request->data = $this->Session->read('SchulprofilFinder.Options');

			$schulprofilCounter = $this->__getCounter($this->request->data);
		} else {
			$this->request->data = array();

			$schulprofilCounter = $this->__getCounter($this->request->data);
		}
		$this->set(compact('schulprofilCounter'));

		// Flughafen Options
		$airportOptions = array();
		if (!empty($this->request->data['Schulprofil']['Area'])) {
			$airportConditions = array(
				array('Area' => $this->request->data['Schulprofil']['Area']),
				array('Flughafen NOT LIKE' => ''),
				array('Schule.id !=' => '')
			);
		} else {
			$airportConditions = array(
				array('OR' => array(
					array('Area' => 'gb'),
					array('Area' => 'usa')
				)),
				array('Flughafen NOT LIKE' => ''),
				array('Schule.id !=' => '')
			);
		}
		$airports = $this->Schulprofil->find('all', array(
			'conditions' => $airportConditions,
			'fields' => array('DISTINCT Flughafen'),
			'order' => array('Flughafen')
		));
		if (!empty($airports)) {
			foreach ($airports as $airport) {
				$airportOptions[$airport['Schulprofil']['Flughafen']] = $airport['Schulprofil']['Flughafen'];
			}
		}
		$this->set(compact('airportOptions'));

		// Schultage Options
		/*$educationDayOptions = array();
		$educationDays = $this->Schulprofil->find('all', array(
			'conditions' => array('Schultage >' => 0),
			'fields' => array('DISTINCT Schultage'),
			'order' => array('Schultage')
		));
		if (!empty($educationDays)) {
			foreach ($educationDays as $educationDay) {
				$educationDayOptions[$educationDay['Schulprofil']['Schultage']] = $educationDay['Schulprofil']['Schultage'];
			}
		}
		$this->set(compact('educationDayOptions'));*/

		// TermfeeVon Placeholder
		$termfeeVonPlaceholder = $this->Schulprofil->find('first', array(
			'conditions' => array(
				array('Termfee !=' => 0),
				array('Schule.id !=' => '')
			),
			'fields' => array('MIN(Termfee) AS TermfeeVon')
		));
		$termfeeVonPlaceholder = $termfeeVonPlaceholder[0]['TermfeeVon'];
		$this->set(compact('termfeeVonPlaceholder'));

		// TermfeeBis Placeholder
		$termfeeBisPlaceholder = $this->Schulprofil->find('first', array(
			'conditions' => array(
				array('Schule.id !=' => '')
			),
			'fields' => array('MAX(Termfee) AS TermfeeBis')
		));
		$termfeeBisPlaceholder = $termfeeBisPlaceholder[0]['TermfeeBis'];
		$this->set(compact('termfeeBisPlaceholder'));

		// entranceYear Selected
		if (!empty($this->request->data['Schulprofil']['entranceYear'])) {
			$entranceYearSelected = $this->request->data['Schulprofil']['entranceYear'];
		} else {
			$entranceYearSelected = "";
		}
		$this->set(compact('entranceYearSelected'));

		// Verweildauer Selected
		if (!empty($this->request->data['Schulprofil']['Verweildauer'])) {
			$verweildauerSelected = $this->request->data['Schulprofil']['Verweildauer'];
		} else {
			$verweildauerSelected = "";
		}
		$this->set(compact('verweildauerSelected'));
	}

/**
 * result method
 *
 * @return void
 */
	public function result() {
		$this->Schulprofil->recursive = 0;
		
		if ($this->RequestHandler->isAjax()) {
			if ($this->request->is('post')) {

				array_push($this->paginate['conditions'], $this->__getFilterConditions($this->request->data));

				$schulprofilCounter = $this->__getCounter($this->request->data);

				$this->Session->write('SchulprofilFinder.Options', $this->request->data);
			} else {
				array_push($this->paginate['conditions'], $this->__getFilterConditions($this->Session->read('SchulprofilFinder.Options')));

				$schulprofilCounter = $this->__getCounter($this->Session->read('SchulprofilFinder.Options'));
			}

			$this->Paginator->settings = $this->paginate;
			$this->Paginator->paginate();
			$schulprofils = $this->Paginator->paginate();
			$this->set(compact('schulprofils'));

			
			$this->set(compact('schulprofilCounter'));
		}
	}

	/**
	 * getSelectOptions method
	 * 
	 * Get select options by given field name.
	 *
	 * @param string $changedField
	 * @return void
	 */
	public function getSelectOptions($changedField = null) {
		// set auto render false
		$this->autoRender = false;

		$selectOptions = array();
		
		if ($this->request->is('post') && !empty($changedField)) {
			$this->Schulprofil->recursive = 0;

			$conditions = array();
			$fields = array();
			$order = array();

			switch ($changedField) {
				case 'Area':
					if (!empty($this->request->data['Schulprofil']['Area'])) {
						array_push($conditions, array('Area' => $this->request->data['Schulprofil']['Area']));
					}
					array_push($conditions, array('Flughafen NOT LIKE' => ''));
					array_push($conditions, array('Schule.id !=' => ''));
					array_push($fields, 'DISTINCT Flughafen');
					array_push($order, 'Flughafen');

					$selectOptions = $this->Schulprofil->find('all', array(
						'conditions' => $conditions,
						'fields' => $fields,
						'order' => $order
					));
					break;
			}
		}

		// return JSON object
		return json_encode($selectOptions);
	}

	/**
	 * getSchulprofilCounter method
	 * 
	 * Get counter by given post request.
	 *
	 * @return void
	 */
	public function getSchulfinderCounter() {
		// set auto render false
		$this->autoRender = false;

		$schulprofilCounter = $this->__getCounter($this->request->data);

		return $schulprofilCounter;
	}

	/**
	 * __getCounter method
	 * 
	 * Get counter of schulprofils by given post request.
	 *
	 * @param Array $data
	 * @return int $counter
	 */
	private function __getCounter($data) {
		$this->Schulprofil->recursive = 0;

		$counter = 0;

		$conditions = $this->__getFilterConditions($data);
		array_push($conditions, array('Schule.id !=' => ''));

		$counter = $this->Schulprofil->find('count', array(
			'conditions' => $conditions
		));

		return $counter;
	}

	/**
	 * __getFilterConditions method
	 * 
	 * Get query conditions by given post request.
	 *
	 * @param Array $data
	 * @return Array $conditions
	 */
	private function __getFilterConditions($data) {
		$conditions = array();
		
		if (!empty($data)) {
			foreach ($data['Schulprofil'] as $field => $value) {
				switch($field) {
					/*case "SchuelerzahlVon":
						if (!empty($value)) $conditions['Schuelerzahl >='] = $value;
						break;
					case "SchuelerzahlBis":
						if (!empty($value)) $conditions['Schuelerzahl <='] = $value;
						break;*/
					case "TermfeeVon":
						if (!empty($value)) array_push($conditions, array('Termfee >=' => $value));
						break;
					case "TermfeeBis":
						if (!empty($value)) array_push($conditions, array('Termfee <=' => $value));
						break;
					/*case "apKurseVon":
						if (!empty($value)) $conditions['apKurse >='] = $value;
						break;
					case "apKurseBis":
						if (!empty($value)) $conditions['apKurse <='] = $value;
						break;*/
					case "entranceYear":
						if (!empty($value)) array_push($conditions, array($value => 1));
						break;
					default:
						if ($value != "") {
							if (strstr($field, 'Sportangebot_')) {
								$field = explode('Sportangebot_', $field);
								$field = $field[1];
								array_push($conditions, array('OR' => array(
									array($field => 'x'),
									array($field => 's')
								))); 
							} else {
								array_push($conditions, array($field => $value));
							}
						} else {
							if ($field == "Area") {
								array_push($conditions, array('OR' => array(
									array('Area' => 'gb'),
									array('Area' => 'usa')
								)));
							}
						}
				}
			}
		} else {
			array_push($conditions, array('OR' => array(
				array('Area' => 'gb'),
				array('Area' => 'usa')
			)));
		}

		return $conditions;
	}

}
