function fn_search($params, $items_per_page = 0, $lang_code = CART_LANGUAGE) { $data = array(); $search = fn_get_search_objects(); $pieces = array(); $search_type = ''; $default_params = array('page' => 1, 'items_per_page' => $items_per_page, 'objects' => array()); $params = array_merge($default_params, $params); foreach ($search['conditions']['functions'] as $object => $function) { if (in_array($object, $search['default'])) { continue; } if (!in_array($object, $params['objects'])) { unset($search['conditions']['functions'][$object]); } } if (empty($params['q'])) { $params['q'] = ''; } if (empty($params['match'])) { $params['match'] = 'any'; } $params['search_string'] = $params['q']; foreach ($search['conditions']['functions'] as $object => $function) { if (!empty($function) && is_callable($function)) { $_params = $params; if (!empty($search['default_params'][$object])) { $_params = fn_array_merge($_params, $search['default_params'][$object]); } $search['conditions']['values'][$object] = call_user_func($function, $_params, $lang_code); $search['action_links'][$object] = str_replace('%search%', urlencode($params['q']), $search['action_links'][$object]); } } fn_set_hook('search_by_objects', $search['conditions']['values'], $params); if (count($search['conditions']['values']) == 1 && (!empty($params['compact']) && $params['compact'] == 'Y')) { list($object) = each($search['conditions']['values']); return fn_search_simple($params, $search, $object, $items_per_page, $lang_code); } elseif (count($search['conditions']['values'])) { db_query("CREATE TEMPORARY TABLE _search (id int NOT NULL, object varchar(30) NOT NULL, sort_field varchar(255) NOT NULL)ENGINE=HEAP;"); foreach ($search['conditions']['values'] as $object => $entry) { $entry['table'] = !empty($entry['table']) ? $entry['table'] : "?:" . $object; $select = db_quote("SELECT {$entry['table']}.{$entry['key']}, '{$object}', {$entry['sort']} FROM ?:{$object} as {$entry['table']} {$entry['join']} WHERE {$entry['condition']} GROUP BY {$entry['table']}.{$entry['key']}"); db_query("INSERT INTO _search (id, object, sort_field) ?p", $select); } if (!empty($params['items_per_page'])) { $params['total_items'] = db_get_field('SELECT COUNT(id) FROM _search'); $limit = db_paginate($params['page'], $params['items_per_page'], $params['total_items']); if (preg_match("/\\s+(\\d+),/", $limit, $begin)) { $begin = intval($begin[1]); } else { $begin = 0; } } else { $limit = ''; $begin = 0; } $objects_count = db_get_hash_array('SELECT COUNT(*) as count, object, id FROM _search GROUP BY object', 'object'); if (!empty($params['compact']) && ($params['compact'] = 'Y')) { return array($objects_count, $search); } $results = db_get_array('SELECT id, object FROM _search ORDER BY sort_field ' . $limit, 'id'); if ($results) { $ids = array(); foreach ($results as $id => $entry) { $ids[$entry['object']][] = $entry['id']; } $_data = array(); foreach ($search['conditions']['values'] as $object => $entry) { if (empty($ids[$object]) || !count($ids[$object])) { continue; } $entry['table'] = !empty($entry['table']) ? $entry['table'] : "?:" . $object; $_data[$object] = db_get_hash_array("SELECT " . implode(', ', $entry['fields']) . " FROM ?:{$object} as {$entry['table']} {$entry['join']} WHERE {$entry['condition']} AND {$entry['table']}.{$entry['key']} IN ('" . join("', '", $ids[$object]) . "') GROUP BY {$entry['table']}.{$entry['key']}", $entry['key']); } $num = 0; foreach ($_data as $object => &$__data) { if (!empty($search['bulk_data'][$object])) { $search['bulk_data'][$object]($__data); } } foreach ($results as $key => $entry) { $data[$num] = $_data[$entry['object']][$entry['id']]; $data[$num]['object'] = $entry['object']; if (!empty($search['more_data'][$entry['object']])) { $search['more_data'][$entry['object']]($data[$num]); } $data[$num]['result_number'] = $begin + $num + 1; if (count($search['conditions']['values']) == 1) { $data[$num]['result_type'] = 'full'; } else { $data[$num]['result_type'] = 'short'; } $num++; } $data[0]['first'] = true; unset($_data); if (empty($params['total_items'])) { $params['total_items'] = count($data); } } } return array($data, $params); }
function fn_search($params, $items_per_page = 0, $lang_code = CART_LANGUAGE) { $data = array(); $search = Registry::get('search_object'); $pieces = array(); $search_type = ''; if (empty($params['objects'])) { $params['objects'] = array(); } if (empty($params['page'])) { $params['page'] = 1; } foreach ($search['conditions']['functions'] as $object => $function) { if ($search['default'] == $object) { continue; } if (!in_array($object, $params['objects'])) { unset($search['conditions']['functions'][$object]); } } if (empty($params['q'])) { $params['q'] = ''; } if (empty($params['match'])) { $params['match'] = 'any'; } $params['search_string'] = $params['q']; foreach ($search['conditions']['functions'] as $object => $function) { if (!empty($function) && function_exists($function)) { $_params = $params; if (!empty($search['default_params'][$object])) { $_params = fn_array_merge($_params, $search['default_params'][$object]); } $search['conditions']['values'][$object] = $function($_params, $lang_code); } } fn_set_hook('search_by_objects', $search['conditions']['values']); if (count($search['conditions']['values']) == 1) { list($object) = each($search['conditions']['values']); return fn_search_simple($params, $search, $object, $items_per_page, $lang_code); } elseif (count($search['conditions']['values'])) { db_query("CREATE TEMPORARY TABLE _search (id int NOT NULL, object varchar(30) NOT NULL, sort_field varchar(255) NOT NULL)ENGINE=HEAP;"); foreach ($search['conditions']['values'] as $object => $entry) { $entry['table'] = !empty($entry['table']) ? $entry['table'] : "?:" . $object; $select = db_quote("SELECT {$entry['table']}.{$entry['key']}, '{$object}', {$entry['sort']} FROM ?:{$object} as {$entry['table']} {$entry['join']} WHERE {$entry['condition']} GROUP BY {$entry['table']}.{$entry['key']}"); if (AREA == 'A' && Registry::is_exist('revisions')) { fn_revisions_process_select($select); } db_query("INSERT INTO _search (id, object, sort_field) ?p", $select); } if ($items_per_page) { $total = db_get_field('SELECT COUNT(id) FROM _search'); $limit = fn_paginate($params['page'], $total, $items_per_page); if (preg_match("/\\s+(\\d+),/", $limit, $begin)) { $begin = intval($begin[1]); } else { $begin = 0; } } else { $limit = ''; $total = 0; $begin = 0; } $results = db_get_array('SELECT id, object FROM _search ORDER BY sort_field ' . $limit, 'id'); if ($results) { $ids = array(); foreach ($results as $id => $entry) { $ids[$entry['object']][] = $entry['id']; } $_data = array(); foreach ($search['conditions']['values'] as $object => $entry) { if (empty($ids[$object]) || !count($ids[$object])) { continue; } $entry['table'] = !empty($entry['table']) ? $entry['table'] : "?:" . $object; $_data[$object] = db_get_hash_array("SELECT " . implode(', ', $entry['fields']) . " FROM ?:{$object} as {$entry['table']} {$entry['join']} WHERE {$entry['condition']} AND {$entry['table']}.{$entry['key']} IN ('" . join("', '", $ids[$object]) . "') GROUP BY {$entry['table']}.{$entry['key']}", $entry['key']); } $num = 0; foreach ($results as $key => $entry) { $data[$num] = $_data[$entry['object']][$entry['id']]; $data[$num]['object'] = $entry['object']; if (!empty($search['more_data'][$entry['object']])) { $search['more_data'][$entry['object']]($data[$num]); } $data[$num]['result_number'] = $begin + $num + 1; if (count($_data) == 1) { $data[$num]['result_type'] = 'full'; } else { $data[$num]['result_type'] = 'short'; } $num++; } $data[0]['first'] = true; unset($_data); if (!$total) { $total = count($data); } } } return array($data, $params, $total); }