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

/**
 * Use database config
 *
 * @var string
 */
	public $useDbConfig = 'gearhead';

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

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'value' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'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
			),
			'range' => array(
				'rule' => array('range', 17, 101),
				'message' => 'Please enter a number between 18 and 100'
			),
			'notEmpty' => array(
				'rule' => 'notBlank',
				//'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
			),
		),
	);

	public function findRanges()
	{
		$ages = $this->find('all', array(
			'order' => array(
				'Age.value' => 'asc',
			),
		));

		for ($age = 0; $age < count($ages); $age++)
		{
			if ($age == (count($ages)-1))
			{
				$ages[$age]['Age']['range_min'] = $ages[$age]['Age']['value'];
				$ages[$age]['Age']['range_max'] = null;
			}
			else
			{
				$ages[$age]['Age']['range_min'] = $ages[$age]['Age']['value'];
				$ages[$age]['Age']['range_max'] = ($ages[$age+1]['Age']['value']-1);
			}
		}
		return $ages;
	}
}
