From 2d2b0f200d620e980c42a149695a645673a0ab6a Mon Sep 17 00:00:00 2001 From: nbm Date: Thu, 15 Sep 2005 10:47:59 +0000 Subject: [PATCH] Add KTBrowseUtil, which currently only handles converting a path into a tuple of the folder and document that path refers to. And, potentially, an additional label attached at the end of the path. --- lib/browse/browseutil.inc.php | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/browseutil/folderOrDocument.php | 15 +++++++++++++++ 2 files changed, 166 insertions(+), 0 deletions(-) create mode 100644 lib/browse/browseutil.inc.php create mode 100644 tests/browseutil/folderOrDocument.php diff --git a/lib/browse/browseutil.inc.php b/lib/browse/browseutil.inc.php new file mode 100644 index 0000000..3bcc312 --- /dev/null +++ b/lib/browse/browseutil.inc.php @@ -0,0 +1,151 @@ +, Jam Warehouse (Pty) Ltd, South Africa + * @package lib.browse + */ + +require_once(KT_LIB_DIR . '/actions/documentaction.inc.php'); + +class KTBrowseUtil { + // {{{ folderOrDocument + function folderOrDocument($sPath, $bAction = false) { + $sFileName = basename($sPath); + $sFolderPath = dirname($sPath); + + $aFolderInfo = KTBrowseUtil::_folderOrDocument($sFolderPath); + + if ($aFolderInfo === false) { + return $aFolderInfo; + } + + list($iFolderID, $iDocumentID) = $aFolderInfo; + + if ($iDocumentID && $bAction) { + $aActions = array_keys(KTDocumentActionUtil::getDocumentActions()); + if (in_array($sFileName, $aActions)) { + return array($iFolderID, $iDocumentID, $sFileName); + } + return false; + } + + $sQuery = "SELECT id FROM folders WHERE parent_id = ? AND name = ?"; + $aParams = array($iFolderID, $sFileName); + $id = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id'); + if (PEAR::isError($id)) { + // XXX: log error + return false; + } + if ($id) { + return array($id, null, null); + } + + $sQuery = "SELECT id FROM documents WHERE folder_id = ? AND filename = ?"; + $aParams = array($iFolderID, $sFileName); + $iDocumentID = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id'); + + if (PEAR::isError($iDocumentID)) { + // XXX: log error + return false; + } + + if ($iDocumentID) { + return array($iFolderID, $iDocumentID, null); + } + + if ($bAction) { + // $aActions = array_keys(KTFolderAction::getFolderActions()); + $aActions = array('ktcore.delete'); + if (in_array($sFileName, $aActions)) { + return array($iFolderID, null, $sFileName); + } + } + return false; + } + + function _folderOrDocument($sPath) { + global $default; + $sFileName = basename($sPath); + $sFolderPath = dirname($sPath); + + $aFolderNames = split('/', $sFolderPath); + + $iFolderID = 0; + + $aRemaining = $aFolderNames; + while (count($aRemaining)) { + $sFolderName = $aRemaining[0]; + $aRemaining = array_slice($aRemaining, 1); + if ($sFolderName === "") { + continue; + } + $sQuery = "SELECT id FROM folders WHERE parent_id = ? AND name = ?"; + $aParams = array($iFolderID, $sFolderName); + $id = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id'); + if (PEAR::isError($id)) { + // XXX: log error + return false; + } + if (is_null($id)) { + // Some intermediary folder path doesn't exist + return false; + } + $default->log->error("iFolderID set to " . print_r($id, true)); + $iFolderID = (int)$id; + } + + $sQuery = "SELECT id FROM documents WHERE folder_id = ? AND filename = ?"; + $aParams = array($iFolderID, $sFileName); + $iDocumentID = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id'); + + if (PEAR::isError($iDocumentID)) { + // XXX: log error + return false; + } + + if ($iDocumentID === null) { + $sQuery = "SELECT id FROM folders WHERE parent_id = ? AND name = ?"; + $aParams = array($iFolderID, $sFileName); + $id = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id'); + if (PEAR::isError($id)) { + // XXX: log error + return false; + } + if (is_null($id)) { + if ($sFileName === "") { + return array($iFolderID, null); + } + // XXX: log error + return array($iFolderID, false); + } + /* if (substr($path, -1) !== "/") { + header("Location: " . $_SERVER["PHP_SELF"] . "/"); + } */ + return array($id, null); + } + + return array($iFolderID, (int)$iDocumentID); + } + // }}} +} diff --git a/tests/browseutil/folderOrDocument.php b/tests/browseutil/folderOrDocument.php new file mode 100644 index 0000000..ea25ad0 --- /dev/null +++ b/tests/browseutil/folderOrDocument.php @@ -0,0 +1,15 @@ + -- libgit2 0.21.4