<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * Application Module Method Model
 *
 * @package    lnAuth
 * @category   Models
 * @author     Deon George
 * @copyright  (c) 2014 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
abstract class lnAuth_Model_Module_Method extends ORM {
	// This module doesnt keep track of column updates automatically
	protected $_created_column = FALSE;
	protected $_updated_column = FALSE;

	// Relationships
	protected $_belongs_to = array(
		'module'=>array(),
	);
	protected $_has_one = array(
		'record_id'=>array(),
	);
	protected $_has_many = array(
		'group'=>array('through'=>'group_method','foreign_key'=>'method_id')
	);

	protected $_sorting = array(
		'name'=>'ASC',
	);

	protected $_nullifempty = array(
		'menu_display',
	);

	protected $status;

	public function controller_sub() {
		return substr_count($this->name,'_') ? substr($this->name,($x=strpos($this->name,'_')),strpos($this->name,':')-$x) : '';
	}

	public function controller() {
		return Kohana::classname(sprintf('Controller%s_%s',($this->directory() ? '_' : '').$this->directory(),$this->module->name).$this->controller_sub());
	}

	public function directory() {
		return substr($this->name,0,substr_count($this->name,'_') ? strpos($this->name,'_') : strpos($this->name,':'));
	}

	public function method() {
		return substr($this->name,strpos($this->name,':')+1);
	}

	/**
	 * Calculate the description for this method on any menu link
	 */
	public function menu_display() {
		return $this->menu_display ? $this->menu_display : sprintf('%s: %s',$this->module->name,$this->name);
	}

	public function status($status=NULL) {
		if ($status)
			$this->status = $status;

		return $this->status;
	}

	public function url() {
		if (! preg_match('/:/',$this->name))
			return NULL;

		list($type,$action) = preg_split('/:/',$this->name,2);

		return URL::link($this->directory(),$this->module->name.$this->controller_sub().'/'.$action);
	}
}
?>