advancedSearchUtil.inc
6.15 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* $Id$
*
* Business logic used to perform advanced search. Advanced search allows
* users to search by meta data types.
*
* Copyright (c) 2005 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 Neil Blakey-Milner, Jam Warehouse (Pty) Ltd, South Africa
* @package search
*/
require_once(KT_LIB_DIR . '/permissions/permissionutil.inc.php');
function searchCriteria ($var) {
return preg_match('/^bmd(_?\d+)/', $var);
}
function criteriaNumber ($var) {
$res = preg_replace('/^bmd(_?\d+)(\D.*)?/', '\\1', $var);
if ($res !== false) {
$res = strtr($res, '_', '-');
}
return $res;
}
function getAdvancedSearchResults($aOrigReq, $iStartIndex) {
global $default;
$sRefreshMessage = "<table><tr><td align=\"center\">" . _("If your browser displays a 'Warning: Page has Expired' message when you attempt to return to these search results, please click your browser's 'Refresh' button") . "</td></tr></table>";
$aReq = array();
foreach ($aOrigReq as $k => $v) {
if (searchCriteria($k) === 1) {
$v = trim($v);
if ($v === "") {
continue;
}
if ($v === "-1") {
continue;
}
$aReq[$k] = $v;
}
}
$aIDs = array_unique(array_map("criteriaNumber", array_keys($aReq)));
$aSQL = array();
$aJoinSQL = array();
foreach ($aIDs as $iID) {
$oCriterion =& Criteria::getCriterionByNumber($iID);
$res = $oCriterion->searchSQL($aReq);
if (!is_null($res)) {
$aSQL[] = $res;
}
$res = $oCriterion->searchJoinSQL();
if (!is_null($res)) {
$aJoinSQL[] = $res;
}
}
$aCritParams = array();
$aCritQueries = array();
foreach ($aSQL as $sSQL) {
if (is_array($sSQL)) {
$aCritQueries[] = $sSQL[0];
$aCritParams = array_merge($aCritParams , $sSQL[1]);
} else {
$aCritQueries[] = $sSQL;
}
}
if (count($aCritQueries) == 0) {
return "No search criteria were specified";
}
$sSQLSearchString = join(" AND ", $aCritQueries);
$sJoinSQL = join(" ", $aJoinSQL);
$sToSearch = KTUtil::arrayGet($aOrigReq, 'fToSearch', 'Live');
$oPermission =& KTPermission::getByName('ktcore.permissions.read');
$sPermissionLookupsTable = KTUtil::getTableName('permission_lookups');
$sPermissionLookupAssignmentsTable = KTUtil::getTableName('permission_lookup_assignments');
$sPermissionDescriptorsTable = KTUtil::getTableName('permission_descriptors');
$aGroups = GroupUtil::listGroupsForUserExpand($_SESSION['userID']);
$aPermissionDescriptors = KTPermissionDescriptor::getByGroups($aGroups, array('ids' => true));
$sPermissionDescriptors = DBUtil::paramArray($aPermissionDescriptors);
$sQuery = DBUtil::compactQuery("
SELECT
F.name AS folder_name, F.id AS folder_id, D.id AS document_id,
D.name AS document_name, D.filename AS file_name, COUNT(D.id) AS doc_count, 'View' AS view
FROM
$default->documents_table AS D
INNER JOIN $default->folders_table AS F ON D.folder_id = F.id
$sJoinSQL
INNER JOIN $default->status_table AS SL ON D.status_id=SL.id
INNER JOIN $sPermissionLookupsTable AS PL ON D.permission_lookup_id = PL.id
INNER JOIN $sPermissionLookupAssignmentsTable AS PLA ON PL.id = PLA.permission_lookup_id AND PLA.permission_id = ?
WHERE
PLA.permission_descriptor_id IN ($sPermissionDescriptors)
AND SL.name = ?
AND ($sSQLSearchString)
GROUP BY D.id
ORDER BY doc_count DESC");
$aParams = array();
$aParams[] = $oPermission->getId();
$aParams = array_merge($aParams, $aPermissionDescriptors);
$aParams[] = $sToSearch;
$aParams = array_merge($aParams, $aCritParams);
//var_dump(DBUtil::getResultArray(array($sQuery, $aParams)));
//exit(0);
$aColumns = array("folder_name", "file_name", "document_name", "doc_count", "view");
$aColumnTypes = array(3,3,3,1,3);
$aColumnHeaders = array("<font color=\"ffffff\"><img src=$default->graphicsUrl/widgets/dfolder.gif>" . _("Folder") . "</font>", "<font color=\"ffffff\">" . _("Name") . "</font>", "<font color=\"ffffff\">" . _("Title") . "</font>", "<font color=\"ffffff\">" . _("Matches") . "</font>", "<font color=\"ffffff\">" . _("View") . "</font>");
$aLinkURLs = array("$default->rootUrl/control.php?action=browse","$default->rootUrl/control.php?action=viewDocument", "$default->rootUrl/control.php?action=viewDocument", null, "$default->rootUrl/control.php?action=downloadDocument");
$aDBQueryStringColumns = array("document_id","folder_id");
$aQueryStringVariableNames = array("fDocumentID", "fFolderID");
$oPatternBrowse = & new PatternBrowseableSearchResults(array($sQuery, $aParams), 10, $aColumns, $aColumnTypes, $aColumnHeaders, $aLinkURLs, $aDBQueryStringColumns, $aQueryStringVariableNames);
$oPatternBrowse->setStartIndex($iStartIndex);
$oPatternBrowse->setSearchText("");
$oPatternBrowse->setRememberValues($aReq);
$sForSearch = "<input type=\"hidden\" name=\"fForSearch\" value=\"1\" />";
return renderHeading(_("Advanced Search")) . $oPatternBrowse->render() . $sForSearch . $sRefreshMessage;
}
function dealWithAdvancedSearch($aReq, $iStartIndex) {
global $main;
$oPatternCustom = & new PatternCustom();
$oPatternCustom->setHtml(getAdvancedSearchResults($aReq, $iStartIndex));
$main->setCentralPayload($oPatternCustom);
$main->render();
}
?>