Commit 26f29ec8844085601747101dccbef631f6f77032

Authored by Neil Blakey-Milner
1 parent aab4536c

More documentation


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3962 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 40 additions and 1 deletions
lib/search/searchutil.inc.php
... ... @@ -15,6 +15,13 @@ class KTSearchUtil {
15 15 $aSQL = array();
16 16 $aJoinSQL = array();
17 17 $criteria_set = array();
  18 +
  19 + /*
  20 + * First phase: get criterion object for search or the direct
  21 + * SQL to use.
  22 + *
  23 + * XXX: Why is there $order there?
  24 + */
18 25 foreach ($aOneCriteriaSet as $order => $dataset) {
19 26 $type = KTUtil::arrayGet($dataset, "type");
20 27 $sql = KTUtil::arrayGet($dataset, "sql");
... ... @@ -30,6 +37,10 @@ class KTSearchUtil {
30 37 return PEAR::raiseError('Invalid criteria specified.');
31 38 }
32 39 }
  40 +
  41 + /*
  42 + * Second phase: Create an individual SQL query per criteria.
  43 + */
33 44 foreach ($criteria_set as $oCriterionPair) {
34 45 $oCriterion = $oCriterionPair[0];
35 46 $aReq = $oCriterionPair[1];
... ... @@ -47,6 +58,10 @@ class KTSearchUtil {
47 58 }
48 59 }
49 60  
  61 + /*
  62 + * Third phase: build up $aCritQueries and $aCritParams, and put
  63 + * parentheses around them.
  64 + */
50 65 $aCritParams = array();
51 66 $aCritQueries = array();
52 67 foreach ($aSQL as $sSQL) {
... ... @@ -85,7 +100,16 @@ class KTSearchUtil {
85 100 $aJoinSQL = array();
86 101 $aSearchStrings = array();
87 102 $aParams = array();
  103 + /*
  104 + * XXX: We unnecessarily force the base criteria to have
  105 + * subgroups at the top level, even though we most often only
  106 + * have a single "subgroup".
  107 + */
88 108 foreach ($aCriteriaSet["subgroup"] as $k => $aOneCriteriaSet) {
  109 + /*
  110 + * Each subgroup will either have values or it will have
  111 + * subgroups. They can't be mixed.
  112 + */
89 113 $aValues = KTUtil::arrayGet($aOneCriteriaSet, "values");
90 114 $aSubgroup = KTUtil::arrayGet($aOneCriteriaSet, "subgroup");
91 115 if (!empty($aValues)) {
... ... @@ -95,6 +119,11 @@ class KTSearchUtil {
95 119 $tabs = str_repeat("\t", ($iRecurseLevel + 2));
96 120 $aSearchStrings[] = "\n$tabs(\n$tabs\t" . join("\n " . KTUtil::arrayGet($aOneCriteriaSet, 'join', "AND") . " ", $aThisCritQueries) . "\n$tabs)";
97 121 } else if (!empty($aSubgroup)) {
  122 + /*
  123 + * Recurse if we have a criteria set with subgroups.
  124 + * Recurselevel makes the tabs increase as we recurse so
  125 + * that the SQL statement is somewhat understandable.
  126 + */
98 127 list($sThisSearchString, $aThisParams, $sThisJoinSQL) =
99 128 KTSearchUtil::criteriaSetToSQL($aOneCriteriaSet, $iRecurseLevel + 1);
100 129 $aJoinSQL[] = $sThisJoinSQL;
... ... @@ -190,8 +219,12 @@ class KTSearchUtil {
190 219 $sToSearch = KTUtil::arrayGet($aOrigReq, 'fToSearch', 'Live'); // actually never present in this version.
191 220  
192 221 list ($sPermissionString, $aPermissionParams, $sPermissionJoin) = KTSearchUtil::permissionToSQL($oUser, $sPermissionName);
193   - //$sQuery = DBUtil::compactQuery("
194 222  
  223 + /*
  224 + * This is to overcome the problem where $sPermissionString (or
  225 + * even $sSQLSearchString) is empty, leading to leading or
  226 + * trailing ANDs.
  227 + */
195 228 $aPotentialWhere = array($sPermissionString, 'SL.name = ?', "($sSQLSearchString)");
196 229 $aWhere = array();
197 230 foreach ($aPotentialWhere as $sWhere) {
... ... @@ -208,6 +241,7 @@ class KTSearchUtil {
208 241 $sWhere = "\tWHERE " . join(" AND ", $aWhere);
209 242 }
210 243  
  244 + //$sQuery = DBUtil::compactQuery("
211 245 $sQuery = ("
212 246 SELECT
213 247 $sSelect
... ... @@ -243,6 +277,11 @@ class KTSearchUtil {
243 277 $oSearch =& KTUtil::getObject('KTSavedSearch', $oSearch);
244 278 $iDocumentId = KTUtil::getId($oDocument);
245 279  
  280 + /*
  281 + * Make a new criteria set, an AND of the existing criteria set
  282 + * and the sql statement requiring that D.id be the document id
  283 + * given to us.
  284 + */
246 285 $aCriteriaSet = array(
247 286 "join" => "AND",
248 287 "subgroup" => array(
... ...