<commit_before><?php
/**
 * @version 	$Id$
 * @package		ExtranetOffice
 * @subpackage	com_projects
 * @copyright	Copyright (C) 2009 E-noise.com Limited. All rights reserved.
 * @license		BSD revised. See LICENSE.
 * @author 		<NAME> [e-noise.com]
 */

defined( '_EXEC' ) or die( 'Restricted access' );

/**
 * projectsModelProjects Class
 * 
 * @package		ExtranetOffice
 * @subpackage 	com_projects
 * @author 		<NAME> [e-noise.com]
 * @since 		1.0
 * @see 		model
 */
class projectsModelProjects extends model {
	var $config=null;
	var $user=null;
	var $db=null;
	var $view=null;
	var $layout=null;
	var $projectid=null;
	
	/**
	 * Constructor
	 *
	 * @since 1.0
	 */
	function __construct() {
		$this->init();
		parent::__construct();
	}
	
	function init() {
		$this->config =& factory::getConfig();
		$this->user =& factory::getUser();
		$this->db =& factory::getDB();
		
		$this->view = request::getVar('view', 'projects');
		$this->layout = request::getVar('layout', 'list');
		$this->projectid = request::getVar('projectid', 0);
		
		// Check whether project directories exists and are writable
		$projects_upload_dir = _ABS_PATH.DS.$this->config->upload_dir.DS."projects";
		//TODO: Have to catch errors here and look at file permissions
		if (!is_dir($projects_upload_dir)) {
			mkdir($projects_upload_dir, 0771);
		}
		$projects_filesystem = $this->config->filesystem.DS."projects";
		if (!is_dir($projects_filesystem)) {
			mkdir($projects_filesystem, 0771);
		}
			
		// Check whether project specific directories exists and are writable
		if (!empty($this->projectid)) {
			//TODO: Have to catch errors here and look at file permissions
			$project_specific_upload_dir = _ABS_PATH.DS.$this->config->upload_dir.DS."projects".DS.$this->projectid;
			if (!is_dir($project_specific_upload_dir)) {
				mkdir($project_specific_upload_dir, 0771);
			}
			$project_specific_filesystem = $this->config->filesystem.DS."projects".DS.$this->projectid;
			if (!is_dir($project_specific_filesystem)) {
				mkdir($project_specific_filesystem, 0771);
			}
		}
	}
	
	function getProjects($projectid=0) {
		// Only apply filtering and ordering if browsing projects list
		if (empty($projectid)) {
			$filter_order = request::getVar('filter_order', 'p.name');
			$filter_order_Dir = request::getVar('filter_order_Dir', '');
			$search = request::getVar('search', '');
			$search = strtolower( $search );
			$limitstart = request::getVar('limitstart', 0);
			$limit = request::getVar('limit', 20);
		}

		$where = array();
		
		// Show only public projects or projects where user has an assigned role
		$where[] = "( p.access = '0' OR (".$this->user->id." IN (SELECT userid FROM #__users_roles WHERE projectid = p.id) ) )";
		
		if ($search) {
			$where[] = "p.name LIKE '%".$this->db->getEscaped($search)."%'";
		}
		
		if (!empty($projectid)) {
			$where[] = "p.id = ".$projectid;	
		}

		$where = ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );
		
		if (empty($filter_order)) {
			$orderby = ' ORDER BY p.name ';
		} else {
			$orderby = ' ORDER BY '. $filter_order .' '. $filter_order_Dir .', p.name ';
		}
		
		// get the total number of records
		$query = "SELECT 
				  p.id
				  FROM #__projects AS p 
				  JOIN #__users u ON u.id = p.created_by 
				  LEFT JOIN #__project_types pt ON pt.id = p.project_type  
				  LEFT JOIN #__users_roles ur ON p.id = ur.projectid "
				  . $where . 
				  " GROUP BY p.id ";
				  
		$this->db->setQuery($query);
		$this->db->query();
		$total = $this->db->getNumRows();
		
		// get the subset (based on limits) of required records
		$query = "SELECT 
				  p.*, 
				  u.username AS created_by_name, 
				  pt.name AS project_type_name, 
				  GROUP_CONCAT(ur.userid) members
				  FROM #__projects AS p 
				  JOIN #__users u ON u.id = p.created_by 
				  LEFT JOIN #__project_types pt ON pt.id = p.project_type  
				  LEFT JOIN #__users_roles ur ON p.id = ur.projectid "
				  . $where . 
				  " GROUP BY p.id ";
		
