public function get_search_items($parent_id = 0) { // Reset $parent_id if its invalid value is_numeric($parent_id) or $parent_id = 0; /************ Start generating query ***********/ $items = Model_Casestudy::find(function ($query) use($parent_id) { // Select only root pages // $query->where('parent_id', $parent_id); // Get search filters foreach (\Input::get() as $key => $value) { if (!empty($value) || $value == '0') { switch ($key) { case 'title': $query->where($key, 'like', "%{$value}%"); break; case 'status': if (is_numeric($value)) { $query->where($key, $value); } break; case 'active_from': $date = strtotime($value); if ($date) { $query->where($key, '>=', $date); } break; case 'active_to': $date = strtotime($value); if ($date) { $query->where($key, '<=', $date); } break; } } } // Order query $query->order_by('sort', 'asc'); $query->order_by('id', 'asc'); }); /************ End generating query ***********/ // Reset to empty array if there are no result found by query if (is_null($items)) { $items = array(); } // Initiate pagination $pagination = \Hybrid\Pagination::make(array('total_items' => count($items), 'per_page' => \Input::get('per_page', 10), 'uri_segment' => null)); // Remove unwanted items, and show only required ones $items = array_slice($items, $pagination->offset, $pagination->per_page); $status = array('false' => 'Select', '1' => 'Active', '0' => 'Inactive', '2' => 'Active in period'); return array('items' => $items, 'pagination' => $pagination, 'status' => $status); }
/** * Get additional content data selected * * @param $result = Query result */ public static function post_find($result) { if ($result !== null) { if (is_array($result)) { foreach ($result as $item) { // It will first check if we already have result in temporary result, // and only execute query if we dont. That way we dont have duplicate queries // Get content images $item->get_images = static::lazy_load(function () use($item) { return Model_Image::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'images'); // Get content files $item->get_files = static::lazy_load(function () use($item) { return Model_File::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'files'); // Get content videos $item->get_videos = static::lazy_load(function () use($item) { return Model_Video::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'videos'); // Get content children $item->get_children = static::lazy_load(function () use($item) { return Model_Casestudy::find(array('where' => array('parent_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'children'); // Get content accordions $item->get_accordions = static::lazy_load(function () use($item) { return Model_Accordion::find(array('where' => array('parent_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'accordions'); // Get content children $item->get_seo = static::lazy_load(function () use($item) { return Model_Seo::find_one_by_content_id($item->id); }, $item->id, 'seo', 'object'); // Get related products $item->get_related_products = static::lazy_load(function () use($item) { $related = Model_Application_To_Related::find(function ($query) use($item) { return $query->where('application_id', $item->id); }, 'related_id'); if (!empty($related)) { $related = '(' . implode(',', array_keys($related)) . ')'; return \Product\Model_Product::find(function ($query) use($related, $item) { $query->where('id', 'IN', \DB::expr($related)); $query->where('id', '<>', $item->id); $query->order_by('sort', 'asc'); return $query; }, 'id'); } return array(); }, $item->id, 'related_products'); // Get hotspot images $item->get_hotspot = static::lazy_load(function () use($item) { $return = new \stdClass(); $return->images = Model_Application_Image::find_by_application_id($item->id); return $return; }, $item->id, 'hotspot', 'object'); } } } // return the result return $result; }
/** * Get additional content data selected * * @param $result = Query result */ public static function post_find($result) { if ($result !== null) { if (is_array($result)) { foreach ($result as $item) { // It will first check if we already have result in temporary result, // and only execute query if we dont. That way we dont have duplicate queries // Get content images $item->get_images = static::lazy_load(function () use($item) { return Model_Image::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'images'); // Get content files $item->get_files = static::lazy_load(function () use($item) { return Model_File::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'files'); // Get content videos $item->get_videos = static::lazy_load(function () use($item) { return Model_Video::find(array('where' => array('content_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'videos'); // Get content children $item->get_children = static::lazy_load(function () use($item) { return Model_Casestudy::find(array('where' => array('parent_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'children'); // Get content accordions $item->get_accordions = static::lazy_load(function () use($item) { return Model_Accordion::find(array('where' => array('parent_id' => $item->id), 'order_by' => array('sort' => 'asc'))); }, $item->id, 'accordions'); // Get content children $item->get_seo = static::lazy_load(function () use($item) { return Model_Seo::find_one_by_content_id($item->id); }, $item->id, 'seo', 'object'); } } } // return the result return $result; }