Devin Smith 35b0f32e83 WARNING: THIS MIGHT BREAK EVERYTHING
Upgraded all db stuff to PDO
fixes #2195
2015-02-27 17:46:24 -08:00

62 lines
1.5 KiB
PHP

<?php
// query functions until i build a real query buider for cana/tipsy
class Crunchbutton_Query extends Cana_Model {
public static function search($params) {
$search = $params['search'];
$q = '';
if ($search) {
if ($params['stripslashes']) {
$search = stripslashes($search);
}
$words = preg_split("/[\s,]*\\\"([^\\\"]+)\\\"[\s,]*|" . "[\s,]*'([^']+)'[\s,]*|" . "[\s,]+/", $search, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$sq = '';
foreach ($words as $k => $word) {
$sq .= ($sq ? ' AND ' : '').'(';
if ($word{0} == '-') {
$word = substr($word, 1);
$match = false;
} else {
$match = true;
}
if (!$word) {
continue;
}
foreach ($params['fields'] as $key => $type) {
$w = null;
$sqq .= $sqq ? ' '.($match ? 'OR' : 'AND').' ' : '';
if ($type == 'like' || $type == 'liker' || $type == 'likel') {
$sqq .= ' '.$key.' '.($match ? '' : 'NOT').' LIKE ? ';
$w = ($type == 'like' || $type == 'likel' ? '%' : '').$word.($type == 'like' || $type == 'liker' ? '%' : '');
} elseif ($type == 'eq') {
$sqq .= ' '.$key.' '.($match ? '' : '!').'= ? ';
} elseif ($type == 'gt') {
$sqq .= ' '.$key.' > ? ';
} elseif ($type == 'lt') {
$sqq .= ' '.$key.' < ? ';
}
$sqq .= "\n";
$keys[] = $w ? $w : $word;
}
$sq .= $sqq.')';
$sqq = '';
}
$q .= '
AND ('.$sq.')
';
$sq = '';
}
return ['query' => $q, 'keys' => $keys];
}
}