'id', 'sName' => 'name', 'sDescription' => 'description', 'iParentID' => 'parent_id', 'iCreatorID' => 'creator_id', 'iUnitID' => 'unit_id', 'bIsPublic' => 'is_public', 'sFullPath' => 'full_path', 'sParentFolderIDs' => 'parent_folder_ids', 'iPermissionObjectID' => 'permission_object_id', 'iPermissionLookupID' => 'permission_lookup_id', 'bRestrictDocumentTypes' => 'restrict_document_types', ); // }}} /** * 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 = null, $sNewDescription = null, $iNewParentID = null, $iNewCreatorID = null, $iNewUnitID = null, $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; } function getID() { return $this->iId; } function getName() { return $this->sName; } function setName($sNewValue) { $this->sName = $sNewValue; } function getDescription() { return $this->sDescription; } function setDescription($sNewValue) { $this->sDescription = $sNewValue; } function getParentID() { return $this->iParentID; } function setParentID($iNewValue) { $this->iParentID = $iNewValue; } function getCreatorID() { return $this->iCreatorID; } function setCreatorID($iNewValue) { $this->iCreatorID = $iNewValue; } function getUnitID() { return $this->iUnitID; } function setUnitID($iNewValue) { $this->iUnitID = $iNewValue; } function getIsPublic() { return $this->bIsPublic; } function setIsPublic($bNewValue) { $this->bIsPublic = $bNewValue; } function getFullPath() { return $this->sFullPath; } function getParentFolderIDs() { return $this->sParentFolderIDs; } function getPermissionObjectID() { return $this->iPermissionObjectID; } function setPermissionObjectID($iPermissionObjectID) { $this->iPermissionObjectID = $iPermissionObjectID; } function getPermissionLookupID() { return $this->iPermissionLookupID; } function setPermissionLookupID($iPermissionLookupID) { $this->iPermissionLookupID = $iPermissionLookupID; } function getRestrictDocumentTypes() { return $this->bRestrictDocumentTypes; } function setRestrictDocumentTypes($bRestrictDocumentTypes) { $this->bRestrictDocumentTypes = $bRestrictDocumentTypes; } // {{{ create() function create () { $oParentFolder =& Folder::get($this->iParentID); $this->iPermissionObjectID = $oParentFolder->getPermissionObjectID(); $res = parent::create(); if ($res === true) { KTPermissionUtil::updatePermissionLookup($this); } return $res; } // }}} /** * Recursive function to 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(array("SELECT parent_id FROM $default->folders_table WHERE ID = ?", $iFolderID));/*ok*/ $sql->next_record(); return Folder::generateParentFolderIDS($sql->f("parent_id")) . ",$iFolderID"; } return; } /** * Returns a comma delimited string containing the parent folder ids, strips leading / * * @return String comma delimited string containing the parent folder ids */ function generateFolderIDs($iFolderID) { $sFolderIDs = Folder::generateParentFolderIDS($iFolderID); return substr($sFolderIDs, 1, strlen($sFolderIDs)); } /** * Recursively generates 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(array("SELECT name, parent_id FROM $default->folders_table WHERE ID = ?", $iFolderID));/*ok*/ $sql->next_record(); return $this->generateFullFolderPath($sql->f("parent_id")) . "/" . $sql->f("name"); } return; } /** * Returns a forward slash deliminated string giving full path of document, strips leading / */ function generateFolderPath($iFolderID) { global $default; $sPath = $this->generateFullFolderPath($iFolderID); $sPath = substr($sPath, 1, strlen($sPath)); $sPath = addslashes($sPath); return $sPath; } function _fieldValues () { $this->sFullPath = $this->generateFolderPath($this->iParentID); $this->sParentFolderIDs = $this->generateFolderIDs($this->iParentID); return parent::_fieldValues(); return array( 'name' => $this->sName, 'description' => $this->sDescription, 'parent_id' => $this->iParentID, 'creator_id' => $this->iCreatorID, 'unit_id' => $this->iUnitID, 'is_public' => KTUtil::anyToBool($this->bIsPublic), 'full_path' => $this->sFullPath, 'parent_folder_ids' => $this->sParentFolderIDs, 'permission_object_id' => $this->iPermissionObjectID, 'permission_lookup_id' => $this->iPermissionLookupID, ); } function _table () { global $default; return $default->folders_table; } /** * Update the current folder values in the database * * @return boolean true on successful update, false otherwise and set $_SESSION["errorMessage"] */ function update($bPathChange = false) { $res = parent::update(); if ($res === true) { if ($bPathChange) { // XXX: TransactionCheckPoint $this->updateChildPaths($this->iId); $this->updateDocumentPaths($this->iId); } } return $res; } 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) { global $default; //get the direct children $sql = $default->db; $sql->query(array("SELECT id from $default->folders_table WHERE parent_id = ?", $iId));/*ok*/ while ($sql->next_record()) { //force an update, this will cause this child's children to be updated $oFolder = Folder::get($sql->f("id")); $oFolder->update(true); } return; } /** * When a folder's path changes, we must update the paths in the * documents in that folder. Sub-folders are handled elsewhere in * update(). */ function updateDocumentPaths($iId) { $aDocuments = Document::getList(array('folder_id = ?', $iId)); if (PEAR::isError($aDocuments)) { return $aDocuments; } foreach ($aDocuments as $oDocument) { // Document->update() automatically adjusts the path. $oDocument->update(); // XXX: Should handle failure here somehow, but rather get // most working than just the first two. Must find a sane // way to handle transactions. // TransactionCheckPoint } return true; } /** * 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) { global $default; $sql = $default->db; $sql->query(array("SELECT id from $default->folders_table WHERE parent_id = ?", $iFolderID));/*ok*/ while ($sql->next_record()) { $aChildren[count($aChildren)] = $sql->f("id"); Folder::getChildren($sql->f("id"), $aChildren); } return $aChildren; } /** * Returns the documents in this folder */ function getDocumentIDs($iFolderID) { global $default; $sql = $default->db; $sql->query(array("SELECT id from $default->documents_table WHERE folder_id = ?", $iFolderID));/*ok*/ while ($sql->next_record()) { $sChildString .= $sql->f("id") . ","; } return substr($sChildString,0, strlen($sChildString)-1); } function &get($iFolderID) { return KTEntityUtil::get('Folder', $iFolderID); } /** * Checks if a folder with the same name and parent exists in the database already */ function exists() { global $default; $sql = $default->db; $sQuery = "SELECT id FROM $default->folders_table WHERE name = ? AND parent_id = ?";/*ok*/ $aParams = array($this->sName, $this->iParentID); $sql->query(array($sQuery, $aParams)); if ($sql->next_record()) { return true; } else { 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) { return KTEntityUtil::getList(Folder::_table(), 'Folder', $sWhereClause); } /** * 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; $oFolder = Folder::get($iFolderID); $sPath = $default->documentRoot . "/" . $oFolder->sFullPath . "/" . $oFolder->getName() . "/"; return $sPath; } /** * 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 = array(); if ($oFolder) { 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; } // {{{ function getPathArray() { return Folder::getFolderPathNamesAsArray($this->getID()); } // }}} /** * 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); if ($oFolder === false) { return false; } if (strlen($oFolder->sParentFolderIDs) > 0) { if ($oFolder->iParentID == 0) { $aPathArray = array(); } else 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; $aPathNamesArray = Folder::getFolderPathNamesAsArray($iFolderID); if (count($aPathNamesArray) > 0) { return implode(" » ", $aPathNamesArray); } else { return ""; } } /** * 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(array("SELECT parent_id FROM " . $default->folders_table . " WHERE id = ?", $iFolderID));/*ok*/ $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(array("SELECT document_type_id FROM " . $default->folder_doctypes_table . " WHERE folder_id = ?", $iFolderID));/*ok*/ 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) { global $default, $lang_err_folder_exist; $sql = $default->db; $sQuery = "SELECT * FROM " . $default->folders_table . " WHERE name = ? AND parent_id = ?";/*ok*/ $aParams = array($sName, $iParentID); $sql->query(array($sQuery, $aParams)); 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(array("SELECT * FROM " . $default->folders_table . " WHERE id = ?", $iFolderID));/*ok*/ 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(array("SELECT name FROM " . $default->folders_table . " WHERE id = ?", $iFolderID));/*ok*/ if ($sql->next_record()) { return $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(array("SELECT id FROM " . $default->folders_table . " WHERE name = ?", $sFolderName));/*ok*/ 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; $sQuery = "SELECT id FROM $default->folder_doctypes_table WHERE document_type_id = ? and folder_id = ?";/*ok*/ $aParams = array($iDocTypeID, $iFolderID); $sql->query(array($sQuery, $aParams)); if ($sql->next_record()) { return true; } return false; } /** * Checks if a folder is a unit's root folder. * A folder is assumed to be a unit's root folder if it has the * same name as a unit and has an appropriate description * */ function folderIsUnitRootFolder($iFolderID) { global $default; $sQuery = "SELECT F.id FROM $default->folders_table AS F " . "INNER JOIN $default->folders_table AS P ON F.parent_id=P.id " . "INNER JOIN $default->units_table AS U ON F.unit_id=U.id " . "WHERE F.id = ? " . " AND P.unit_id<>F.unit_id "; $aParams = array($iFolderID); $sql = $default->db; $sql->query(array($sQuery, $aParams)); if ($sql->next_record()) { return true; } return false; } function getByParentIDAndLookupID($iParentID, $iLookupID) { return KTEntityUtil::getByDict('Folder', array( 'parent_id' => $iParentID, 'permission_lookup_id' => $iLookupID, ), array('multi' => true)); } // STATIC function &createFromArray($aOptions) { return KTEntityUtil::createFromArray('Folder', $aOptions); } } ?>