<commit_before><?php
/**
 * @version    	$Id$
 * @category	Nooku
 * @package    	Nooku_Server
 * @subpackage 	Extensions
 * @copyright  	Copyright (C) 2011 Timble CVBA and Contributors. (http://www.timble.net).
 * @license    	GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link       	http://www.nooku.org
 */

/**
 * Modules Model Class
 *
 * @author		<NAME> <http://nooku.assembla.com/profile/stiandidriksen>
 * @category	Nooku
 * @package		Nooku_Server
 * @subpackage	Extensions  
 */

class ComExtensionsModelModules extends ComDefaultModelDefault
{
	public function __construct(KConfig $config)
	{
		parent::__construct($config);
	
		$this->_state
		 	->insert('application', 'cmd', 'site')
		 	->insert('sort'  	  , 'cmd', array('position', 'ordering'))
		 	->insert('enabled'	  , 'int')
		 	->insert('position'   , 'cmd')
		 	->insert('type' 	  , 'cmd')
		 	->insert('installed'  , 'boolean', false);
	}

	protected function _buildQueryJoin(KDatabaseQuery $query)
	{
		$query
			->join('left', 'users AS user', 'user.id = tbl.checked_out')
			->join('left', 'groups AS group', 'group.id = tbl.access')
			->join('left', 'modules_menu AS module_menu', 'module_menu.moduleid = tbl.id');

		parent::_buildQueryJoin($query);
	}

	protected function _buildQueryWhere(KDatabaseQuery $query)
	{
		$state = $this->_state;

		if($state->search) {
			$query->where('tbl.title', 'LIKE', '%'.$state->search.'%');
		}

		if($state->position) {
			$query->where('tbl.position', '=', $state->position);
		}
		
		if($state->type) {
			$query->where('tbl.module', '=', $state->type);
		}

		if($state->enabled !== '' && $state->enabled !== null) {
			$query->where('tbl.published', '=', $state->enabled);
		}
		
		$client	= JApplicationHelper::getClientInfo($state->application, true);
		$query->where('tbl.client_id', '=', $client->id);

		parent::_buildQueryWhere($query);
	}

	/**
	 * Get the list of items based on the distinct column values
	 *
	 * We are specializing it because of the admin/site state filter
	 *
	 * @param string	The column name
	 * @return KDatabaseRowset
	 */
	public function getColumn($column)
	{	
		if (!isset($this->_column[$column])) 
		{	
			if($table = $this->getTable()) 
			{
				$client	= JApplicationHelper::getClientInfo($this->_state->application, true);
				
			    $query = $table->getDatabase()->getQuery()
					->distinct()
					->group('tbl.'.$table->mapColumns($column))
					->where('tbl.client_id', '=', $client->id);

				$this->_buildQueryOrder($query);

				$this->_column[$column] = $table->select($query);
			}
		}
			
		return $this->_column[$column];
	}

	/**
	 * Method to get a item object which represents a table row
	 *
	 * If the model state is unique a row is fetched from the database based on the state.
	 * If not, an empty row is be returned instead.
	 *
	 * This method is customized in order to set the default module type on new rows.
	 *
	 * @return KDatabaseRow
	 */
	public function getItem()
	{
		if (!isset($this->_item))
		{
			$this->_item = parent::getItem();

			if($this->_item->isNew() && $this->_state->type) {
				$this->_item->type = $this->_state->type;
			}
		}

		return $this->_item;
	}

    /**
     * Get a list of items
     * 
     * If the installed state is TRUE this function will return a list of the installed
     * modules.<commit_msg>Removed getColumn function. No longer used.<commit_after><?php
/**
 * @version    	$Id$
 * @category	Nooku
 * @package    	Nooku_Server
 * @subpackage 	Extensions
 * @copyright  	Copyright (C) 2011 Timble CVBA and Contributors. (http://www.timble.net).
 * @license    	GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link       	http://www.nooku.org
 */

/**
 * Modules Model Class
 *
 * @author		<NAME> <http://nooku.assembla.com/profile/stiandidriksen>
 * @category	Nooku
 * @package		Nooku_Server
 * @subpackage	Extensions  
 */

class ComExtensionsModelModules extends ComDefaultModelDefault
{
	public function __construct(KConfig $config)
	{
		parent::__construct($config);
	
		$this->_state
		 	->insert('application', 'cmd', 'site')
		 	->insert('sort'  	  , 'cmd', array('position', 'ordering'))
		 	->insert('enabled'	  , 'int')
		 	->insert('position'   , 'cmd')
		 	->insert('type' 	  , 'cmd')
		 	->insert('installed'  , 'boolean', false);
	}

	protected function _buildQueryJoin(KDatabaseQuery $query)
	{
		$query
			->join('left', 'users AS user', 'user.id = tbl.checked_out')
			->join('left', 'groups AS group', 'group.id = tbl.access')
			->join('left', 'modules_menu AS module_menu', 'module_menu.moduleid = tbl.id');

		parent::_buildQueryJoin($query);
	}

	protected function _buildQueryWhere(KDatabaseQuery $query)
	{
		$state = $this->_state;

		if($state->search) {
			$query->where('tbl.title', 'LIKE', '%'.$state->search.'%');
		}

		if($state->position) {
			$query->where('tbl.position', '=', $state->position);
		}
		
		if($state->type) {
			$query->where('tbl.module', '=', $state->type);
		}

		if($state->enabled !== '' && $state->enabled !== null) {
			$query->where('tbl.published', '=', $state->enabled);
		}
		
		$client	= JApplicationHelper::getClientInfo($state->application, true);
		$query->where('tbl.client_id', '=', $client->id);

		parent::_buildQueryWhere($query);
	}

	/**
	 * Method to get a item object which represents a table row
	 *
	 * If the model state is unique a row is fetched from the database based on the state.
	 * If not, an empty row is be returned instead.
	 *
	 * This method is customized in order to set the default module type on new rows.
	 *
	 * @return KDatabaseRow
	 */
	public function getItem()
	{
		if (!isset($this->_item))
		{
			$this->_item = parent::getItem();

			if($this->_item->isNew() && $this->_state->type) {
				$this->_item->type = $this->_state->type;
			}
		}

		return $this->_item;
	}

    /**
     * Get a list of items
     * 
     * If the installed state is TRUE this function will return a list of the installed
     * modules.
{'fraction_non_alphanumeric': 0.17704312804161773, 'fraction_numerical': 0.0016781339150864238, 'mean_word_length': 3.3952802359882006, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 100, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1791380', 'n_tokens_mistral': 2202, 'n_tokens_neox': 1953, 'n_words': 455}