function query_content($args = array())
{
global $hmcontent;
$hmdb = new MySQL(true, DB_NAME, DB_HOST, DB_USER, DB_PASSWORD, DB_CHARSET);
hook_filter('before_query_content', $args);
hook_action('query_content');
if (!is_array($args)) {
parse_str($args, $args);
}
/** Lọc theo content_key */
if (isset($args['content_key'])) {
/** Nếu yêu cầu content key thì lấy các id có key như query yêu cầu */
$content_key = $args['content_key'];
/** Nếu content key là một mảng */
if (is_array($content_key)) {
$where_key = '';
$i = 0;
foreach ($content_key as $key) {
if ($i == 0) {
$where_key .= " `key` = '" . $key . "' ";
} else {
$where_key .= " OR `key` = '" . $key . "' ";
}
$i++;
}
$where_content_key = "WHERE " . $where_key;
} else {
$where_content_key = "WHERE `key` = '" . $content_key . "'";
}
} else {
/** Không yêu cầu content key, kiểm tra xem có đang ở template taxonomy không */
if (is_taxonomy() == TRUE) {
$taxonomy_id = get_id();
$content_key = taxonomy_get_content_key($taxonomy_id);
if ($content_key != FALSE) {
$where_content_key = "WHERE `key` = '" . $content_key . "'";
}
} else {
$where_content_key = '';
}
}
$hmdb->Release();
$query_content_key = "SELECT `id` FROM `" . DB_PREFIX . "content` " . $where_content_key;
/** Lọc theo taxonomy */
$where_taxonomy = '';
if (isset($args['taxonomy'])) {
/** Nếu yêu cầu trong một taxonomy nhất định thì lấy các object_id có relationship như query yêu cầu */
$taxonomy_id = $args['taxonomy'];
/** Nếu taxonomy là một mảng */
if (is_array($taxonomy_id)) {
$implode = implode($taxonomy_id, ',');
if ($implode != '') {
$where_taxonomy = ' WHERE `target_id` IN (' . $implode . ') ';
}
} else {
$where_taxonomy = 'WHERE `target_id` = ' . $taxonomy_id;
}
} else {
/** Không yêu cầu taxonomy nhất định, kiểm tra xem có đang ở template taxonomy không */
if (is_taxonomy() == TRUE) {
$taxonomy_id = get_id();
$where_taxonomy = 'WHERE `target_id` = ' . $taxonomy_id;
}
}
if ($where_taxonomy != '') {
$hmdb->Release();
$query_in_taxonomy = "SELECT `object_id` FROM `" . DB_PREFIX . "relationship` " . $where_taxonomy . " AND `relationship` = 'contax'";
}
/** Lọc theo field */
if (isset($args['field_query'])) {
$field_query = $args['field_query'];
} else {
$field_query = array(array('field' => 'status', 'compare' => '=', 'value' => 'public'), array('field' => 'public_time', 'compare' => '<=', 'value' => time()));
}
$all_field_query = array();
foreach ($field_query as $item) {
/** check đủ điều kiện tạo field query */
if (isset($item['field']) and isset($item['compare']) and isset($item['value'])) {
$field = $item['field'];
$compare = $item['compare'];
$value = $item['value'];
$numerically = FALSE;
/** build query */
if (is_numeric($value)) {
$value_query = $value;
} else {
$value_query = "'{$value}'";
}
if ($compare == 'like%') {
$all_field_query[$field] = " ( `name` = '{$field}' AND `val` LIKE '%{$value}%' )";
} else {
$all_field_query[$field] = " ( `name` = '{$field}' AND `val` {$compare} {$value_query} )";
}
}
}
/** nếu size của mảng chứa các kết quả của các field query >= 2 */
$size = sizeof($all_field_query);
$query_field = "SELECT `object_id` FROM `" . DB_PREFIX . "field` WHERE";
if ($size > 1) {
if (isset($args['field_query_relation'])) {
$field_query_relation = $args['field_query_relation'];
} else {
$field_query_relation = 'and';
}
switch ($field_query_relation) {
case 'or':
$i = 0;
foreach ($all_field_query as $single_field_query) {
if ($i == 0) {
$query_field .= " " . $single_field_query . " ";
} else {
$query_field .= " OR " . $single_field_query . " ";
}
$i++;
}
break;
case 'and':
$i = 0;
foreach ($all_field_query as $single_field_query) {
if ($i == 0) {
$query_field .= " " . $single_field_query . " ";
} else {
$query_field .= " OR " . $single_field_query . " ";
}
$i++;
}
$query_field .= " GROUP BY `object_id` HAVING COUNT(*) = {$size} ";
break;
}
/**
* Đưa ra kết quả dựa trên mối quan hệ giữa các field query ( field_query_relation )
* ( thỏa mãn tất cả các field query hay chỉ cần đáp ứng được 1 trong những field query )
*/
} else {
$query_field = $query_field . array_shift(array_values($all_field_query));
}
/** Kiểm tra yêu cầu kết hợp kết quả từ content key, in taxonomy, field query là tất cả hay chỉ 1 */
if (isset($args['join'])) {
$join = $args['join'];
} else {
$join = 'and';
}
$query_join = '';
switch ($join) {
case 'or':
if ($query_content_key) {
$query_join .= " AND `object_id` IN (" . $query_content_key . ") ";
}
if ($query_in_taxonomy) {
$query_join .= " OR `object_id` IN (" . $query_in_taxonomy . ") ";
}
$query_join .= " OR `object_id` IN (" . $query_field . ") ";
break;
case 'and':
if ($query_content_key) {
$query_join .= " AND `object_id` IN (" . $query_content_key . ") ";
}
if ($query_in_taxonomy) {
$query_join .= " AND `object_id` IN (" . $query_in_taxonomy . ") ";
}
$query_join .= " AND `object_id` IN (" . $query_field . ") ";
break;
default:
if ($query_content_key) {
$query_join .= " AND `object_id` IN (" . $query_content_key . ") ";
}
if ($query_in_taxonomy) {
$query_join .= " AND `object_id` IN (" . $query_in_taxonomy . ") ";
}
$query_join .= " AND `object_id` IN (" . $query_field . ") ";
}
/** Kết thúc các query lấy các content id thỏa mãn yêu cầu */
/** Order theo 1 field và limit */
if (isset($args['order'])) {
$order_by = $args['order'];
} else {
$order_by = 'public_time,desc,number';
}
if (isset($args['limit'])) {
$limit = $args['limit'];
} else {
$limit = get_option(array('section' => 'system_setting', 'key' => 'post_per_page', 'default_value' => '12'));
}
if (isset($args['offset']) and is_numeric($args['offset'])) {
$offset = $args['offset'];
} else {
$offset = 0;
}
if (isset($args['paged'])) {
$paged = $args['paged'];
} else {
$paged = get_current_pagination();
}
$paged = $paged - 1;
if ($paged < 0) {
$paged = 0;
}
/** Tạo query ORDER */
$ex = explode(',', $order_by);
$ex = array_map("trim", $ex);
$order_field = $ex[0];
$order = strtoupper($ex[1]);
if (isset($ex[2])) {
$numerically = $ex[2];
} else {
$numerically = FALSE;
}
if ($numerically == 'number') {
$order_query = " AND `name` = '" . $order_field . "' ORDER BY CAST(val AS unsigned) " . $order . " ";
} else {
$order_query = " AND `name` = '" . $order_field . "' ORDER BY `val` " . $order . " ";
}
/** Tạo query LIMIT */
if (is_numeric($limit)) {
$limit_query = " LIMIT {$limit} ";
} else {
$limit_query = '';
}
/** Tạo query OFFSET */
if ($limit == FALSE) {
$offset_query = '';
} else {
$offset_query_page = $paged * $limit;
$offset_query_page = $offset_query_page + $offset;
$offset_query = " OFFSET {$offset_query_page} ";
}
/** Tạo câu lệnh select từ chuỗi các id thỏa mãn */
$result = array();
$sql = "SELECT `object_id`" . " FROM `" . DB_PREFIX . "field`" . " WHERE `object_type` = 'content'" . " " . $query_join . " " . " " . $order_query . " ";
$hmdb->Query($sql);
$total_result = $hmdb->RowCount();
$sql = "SELECT `object_id`" . " FROM `" . DB_PREFIX . "field`" . " WHERE `object_type` = 'content'" . " " . $query_join . " " . " " . $order_query . " " . $limit_query . " " . $offset_query . " ";
$hmdb->Query($sql);
$base = get_current_uri();
if ($base == '') {
$base = '/';
}
$hmcontent->set_val(array('key' => 'total_result', 'val' => $total_result));
$hmcontent->set_val(array('key' => 'paged', 'val' => $paged + 1));
$hmcontent->set_val(array('key' => 'perpage', 'val' => $limit));
$hmcontent->set_val(array('key' => 'base', 'val' => $base));
while ($row = $hmdb->Row()) {
$result[] = $row->object_id;
}
return $result;
}