/** * Parses a search phrase to find the excluding matches, exact matches, * any matches and all other words * * @access public * @param string $phrase Phrase to parse * @param array $searchable List of searchable gadgets * @return array An array with the following indexes (and results): * - all, exact, least and exclude */ function parseSearch($options, &$searchable) { $phrase = $options['all']; if (!empty($phrase)) { $phrase .= chr(32); } $newOptions = array('all' => '', 'exact' => '', 'least' => '', 'exclude' => '', 'date' => ''); $size = Jaws_UTF8::strlen($phrase); $lastKey = ''; $tmpWord = ''; for ($i = 0; $i < $size; $i++) { $word = Jaws_UTF8::substr($phrase, $i, 1); $ord = Jaws_UTF8::ord($word); $tmpWord .= $word; switch ($ord) { case 34: // Quotes.. if ($lastKey == 'exact') { //Open exact was open, we are closing it $newOptions['exact'] .= Jaws_UTF8::substr($tmpWord, 1, Jaws_UTF8::strlen($tmpWord) - 2); $lastKey = ''; $tmpWord = ''; } else { if (empty($lastKey)) { $lastKey = 'exact'; //We open the exact match } } break; case 43: //Plus if ($lastKey != 'exact') { $lastKey = 'least'; } break; case 45: //Minus if ($lastKey != 'exclude') { $lastKey = 'exclude'; } break; case 32: //Space if ($lastKey != 'exact' && !empty($lastKey)) { if ($lastKey != 'all') { $substrCount = 1; if ($tmpWord[0] == ' ') { $substrCount = 2; } $newOptions[$lastKey] .= Jaws_UTF8::substr($tmpWord, $substrCount); } else { $newOptions[$lastKey] .= $tmpWord; } $lastKey = ''; $tmpWord = ''; } break; default: //Any other word opens all if (empty($lastKey)) { $lastKey = 'all'; } break; } } $options['all'] = ''; $min_key_len = $this->gadget->registry->fetch('min_key_len'); foreach (array_keys($newOptions) as $option) { if (!empty($newOptions[$option])) { $options[$option] = trim(isset($options[$option]) ? $options[$option] . ' ' . $newOptions[$option] : $newOptions[$option]); } $content = isset($options[$option]) ? $options[$option] : ''; $content = $content; $content = Jaws_UTF8::strtolower(Jaws_UTF8::trim($content)); if (Jaws_UTF8::strlen($content) >= $min_key_len) { $searchable = true; } $options[$option] = ''; switch ($option) { case 'exclude': case 'least': case 'all': $options[$option] = array_filter(explode(' ', $content)); break; case 'exact': $options[$option] = array($content); break; case 'date': if (in_array($content, array('past_1month', 'past_2month', 'past_3month', 'past_6month', 'past_1year', 'anytime'))) { $options[$option] = array($content); } else { $options[$option] = array('anytime'); } break; } } return $options; }