d1rk
11/21/2011 - 2:23 PM

just a model

just a model

<?php
// [ ... ]

/**
 * conditions for custom find and paginate count methods
 *
 * @var array $findConditions
 * @access public
 */
	public $findConditions = array(
		'single' => array(
			'conditions' => array(
			),
			'contain' => array(
				'User' => array('fields' => array('id', 'username', 'gender', 'city', 'country')),
				'Tag',
				'Inventory' => array('Category', 'Designer', 'Store', 'City', 'Country'),
				'Comment' => array('Author' => array('fields' => array('id', 'username', 'gender', 'city', 'country')))
			),
		),
		'recent' => array(
			'conditions' => array(
			),
			'order' => array(
				'Photo.created' => 'DESC',
			),
		),
		'popular' => array(
			'conditions' => array(
				'Photo.views >' => 0,
			),
			'order' => array(
				'Photo.views' => 'DESC',
			),
		),
		'discussed' => array(
			'conditions' => array(
				'Photo.comment_count >' => 0,
			),
			'order' => array(
				'Photo.comment_count' => 'DESC',
			),
		),
		'todays' => array(
			'conditions' => array(
				'Photo.created >=' => 'DATE_SUB(NOW(), INTERVAL 1 DAY)',
			),
			'order' => array(
				'Photo.created' => 'DESC',
			),
		),
		'label' => array(
			'conditions' => array(
			),
			'order' => array(
				'Photo.created' => 'DESC',
			),
		),
	);

/**
 * Custom find methods
 *
 * @var array $_findMethods
 * @access public
 */
	public $_findMethods = array(
		'single' => true,
		'recent' => true,
		'popular' => true,
		'discussed' => true,
		'todays' => true,
		'label' => true,
	);


/**
 * all virtual fields
 *
 * @var array $virtualFields
 * @access public
 */
	public $virtualFields = array();

	public function __construct($id = false, $table = null, $ds = null)
	{
		parent::__construct($id, $table, $ds);
		// $this->virtualFields['views'] = sprintf('SUM(views)', $this->alias, $this->alias);
		$this->virtualFields['full_filename'] = sprintf('CONCAT(%s.filename, %s.type)', $this->alias, $this->alias);
	}


/**
 * custom find method 'todays'
 *
 * @param string $state 'before' or 'after'
 * @param string $query
 * @param string $results 
 * @return mixed based on $state, returns $query or $results
 * @access protected
 */
	protected function _findTodays($state, $query, $results = array()) {
		if ($state == 'before') {
			$query = Set::merge(
				$query,
				$this->findConditions['todays']
			);
			return $query;
		} elseif ($state == 'after') {
			return $results;
		}
	}

/**
 * custom find method 'single'
 *
 * @param string $state 'before' or 'after'
 * @param string $query
 * @param string $results 
 * @return mixed based on $state, returns $query or $results
 * @access protected
 */
	protected function _findSingle($state, $query, $results = array()) {
		if ($state == 'before') {
			$query = Set::merge(
				$query,
				$this->findConditions['single']
			);
			return $query;
		} elseif ($state == 'after') {
			if(!empty($results))
			{
				return $results[0];
			}
			return $results;
		}
	}

/**
 * custom find method 'recent'
 *
 * @param string $state 'before' or 'after'
 * @param string $query
 * @param string $results 
 * @return mixed based on $state, returns $query or $results
 * @access protected
 */
	protected function _findRecent($state, $query, $results = array()) {
		if ($state == 'before') {
			$query = Set::merge(
				$query,
				$this->findConditions['recent']
			);
			return $query;
		} elseif ($state == 'after') {
			return $results;
		}
	}

/**
 * custom find method 'popular'
 *
 * @param string $state 'before' or 'after'
 * @param string $query
 * @param string $results 
 * @return mixed based on $state, returns $query or $results
 * @access protected
 */
	protected function _findPopular($state, $query, $results = array()) {
		if ($state == 'before') {
			$query = Set::merge(
				$query,
				$this->findConditions['popular']
			);
			return $query;
		} elseif ($state == 'after') {
			return $results;
		}
	}

/**
 * custom find method 'discussed'
 *
 * @param string $state 'before' or 'after'
 * @param string $query
 * @param string $results 
 * @return mixed based on $state, returns $query or $results
 * @access protected
 */
	protected function _findDiscussed($state, $query, $results = array()) {
		if ($state == 'before') {
			$query = Set::merge(
				$query,
				$this->findConditions['discussed']
			);
			return $query;
		} elseif ($state == 'after') {
			return $results;
		}
	}

/**
 * custom find method 'label'
 *
 * @param string $state 'before' or 'after'
 * @param string $query
 * @param string $results 
 * @return mixed based on $state, returns $query or $results
 * @access protected
 */
	protected function _findLabel($state, $query, $results = array()) {
		if ($state == 'before') {
			$query = Set::merge(
				$query,
				$this->findConditions['label']
			);
			$query['conditions']['Photo.id'] = $this->_findByLabel($query['label'], $query['conditions']);
			return $query;
		} elseif ($state == 'after') {
			return $results;
		}
	}


// [ ... ]
?>