Group.inc 7.06 KB
<?php
/**
 * $Id$
 *
 * Represents a Group as per the database table groups.
 *
 * Copyright (c) 2003 Jam Warehouse http://www.jamwarehouse.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @version $Revision$
 * @author Rob Cherry, Jam Warehouse (Pty) Ltd, South Africa
 * @package lib.groups 
 */
class Group extends KTEntity {
	
	/** primary key of current object */
	var $iId;
	/** group name */
	var $sName;
	/** is the group a unit admin */
	var $bIsUnitAdmin;
	/** is the group a sys admin */
	var $bIsSysAdmin;
	
	
	function Group($sNewName, $bNewIsUnitAdmin = false, $bNewIsSysAdmin = false) {
		$this->iId = -1;
		$this->sName = $sNewName;
		$this->bIsUnitAdmin = $bNewIsUnitAdmin;
		$this->bIsSysAdmin = $bNewIsSysAdmin;
	}
	
	function getUnitAdmin() {
		return $this->bIsUnitAdmin;
	}
	
	function setUnitAdmin($bNewValue) {
		$this->bIsUnitAdmin = $bNewValue;
	}
	
	function setID($iNewValue) {
		$this->iId = $iNewValue;
	}
	
	function getID() {
		return $this->iId;
	}
	
	function getSysAdmin() {
		return $this->bIsSysAdmin;
	}
	
	function setSysAdmin($bNewValue) {
		$this->bIsSysAdmin = $bNewValue;
	}
	
	function getName() {
		return $this->sName;
	}
	
	function setName($sNewValue) {
		$this->sName = $sNewValue;
	}
	

	/**
	 * Checks if this group has users mapped to it or not
	 */
	function hasUsers() {
		global $default;
		$sql = $default->db;
		$sql->query(array("SELECT id FROM $default->users_groups_table WHERE group_id = ?", $this->iId));/*ok*/
       	$rows = $sql->num_rows();
        if ($rows > 0) {
        	return true;
        } else {
        	return false;
        }
	}

	/**
	 * Checks if this group has users mapped to it or not
	 */	
	function hasUnit() {
		global $default;

		$sql = $default->db;
		$query = "SELECT id FROM $default->groups_units_table WHERE group_id = ?";/*ok*/
        $aParams = array($this->iId);
	    $sql->query(array($query, $aParams));
	   	$rows = $sql->num_rows();
		if ($rows > 0){
	    	return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Checks if this group has users mapped to it or not
	 */	
	function hasRoutingSteps() {
		global $default;

		$sql = $default->db;
		$query = "SELECT id FROM $default->groups_folders_approval_table WHERE group_id = ?";/*ok*/
        $aParams = array($this->iId);
	    $sql->query(array($query, $aParams));
	   	$rows = $sql->num_rows();
		if ($rows > 0){
	    	return true;
		} else {
			return false;
		}
	}

    function _fieldValues () {
        return array(
            'name' => $this->sName,
            'is_sys_admin' => KTUtil::anyToBool($this->bIsSysAdmin),
            'is_unit_admin' => KTUtil::anyToBool($this->bIsUnitAdmin),
        );
    }

    function _table () {
        global $default;
        return $default->groups_table;
    }
		
	/**
	* Static function.
	* Given a groups primary key it will create a 
	* Group object and populate it with the 
	* corresponding database values
	*
	* @return Group populated Group object on successful query, false otherwise and set $_SESSION["errorMessage"]
	*/
	function & get($iGroupID) {
		global $default;
		$sql = $default->db;
		$result = $sql->query(array("SELECT * FROM $default->groups_table WHERE id = ?", $iGroupID));/*ok*/
		if ($result) {
			if ($sql->next_record()) {
				$oGroup = & new Group($sql->f("name"), $sql->f("is_unit_admin"), $sql->f("is_sys_admin"));
				$oGroup->iId = $iGroupID;
				return $oGroup;
			}
			$_SESSION["errorMessage"] = $lang_err_object_not_exist . "id = " . $iGroupID . " table = $default->groups_table";
			return false;
		}
		$_SESSION["errorMessage"] = $lang_err_database;
		return false;
	}

   /**
	* Static function
	* Get a list of web documents
	*
	* @param 	String		Where clause (not required)
	*
	* @return Array array of Group objects, false otherwise and set $_SESSION["errorMessage"]
	*/
	function getList($sWhereClause = null) {
            return KTEntityUtil::getList(Group::_table(), 'Group', $sWhereClause);
	}
	
	/**
	 * Returns an array of Users in this group.
	 */
	function getUsers() {
		global $default;
		$sql = $default->db;
		$result = $sql->query(array("SELECT user_id FROM $default->users_groups_table WHERE group_id = ?", $this->iId));/*ok*/
		$aUsers = array();		
		if ($result) {			
			$iCount = 0;
			while ($sql->next_record()) {
				$oUser = & User::get($sql->f("user_id"));
				$aUsers[$iCount++] = $oUser;
			}
		}
		return $aUsers;		
	}
	
    /**
     * static function
     *
     * gets the name of a unit using their id
     *
     * @param   String
     *   The name
     *   
     */
	function getGroupName($id) {
        global $default;
        $sName = lookupField("$default->groups_table", "name", "id", $id );
        return $sName;
	}

    function &getMembers() {
        require_once(KT_LIB_DIR . '/groups/GroupUserLink.inc');
        require_once(KT_LIB_DIR . '/users/User.inc');
        $aLinks = GroupUserLink::getList(array('group_id = ?', $this->getID()));
        $aMembers = array();
        foreach ($aLinks as $oLink) {
            $oUser = User::get($oLink->getUserID());
            if ($oUser !== false) {
                $aMembers[] =& $oUser;
            }
        }
        return $aMembers;
    }

    function delete() {
        require_once(KT_LIB_DIR . '/groups/GroupUserLink.inc');
        require_once(KT_LIB_DIR . '/groups/GroupUnitLink.inc');

        // XXX: Urg, should I just DELETE instead?
        $aLinks = GroupUserLink::getList(array('group_id = ?', $this->getID()));
        foreach ($aLinks as $oLink) {
            $oLink->delete();
        }

        $aLinks = GroupUnitLink::getList(array('group_id = ?', $this->getID()));
        foreach ($aLinks as $oLink) {
            $oLink->delete();
        }
        return parent::delete();
    }
}


/*
     * static function
     *
     * gets the name of a unit using their id
     *
     * @param   String
     *   The name
     *   
     */
     
	function getUnitName($id)
	 {
	       global $default;
	
	        $name = lookupField("$default->groups_table", "name", "id", $id );
	        
	        $this->sName= $name;
	}



/**
* Static function
*
* Creates a Group object from an array
*
* @param 	Array		Array of parameters.  Must match order of parameters in constructor
*
* @return User user object
*/
function & groupCreateFromArray($aParameters) {
	$oGroup = & new Group($aParameters[0], $aParameters[1], $aParameters[2], $aParameters[3], $aParameters[4], $aParameters[5], $aParameters[6], $aParameters[7], $aParameters[8], $aParameters[9], $aParameters[10]);
	return $oGroup;
}

?>