public function index($image_id)
 {
     $arrReturn = array();
     $arrImage = array();
     $imageObj = VIImage::select('images.id', 'images.name', 'images.short_name', 'images.description', 'images.keywords', 'images.artist', 'images.author_id', 'image_details.path', 'image_details.width', 'image_details.height', 'image_details.ratio')->join('image_details', 'image_details.image_id', '=', 'images.id')->where('images.id', $image_id)->first();
     if (is_object($imageObj)) {
         if ($imageObj->ratio > 1) {
             $width = 450;
             $height = $width / $imageObj->ratio;
         } else {
             $height = 450;
             $width = $height * $imageObj->ratio;
         }
         $arrImage = ['image_id' => $imageObj->id, 'name' => $imageObj->name, 'short_name' => $imageObj->short_name, 'description' => $imageObj->description, 'keywords' => $imageObj->keywords, 'artist' => $imageObj->artist, 'author_id' => $imageObj->author_id, 'width' => $width, 'height' => $height, 'path' => '/pic/with-logo/' . $imageObj->short_name . '-' . $imageObj->id . '.jpg', 'path_thumb' => '/pic/thumb/' . $imageObj->short_name . '-' . $imageObj->id . '.jpg'];
         $htmlChooseDownload = $this->loadChooseDownload($imageObj->id);
         $product = Product::with(['type', 'sizeLists', 'optionGroups' => function ($query) {
             $query->select('option_groups.id', 'name', 'key');
         }, 'options' => function ($query) {
             $query->select('options.id', 'name', 'key', 'option_group_id');
         }]);
         $product = $product->where('active', 1)->get();
         $arrProduct = array();
         if (is_object($product)) {
             $arrProduct = $product->toArray();
         }
         // echo '<pre>';
         // print_r($arrProduct[0]['option_groups']);
         // echo '</pre>';
         $htmlOrder = $this->loadOrder($arrImage, $arrProduct);
         $htmlSignin = View::make('frontend.account.signin')->with([])->render();
         $arrKeywords = explode(',', $imageObj->keywords);
         $arrReturn['htmlKeywords'] = View::make('frontend.images.view-keywords')->with('arrKeywords', $arrKeywords)->render();
         $action = Input::has('a') ? Input::get('a') : '';
         $arrReturn['htmlMainImage'] = View::make('frontend.images.main-image')->with(['imageObj' => $arrImage, 'htmlChooseDownload' => $htmlChooseDownload, 'htmlOrder' => $htmlOrder, 'htmlSignin' => $htmlSignin, 'action' => $action, 'htmlKeywords' => $arrReturn['htmlKeywords']])->render();
         $arrReturn['htmlSameArtist'] = $this->loadSameArtist($imageObj->author_id);
         $arrReturn['htmlSimilarImages'] = $this->getSimilarImage(array($imageObj->id));
         $this->layout->metaTitle = $imageObj->name;
         //save to recently_view_images table
         //App::make('AccountController')->addToRecentlyViewImages($imageObj->id);
         if (Auth::user()->check()) {
             BackgroundProcess::actionSearch(['type' => 'recently-view', 'image_id' => $imageObj->id, 'user_id' => Auth::user()->get()->id]);
         }
     } else {
         return App::abort(404);
     }
     $arrReturn['imageObj'] = $arrImage;
     $categories = $this->layout->categories;
     $arrReturn['htmlCategories'] = View::make('frontend.account.view-categories')->with('categories', $categories)->render();
     $lightboxes = array();
     if (Auth::user()->check()) {
         $lightboxes = Lightbox::where('user_id', '=', Auth::user()->get()->id)->get()->toArray();
         foreach ($lightboxes as $key => $lightbox) {
             $lightboxes[$key]['total'] = LightboxImages::where('lightbox_id', '=', $lightbox['id'])->count();
         }
     }
     $arrReturn['lightboxes'] = $lightboxes;
     $arrReturn['mod_download'] = Configure::GetValueConfigByKey('mod_download');
     $arrReturn['mod_order'] = Configure::GetValueConfigByKey('mod_order');
     $arrReturn['arrProduct'] = $arrProduct;
     $this->layout->content = View::make('frontend.images.index')->with($arrReturn);
 }
 public function searchImage()
 {
     $keyword = Input::has('keyword') ? Input::get('keyword') : '';
     //for recently searched images
     $keyword_searched = $keyword;
     $short_name = Input::has('short_name') ? Input::get('short_name') : '';
     $sort_method = Input::has('sort_method') ? Input::get('sort_method') : 'new';
     $sort_style = Input::has('sort_style') ? Input::get('sort_style') : 'mosaic';
     if (trim($keyword) != '') {
         $keyword = '*' . trim($keyword) . '*';
     }
     $page = Input::has('page') ? Input::get('page') : 1;
     $take = Input::has('take') ? Input::get('take') : 30;
     $type = Input::has('type') ? intval(Input::get('type')) : 0;
     $orientation = Input::has('orientation') ? Input::get('orientation') : 'any';
     $skip = ($page - 1) * $take;
     $category = Input::has('category') ? intval(Input::get('category')) : 0;
     $search_type = Input::has('search_type') ? Input::get('search_type') : 'search';
     $exclude_keyword = Input::has('exclude_keyword') ? '*' . Input::get('exclude_keyword') . '*' : '';
     $exclude_people = Input::has('exclude_people');
     $include_people = Input::has('include_people');
     $gender = Input::has('gender') ? Input::get('gender') : 'any';
     $age = Input::has('age') ? Input::get('age') : 'any';
     $ethnicity = Input::has('ethnicity') ? Input::get('ethnicity') : 'any';
     $number_people = Input::has('number_people') ? Input::get('number_people') : 'any';
     $color = Input::has('color') ? Input::get('color') : '';
     $images = VIImage::select('images.short_name', 'images.id', 'images.name', 'images.main_color', DB::raw('count(lightbox_images.id) as count_favorite'))->leftJoin('lightbox_images', 'images.id', '=', 'lightbox_images.image_id')->groupBy('images.id')->with('downloads');
     switch ($sort_method) {
         case 'new':
             $images = $images->with('statistic')->orderBy('images.id', 'desc')->withType('main')->withOrientation($orientation);
             break;
         case 'relevant':
             $images = $images->with('statistic')->orderBy('images.id', 'desc')->withType('main')->withOrientation($orientation);
             break;
         case 'popular':
             $images = $images->join('statistic_image', 'statistic_image.image_id', '=', 'images.id')->with('statistic')->orderBy('view', 'desc')->orderBy('download', 'desc')->withType('main')->withOrientation($orientation);
             break;
         case 'undiscovered':
             $images = $images->join('statistic_image', 'statistic_image.image_id', '=', 'images.id')->with('statistic')->orderBy('view', 'asc')->orderBy('download', 'asc')->withType('main')->withOrientation($orientation);
             break;
         default:
             $images = $images->with('statistic')->orderBy('images.id', 'desc')->withType('main')->withOrientation($orientation);
             break;
     }
     if ($search_type == 'search' && $keyword != '') {
         $images = $images->search($keyword);
     }
     if ($exclude_keyword != '') {
         $images = $images->notSearchKeyWords($exclude_keyword);
     }
     if ($exclude_people) {
         $images = $images->where('number_people', '=', 0);
     }
     if ($include_people) {
         if ($gender != 'any') {
             $images = $images->where('gender', '=', $gender);
         }
         if ($age != 'any') {
             $age = explode('-', $age);
             $from = intval($age[0]);
             $to = intval($age[1]);
             $images = $images->where('age_from', '<=', $to)->where('age_to', '>=', $from);
         }
         if ($ethnicity != 'any') {
             $images = $images->where('ethnicity', '=', $ethnicity);
         }
         if ($number_people != 'any') {
             if ($number_people < 4) {
                 $images = $images->where('number_people', '=', $number_people);
             } else {
                 $images = $images->where('number_people', '>=', $number_people);
             }
         }
     }
     if (!(Input::has('editorial') && Input::has('non_editorial'))) {
         if (Input::has('editorial')) {
             $images = $images->where('editorial', '=', 1);
         }
         if (Input::has('non_editorial')) {
             $images = $images->where('editorial', '=', 0);
         }
     }
     if ($type != 0) {
         $images = $images->where('type_id', '=', $type);
     }
     if ($category != 0) {
         $images = $images->join('images_categories', 'images_categories.image_id', '=', 'images.id')->where('images_categories.category_id', '=', $category);
     }
     $total_image = $images->count();
     $images = $images->get()->toArray();
     $images = $this->searchColorFromArray($color, $images);
     $total_image = count($images);
     $images = array_slice($images, $skip, $take);
     //pr(DB::getQueryLog());die;
     $total_page = ceil($total_image / $take);
     //for recently searched images
     if ((trim($keyword_searched) != '' || $short_name != '') && count($images) > 0) {
         if ($keyword_searched == '') {
             $keyword_searched = str_replace('-', ' ', $short_name);
         }
         if (Auth::user()->check()) {
             BackgroundProcess::actionSearch(['type' => 'recently-search', 'keyword' => $keyword_searched, 'image_id' => $images[0]['id'], 'user_id' => Auth::user()->get()->id, 'query' => Request::server('REQUEST_URI')]);
         }
         BackgroundProcess::actionSearch(['type' => 'popular-search', 'keyword' => $keyword_searched, 'image_id' => $images[0]['id'], 'query' => Request::server('REQUEST_URI')]);
     }
     $lightboxes = array();
     if (Auth::user()->check()) {
         $lightboxes = Lightbox::where('user_id', '=', Auth::user()->get()->id)->get()->toArray();
         foreach ($lightboxes as $key => $lightbox) {
             $lightboxes[$key]['total'] = LightboxImages::where('lightbox_id', '=', $lightbox['id'])->count();
         }
     }
     $image_action_title = 'Like this item';
     if (Auth::user()->check()) {
         $image_action_title = 'Save to lightbox';
     }
     $arr_sort_method = array('new' => 'New', 'popular' => 'Popular', 'relevant' => 'Relevant', 'undiscovered' => 'Undiscovered');
     $arrReturn = array('images' => $images, 'total_page' => $total_page, "total_image" => $total_image, "sort_style" => $sort_style, "search_type" => $search_type, "categories" => $this->layout->categories, "lightboxes" => $lightboxes, "image_action_title" => $image_action_title, "arr_sort_method" => $arr_sort_method, "sort_method" => $sort_method, 'mod_download' => Configure::GetValueConfigByKey('mod_download'), 'mod_order' => Configure::GetValueConfigByKey('mod_download'));
     if (Request::ajax()) {
         return $arrReturn;
     }
     $this->layout->in_search = 1;
     $this->layout->content = View::make('frontend.search')->with($arrReturn);
 }