advancedSearchBL.php
7.96 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* $Id$
*
* Business logic used to perform advanced search. Advanced search allows
* users to search by meta data types
*
* 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 Rob Cherry, Jam Warehouse (Pty) Ltd, South Africa
* @package search
*/
require_once("../../../../config/dmsDefaults.php");
KTUtil::extractGPC('fForSearch', 'fSearchString', 'fShowSection', 'fStartIndex', 'fToSearch');
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, '_', '-');
}
var_dump($res);
return $res;
}
function getAdvancedSearchResults($aOrigReq, $iStartIndex) {
global $default;
$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();
foreach ($aIDs as $iID) {
$oCriterion =& Criteria::getCriterionByNumber($iID);
$res = $oCriterion->searchSQL($aReq);
if (!is_null($res)) {
$aSQL[] = $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;
}
}
$sSQLSearchString = join(" AND ", $aCritQueries);
$sQuery = DBUtil::compactQuery("
SELECT
F.name AS folder_name, F.id AS folder_id, D.id AS document_id,
D.name AS document_name, COUNT(D.id) AS doc_count
FROM
$default->documents_table AS D
INNER JOIN $default->folders_table AS F ON D.folder_id = F.id
LEFT JOIN $default->document_fields_link_table AS DFL ON DFL.document_id = D.id
LEFT JOIN $default->document_fields_table AS DF ON DF.id = DFL.document_field_id
INNER JOIN $default->search_permissions_table AS SDUL ON SDUL.document_id = D.id
INNER JOIN $default->status_table AS SL on D.status_id=SL.id
WHERE
SDUL.user_id = ?
AND SL.name = ?
AND ($sSQLSearchString)
GROUP BY D.id
ORDER BY doc_count DESC");
$aParams = array();
$aParams[] = $_SESSION["userID"];
$aParams[] = "Live";
$aParams = array_merge($aParams, $aCritParams);
//var_dump(DBUtil::getResultArray(array($sQuery, $aParams)));
//exit(0);
$aColumns = array("folder_name", "document_name", "doc_count");
$aColumnTypes = array(3,3,1);
$aColumnHeaders = array("<font color=\"ffffff\"><img src=$default->graphicsUrl/widgets/dfolder.gif>" . _("Folder") . "</font>","<font color=\"ffffff\">" . _("Document") . "</font>", "<font color=\"ffffff\">" . _("Matches") . "</font>");
$aLinkURLs = array("$default->rootUrl/control.php?action=browse","$default->rootUrl/control.php?action=viewDocument");
$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("");
$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>";
return renderHeading(_("Advanced Search")) . $oPatternBrowse->render() . $sRefreshMessage . getSearchVariablesHtml($sSearchString, $sStatus, $sMetaTagIDs);
}
function dealWithAdvancedSearch($aReq, $iStartIndex) {
global $main;
$oPatternCustom = & new PatternCustom();
$oPatternCustom->setHtml(getAdvancedSearchResults($aReq, $iStartIndex));
$main->setCentralPayload($oPatternCustom);
$main->render();
}
if (checkSession()) {
require_once("$default->fileSystemRoot/lib/visualpatterns/PatternBrowsableSearchResults.inc");
require_once("$default->fileSystemRoot/lib/visualpatterns/PatternEditableTableSqlQuery.inc");
require_once("$default->fileSystemRoot/lib/visualpatterns/PatternCustom.inc");
require_once("$default->fileSystemRoot/lib/foldermanagement/Folder.inc");
require_once("$default->fileSystemRoot/lib/documentmanagement/Document.inc");
require_once("$default->fileSystemRoot/lib/security/Permission.inc");
require_once("$default->fileSystemRoot/presentation/Html.inc");
require_once("advancedSearchUI.inc");
require_once("advancedSearchUtil.inc");
require_once("$default->fileSystemRoot/presentation/webpageTemplate.inc");
if (!isset($fStartIndex)) {
$fStartIndex = 1;
}
if (strlen($fForSearch)) {
if (array_key_exists('advancedSearch_x', $_REQUEST)) {
dealWithAdvancedSearch($_REQUEST, $fStartIndex);
} else if (strlen($fSearchString) > 0) {
$oPatternCustom = & new PatternCustom();
//display search results
$sMetaTagIDs = getChosenMetaDataTags($_POST);
if (strlen($sMetaTagIDs) > 0) {
$sSQLSearchString = getSQLSearchString($fSearchString);
$oPatternCustom->setHtml(getSearchResults($sMetaTagIDs, $sSQLSearchString, $fStartIndex, $fSearchString, $fToSearch));
$main->setCentralPayload($oPatternCustom);
$main->render();
} else {
$oPatternCustom->setHtml(getSearchPage($fSearchString));
$main->setCentralPayload($oPatternCustom);
$main->setErrorMessage(_("Please select at least one criteria to search by"));
$main->setHasRequiredFields(true);
$main->setFormAction($_SERVER["PHP_SELF"] . "?fForSearch=1");
$main->setOnLoadJavaScript("switchDiv('" . (isset($fShowSection) ? $fShowSection : "searchLess") . "', 'search')");
$main->render();
}
} else {
$sMetaTagIDs = getChosenMetaDataTags($_POST);
$aMetaTagIDs = explode(",", $sMetaTagIDs);
$oPatternCustom = & new PatternCustom();
$oPatternCustom->setHtml(getSearchPage($fSearchString, $aMetaTagIDs));
$main->setCentralPayload($oPatternCustom);
$main->setErrorMessage(_("Please enter text to search on"));
$main->setHasRequiredFields(true);
$main->setFormAction($_SERVER["PHP_SELF"] . "?fForSearch=1");
$main->setOnLoadJavaScript("switchDiv('" . (isset($fShowSection) ? $fShowSection : "searchLess") . "', 'search')");
$main->render();
}
} else {
//display search criteria
$oPatternCustom = & new PatternCustom();
$oPatternCustom->setHtml(getSearchPage($fSearchString));
$main->setHasRequiredFields(true);
$main->setCentralPayload($oPatternCustom);
$main->setFormAction($_SERVER["PHP_SELF"] . "?fForSearch=1");
$main->setOnLoadJavaScript("switchDiv('" . (isset($fShowSection) ? $fShowSection : "searchLess") . "', 'search')");
$main->render();
}
}
?>