/**
  * Take a valid yyyy / yyyy-m / yyyy-mm / yyyy-m-d / yyyy-mm-d / yyyy-mm-dd
  * and transform it towards a left edge of possibly yyyy-01-01 or yyyy-12-31
  * or at least add leading zeros, as in yyyy-0m-0d
  *
  * @param string $date to be updated
  * @param int edge -- -1 -> left edge (-01-01) / +1 -> right edge (-12-31)
  * @result string $date -- transformed towards edge and with leading zeros
  */
 protected function _updateDate($date, $edge)
 {
     $result = $date;
     $regEx = SELF::_constructRegEx();
     $year = $regEx["year"];
     $month = $regEx["month"];
     $day = $regEx["day"];
     $yearOnly = "^{$year}\$";
     $yearMonth = "^{$year}-{$month}\$";
     $yearMonthDay = "^{$year}-{$month}-{$day}\$";
     if (preg_match("({$yearOnly})", $result)) {
         $result = $result . "-" . ($edge < 0 ? "1" : "12");
     }
     if (preg_match("({$yearMonth})", $result)) {
         $result = $result . "-" . ($edge < 0 ? "1" : "31");
     }
     if (preg_match("({$yearMonthDay})", $result)) {
         $oneDigit = "\\b(\\d)\\b";
         $result = preg_replace("({$oneDigit})", '0${0}', $result);
     }
     return $result;
 }
<?php

if (!defined("LITYLOADED")) {
    queue_css_file("lity.min");
    queue_js_file('lity.min');
    DEFINE("LITYLOADED", 1);
}
queue_js_file('rangesearch');
queue_css_file('rangesearch');
$selectFirst = __("Please select a target text area first.");
$selectUnit = __("Please select a unit.");
$enterNumber = __("Please enter a number.");
# queue_js_string() of these variables further down below
$view = get_view();
$regEx = SELF::_constructRegEx();
# foreach($regEx as $key => $val) { $$key = $val; }
# echo "<!--" . print_r($regEx, true) . "-->\n";
$combined = $regEx["combinedRegEx"];
$fullMatchRegEx = <<<EOT
      function rangeSearchFullMatch(str) {
        return str.match(/^{$combined}\$/i);
      }
EOT;
# queue_js_string() of these variables further down below
// ------------------------------------------------------
function editFieldHTML($textField, $view = false)
{
    if (!$view) {
        $view = get_view();
    }
    return $view->formInput($textField, null, array("type" => "text", "class" => "rangeSearchTextField", "size" => 4, "maxlength" => 10));
 /**
  * Main regex processing to extract numbers and ranges, to be able to expand them later
  */
 private function _processRangeText($text)
 {
     $regEx = SELF::_constructRegEx();
     # echo "<pre>$text\n" . print_r($regEx,true) . "</pre>";
     foreach ($regEx as $key => $val) {
         ${$key} = $val;
     }
     $allCount = preg_match_all("({$combinedRegEx})i", $text, $allMatches);
     // echo "<p>$text</p>";
     // echo "<pre>Count: $allCount\n" . print_r($allMatches,true) . "</pre>"; // die();
     $cookedRanges = array();
     foreach (array_unique($allMatches[0]) as $singleMatch) {
         $usedRegExId = false;
         foreach ($singleRegEx as $id => $testString) {
             $count = preg_match("({$testString})i", $singleMatch);
             if ($count) {
                 $usedRegExId = $id;
                 break;
             }
         }
         if ($usedRegExId) {
             $usedUnit = substr($usedRegExId, 0, strrpos($usedRegExId, "/"));
             $number = $unitRegEx[$usedUnit];
             #echo "<pre>'$singleMatch' = $usedRegExId / $usedUnit ($number)</pre>";
             $singleCount = preg_match_all("({$number})i", $singleMatch, $singleSplit);
             # echo "<pre>singleCount: $singleCount\n" . print_r($singleSplit,true) . "</pre>";
             $numberRange = array();
             $numberRange[] = $singleSplit[0][0];
             $numberRange[] = $singleSplit[0][$singleCount == 2 ? 1 : 0];
             $numberRange = SELF::_expandNumberRange($numberRange);
             # echo "<pre>" . print_r($numberRange,true) . "</pre>";
             $numberRange[] = $usedUnit;
             $cookedRanges[] = $numberRange;
         }
     }
     // echo "<pre>" . print_r($cookedDates,true) . "</pre>"; die();
     return $cookedRanges;
 }
 /**
  * Filter for a measurement after search page submission.
  *
  * @param array $args
  */
 public function hookItemsBrowseSql($args)
 {
     $select = $args['select'];
     $params = $args['params'];
     $regEx = SELF::_constructRegEx();
     foreach ($regEx as $key => $val) {
         ${$key} = $val;
     }
     if (isset($params['measurement_search_term']) and preg_match("({$oneOrTwoNumbers})", $params['measurement_search_term'])) {
         # echo "<pre>" . $params['measurement_search_term'] . " / " . $oneOrTwoNumbers . "</pre>";
         $singleCount = preg_match_all("({$oneNumber})", $params['measurement_search_term'], $singleNumbers);
         # echo "<pre>$singleCount: " . print_r($singleNumbers,true) . "</pre>";
         $lower = $singleNumbers[0][0];
         $higher = isset($singleNumbers[0][1]) ? $singleNumbers[0][1] : $lower;
         $lower = str_pad($lower, MEASUREMENTSEARCH_NUM_MAXLEN, "0", STR_PAD_LEFT);
         $higher = str_pad($higher, MEASUREMENTSEARCH_NUM_MAXLEN, "0", STR_PAD_LEFT);
         # echo "<pre>$lower / $higher</pre>";
         $db = get_db();
         $select->join(array('measurement_search_values' => $db->MeasurementSearchValues), "measurement_search_values.item_id = items.id", array())->where("('{$higher}' >= measurement_search_values.height and '{$lower}' <= measurement_search_values.height) or " . "('{$higher}' >= measurement_search_values.width  and '{$lower}' <= measurement_search_values.width) or " . "('{$higher}' >= measurement_search_values.depth  and '{$lower}' <= measurement_search_values.depth)");
         # echo "<pre>$select</pre>";
         if (isset($params['measurement_search_unit']) and is_array($params['measurement_search_unit'])) {
             $measurementSearchFormUnits = array();
             foreach ($params['measurement_search_unit'] as $unit) {
                 $measurementSearchFormUnits[] = intval($unit);
             }
             if ($measurementSearchFormUnits) {
                 $validUnits = SELF::_decodeUnitsForRegEx();
                 if ($validUnits) {
                     $MeasurementSearchUnits = array_keys($validUnits);
                     $dbUnits = array();
                     foreach ($measurementSearchFormUnits as $unit) {
                         if (isset($MeasurementSearchUnits[$unit])) {
                             $dbUnits[] = addslashes($validUnits[$unit]);
                         }
                     }
                     #echo "<pre>" . print_r($dbUnits, true) . "</pre>"; die();
                     if ($dbUnits) {
                         $dbUnits = "'" . implode("','", $dbUnits) . "'";
                         $select->where("measurement_search_values.unit in ({$dbUnits})");
                     }
                 }
                 # if ($validUnits)
             }
             # if ($measurementSearchFormUnits)
         }
         # if ( (isset($params['measurement_search_unit']))
     }
     # if (	(isset($params['measurement_search_term'])) ...
 }