public function getCurrentInventory($make = NULL, $model = NULL, $min = NULL, $max = NULL, $order = NULL, $featured = NULL, $page = 1) { global $wpdb; if (!is_numeric($this->getAttribute('per_page'))) { $page_size = $featured == 'true' ? 5 : 10; } else { $page_size = abs(round($this->getAttribute('per_page'))); } $page = is_numeric($page) ? abs(round($page)) : 1; /** @var Auto[] $autos */ $autos = array(); $from = "\n\t\t\t" . $wpdb->prefix . "squirrels_inventory si\n\t\t\t\tJOIN " . $wpdb->prefix . "posts p_makes\n\t\t\t\t\tON p_makes.id = si.make_id\n\t\t\t\tJOIN " . $wpdb->prefix . "posts p_models\n\t\t\t\t\tON p_models.id = si.model_id\n\t\t\t\tJOIN " . $wpdb->prefix . "posts p_types\n\t\t\t\t\tON p_types.id = si.type_id\n\t\t\t\tLEFT JOIN " . $wpdb->prefix . "squirrels_images im\n\t\t\t\t\tON si.id = im.inventory_id\n\t\t\t\tLEFT JOIN " . $wpdb->prefix . "postmeta pm\n\t\t\t\t\tON im.media_id = pm.post_id AND pm.meta_key = '_wp_attachment_metadata'"; $where = "\n\t\t\tsi.is_visible = 1"; if (strlen($make) > 0 && is_numeric($make)) { $where .= "\n AND si.make_id = " . abs(round($make)); } if (strlen($model) > 0 && is_numeric($model)) { $where .= "\n AND si.model_id = " . abs(round($model)); } if (strlen($min) > 0 && is_numeric($min)) { $where .= "\n AND COALESCE(si.price, 0) >= " . abs(round($min)); } if (strlen($max) > 0 && is_numeric($max)) { $where .= "\n AND COALESCE(si.price, 0) <= " . abs(round($max)); } if ($featured == 'true') { $where .= "\n AND si.is_featured = 1"; } switch ($order) { case 'price_asc': $order_by = "\n\t\t\t\t\tCOALESCE(si.price, 0) ASC"; break; case 'price_desc': $order_by = "\n\t\t\t\t\tCOALESCE(si.price, 0) DESC"; break; case 'year_asc': $order_by = "\n\t\t\t\t\tsi.year ASC"; break; case 'year_desc': $order_by = "\n\t\t\t\t\tsi.year DESC"; break; default: $order_by = "\n\t\t\t\t\tp_makes.post_title, p_models.post_title"; } $order_by .= ",im.is_default DESC, im.id"; $sql = "\n\t\t\tSELECT\n\t\t\t\tCOUNT( DISTINCT si.id) AS result_count\n\t\t\tFROM\n\t\t\t\t" . $from . "\n\t\t\tWHERE\n\t\t\t\t" . $where; $result = $wpdb->get_row($sql); $this->attributes['results'] = $result->result_count; $this->attributes['pages'] = ceil($result->result_count / $page_size); $sql = "\n\t\t\tSELECT\n\t\t\t\tDISTINCT si.id\n\t\t\tFROM\n\t\t\t\t" . $from . "\n\t\t\tWHERE\n\t\t\t\t" . $where . "\n\t\t\tORDER BY\n\t\t\t\t" . $order_by . "\n\t\t\tLIMIT " . round($page_size * ($page - 1)) . ", " . $page_size; $ids = array(); $results = $wpdb->get_results($sql); foreach ($results as $result) { $ids[] = $result->id; } $sql = "\n\t\t\tSELECT\n\t\t\t\tp_makes.post_title AS make,\n\t\t\t\tp_models.post_title AS model,\n\t\t\t\tp_types.post_title AS `type`,\n\t\t\t\tim.id AS image_id,\n\t\t\t\tim.media_id,\n\t\t\t\tim.url,\n\t\t\t\tim.is_default,\n\t\t\t\tim.created_at AS image_created_at,\n\t\t\t\tim.updated_at AS image_updated_at,\n\t\t\t\tpm.meta_value,\n\t\t\t\tsi.*\n\t\t\tFROM\n\t\t\t\t" . $from . "\n\t\t\tWHERE\n\t\t\t\tsi.id IN ( " . implode(',', $ids) . " )\n\t\t\tORDER BY\n\t\t\t\t" . $order_by; $results = $wpdb->get_results($sql); foreach ($results as $result) { if (!array_key_exists($result->id, $autos)) { $auto = new Auto(); $auto->loadFromRow($result); $autos[$auto->getId()] = $auto; } if ($result->image_id !== NULL) { $thumbnail = ''; $image_meta = maybe_unserialize($result->meta_value); if (is_array($image_meta) && isset($image_meta['sizes'])) { foreach ($image_meta['sizes'] as $size => $data) { if ($size == 'thumbnail') { $thumbnail = $data['file']; $url_parts = explode('/', $result->url); unset($url_parts[count($url_parts) - 1]); $url_parts[] = $thumbnail; $thumbnail = implode('/', $url_parts); break; } } } $image = new Image(); $image->setId($result->image_id)->setInventoryId($result->id)->setMediaId($result->media_id)->setUrl($result->url)->setThumbnail($thumbnail)->setIsDefault($result->is_default)->setCreatedAt($result->image_created_at)->setUpdatedAt($result->image_updated_at); $autos[$result->id]->addImage($image); } } return $autos; }