Exemplo n.º 1
0
 /**
  * All for folders
  * Converts a criteria set to the SQL joins, where clause, and
  * parameters necessary to ensure that the criteria listed restrict
  * the folders returned to those that match the criteria.
  *
  * Perhaps poorly called recursively to handle criteria that involve
  * subgroups to allow infinitely nested criteria.
  *
  * Returns a list of the following elements:
  *      - String representing the where clause
  *      - Array of parameters that go with the where clause
  *      - String with the SQL necessary to join with the tables in the
  *        where clause
  */
 function criteriaFolderSetToSQL($aCriteriaSet, $iRecurseLevel = 0)
 {
     $aJoinSQL = array();
     $aSearchStrings = array();
     $aParams = array();
     /*
      * XXX: We unnecessarily force the base criteria to have
      * subgroups at the top level, even though we most often only
      * have a single "subgroup".
      */
     foreach ($aCriteriaSet["subgroup"] as $k => $aOneCriteriaSet) {
         /*
          * Each subgroup will either have values or it will have
          * subgroups.  They can't be mixed.
          */
         $aValues = KTUtil::arrayGet($aOneCriteriaSet, "values");
         $aSubgroup = KTUtil::arrayGet($aOneCriteriaSet, "subgroup");
         if (!empty($aValues)) {
             $res = KTSearchUtil::_oneCriteriaFolderSetToSQL($aOneCriteriaSet["values"]);
             if (PEAR::isError($res)) {
                 return $res;
             }
             list($aThisCritQueries, $aThisParams, $aThisJoinSQL) = $res;
             $aJoinSQL = kt_array_merge($aJoinSQL, $aThisJoinSQL);
             $aParams = kt_array_merge($aParams, $aThisParams);
             $tabs = str_repeat("\t", $iRecurseLevel + 2);
             $aSearchStrings[] = "\n{$tabs}(\n{$tabs}\t" . join("\n " . KTUtil::arrayGet($aOneCriteriaSet, 'join', "AND") . " ", $aThisCritQueries) . "\n{$tabs})";
         } else {
             if (!empty($aSubgroup)) {
                 /*
                  * Recurse if we have a criteria set with subgroups.
                  * Recurselevel makes the tabs increase as we recurse so
                  * that the SQL statement is somewhat understandable.
                  */
                 list($sThisSearchString, $aThisParams, $sThisJoinSQL) = KTSearchUtil::criteriaFolderSetToSQL($aOneCriteriaSet, $iRecurseLevel + 1);
                 $aJoinSQL[] = $sThisJoinSQL;
                 $aParams = kt_array_merge($aParams, $aThisParams);
                 $aSearchStrings[] = $sThisSearchString;
             }
         }
     }
     $aJoinSQL = array_unique($aJoinSQL);
     $sJoinSQL = join(" ", $aJoinSQL);
     $tabs = str_repeat("\t", $iRecurseLevel + 1);
     $sSearchString = "\n{$tabs}(" . join("\n{$tabs}\t" . $aCriteriaSet['join'] . " ", $aSearchStrings) . "\n{$tabs})";
     return array($sSearchString, $aParams, $sJoinSQL);
 }