		if ($total > 1) {
			$pageNav = new pagination($total, $limitstart, $limit);
			
			$query .= $orderby." LIMIT ".$pageNav->limitstart.", ".$pageNav->limit;
			//echo str_replace('#__', 'eo_', $query); exit;
			$this->db->setQuery($query);
			$rows = $this->db->loadObjectList();
		
			// table ordering
			$lists['order_Dir']	= $filter_order_Dir;
			$lists['order']		= $filter_order;
	
			// search filter
			$lists['search'] = $search;
			
			// pack data into an array to return
			$return['rows'] = $rows;
			$return['pageNav'] = $pageNav;
			$return['lists'] = $lists;
			
			return $return;
		}
		else {
			$this->db->setQuery($query);
			return $this->db->loadObject();
		}
	}

	function saveProject() {
		$row = new iOfficeTableProjects();
		<commit_msg>Fixed small bug in projects model that was making getProjects() return a row object instead of an array with the rows, pageNav and lists when only one result was returned in the list view.<commit_after><?php
/**
 * @version 	$Id$
 * @package		ExtranetOffice
 * @subpackage	com_projects
 * @copyright	Copyright (C) 2009 E-noise.com Limited. All rights reserved.
 * @license		BSD revised. See LICENSE.
 * @author 		<NAME> [e-noise.com]
 */

defined( '_EXEC' ) or die( 'Restricted access' );

/**
 * projectsModelProjects Class
 * 
 * @package		ExtranetOffice
 * @subpackage 	com_projects
 * @author 		<NAME> [e-noise.com]
 * @since 		1.0
 * @see 		model
 */
class projectsModelProjects extends model {
	var $config=null;
	var $user=null;
	var $db=null;
	var $view=null;
	var $layout=null;
	var $projectid=null;
	
	/**
	 * Constructor
	 *
	 * @since 1.0
	 */
	function __construct() {
		$this->init();
		parent::__construct();
	}
	
	function init() {
		$this->config =& factory::getConfig();
		$this->user =& factory::getUser();
		$this->db =& factory::getDB();
		
		$this->view = request::getVar('view', 'projects');
		$this->layout = request::getVar('layout', 'list');
		$this->projectid = request::getVar('projectid', 0);
		
		// Check whether project directories exists and are writable
		$projects_upload_dir = _ABS_PATH.DS.$this->config->upload_dir.DS."projects";
		//TODO: Have to catch errors here and look at file permissions
		if (!is_dir($projects_upload_dir)) {
			mkdir($projects_upload_dir, 0771);
		}
		$projects_filesystem = $this->config->filesystem.DS."projects";
		if (!is_dir($projects_filesystem)) {
			mkdir($projects_filesystem, 0771);
		}
			
		// Check whether project specific directories exists and are writable
		if (!empty($this->projectid)) {
			//TODO: Have to catch errors here and look at file permissions
			$project_specific_upload_dir = _ABS_PATH.DS.$this->config->upload_dir.DS."projects".DS.$this->projectid;
			if (!is_dir($project_specific_upload_dir)) {
				mkdir($project_specific_upload_dir, 0771);
			}
			$project_specific_filesystem = $this->config->filesystem.DS."projects".DS.$this->projectid;
			if (!is_dir($project_specific_filesystem)) {
				mkdir($project_specific_filesystem, 0771);
			}
		}
	}
	
	function getProjects($projectid=0) {
		// Only apply filtering and ordering if browsing projects list
		if (empty($projectid)) {
			$filter_order = request::getVar('filter_order', 'p.name');
			$filter_order_Dir = request::getVar('filter_order_Dir', '');
			$search = request::getVar('search', '');
			$search = strtolower( $search );
			$limitstart = request::getVar('limitstart', 0);
			$limit = request::getVar('limit', 20);
		}

		$where = array();
		
		// Show only public projects or projects where user has an assigned role
		$where[] = "( p.access = '0' OR (".$this->user->id." IN (SELECT userid FROM #__users_roles WHERE projectid = p.id) ) )";
		
		if ($search) {
			$where[] = "p.name LIKE '%".$this->db->getEscaped($search)."%'";
		}
		
		if (!empty($projectid)) {
			$where[] = "p.id = ".$projectid;	
		}

		$where = ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );
		
		if (empty($filter_order)) {
			$orderby = ' ORDER BY p.name ';
		} else {
			$orderby = ' ORDER BY '. $filter_order .' '. $filter_order_Dir .', p.name ';
		}
		
		// get the total number of records
		$query = "SELECT 
				  p.id
				  FROM #__projects AS p 
				  JOIN #__users u ON u.id = p.created_by 
				  LEFT JOIN #__project_types pt ON pt.id = p.project_type  
				  LEFT JOIN #__users_roles ur ON p.id = ur.projectid "
				  . $where . 
				  " GROUP BY p.id ";
				  
		$this->db->setQuery($query);
		$this->db->query();
		$total = $this->db->getNumRows();
		
		// get the subset (based on limits) of required records
		$query = "SELECT 
				  p.*, 
				  u.username AS created_by_name, 
				  pt.name AS project_type_name, 
				  GROUP_CONCAT(ur.userid) members
				  FROM #__projects AS p 
				  JOIN #__users u ON u.id = p.created_by 
				  LEFT JOIN #__project_types pt ON pt.id = p.project_type  
				  LEFT JOIN #__users_roles ur ON p.id = ur.projectid "
				  . $where . 
				  " GROUP BY p.id ";
		
		if (empty($projectid)) {
			$pageNav = new pagination($total, $limitstart, $limit);
			
			$query .= $orderby." LIMIT ".$pageNav->limitstart.", ".$pageNav->limit;
			//echo str_replace('#__', 'eo_', $query); exit;
			$this->db->setQuery($query);
			$rows = $this->db->loadObjectList();
		
			// table ordering
			$lists['order_Dir']	= $filter_order_Dir;
			$lists['order']		= $filter_order;
	
			// search filter
			$lists['search'] = $search;
			
			// pack data into an array to return
			$return['rows'] = $rows;
			$return['pageNav'] = $pageNav;
			$return['lists'] = $lists;
			
			return $return;
		}
		else {
			$this->db->setQuery($query);
			return $this->db->loadObject();
		}
	}

	function saveProject() {
		$row = new iOfficeTableProjects();
		
{'fraction_non_alphanumeric': 0.16228796028134052, 'fraction_numerical': 0.006309474555233761, 'mean_word_length': 3.2878048780487803, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 84, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3943389', 'n_tokens_mistral': 3907, 'n_tokens_neox': 3270, 'n_words': 790}