fileSystemRoot/lib/foldermanagement/PhysicalFolderManagement.inc"); class Folder { /** folder primary key */ var $iId; /** folder name */ var $sName; /** folder description */ var $sDescription; /** folder parent primary key */ var $iParentID; /** primary key of user who created folder */ var $iCreatorID; /** primary key of unit to which this folder belongs */ var $iUnitID; /** public status of folder */ var $bIsPublic; /** comma deliminated string of parent ids */ var $sParentFolderIDs; /** forward slash deliminated path from file system root */ var $sFullPath; /** * Folder class constructor * * @param $sNewName Folder name * @param $sNewDescription Folder description * @param $iNewParentID Primary key of parent folder * @param $iNewCreatorID Primary key of user who created folder * @param $iNewDocumentTypeID Primary key of document type folder will hold * @param $iNewUnitID Primary key of unit to which folder belongs * @param $bNewIsPublic Folder public status (is the folder public or not?) */ function Folder($sNewName, $sNewDescription, $iNewParentID, $iNewCreatorID, $iNewUnitID, $bNewIsPublic = false) { //id of -1 means that the object has not yet been stored in the database $this->iId = -1; $this->sName = $sNewName; $this->sDescription = $sNewDescription; $this->iParentID = $iNewParentID; $this->iCreatorID = $iNewCreatorID; $this->iUnitID = $iNewUnitID; $this->bIsPublic = $bNewIsPublic; } /** * Get the primary key of the current folder object * * @return integer primary key of folder * */ function getID() { return $this->iId; } /** * Get the folder name * * @return String folder name */ function getName() { return $this->sName; } /** * Set the folder name * * @param $sNewValue New folder name */ function setName($sNewValue) { $this->sName = $sNewValue; } /** * Get the folder description * * @return String folder description */ function getDescription() { return $this->sDescription; } /** * Set the folder description * * @param $sNewValue New folder description * */ function setDescription($sNewValue) { $this->sDescription = $sNewValue; } /** * Get the parent folder primary key * * @return integer parent folder primary key * */ function getParentID() { return $this->iParentID; } /** * Set the parent folder primary key * * @param $iNewValue New parent folder primary key * */ function setParentID($iNewValue) { $this->iParentID = $iNewValue; } /** * Get the unit primary key * * @return integer primary key of unit to which the folder belongs * */ function getUnitID() { return $this->iUnitID; } /** * Set the unit prinary key * * @param $iNewValue New primary key of unit to which folder belongs * */ function setUnitID($iNewValue) { $this->iUnitID = $iNewValue; } /** * Get the folder public status * * @return boolean true if folder is public, false if folder is not * */ function getIsPublic() { return $this->bIsPublic; } /** * Set the folder public status * * @param $bNewValue New folder public status * */ function setIsPublic($bNewValue) { $this->bIsPublic = $bNewValue; } function getFullPath() { return $this->sFullPath; } function getParentFolderIDs() { return $this->sParentFolderIDs; } /** * Generate a comma delimited string containing * the parent folder ids * * @return String comma delimited string containing the parent folder ids */ function generateParentFolderIDS($iFolderID) { global $default; //if the folder is not the root folder if ($iFolderID != 0) { $sql = $default->db; $sql->query("SELECT parent_id FROM $default->owl_folders_table WHERE ID = $iFolderID"); $sql->next_record(); return $this->generateParentFolderIDS($sql->f("parent_id")) . ",$iFolderID"; } return; } /** * Forward slash deliminated string giving full path of document * from file system root url * */ function generateFullFolderPath($iFolderID) { global $default; //if the folder is not the root folder if ($iFolderID != 0) { $sql = $default->db; $sql->query("SELECT name, parent_id FROM $default->owl_folders_table WHERE ID = $iFolderID"); $sql->next_record(); return $this->generateFullFolderPath($sql->f("parent_id")) . "/" . stripslashes($sql->f("name")); } return; } /** * Create the current folder in the database * * @return boolean true and set $this->iId with new primary key, false otherwise and set $_SESSION["errorMessage"] */ function create() { global $default, $lang_err_database; $lang_err_object_exists; //if the object has not already been stored if ($this->iId < 0) { $this->sFullPath = $this->generateFullFolderPath($this->iParentID); $this->sFullPath = substr($this->sFullPath, 1, strlen($this->sFullPath)); $this->sParentFolderIDs = $this->generateParentFolderIDS($this->iParentID); $this->sParentFolderIDs = substr($this->sParentFolderIDs, 1, strlen($this->sParentFolderIDs)); $sql = $default->db; $result = $sql->query("INSERT INTO " . $default->owl_folders_table . " (name, description, parent_id, creator_id, unit_id, is_public, full_path, parent_folder_ids) " . "VALUES ('" . addslashes($this->sName) . "', '" . addslashes($this->sDescription) . "', $this->iParentID, $this->iCreatorID, $this->iUnitID, " . ($this->bIsPublic ? 1 : 0) . ",'" . addslashes($this->sFullPath) . "','" . addslashes($this->sParentFolderIDs) . "')"); if ($result) { $this->iId = $sql->insert_id(); return true; } $_SESSION["errorMessage"] = $lang_err_database; return false; } $_SESSION["errorMessage"] = $lang_err_object_exists . "id = " . $this->iId . " table = folders"; return false; } /** * Update the current folder values in the database * * @return boolean true on successful update, false otherwise and set $_SESSION["errorMessage"] */ function update($bPathChange) { global $default, $lang_err_database, $lang_err_object_key; //can only update the object if it has already been stored $sOldPath; if ($this->iId >= 0) { if ($bPathChange){ $sOldPath = $default->documentRoot . "/" . $this->sFullPath . "/" . $this->sName; } $sql = $default->db; $sQuery = "UPDATE " . $default->owl_folders_table . " SET " . "name = '" . addslashes($this->sName) . "', " . "description = '" . addslashes($this->sDescription) . "', " . "parent_id = $this->iParentID, " . "creator_id = $this->iCreatorID, " . "unit_id = $this->iUnitID, "; if ($bPathChange) { $sFullPath = $this->generateFullFolderPath($this->iParentID); $this->sFullPath = substr($sFullPath, 1, strlen($sFullPath)); $sParentFolderIDs = $this->generateParentFolderIDS($this->iParentID); $this->sParentFolderIDs = substr($sParentFolderIDs, 1, strlen($sParentFolderIDs)); $sQuery .= "parent_folder_ids = '" . addslashes($this->sParentFolderIDs) . "'," . "full_path = '" . addslashes($this->sFullPath) . "', "; } $sQuery .= "is_public = " . ($this->bIsPublic ? 1 : 0) . " " . "WHERE id = " . $this->iId; $result = $sql->query($sQuery); if ($result) { if ($bPathChange) { //must update all the children paths $this->updateChildPaths($this->iId, $sql); $this->renameFolder($sOldPath); } return true; } $_SESSION["errorMessage"] = $lang_err_database; return false; } $_SESSION["errorMessage"] = $lang_err_object_key; } function renameFolder($sOldPath) { PhysicalFolderManagement::renameFolder($sOldPath, $default->documentRoot . "/" . $this->sFullPath . "/" . $this->sName); } /** * When a folder is renamed, we must update * the paths of the children in the database * */ function updateChildPaths($iID, $sql) { global $default; $sql->query("SELECT id from $default->owl_folders_table WHERE parent_id = $iID"); while ($sql->next_record()) { $oFolder = Folder::get($sql->f("id")); $oFolder->update(true); $this->updateChildPaths($sql->f("id"), $sql); } return; } /** * Get a folder's sub-folders * * @param int primary key of folder to get children for * * @return Array array of child ids */ function getChildren($iFolderID, $aChildren = array()) { global $default; $sql = $default->db; $sql->query("SELECT id from $default->owl_folders_table WHERE parent_id = $iFolderID"); while ($sql->next_record()) { $aChildren[count($aChildren)] = $sql->f("id"); Folder::getChildren($sql->f("id"), & $aChildren); //$sChildString .= $sql->f("id") . "," . Folder::getChildren($sql->f("id")); } //return $sChildString; return $aChildren; } /** * Delete the current object from the database * * @return boolean true and reset id to -1 on successful delete, false otherwise and set $_SESSION["errorMessage"] */ function delete() { global $default, $lang_err_database, $lang_err_object_key; if ($this->iId >= 0) { $sql = $default->db; $result = $sql->query("DELETE FROM " . $default->owl_folders_table . " WHERE id = " . $this->iId); if ($result) { $this->iId = -1; return true; } $_SESSION["errorMessage"] = $lang_err_database; return false; } $_SESSION["errorMessage"] = $lang_err_object_key; return false; } /** * Static function. * Given a folder primary key will create * a folder object and populate it with the corresponding * database values * * @param $iFolderID Primary key of folder to get * * @return Folder folder object on successful retrieval, false otherwise and set $_SESSION["errorMessage"] */ function get($iFolderID) { global $default, $lang_err_object_not_exist; $sql = $default->db; $sql->query("SELECT * FROM " . $default->owl_folders_table . " WHERE id = " . $iFolderID); if ($sql->next_record()) { $oFolder = & new Folder(stripslashes($sql->f("name")), stripslashes($sql->f("description")), $sql->f("parent_id"), $sql->f("creator_id"), $sql->f("unit_id"), $sql->f("is_public")); $oFolder->iId = $iFolderID; $oFolder->sFullPath = stripslashes($sql->f("full_path")); $oFolder->sParentFolderIDs = stripslashes($sql->f("parent_folder_ids")); return $oFolder; } $_SESSION["errorMessage"] = $lang_err_object_not_exist . "id = " . $iFolderID . " table = folders"; return false; } /** * Static function * Get a list of Documents * * @param String Where clause (not required) * * @return Array array of Documents objects, false otherwise and set $_SESSION["errorMessage"] */ function getList($sWhereClause = null) { global $default, $lang_err_database; $aFolderArray; settype($aFolderArray, "array"); $sql = $default->db; // TODO: join on sys_deleted $result = $sql->query("SELECT * FROM " . $default->owl_folders_table . (isset($sWhereClause) ? " WHERE " . $sWhereClause : "")); if ($result) { $iCount = 0; while ($sql->next_record()) { $oFolder = & Folder::get($sql->f("id")); $aFolderArray[$iCount] = $oFolder; $iCount++; } return $aFolderArray; } $_SESSION["errorMessage"] = $lang_err_database; return false; } /** * Static function. * Get the full path for a folder * * @param Primary key of folder to generate path for * * @return String full path of folder */ function getFolderPath($iFolderID) { global $default; //if the folder has a parent /*if (Folder::getParentFolderID($iFolderID) != 0) { $sCurrentPath = Folder::getFolderPath(Folder::getParentFolderID($iFolderID)) . Folder::getFolderName($iFolderID) . "/" . $sCurrentPath; return $sCurrentPath; } return $default->documentRoot . "/" . Folder::getFolderName($iFolderID) . "/";*/ $oFolder = Folder::get($iFolderID); return $default->documentRoot . "/" . $oFolder->sFullPath . "/" . $oFolder->getName() . "/"; } /** * Static function. * Get the full path for a folder as an array * * @param int primary key of folder to generate path for * * @return array full path of folder as an array of folderIDs */ function getFolderPathNamesAsArray($iFolderID) { global $default; $oFolder = Folder::get($iFolderID); $aPathArray; if (strlen($oFolder->sFullPath) > 0) { if (strlen($oFolder->sFullPath) > 1) { $aPathArray = explode("/",$oFolder->sFullPath); } else { $aPathArray = array($oFolder->sFullPath); } $aPathArray[count($aPathArray)] = $oFolder->getName(); } else { $aPathArray = array($oFolder->getName()); } return $aPathArray; } /** * Static function. * Get the full path for a folder as an array * * @param int primary key of folder to generate path for * * @return array full path of folder as an array of folderIDs */ function getFolderPathAsArray($iFolderID) { global $default; $oFolder = Folder::get($iFolderID); $aPathArray; if (strlen($oFolder->sParentFolderIDs) > 0) { if (strlen($oFolder->sParentFolderIDs) > 1) { $aPathArray = explode(",",$oFolder->sParentFolderIDs); } else { $aPathArray = array($oFolder->sParentFolderIDs); } $aPathArray[count($aPathArray)] = $oFolder->getID(); } else { $aPathArray = array($oFolder->getID()); } return $aPathArray; } /** * Static function. * Get the path for a folder that will be displated to the user * * @param Primary key of folder to generate path for * * @return String full path of folder */ function getFolderDisplayPath($iFolderID) { global $default; //if the folder has a parent $oFolder = Folder::get($iFolderID); return (strlen($oFolder->sFullPath) > 0 ? $oFolder->sFullPath . " > " : "") . $oFolder->getName(); } /** * Static function * Get the primary key of the parent folder * * @param $iFolderID Primary key of folder to get parent for * * @return integer primary key of parent folder */ function getParentFolderID($iFolderID) { if ($iFolderID != 0) { global $default; $sql = $default->db; $sql->query("SELECT parent_id FROM " . $default->owl_folders_table . " WHERE id = " . $iFolderID); $sql->next_record(); return $sql->f("parent_id"); } return 0; } /** * Static function * Gets the default document type for a folder (where default is simply the first one) * * @param $iFolderID * * @return integer document type primary key, false otherwise and set $_SESSION["errorMessage"] */ function getDefaultFolderDocumentType($iFolderID) { global $default, $lang_err_database; if (Folder::folderExistsID($iFolderID)) { $sql = $default->db; $sql->query("SELECT document_type_id FROM " . $default->owl_folder_doctypes_table . " WHERE folder_id = " . $iFolderID); if ($sql->next_record()) { return $sql->f("document_type_id"); } $_SESSION["errorMessage"] = $lang_err_database; } //error message set by Folder::folderExists return false; } /** * Static function * Checks if a given folder already exists using the folder name * * @param $sName Name of folder * @param $iParentID Primary key of parent folder * * @return true if the folder exists, false otherwise and set $_SESSION["errorMessage"] */ function folderExistsName($sName, $iParentID) { $sName = addslashes($sName); global $default, $lang_err_folder_exist; $sql = $default->db; $sql->query("SELECT * FROM " . $default->owl_folders_table . " WHERE name = '" . $sName . "' AND parent_id = " . $iParentID); if ($sql->next_record()) { return true; } $_SESSION["errorMessage"] = $lang_err_folder_exist . $sName . " parent_id " . $iParentID; return false; } /** * Checks if a given folder already exists using the folder name * * @param $iFolderID Primary key of folder * * @return true if the folder exists, false otherwise and set $_SESSION["errorMessage"] */ function folderExistsID($iFolderID) { global $default, $lang_err_folder_exist; $sql = $default->db; $sql->query("SELECT * FROM " . $default->owl_folders_table . " WHERE id = " . $iFolderID); if ($sql->next_record()) { return true; } $_SESSION["errorMessage"] =$lang_err_folder_exist . "id " . $iFolderID; return false; } /** * Get the folder name using the primary key * * @param int primary key of folder to get name for * * @return String name on success, false otherwise and set $_SESSION["errorMessage"] */ function getFolderName($iFolderID) { global $default, $lang_err_database; $sql = $default->db; $sql->query("SELECT name FROM " . $default->owl_folders_table . " WHERE id = " . $iFolderID); if ($sql->next_record()) { return stripslashes($sql->f("name")); } $_SESSION["errorMessage"] = $lang_err_database; return false; } /** * Get the folder id using the folder name * * @param string the name of the folder to get the ID for * * @return int name on success, false otherwise and set $_SESSION["errorMessage"] */ function getFolderID($sFolderName) { global $default, $lang_err_database; $sql = $default->db; $sql->query("SELECT id FROM " . $default->owl_folders_table . " WHERE name = '" . addslashes($sFolderName) . "'"); if ($sql->next_record()) { return $sql->f("id"); } $_SESSION["errorMessage"] = $lang_err_database; return false; } /** * Checks if a document type is already * linked to a folder * */ function folderIsLinkedToDocType($iFolderID, $iDocTypeID) { global $default; $sql = $default->db; $sql->query("SELECT id FROM $default->owl_folder_doctypes_table WHERE document_type_id = $iDocTypeID and folder_id = $iFolderID"); if ($sql->next_record()) { return true; } return false; } } ?>