CategoryBrowser.inc
5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
require_once(KT_LIB_DIR . '/security/Permission.inc');
require_once(KT_LIB_DIR . '/users/User.inc');
require_once(KT_LIB_DIR . '/documentmanagement/Document.inc');
require_once(KT_LIB_DIR . '/foldermanagement/Folder.inc');
/**
* $Id$
*
* Contains category view document browsing business logic.
*
* Copyright (c) 2003 Jam Warehouse http://www.jamwarehouse.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @version $Revision$
* @author Michael Joseph <michael@jamwarehouse.com>, Jam Warehouse (Pty) Ltd, South Africa
* @package lib.browse
*/
class CategoryBrowser extends Browser {
/**
* Construct a new FolderBrowser instance with the specified sort criteria
*
* @param string the field to sort the results by
* @param string the direction to sort the results
*/
function CategoryBrowser($sSortField = "name", $sSortDirection = "asc", $aNewSortCriteria = array()) {
Browser::Browser($sSortField, $sSortDirection, $aNewSortCriteria);
}
/**
* Browse the documents by category
*
* @return array categories and documents
*/
function browse() {
global $default;
// XXX: Shouldn't be picking it out of $_REQUEST
$sCategoryName = $_REQUEST['fCategoryName'];
// browsing by category
$this->setBrowseStart($sCategoryName);
$sCategoryName = $sCategoryName;
// TODO: add this to default inserts
$categoryField = "Category";
$results = array();
$sql = $default->db;
// lookup document_fields id for category
$categoryFieldID = lookupID($default->document_fields_table, "name", "$categoryField");
$default->log->debug("CategoryBrowser::browse() categoryFieldID=$categoryFieldID");
if ($sCategoryName == "") {
if ($categoryFieldID) {
// no category value supplied, so return a list of categories
// set the first value to "categories"
$results["categories"][] = "Categories";
// get a list of category values
// FIXME: exclude deleted document metadata
$query = "SELECT DISTINCT value FROM $default->document_fields_link_table WHERE document_field_id = ? ORDER BY value " . ($this->sSortField == "name" ? $this->sSortDirection : "ASC");/*ok*/
$aParams = array($categoryFieldID);
$default->log->info("CategoryBrowser::browse() category listing query=$query; $this->sSortField");
$sql->query(array($query, $aParams));
// loop through resultset, build array and return
while ($sql->next_record()) {
$results["categories"][] = $sql->f("value");
}
}
// its ok if we return an empty array- the UI's responsibility to check and print an error
return $results;
} else {
$aLookupCriteria = $this->aSortCriteria[$this->sSortField]["lookup"];
$results["categories"][] = $sCategoryName;
// we have a category to use, so find all the documents
// with this category value
$categoryQuery = "SELECT df.document_id FROM $default->document_fields_link_table df " . /*ok*/
"INNER JOIN $default->documents_table d ON df.document_id = d.id ";
if ( isset($aLookupCriteria) ) {
$categoryQuery .= "INNER JOIN " . $aLookupCriteria["table"] . " lt ON ";
$categoryQuery .= "d.$this->sSortField" . "=lt." . (isset($aLookupCriteria["joinColumn"]) ? $aLookupCriteria["joinColumn"] : "id");
}
$categoryQuery .= " WHERE df.document_field_id = ? AND value = ? " . (isset($aLookupCriteria["whereClause"]) ? "AND lt." . $aLookupCriteria["whereClause"] : "") . " ";;
$aParams = array($categoryFieldID, $sCategoryName);
if ( isset($aLookupCriteria) ) {
$categoryQuery .= "ORDER BY lt." . $aLookupCriteria["field"] . " $this->sSortDirection";
} else {
$categoryQuery .= "ORDER BY d.$this->sSortField $this->sSortDirection";
}
$default->log->debug("categoryQuery=$categoryQuery");
$sql->query(array($categoryQuery, $aParams));
// loop through resultset and add to array
$results["accessDenied"] = false;
while ($sql->next_record()) {
$oDocument = & Document::get($sql->f("document_id"));
// only if the document is live
if ($oDocument->isLive()) {
if (Permission::userHasDocumentReadPermission($oDocument)) {
$results["documents"][] = $oDocument;
} else {
$results["accessDenied"] = true;
}
}
}
return $results;
}
}
function getSectionName() {
return "Manage Categories";
}
}