diff --git a/lib/browse/Browser.inc b/lib/browse/Browser.inc index d615a96..1822278 100644 --- a/lib/browse/Browser.inc +++ b/lib/browse/Browser.inc @@ -28,6 +28,215 @@ require_once("$default->fileSystemRoot/lib/foldermanagement/Folder.inc"); * @author Michael Joseph , Jam Warehouse (Pty) Ltd, South Africa * @package lib.browse */ + +class BrowseCriterion { + var $sDisplay; + var $sDocumentField; + var $sSortField; + var $aLookup = null; + var $oBrowser; + var $bFolderCriterion = false; + + function BrowseCriterion ($sDisplay, $sDocumentField, $sSortField, &$oBrowser) { + $this->sDisplay =& $sDisplay; + $this->sDocumentField =& $sDocumentField; + $this->sSortField =& $sSortField; + $this->oBrowser =& $oBrowser; + } + + function headerDisplay () { + return $this->sDisplay; + } + + // dummy function + function documentDisplay ($oDocument) { + return $this->sDisplay; + } + + function folderDisplay ($oDocument) { + return " "; + } + + function folderQuery ($iParentID) { + global $default; + $sFolderQuery = "SELECT f.id FROM $default->folders_table AS f ";/*ok*/ + if (!$this->bFolderCriterion) { + $sFolderQuery .= "WHERE parent_id = ? ORDER BY f.name asc"; + $aParams = array($iParentID); + return array($sFolderQuery, $aParams); + } + + if (!is_null($this->aLookup)) { + $sFolderQuery .= "INNER JOIN " . $this->aLookup["table"] . " lt ON f.$this->sDocumentField = lt.id WHERE parent_id = ?"; + $sFolderQuery .= " ORDER BY lt." . $this->aLookup["field"] . " " . $this->oBrowser->sSortDirection; + $aParams = array($iParentID); + return array($sFolderQuery, $aParams); + } + + $sFolderQuery .= "WHERE parent_id = ? ORDER BY $this->sSortField " . $this->oBrowser->sSortDirection; + $aParams = array($iParentID); + return array($sFolderQuery, $aParams); + } + + function documentQuery ($iFolderID) { + global $default; + // create query to retrieve documents in this folder + $documentQuery = "SELECT d.id as id FROM $default->documents_table AS d ";/*wc*/ + + if (!is_null($this->aLookup)) { + $sDocumentJoinField = $this->getDocumentField(); + $documentQuery .= "INNER JOIN " . $this->aLookup["table"] . " lt ON "; + if (array_key_exists('joinColumn', $this->aLookup)) { + $documentQuery .= "d.$sDocumentJoinField" . " = lt." . $this->aLookup["joinColumn"]; + } else { + $documentQuery .= "d.$sDocumentJoinField" . " = lt.id"; + } + } + + $documentQuery .= " WHERE d.folder_id = ? "; + $aParams = array($iFolderID); + if (!is_null($this->aLookup)) { + if (array_key_exists("whereClause", $this->aLookup)) { + $documentQuery .= "AND lt." . $this->aLookup["whereClause"] . " "; + } + + $documentQuery .= "ORDER BY lt." . $this->aLookup["field"] . " " . $this->oBrowser->sSortDirection; + } else { + $sDocumentJoinField = $this->getDocumentField(); + // $sSortField = $this->getSortField(); + $documentQuery .= "ORDER BY $this->sSortField " . $this->oBrowser->sSortDirection; + } + + return array($documentQuery, $aParams); + } + + function getDocumentField () { + return $this->sDocumentField; + } + + function getSortField () { + return $this->sSortField; + } + + function getLookup () { + return $this->aLookup; + } +} + +class NameCriterion extends BrowseCriterion { + var $bFolderCriterion = true; + function documentDisplay ($oDocument) { + $aOptions =& $this->oBrowser->getOptions(); + if (array_key_exists('displayFullPath', $aOptions)) { + $bDisplayFullPath = $aOptions['displayFullPath']; + } else { + $bDisplayFullPath = false; + } + if (array_key_exists('templateBrowsing', $aOptions)) { + $bTemplateBrowsing = $aOptions['templateBrowsing']; + } else { + $bTemplateBrowsing = false; + } + + if ($bTemplateBrowsing) { + return displayDocumentLinkForTemplateBrowsing($oDocument, $bDisplayFullPath); + } else { + return displayDocumentLink($oDocument, $bDisplayFullPath); + } + } + + function folderDisplay($oFolder) { + return displayFolderLink($oFolder); + } +} + +class TitleCriterion extends BrowseCriterion { + function documentDisplay ($oDocument) { + return $oDocument->getName(); + } + function folderDisplay($oFolder) { + return $oFolder->getDescription(); + } +} + +class CreatorCriterion extends BrowseCriterion { + var $bFolderCriterion = true; + var $aLookup = array( + "table" => "users", + "field" => "name", + ); + function documentDisplay ($oDocument) { + $oCreator = User::get($oDocument->getCreatorID()); + if ($oCreator) { + return $oCreator->getName(); + } + return " "; + } + function folderDisplay($oFolder) { + return $this->documentDisplay($oFolder); + } +} + +class DateCreatedCriterion extends BrowseCriterion { + var $aLookup = array( + "table" => "document_transactions", + "field" => "datetime", + "joinColumn" => "document_id", + "whereClause" => "transaction_id=1", + ); + + function documentDisplay ($oDocument) { + $aDocumentTransaction = DocumentTransaction::getList("transaction_id=1 AND document_id=" . $oDocument->getID()); + return $aDocumentTransaction[0]->dDateTime; + } +} + +class DocumentTypeCriterion extends BrowseCriterion { + var $aLookup = array( + "table" => "document_types_lookup", + "field" => "name" + ); + + function documentDisplay ($oDocument) { + $oDocumentType = DocumentType::get($oDocument->getDocumentTypeID()); + if ($oDocumentType) { + return $oDocumentType->getName(); + } + return " "; + } +} + +class GenericMetadataCriterion extends BrowseCriterion { + var $aLookup = array( + "table" => "document_fields_link", + "field" => "value", + "joinColumn" => "document_id", + ); + var $iFieldID; + + function GenericMetadataCriterion ($sDisplay, $sDocumentField, $sSortField, &$oBrowser, $iFieldID) { + $this->iFieldID = $iFieldID; + $this->BrowseCriterion($sDisplay, $sDocumentField, $sSortField, $oBrowser); + $this->aLookup['whereClause'] = 'document_field_id = ' . $iFieldID; + } + + function documentDisplay ($oDocument) { + global $default; + $sQuery = "SELECT DFL.value as value " . + "FROM $default->document_fields_link_table AS DFL " . + "WHERE DFL.document_id = ? " . + "AND DFL.document_field_id = ?"; + $aParams = array($oDocument->getID(), $this->iFieldID); + + $res = DBUtil::getOneResultKey(array($sQuery, $aParams), 'value'); + if (PEAR::isError($res)) { + // WARN: Add log warning + return " "; + } + return $res; + } +} + class Browser { /** @@ -48,6 +257,8 @@ class Browser { * The direction to sort the results in */ var $sSortDirection; + + var $aOptions; /** * Constructs a new Browser instance @@ -60,35 +271,12 @@ class Browser { // default sort criteria if (count($aNewSortCriteria) == 0) { $aNewSortCriteria = array ( - "name" => array( - "display" => _("Title"), - ), - "filename" => array( - "display" => _("Description"), - ), - "creator_id" => array( - "display" => _("Creator"), - "lookup" => array( - "table" => "users", - "field" => "name", - ), - ), - "id" => array( - "display" => _("Date Created"), - "lookup" => array( - "table" => "document_transactions", - "field" => "datetime", - "joinColumn" => "document_id", - "whereClause" => "transaction_id=1", - ), - ), - "document_type_id" => array( - "display" => _("Document Type"), - "lookup" => array( - "table" => "document_types_lookup", - "field" => "name" - ), - ), + "name" => new NameCriterion(_("Title"), 'name', 'name', $this), + "filename" => new TitleCriterion(_("Description"), 'filename', 'filename', $this), + "creator_id" => new CreatorCriterion(_("Creator"), 'creator_id', 'creator_id', $this), + "id" => new DateCreatedCriterion(_("Date Created"), 'id', 'id', $this), + "document_type_id" => new DocumentTypeCriterion(_("Document Type"), 'document_type_id', 'document_type_id', $this), + "category" => new GenericMetadataCriterion("Category", 'id', 'id', $this, 1), ); } @@ -153,6 +341,15 @@ class Browser { $this->sSortDirection = $sNewSortDirection; } + + function setOptions($aOptions) { + $this->aOptions = array_merge($this->aOptions, $aOptions); + } + + function getOptions() { + return $this->aOptions; + } + /** * [Abstract] Browse the documents * @@ -161,4 +358,5 @@ class Browser { function browse() { } + } diff --git a/lib/browse/BrowserFactory.inc b/lib/browse/BrowserFactory.inc index 1ddebbd..639fd71 100644 --- a/lib/browse/BrowserFactory.inc +++ b/lib/browse/BrowserFactory.inc @@ -39,7 +39,7 @@ class BrowserFactory { * @param string the direction to sort in * @return Browser the correct Browser implementation class */ - function create($sBrowseBy, $sSortField, $sSortDirection) { + function &create($sBrowseBy, $sSortField, $sSortDirection) { switch ($sBrowseBy) { case "folder" : // retrieve folderID if present @@ -59,4 +59,4 @@ class BrowserFactory { } } -} \ No newline at end of file +} diff --git a/lib/browse/FolderBrowser.inc b/lib/browse/FolderBrowser.inc index b727b7d..615ff12 100644 --- a/lib/browse/FolderBrowser.inc +++ b/lib/browse/FolderBrowser.inc @@ -114,36 +114,28 @@ class FolderBrowser extends Browser { // now find all the child folders relative to this one // FIXME: in the same unit? - $aLookupCriteria = $this->aSortCriteria[$this->sSortField]["lookup"]; + $oSortCriterion = $this->aSortCriteria[$this->sSortField]; + $aLookupCriteria = $oSortCriterion->getLookup(); - // if we're sorting by name or creator_id then sort folders in the appropriate direction + $aQuery = $oSortCriterion->folderQuery($iFolderID); - $aParams = array(); - $sFolderQuery = "SELECT f.id FROM $default->folders_table AS f ";/*ok*/ - if (in_array($this->sSortField, array("name", "creator_id"))) { - if (isset($aLookupCriteria)) { - $sFolderQuery .= "INNER JOIN " . $aLookupCriteria["table"] . " lt ON f.$this->sSortField=lt.id WHERE parent_id = ?"; - $sFolderQuery .= " ORDER BY lt." . $aLookupCriteria["field"] . " $this->sSortDirection"; - } else { - $sFolderQuery .= "WHERE parent_id = ? ORDER BY $this->sSortField $this->sSortDirection"; - } - } else { - $sFolderQuery .= "WHERE parent_id = ? ORDER BY f.name asc"; + $oResult = DBUtil::runQuery($aQuery); + if (PEAR::isError($oResult)) { + var_dump($oResult); + exit(0); } - $aParams[] = $iFolderID; - $default->log->debug("Ordering folderQuery = $sFolderQuery"); - if ($sql->query(array($sFolderQuery, $aParams))) { - while ($sql->next_record()) { - $default->log->debug("In folder iteration while, with folder_id " . $sql->f("id")); + if ($oResult->numRows()) { + while ($aRow = $oResult->fetchRow()) { + $default->log->debug("In folder iteration while, with folder_id " . $aRow["id"]); // check whether to display folders which are not readable and display/hide these accordingly - $oFolder = Folder::get($sql->f("id")); + $oFolder = Folder::get($aRow["id"]); if ($default->folderHidingFlag) { if (Permission::userHasFolderReadPermission($oFolder)) { - $default->log->debug("FOLDER PERMISSIONS: Does have permission for folder " . $oFolder->getID() . ":" . $sql->f("id") ); + $default->log->debug("FOLDER PERMISSIONS: Does have permission for folder " . $oFolder->getID() . ":" . $aRow["id"]); $results["folders"][] = $oFolder; } else { - $default->log->debug("FOLDER PERMISSIONS: Does NOT have permission for folder " . $sql->f("id") ); + $default->log->debug("FOLDER PERMISSIONS: Does NOT have permission for folder " . $aRow["id"] ); } } else{ $results["folders"][] = $oFolder; @@ -153,29 +145,21 @@ class FolderBrowser extends Browser { $default->log->debug("Going on to document checking"); - // create query to retrieve documents in this folder - $documentQuery = "SELECT d.id as id FROM $default->documents_table AS d ";/*wc*/ - if (isset($aLookupCriteria)) { - $documentQuery .= "INNER JOIN " . $aLookupCriteria["table"] . " lt ON "; - $documentQuery .= "d.$this->sSortField" . "=lt." . (isset($aLookupCriteria["joinColumn"]) ? $aLookupCriteria["joinColumn"] : "id"); + $aQuery = $oSortCriterion->documentQuery($iFolderID); + $oResult = DBUtil::runQuery($aQuery); + if (PEAR::isError($oResult)) { + var_dump($oResult); + exit(0); } - $documentQuery .= " WHERE d.folder_id=$iFolderID " . (isset($aLookupCriteria["whereClause"]) ? "AND lt." . $aLookupCriteria["whereClause"] : "") . " "; - if (isset($aLookupCriteria)) { - $documentQuery .= "ORDER BY lt." . $aLookupCriteria["field"] . " $this->sSortDirection"; - } else { - $documentQuery .= "ORDER BY $this->sSortField $this->sSortDirection"; - } - $default->log->debug("docQuery=$documentQuery"); // initialise access flag; $results["accessDenied"] = false; - if ($sql->query($documentQuery)) { - + if ($oResult->numRows()) { // do the check for whether this documents have folder read permission, if they do, it's all good. $hasFolderRead = Permission::userHasFolderReadPermission($rootFolder); - while ($sql->next_record()) { - $oDocument = & Document::get($sql->f("id")); + while ($aRow = $oResult->fetchRow()) { + $oDocument = &Document::get($aRow["id"]); // proceed if the document is live if ($oDocument->isLive()) { // check permissions diff --git a/presentation/lookAndFeel/knowledgeTree/documentmanagement/browseBL.php b/presentation/lookAndFeel/knowledgeTree/documentmanagement/browseBL.php index 328735e..f40abd7 100644 --- a/presentation/lookAndFeel/knowledgeTree/documentmanagement/browseBL.php +++ b/presentation/lookAndFeel/knowledgeTree/documentmanagement/browseBL.php @@ -95,7 +95,7 @@ if (!$fSortDirection) { } // fire up the document browser -$oBrowser = BrowserFactory::create($fBrowseType, $fSortBy, $fSortDirection); +$oBrowser =& BrowserFactory::create($fBrowseType, $fSortBy, $fSortDirection); $sectionName = $oBrowser->getSectionName(); // instantiate my content pattern diff --git a/presentation/lookAndFeel/knowledgeTree/documentmanagement/browseUI.inc b/presentation/lookAndFeel/knowledgeTree/documentmanagement/browseUI.inc index 831dcc6..8cef6f0 100644 --- a/presentation/lookAndFeel/knowledgeTree/documentmanagement/browseUI.inc +++ b/presentation/lookAndFeel/knowledgeTree/documentmanagement/browseUI.inc @@ -199,27 +199,24 @@ function renderDocumentTypeResults($aResults) { */ function renderFolderResults($aResults, $bTemplateBrowsing = false) { global $default; + global $oBrowser; $sToRender = ""; // now loop through the rest of the folders and display links if (count($aResults["folders"]) > 1) { for ($i=1; $igetCreatorID()); // the first element of the array contains the current folder name $sToRender .= "\n"; $sToRender .= " \n"; // for the checkboxes - // folder name - $sToRender .= "" . $sFolderLink . "\n"; - // blank filename (folder description?) - $sToRender .= "" . $aResults["folders"][$i]->getDescription() . "\n"; - // creator name - $sToRender .= "" . ($oCreator ? $oCreator->getName() : " ") . "\n"; - // modified date (TODO: add to db) - $sToRender .= " \n"; - // document type (??: display one of the mapped document types? which one?) - $sToRender .= " \n"; + + foreach (array_values($oBrowser->getSortCriteria()) as $oCriterion) { + $sToRender .= "" . $oCriterion->folderDisplay($oFolder) . "\n"; + } $sToRender .= "\n"; } } else { @@ -254,7 +251,11 @@ function renderSortHeadings($sSortBy, $sSortDirection) { $sToRender .= " \n"; // For the checkboxes while (list($key, $value) = each ($aSortCriteria)) { $sCurrentSortDirection = "asc"; - $displayText = $value["display"]; + if (is_array($value)) { + $displayText = $value["display"]; + } else { + $displayText = $value->headerDisplay(); + } // if the current heading is being sorted then flip the sort direction if ($sSortBy == $key) { $sCurrentSortDirection = ($sSortDirection == "asc" ? "desc" : "asc"); @@ -288,6 +289,14 @@ function renderSortHeadings($sSortBy, $sSortDirection) { */ function renderDocumentList($aResults, $sNoDocumentsMessage, $sNoPermissionMessage, $bDisplayFullPath = false, $bTemplateBrowsing = false) { global $default; + global $oBrowser; + + $oBrowser->setOptions(array( + 'displayFullPath' => $bDisplayFullPath, + 'templateBrowsing' => $bTemplateBrowsing, + )); + + $aSortCriteria = $oBrowser->getSortCriteria(); $iFolderCount = count($aResults["folders"]) - 1; // loop through the files and display links @@ -295,21 +304,23 @@ function renderDocumentList($aResults, $sNoDocumentsMessage, $sNoPermissionMessa for ($i=0; $i\n"; $sToRender .= "" . "getID() . "\"/>\n"; - $sToRender .= "" . ($bTemplateBrowsing ? displayDocumentLinkForTemplateBrowsing($aResults["documents"][$i], $bDisplayFullPath) : - displayDocumentLink($aResults["documents"][$i], $bDisplayFullPath)) . ""; - // #3425 the title is now the filename, and the description is the title - $sToRender .= "" . $aResults["documents"][$i]->getName() . ""; - $oCreator = User::get($aResults["documents"][$i]->getCreatorID()); - $sToRender .= "" . ($oCreator ? $oCreator->getName() : "") . ""; - $aDocumentTransaction = DocumentTransaction::getList("transaction_id=1 AND document_id=" . $aResults["documents"][$i]->getID()); - $sToRender .= "" . $aDocumentTransaction[0]->dDateTime . ""; - $oDocumentType = DocumentType::get($aResults["documents"][$i]->getDocumentTypeID()); - if ($oDocumentType) { - $sToRender .= "" . $oDocumentType->getName() . ""; + + /*$sToRender .= "" . $aSortCriteria["name"]->documentDisplay($oDocument) . ""; + $sToRender .= "" . $aSortCriteria["filename"]->documentDisplay($oDocument) . ""; + $sToRender .= "" . $aSortCriteria["creator_id"]->documentDisplay($oDocument) . ""; + $sToRender .= "" . $aSortCriteria['id']->documentDisplay($oDocument) . ""; + $sToRender .= "" . $aSortCriteria['document_type_id']->documentDisplay($oDocument) . ""; + $sToRender .= "" . $aSortCriteria['category']->documentDisplay($oDocument) . ""; + */ + foreach (array_values($aSortCriteria) as $oCriterion) { + $sToRender .= "" . $oCriterion->documentDisplay($oDocument) . ""; } + $sToRender .= "\n"; }