static function __combine_where($keys, $revelance = FALSE) { self::$revelance = FALSE; $implode = ' AND '; if (!is_array($keys) or empty($keys)) { return ''; } if (self::$SAFE_MODE_WHERE) { foreach ($keys as $key => $value) { if (is_null($value)) { $a[] = self::__combine_keyn($key) . ' IS NULL'; } else { $a[] = self::__combine_keyn($key) . '=' . sql::escape($value); } } } else { $a = array(); foreach ($keys as $key => $v) { if (!is_array($v)) { $v = array($v); } foreach ($v as $value) { if ($key === 'OR') { $implode = ' OR '; } elseif (substr($key, -1) == '!' and is_null($value) or $value === '') { $a[] = self::__combine_keyn(substr($key, 0, -1)) . ' IS NOT NULL'; } elseif (is_null($value) or $value === '') { $a[] = self::__combine_keyn($key) . ' IS NULL'; } elseif (substr($key, -1) == '!') { $a[] = self::__combine_keyn(substr($key, 0, -1)) . '!=' . sql::escape($value); } elseif ($key == '^') { $a[] = 'MAX(' . self::__combine_keyn($value) . ')'; } elseif (substr($key, -2) == '~~') { $temp = 'MATCH (' . self::__combine_keyn(substr($key, 0, -2)) . ') AGAINST (' . sql::escape($value) . ')'; if ($revelance) { self::$revelance = $temp . ' AS `revelance`'; } $a[] = $temp; } elseif (substr($key, -1) == '~') { $a[] = self::__combine_keyn(substr($key, 0, -1)) . ' LIKE ' . sql::escape($value); } elseif (substr($key, -2) == '>=') { $a[] = self::__combine_keyn(substr($key, 0, -2)) . '>=' . sql::escape($value); } elseif (substr($key, -2) == '<=') { $a[] = self::__combine_keyn(substr($key, 0, -2)) . '<=' . sql::escape($value); } elseif (substr($key, -1) == '>') { $a[] = self::__combine_keyn(substr($key, 0, -1)) . '>' . sql::escape($value); } elseif (substr($key, -1) == '<') { $a[] = self::__combine_keyn(substr($key, 0, -1)) . '<' . sql::escape($value); } else { $a[] = self::__combine_keyn($key) . '=' . sql::escape($value); } } } } return ' WHERE ' . implode($implode, $a) . $addon; }
static function szukaj_info($dane, $order = NULL, $start = NULL, $limit = 30) { $allow = array('id', 'tytul', 'autor', 'wydawnictwo', 'miejsce', 'rok', 'wydanie', 'wycofana'); $replace = array('tytul' => 'tytul~~', 'autor' => 'autor~~', 'wydawnictwo' => 'wydawnictwo~~'); $where = array(); foreach ($dane as $key => $value) { if (!in_array($key, $allow) or $value === '') { continue; } if ($replace[$key]) { $key = $replace[$key]; } $where[$key] = $value; } if ($where['id']) { validate::$kod = TRUE; switch (validate::type($where['id'])) { case 'ISBN': $where['ISBN'] = $where['id']; unset($where['id']); break; case 'ISSN': $where['ISSN'] = $where['id']; unset($where['id']); break; case 'MSC': $where['regal'] = $where['id']; if ($dane['polka']) { $where['polka'] = $dane['polka']; } if ($dane['rzad']) { $where['rzad'] = $dane['rzad']; } unset($where['id']); break; } validate::$kod = FALSE; } if (!$where['regal']) { unset($where['polka']); unset($where['rzad']); } if ($where['id']) { $ret[] = self::szukaj_KOD($where['id']); $num = count($ret); } else { if ($dane['do']) { $num = db2::num('pozycz', 'id'); if ($num == 0) { $ret = array(); } else { $ret = db2::get(array('pozycz', array('J', 'ksiazki', 'USING', 'id')), '*', NULL, $order, $start, $limit); } } else { $num = db2::num('ksiazki', 'id', $where); if ($num == 0) { $ret = array(); } else { $where = db2::__combine_where($where, TRUE); $ret = db2::escape_data(sql::fetch(sql::query('SELECT `ksiazki`.*, `pozycz`.`od`, `pozycz`.`kto`' . (db2::revelance() ? ', ' . db2::$revelance : '') . ' FROM `ksiazki` LEFT OUTER JOIN `pozycz` ON `pozycz`.`id`=`ksiazki`.`id` ' . $where . db2::__combine_order($order, TRUE) . db2::__combine_limit($start, $limit)))); } } self::cache_addarray($ret); } return array($num, $ret, db2::revelance()); }