<?php

/**
 * staff.php
 * Defines staffmember class for staffman module
 *
 * This class is an extension of the PHPWS_Item class defined in core/Item.php.
 * Its function is to create, edit, delete, and view one of the items in a list.
 * The class defines additional variables to the standard ones handled automatically
 * by PHPWS_Item, defines a constructor, edit(), delete(), and view() function, and
 * an action() switch that handles other procedures (here, a save() function) that
 * are needed.
 *
 * The listing of these items is handled by an extension of the PHPWS_Manager
 * class, defined in core/Manager.php.
 *
 * @version $Id: staff.php,v 1.6 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/Item.php");
	require_once(PHPWS_SOURCE_DIR . "core/Template.php");
	require_once(PHPWS_SOURCE_DIR . "core/EZform.php");
	require_once(PHPWS_SOURCE_DIR . "core/Text.php");
	require_once(PHPWS_SOURCE_DIR . "core/Error.php");
}

class staffmember extends PHPWS_Item {

	/**
	* last name of this staffmember is stored in the item's Label field
	*/

	/**
	* first name of this staffmember -- full or part time
	* @var string
	*/
	var $_firstname = NULL;
	/**
	* position or title of this staffmember
	* @var string
	*/
	var $_position = NULL;
	/**
	* contact for this staffmember
	* @var string
	*/
	var $_contact = NULL;
	/**
	* notes for this staffmember
	* @var string
	*/
	var $_notes = NULL;
	/**
	* photo file name for this staffmember
	* @var string
	*/
	var $_photo = NULL;
	/**
	* photo file type for this staffmember
	* @var string
	*/
	var $_type = NULL;
	/**
	* photo image width for this staffmember
	* @var string
	*/
	var $_width = NULL;
	/**
	* photo image height for this staffmember
	* @var string
	*/
	var $_height = NULL;


	/**
	 * Constructor for the staffmember class
	 * Sets all the staffmember attributes
	 *
	 * @param int $staffmember_id id of the staffmember to be constructed; NULL if new staffmember
	 */
	 
	function staffmember($staffmember_id = NULL) {
		// exclude variables we don't need to save
		$exclude= array('_type','_width','_height');		
		$this->addExclude($exclude);
	 
		/* point to the staffmember data table */
		$this->setTable("mod_staffman_staffmember");
		
		/* if the staffmember exists, initialize its variables from the database */
		if (isset($staffmember_id)) {
			$error = $this->setId($staffmember_id);		/* setId returns a PHPWS_Error object */
			if(PHPWS_Error::isError($error)) {
				$error->message('CNT_staffman');		/* report the error, if any */
			}
			$this->init();				/* initialize variables */
		}
	 } //END function staffmember
	 
	
	/**
	 * Edit a staffmember object's data
	 *
	 * @author George Brackett <gbrackett@NOSPAM.luceatlux.com>
	 */
	function edit() {
	
		// Create a 'Back' button using EZform
		// Note: form action defaults to "index.php"
		$form = new EZform("STAFF_BACK");
		$form->add("BACK_BUTTON", "submit", $_SESSION["translate"]->it("Back"));
		$form->add("module", "hidden", "staffman");
		$form->add("STAFF_MAN_op", "hidden", "list");

		$tags = array();
		$tags = $form->getTemplate();
		// vary title for new and edited listings
		if ($this->getId()) {
			$tags["TITLE"] = $_SESSION["translate"]->it("Edit Staff Listing");
		} else {
			$tags["TITLE"] = $_SESSION["translate"]->it("Add Staff Listing");
		}
		if (version_compare($GLOBALS["core"]->version,"0.9.3-2") < 0) {
			$content = $GLOBALS["core"]->processTemplate($tags, "staffman", "back.tpl");
		} else {
			$content = PHPWS_Template::processTemplate($tags, "staffman", "back.tpl");
		}		

		// Set up the edit form -- must be on a separate template or there is mixup with {START_FORM} etc.
		$form = new EZform("STAFF_EDIT");

		$form->add("LASTNAME", "text", htmlspecialchars($this->getLabel()));
		$form->setSize("LASTNAME", 33);
		$form->setMaxSize("LASTNAME", 255);
		$form->setTab("LASTNAME", 1);

		$form->add("FIRSTNAME", "text", htmlspecialchars($this->_firstname));
		$form->setSize("FIRSTNAME", 30);
		$form->setMaxSize("FIRSTNAME", 255);
		$form->setTab("FIRSTNAME", 2);	
		
		$form->add("POSITION", "text", htmlspecialchars($this->_position));
		$form->setSize("POSITION", 30);
		$form->setMaxSize("POSITION", 255);
		$form->setTab("POSITION", 3);
		
		$form->add("CONTACT", "text", htmlspecialchars($this->_contact));
		$form->setSize("CONTACT", 50);
		$form->setMaxSize("CONTACT", 255);
		$form->setTab("CONTACT", 4);
		
		// note do not need to convert special characters for a text area
		$form->add("NOTES", "textarea", $this->_notes);
		$form->setTab("NOTES", 5);
		
		if (isset($this->_photo)) {
			$form->add("PHOTO_REMOVE","checkbox");
			$form->setTab("PHOTO_REMOVE",6);
		} else {
			$form->add("PHOTO", "file");
			$form->setTab("PHOTO", 6);
		}
		$form->add("SUBMIT_BUTTON", "submit", $this->getId() ? $_SESSION["translate"]->it("Update") : $_SESSION["translate"]->it("Save"));
		$form->add("module", "hidden", "staffman");
		$form->add("STAFF_op", "hidden", "save");

		$tags = $form->getTemplate();
		
		// include category selection menu, with help
		$tags["CATEGORY"] = $_SESSION['OBJ_fatcat']->showSelect($this->getId(), "multiple", 3, "staffman");
		$tags["CATEGORY_HELP"] = $_SESSION['OBJ_help']->show_link("staffman", "smCategory");
		$tags["CATEGORY_LABEL"] = $_SESSION['translate']->it("Category");
		
		/* include help links also, to display help for each field to be completed */
		$tags["LASTNAME_LABEL"] = $_SESSION['translate']->it("Last Name");
		$tags["LASTNAME_HELP"] = $_SESSION["OBJ_help"]->show_link("staffman", "smLastName");
		$tags["FIRSTNAME_LABEL"] = $_SESSION['translate']->it("First Name");
		$tags["FIRSTNAME_HELP"] = $_SESSION["OBJ_help"]->show_link("staffman", "smFirstName");
		$tags["POSITION_LABEL"] = $_SESSION['translate']->it("Position");
		$tags["POSITION_HELP"] = $_SESSION["OBJ_help"]->show_link("staffman", "smPosition");
		$tags["CONTACT_LABEL"] = $_SESSION['translate']->it("Contact");
		$tags["CONTACT_HELP"] = $_SESSION["OBJ_help"]->show_link("staffman", "smContact");
		$tags["NOTES_LABEL"] = $_SESSION['translate']->it("Notes");
		$tags["NOTES_HELP"] = $_SESSION["OBJ_help"]->show_link("staffman", "smNotes");
		$tags["PHOTO_LABEL"] = $_SESSION['translate']->it("Photo");
		$tags["PHOTO_HELP"] = $_SESSION["OBJ_help"]->show_link("staffman", "smPhoto");

		if (isset($this->_photo)) {
			$tags["PHOTO"] = $this->_photo;	// just get filename
			$tags["PHOTO_REMOVE_LABEL"] = $_SESSION['translate']->it("Remove Photo");
			if (version_compare($GLOBALS["core"]->version,"0.9.3-2") < 0) {
				$content .= $GLOBALS["core"]->processTemplate($tags, "staffman", "editphoto.tpl");
			} else {
				$content .= PHPWS_Template::processTemplate($tags, "staffman", "editphoto.tpl");
			}
		} else {
			if (version_compare($GLOBALS["core"]->version,"0.9.3-2") < 0) {
				$content .= $GLOBALS["core"]->processTemplate($tags, "staffman", "edit.tpl");
			} else {
				$content .= PHPWS_Template::processTemplate($tags, "staffman", "edit.tpl");
			}
		}
		return $content;
	
	} //END function edit
	
	/**
	 * Asks for confirmation and, getting it, deletes the current staff listing
	 *
	 * @author George Brackett <gbrackett@NOSPAM.luceatlux.com>
	 */
	function delete() {
	
		if (isset($_REQUEST["YES_BUTTON"])) {

			// have confirmation; remove category listing while we know the id
			$_SESSION['OBJ_fatcat']->purge($this->getId(), "staffman");
			
			// delete the current item
			$this->_unlink();	// zap any photo file
			$this->kill();		// function defined in core/item.php
			
			// tell 'em it's done
			$content = $this->_continueContent("Staff listing deleted.", "list");

		} elseif (isset($_REQUEST["NO_BUTTON"])) {

			/* tell user all is still okay */
			$content = $this->_continueContent("Staff listing NOT deleted.", "list");

		} else {
		
			/* ask if the user is sure */
			$form = new EZform("STAFF_DELETE");
			$form->add("YES_BUTTON", "submit", $_SESSION["translate"]->it("Yes"));
			$form->add("NO_BUTTON", "submit", $_SESSION["translate"]->it("No"));
			$form->add("module", "hidden", "staffman");
			$form->add("STAFF_MAN_op", "hidden", "delete");
			// note must indicate with [] that the id variable is in an array
			$form->add("PHPWS_MAN_ITEMS[]", "hidden", $this->getId());
			
			$tags = array();
			$tags = $form->getTemplate();
			
			$tags["MESSAGE"] = $_SESSION["translate"]->it("Delete this staff listing?");
			$tags["LASTNAME_LABEL"] = $_SESSION['translate']->it("Last Name");
			$tags["FIRSTNAME_LABEL"] = $_SESSION['translate']->it("First Name");
			$tags["POSITION_LABEL"] = $_SESSION['translate']->it("Position");
			$tags["CONTACT_LABEL"] = $_SESSION['translate']->it("Contact");
			$tags["NOTES_LABEL"] = $_SESSION['translate']->it("Notes");
			$tags["PHOTO_LABEL"] = $_SESSION['translate']->it("Photo");
			
			$tags["LASTNAME"] = $this->getLabel();
			$tags["FIRSTNAME"] = $this->_firstname;
			$tags["POSITION"] = $this->_position;
			$tags["CONTACT"] = $this->_contact;
			$tags["NOTES"] = $this->_notes;
			$tags["PHOTO"] = $this->_photo;
			
			if (version_compare($GLOBALS["core"]->version,"0.9.3-2") < 0) {
				$content .= $GLOBALS["core"]->processTemplate($tags, "staffman", "confirm.tpl");
			} else {
				$content .= PHPWS_Template::processTemplate($tags, "staffman", "confirm.tpl");
			}
		}
		
		return $content;
	
	} //END function delete

	
	/**
	 * Displays current position listing
	 *
	 * @param Boolean $continuebtn = TRUE if continue button is to be shown
	 * @author George Brackett <gbrackett@NOSPAM.luceatlux.com>
	 */
	function view($continuebtn) {
		
		$tags = array();
		if ($continuebtn) {
			$form = new EZform("STAFF_VIEW");
			$form->add("CONTINUE_BUTTON", "submit", $_SESSION["translate"]->it("Continue"));
			$form->add("module", "hidden", "staffman");
			$form->add("STAFF_MAN_op", "hidden", "list");
			$tags = $form->getTemplate();
		}
		$tags["LASTNAME"] = $this->getLabel();
		$tags["FIRSTNAME"] = $this->_firstname;
		if ($this->_position != "") 		//can't use isset() because a null string is set
			$tags["POSITION"] = $this->_position . "<br />";
		if ($this->_contact != "")
			$tags["CONTACT"] = $this->_contact . "<br />";
		if ($this->_notes != "")
			$tags["NOTES"] = $this->_notes . "<br />";
		$tags["PHOTO"] = $this->_photo;
		// get a list of the categories for this position from FatCat
		$tags["CATEGORY"] = $_SESSION['OBJ_fatcat']->fatcatLinks($this->getId(), "staffman");
		// don't list 'NA' or 'Uncategorized'
		if (strpos($tags["CATEGORY"], "NA") || strpos($tags["CATEGORY"], "Uncategorized")) {
			$tags["CATEGORY"] = "";
		}
		
		if ($this->_photo) {
			if (version_compare($GLOBALS["core"]->version,"0.9.3-2") < 0) {
				$content = $GLOBALS["core"]->processTemplate($tags, "staffman", "viewphoto.tpl");
			} else {
				$content = PHPWS_Template::processTemplate($tags, "staffman", "viewphoto.tpl");
			}
		} else {
			if (version_compare($GLOBALS["core"]->version,"0.9.3-2") < 0) {
				$content = $GLOBALS["core"]->processTemplate($tags, "staffman", "view.tpl");
			} else {
				$content = PHPWS_Template::processTemplate($tags, "staffman", "view.tpl");
			}
		}
		return $content;
	
	
	} //END function view
	

	/**
	 * Deletes current photo file
	 *
	 * @author George Brackett <gbrackett@NOSPAM.luceatlux.com>
	 */
	 function _unlink() {
	 	if (isset($this->_photo)) {
	 		@unlink(STAFFMAN_DIR . $this->_photo);
	 	}
	 	// check that it's gone
	 	if (!is_file(STAFFMAN_DIR . $this->_photo)) {
	 		$this->_photo = NULL;
	 		$this->_type = NULL;
	 		$this->_width = NULL;
	 		$this->_height = NULL;
	 		return TRUE;
	 	} else {
	 		return FALSE;
	 	}
	 	
	 } //END function _unlink
	
	/**
	 * Saves data for staffmember object to database
	 *
	 * @author George Brackett <gbrackett@NOSPAM.luceatlux.com>
	 */
	function _save() {
	
		$newstaffmember = ($this->getId() == NULL);
		
		// we MUST have a last name, since this is the label for the data entry; we'll get errors otherwise
		if (!isset($_REQUEST["LASTNAME"]) || (trim($_REQUEST["LASTNAME"]) == "")) {
			$_REQUEST["LASTNAME"] = $_SESSION['translate']->it("Error");
			$message = $_SESSION['translate']->it("You must enter a last name.");
		}
		$this->setLabel(PHPWS_Text::parseInput($_REQUEST["LASTNAME"]));

		// store the remaining input information but don't commit it to the database yet
		// note that parseInput called this way allows the global set of HTML tags to be included
		// leave photo file name and type alone in case of error
		$this->_firstname = PHPWS_Text::parseInput($_REQUEST["FIRSTNAME"]);
		$this->_position = PHPWS_Text::parseInput($_REQUEST["POSITION"]);
		$this->_contact = PHPWS_Text::parseInput($_REQUEST["CONTACT"]);
		$this->_notes = PHPWS_Text::parseInput($_REQUEST["NOTES"]);
		
		if (isset($message)) {
			$this->_edit_error($message);	// display error message above edit form
			return;
		}

		if (isset($_REQUEST["PHOTO_REMOVE"]) && ($_REQUEST["PHOTO_REMOVE"] == 1)) {	// user wants to zap photo
			if (!$this->_unlink()) {
				$message = $_SESSION['translate']->it("Unable to remove photo file.");
				$this->_edit_error($message);
				return;			
			}	
		}
		
		// now check for upload
		if ($_FILES["PHOTO"]["name"] != "") {	// are they uploading an image?
			if (isset($this->_photo)) {			// and there already is one
				$message =  $_SESSION['translate']->it("You must remove the image before uploading a new one.");
				$this->_edit_error($message);
				return;
			}
			
			// proceed to check that all's well
			$name = $_FILES['PHOTO']['name'];
      		$file = STAFFMAN_DIR . $name;
			// If there's already an image file of that name, give the new file a unique name
			if(is_file($file)) {	
				$name = time() . "_" . $_FILES['PHOTO']['name'];
				$file = STAFFMAN_DIR . $name;
			}

			// get uploaded file
			move_uploaded_file($_FILES['PHOTO']['tmp_name'], $file);

			// check for failed upload
			if (!is_file($file)) {
				$message = $_SESSION['translate']->it("There was a problem uploading the image.");
				$this->_edit_error($message);
				return;
			}
			
			$info = @getimagesize($file);
			// check if file the correct type
			$types = explode(",", STAFFMAN_IMAGE_TYPES);
			if (in_array($_FILES['PHOTO']['type'], $types)) {
				$this->_photo = $name;
				$this->_type = $_FILES['PHOTO']['type'];		// save extra info we may need
				$this->_width = $info[0];
				$this->_height = $info[1];
			} else {
				@unlink($file);		// delete the file
				$message = $_SESSION['translate']->it("The image uploaded was not an allowed image type.");
				$this->_edit_error($message);
				return;
			}

		}
		
		$error = $this->commit();
		if(PHPWS_Error::isError($error)) {
			$message = $_SESSION['translate']->it("There was a problem saving the information to the database.");
			$this->_edit_error($message);
			return;
		}
		
		// save the category
		$element_link = "index.php?module=staffman&amp;STAFF_MAN_op=sView&amp;STAFF_id=" . $this->getId();
		// remove old categories first
		$_SESSION['OBJ_fatcat']->purge($this->getId(), "staffman");
		$_SESSION['OBJ_fatcat']->saveSelect($this->getLabel(), $element_link, $this->getId());
		
		// tell user data is saved or updated
		if ($newstaffmember) {
			$content = $this->_continueContent("Staff listing saved.", "list");
		} else {
			$content = $this->_continueContent("Staff listing updated.", "list");
		}
		$GLOBALS["CNT_staffman"]["title"] = $_SESSION['translate']->it("Staff Listing");		
		$GLOBALS["CNT_staffman"]["content"] = $content;
	
	} //END function _save


	/**
	 * Return return to editing with an error message at the top
	 *
	 * @param  string  $message 	The string to display above the edit form, already translated
	 * @author George Brackett <gbrackett@NOSPAM.luceatlux.com>
	 */
	function _edit_error($message) {
		$GLOBALS["CNT_staffman"]["title"] = "<span class=\"errortext\">" . $_SESSION['translate']->it("Error") . "</span>";
		$GLOBALS["CNT_staffman"]["content"] = $message . "<hr>" . $this->edit();
	} //END function _edit_error


	/**
	 * Return content for a display with a message and a continue button
	 *
	 * @param  string  $message 	The string to display above the continue button, already translated
	 * @param  string  $action 		The STAFF_MAN_op action to perform when Continue is pressed
	 * @author George Brackett <gbrackett@NOSPAM.luceatlux.com>
	 */
	function _continueContent($message, $action) {

		$form = new EZform("STAFF_CONTINUE");
		$form->add("module", "hidden", "staffman");
		$form->add("STAFF_MAN_op", "hidden", $action);
		$form->add("CONTINUE_BUTTON", "submit", $_SESSION["translate"]->it("Continue"));
		
		$tags = array();
		$tags = $form->getTemplate();
		$tags["MESSAGE"] = $_SESSION["translate"]->it($message);
		if (version_compare($GLOBALS["core"]->version,"0.9.3-2") < 0) {
			$content = $GLOBALS["core"]->processTemplate($tags, "staffman", "continue.tpl");
		} else {
			$content = PHPWS_Template::processTemplate($tags, "staffman", "continue.tpl");
		}

		return $content;
	
	} //END function _continueContent
	
	
	/**
	 * Perform actions on staffmember object data (not object)
	 *
	 * @author George Brackett <gbrackett@NOSPAM.luceatlux.com>
	 */
	function action() {
	
		if (isset($_REQUEST["STAFF_op"])) {
			switch($_REQUEST["STAFF_op"]) {
			
				case "save":
					$this->_save();
					break;
				  
			}
		}
		
	}// END function action

}

?>
