<?php

/**
 * staffmanager.php
 * Defines the STAFF_ListManager class, which manages the list of individual staff members
 *
 * This class is an extension of the PHPWS_Manager class defined in core/Manager.php.
 * Its function is to display a sortable list of items, and to provide an interface
 * to the editing, viewing and deleting of individual items as specified in an
 * extension of the PHPWS_Item class defined in core/Item.php.  It does so by
 * over-riding the standard functions _list(), _edit(), _view(), and _delete() defined in
 * Manager.php, and adding an action() function to handle additional procedures, in
 * this case the addition of a new staffmember to the list, the viewing of on or all items, 
 * and the creation by administrators of a menu item leading to the listing.
 * item to display the list.
 *
 * @version $Id: staffmanager.php,v 1.7 2004/12/16 16:15:56 gbrackett Exp $
 * @author  George Brackett <gbrackett@NOSPAM.luceatlux.com>
 *
 */
 
/* Make change required for 0.9.3-2 and above */
if (version_compare($GLOBALS["core"]->version, "0.9.3-1") > 0) {
	require_once(PHPWS_SOURCE_DIR . "core/Manager.php");
	require_once(PHPWS_SOURCE_DIR . "core/Template.php");
}

class STAFF_ListManager extends PHPWS_Manager {
 
   /**
	 * Current staffmember listing
	 *
	 * @var    staffmember object
	 * @access private
	 */
	var $staffmember;
   
	/**
	 * Constructor for staff list manager object
	 *
	 * @author George Brackett <gbrackett@NOSPAM.luceatlux.com>
	 */
	function STAFF_ListManager() {
		$this->setModule("staffman");		/* tells PHPWS_Manager where the files are */
		$this->setRequest("STAFF_MAN_op");	/* tells PHPWS_Manager the list manager opcode name */
		$this->setTable("mod_staffman_staffmember");	/* tells PHPWS_Manager the table containing items to list */
		$this->init();	/* initializes the list manager object */
	} // END function STAFF_ListManager


	/**
	 * Provides standard list function: list all items in form appropriate for user
	 * If function is named _list(), is called directly by managerAction() via index.php
	 *
	 * @author George Brackett <gbrackett@NOSPAM.luceatlux.com>
	 */
	function _list() {
	
		/* if user has admin privileges, display menu with "Add" button and admin format list */
		if ($_SESSION["OBJ_user"]->isDeity() || $_SESSION["OBJ_user"]->allow_access("staffman")) {

			/* set tags for the template */
		    $tags["ADD_STAFF_LABEL"] = $_SESSION["translate"]->it("Add Staff Listing");
		    /* add help for this button using mod/help -- see conf/help.php */
		    $tags["ADD_STAFF_HELP"] = $_SESSION["OBJ_help"]->show_link("staffman", "smAddStaffmember");
		    $tags["CREATE_MENU_LINK_LABEL"] = $_SESSION["translate"]->it("Create Menu Link");
		    $tags["CREATE_MENU_LINK_HELP"] = $_SESSION["OBJ_help"]->show_link("staffman", "smAddMenuLink");
			$tags["VIEW_ALL_LABEL"] = $_SESSION['translate']->it("View All");
			$tags["VIEW_ALL_HELP"] = $_SESSION['OBJ_help']->show_link("staffman", "smViewAll");
			/* and process the template to produce the menu  */
			if (version_compare($GLOBALS["core"]->version,"0.9.3-2") < 0) {
		    	$content = $GLOBALS["core"]->processTemplate($tags, "staffman", "amenu.tpl");
		    } else {
		    	$content = PHPWS_Template::processTemplate($tags, "staffman", "amenu.tpl");
		    }
		    /* add admin form of list */
			$content .= $this->getList("admin", $_SESSION['translate']->it("Listing Staffmembers"));		

		} else { 	/* otherwise, display user format list */

			$tags["VIEW_ALL_LABEL"] = $_SESSION['translate']->it("View All");
			$tags["VIEW_ALL_HELP"] = $_SESSION['OBJ_help']->show_link("staffman", "smViewAll");
			if (version_compare($GLOBALS["core"]->version,"0.9.3-2") < 0) {
				$content = $GLOBALS["core"]->processTemplate($tags, "staffman", "umenu.tpl");
			} else {
				$content = PHPWS_Template::processTemplate($tags, "staffman", "umenu.tpl");
			}
			$content .= $this->getList("user", $_SESSION['translate']->it("Listing Staffmembers"));
		}

		/* set display variables */
		$GLOBALS["CNT_staffman"]["title"] = $_SESSION['translate']->it("Staff Listing");
		$GLOBALS["CNT_staffman"]["content"] = $content;

	} //END function _list
	
	/**
	 * Provides standard edit function: edits a list item
	 * If called _edit, is called directly by managerAction() via index.php
	 * Note that managerAction only calls this function if there is an item to edit
	 *
	 * @author George Brackett <gbrackett@NOSPAM.luceatlux.com>
	 */

	function _edit() {
		// warn only one edit at a time
		if (sizeof($_REQUEST["PHPWS_MAN_ITEMS"]) > 1) {
			$content = "<span class=\"errortext\">" . $_SESSION['translate']->it("You may only edit one staffmember listing at a time.") . "</span><br />";
		} else {
			$content = "";
		}

		$this->staffmember = new staffmember($_REQUEST["PHPWS_MAN_ITEMS"][0]);		/* PHPWS_MAN_ITEMS[0] contains the staffmemberid */
		$content .= $this->staffmember->edit();
	
		/* set display variables */
		$GLOBALS["CNT_staffman"]["title"] = $_SESSION['translate']->it("Staff Listing");
		$GLOBALS["CNT_staffman"]["content"] = $content;

	} //END function edit
	
	/**
	 * Provides standard delete function: deletes a list item
	 * If called _delete, is called directly by managerAction() via index.php
	 * Note that managerAction only calls this function if there is an item to delete
	 *
	 * @author George Brackett <gbrackett@NOSPAM.luceatlux.com>
	 */
	function _delete() {
		// warn only one deletion at a time
		if (sizeof($_REQUEST["PHPWS_MAN_ITEMS"]) > 1) {
			$content = "<span class=\"errortext\">" . $_SESSION['translate']->it("You may only delete one staffmember listing at a time.") . "</span><br />";
		} else {
			$content = "";
		}
		$this->staffmember = new staffmember($_REQUEST["PHPWS_MAN_ITEMS"][0]); 		/* PHPWS_MAN_ITEMS[0] contains the staffmemberid */
		$content .= $this->staffmember->delete();
		
		/* set display variables */
		$GLOBALS["CNT_staffman"]["content"] = $content;
		$GLOBALS["CNT_staffman"]["title"] = $_SESSION['translate']->it("Staff Listing");

	} // END function _delete
	
	/**
	 * Provides standard view function: displays a list item
	 * If called _view, is called directly by managerAction() via index.php
	 * Note that managerAction only calls this function if there is an item or
	 * an entire list to view
	 *
	 * @author George Brackett <gbrackett@NOSPAM.luceatlux.com>
	 */
	function _view() {

		/* PHPWS_MAN_ITEMS[0] contains an array of staffmember ids */
		$GLOBALS["CNT_staffman"]["content"] = "";
		if (isset($_REQUEST["PHPWS_MAN_ITEMS"][0])) {		//if list is not empty
			// ask for a continue button only for the last id
			$i = 0;
			foreach($_REQUEST["PHPWS_MAN_ITEMS"] as $staffid) {
				$this->staffmember = new staffmember($staffid); 		
				$content = $this->staffmember->view($i == (sizeof($_REQUEST["PHPWS_MAN_ITEMS"]) - 1));
				$GLOBALS["CNT_staffman"]["content"] .= $content;
				$i++;
			}
		} else { //empty list
			$content = "There are no items in the current list.<br />";
			$this->staffmember = new staffmember();
			$content .= $this->staffmember->view(TRUE); 		//display continue button
			$GLOBALS["CNT_staffman"]["content"] = $content;
		}
		$GLOBALS["CNT_staffman"]["title"] = $_SESSION['translate']->it("Staff Listing");

	} //END function _view
	

	/**
	 * Perform non-standard list manager actions
	 *
	 * @author George Brackett <gbrackett@NOSPAM.luceatlux.com>
	 */
	function action() {
		switch($_REQUEST["STAFF_MAN_op"]) {
			case "Add":
				$this->staffmember = new staffmember();				/* create an 'empty' staffmember object (ref staff.php) */
				$content = $this->staffmember->edit();		/* and edit it */
				$GLOBALS["CNT_staffman"]["title"] = $_SESSION['translate']->it("Staff Listing");
				$GLOBALS["CNT_staffman"]["content"] = $content;
				break;
				
			case "sView":		/* view an item chosen from search results */
				$this->staffmember = new staffmember($_REQUEST["STAFF_id"]);
				$content = $this->staffmember->view(TRUE);		// show continue button
				
				/* set display variables */
				$GLOBALS["CNT_staffman"]["content"] = $content;
				$GLOBALS["CNT_staffman"]["title"] = $_SESSION['translate']->it("Staff Listing");
				break;
				
			case "aView":	/* view all items in the list */
				$this->listName = "user";   // use sort from current user list
				$this->setOrder("label");	// order by last name
				$_REQUEST["PHPWS_MAN_ITEMS"] = $this->_getIds();	//get array of all item ids
				$this->_view();			// and display the corresponding items
				break;
			
			case "Menu":
				/* allow admin to add a menu item pointing to staff list */
				if ($_SESSION["OBJ_user"]->isDeity() || $_SESSION["OBJ_user"]->allow_access("staffman")) {
					if($GLOBALS['core']->moduleExists("menuman")) {
						$_SESSION['OBJ_menuman']->add_module_item("staffman", "&amp;STAFF_MAN_op=list",  "./index.php?module=staffman&amp;STAFF_MAN_op=list", 1);
					}
				}	
				break;	
		}
	
	} //END function action

	
	/**
	 * Searches the list of positions using the mod/search module
	 * Configuration for the search appears in boost/install.php, where
	 * this module is registered with the search module.  Also appears in
	 * conf/search.php for use by phpWS versions > 0.9.3-2.
	 * This function is called by the search module itself, from an accessory block.
	 *
	 * @param $where is a SELECT WHERE clause, passed from search module as
	 * 		specified by user's search terms
	 * @author George Brackett <gbrackett@NOSPAM.luceatlux.com>
	 */

	function search($where) {
		/* set up database query using input from search module */
		$sql = "SELECT id, label FROM " . $GLOBALS['core']->tbl_prefix . "mod_staffman_staffmember " . $where . " AND hidden='0'";
		$linkResult = $GLOBALS['core']->query($sql);			/* perform the selection from the database */

		/* if select returns any database rows */
		if($linkResult->numrows()) {
			/* create an array of the results */
			while($link = $linkResult->fetchrow(DB_FETCHMODE_ASSOC)) {
				$results[$link['id']] = $link['label'];
			}
			return $results;	/* return results */
		} else {
			return FALSE;		/* indicate no results */
		}
	} //END function search
	

} //END class STAFF_ListManager
?>
