public function index()
 {
     if (Auth::user()->check()) {
         $page = Input::has('page') ? Input::get('page') : 1;
         $take = Input::has('take') ? Input::get('take') : 20;
         $skip = ($page - 1) * $take;
         $data = VIImage::select('images.id', 'image_details.detail_id', 'image_details.width', 'image_details.height', 'image_details.dpi', 'image_details.size', 'image_details.size_type', 'image_details.path', 'images.name', 'images.short_name', 'images.description', 'images.store')->leftJoin('image_details', function ($join) {
             $join->on('image_details.image_id', '=', 'images.id');
         })->where('images.author_id', Auth::user()->get()->id)->orderBy('images.id', 'desc')->groupBy('images.id');
         $total_image = $data->get()->count();
         $total_page = ceil($total_image / $take);
         $from = $page - 2 > 0 ? $page - 2 : 1;
         $to = $page + 2 <= $total_page ? $page + 2 : $total_page;
         $data = $data->skip($skip)->take($take)->get();
         $arrImages = array();
         if ($data->count() > 0) {
             $i = 0;
             foreach ($data as $key => $value) {
                 $arrImages[$i]['image_id'] = $value->id;
                 $arrImages[$i]['detail_id'] = $value->detail_id;
                 $arrImages[$i]['name'] = $value->name;
                 $arrImages[$i]['short_name'] = $value->short_name;
                 $arrImages[$i]['description'] = $value->description;
                 $arrImages[$i]['store'] = $value->store;
                 $arrImages[$i]['path'] = '/pic/with-logo/' . $value->short_name . '-' . $value->id . '.jpg';
                 $i++;
             }
         }
         $this->layout->metaTitle = Auth::user()->get()->first_name . '\'s Uploads';
         $this->layout->content = View::make('frontend.upload.index', ['files' => $arrImages, 'categories' => Category::getSource(), 'total_image' => $total_image, 'total_page' => $total_page, 'current' => $page, 'from' => $from, 'to' => $to]);
         return;
     }
     return Redirect::route('account-sign-in');
 }
    public function index()
    {
        if (!Input::has('page')) {
            $pageNum = 1;
        } else {
            $pageNum = (int) Input::get('page');
        }
        $admin_id = Auth::admin()->get()->id;
        $arrCategories = [];
        $name = '';
        $take = $this->take;
        $skip = floor(($pageNum - 1) * $take);
        $images = VIImage::select('id', 'name', 'short_name', 'description', 'keywords', 'artist', 'model', 'gender', 'age_from', 'age_to', 'number_people', DB::raw('(SELECT COUNT(*)
																							FROM notifications
																				         	WHERE notifications.item_id = images.id
																				         		AND notifications.item_type = "Image"
																								AND notifications.admin_id = ' . $admin_id . '
																								AND notifications.read = 0 ) as new'))->withType('main')->with('categories')->with('collections');
        if (Input::has('categories')) {
            $arrCategories = (array) Input::get('categories');
            $images->whereHas('categories', function ($query) use($arrCategories) {
                $query->whereIn('id', $arrCategories);
            });
        }
        if (Input::has('name')) {
            $name = Input::get('name');
            $nameStr = '*' . $name . '*';
            $images->search($nameStr);
        }
        $images = $images->take($take)->skip($skip)->orderBy('id', 'desc')->get();
        $arrImages = [];
        if (!$images->isempty()) {
            $arrImages = $arrRemoveNew = [];
            foreach ($images as $image) {
                $image->path = URL . '/pic/large-thumb/' . $image->short_name . '-' . $image->id . '.jpg';
                $image->dimension = $image->width . 'x' . $image['height'];
                if ($image->new) {
                    $arrRemoveNew[] = $image->id;
                }
                $arrImages[$image->id] = $image;
                foreach (['arrCategories' => ['name' => 'categories', 'id' => 'id'], 'arrCollections' => ['name' => 'collections', 'id' => 'id']] as $key => $value) {
                    $arr = [];
                    foreach ($image->{$value}['name'] as $v) {
                        $arr[] = $v[$value['id']];
                    }
                    $arrImages[$image->id][$key] = $arr;
                }
                unset($arr);
            }
            if (!empty($arrRemoveNew)) {
                Notification::whereIn('item_id', $arrRemoveNew)->where('item_type', 'Image')->where('admin_id', $admin_id)->update(['read' => 1]);
            }
        }
        if (Request::ajax()) {
            return $arrImages;
        }
        $this->layout->title = 'Images';
        $this->layout->content = View::make('admin.images-all')->with(['images' => $arrImages, 'pageNum' => $pageNum, 'categories' => Category::getSource(), 'name' => $name, 'arrCategories' => $arrCategories, 'collections' => Collection::getSource(), 'apiKey' => Configure::getApiKeys()]);
    }
 public function getImages()
 {
     if (!Request::ajax()) {
         return App::abort(404);
     }
     $id = Input::has('id') ? Input::get('id') : 0;
     if (!Input::has('page')) {
         $pageNum = 1;
     } else {
         $pageNum = (int) Input::get('page');
     }
     $name = '';
     $take = 25;
     $skip = floor(($pageNum - 1) * $take);
     $images = VIImage::select('id', 'name', 'short_name')->withType('main')->whereRaw('id NOT IN (SELECT image_id FROM collections_images WHERE collection_id = ?)', [$id]);
     if (Input::has('categories')) {
         $arrCategories = (array) Input::get('categories');
         $images->whereHas('categories', function ($query) use($arrCategories) {
             $query->whereIn('id', $arrCategories);
         });
     }
     if (Input::has('name')) {
         $name = Input::get('name');
         $nameStr = '*' . $name . '*';
         $images->search($nameStr);
     }
     $total = $images->count();
     $images = $images->take($take)->skip($skip)->orderBy('id', 'desc')->get();
     $arrReturn = [];
     $arrReturn['total_page'] = ceil($total / $take);
     $arrReturn['page'] = $pageNum;
     if (!$images->isEmpty()) {
         foreach ($images as $image) {
             if ($image->ratio > 1) {
                 $width = 150;
                 $height = $width / $image->ratio;
             } else {
                 $height = 150;
                 $width = $height * $image->ratio;
             }
             $arrReturn['data'][] = ['id' => $image->id, 'name' => $image->name, 'short_name' => $image->short_name, 'path' => URL . '/pic/thumb/' . $image->short_name . '-' . $image->id . '.jpg', 'width' => $width, 'height' => $height, 'ratio' => $image->ratio];
         }
     }
     return $arrReturn;
 }
 public function getSimilarImage($arr_image_id = array(), $viewAll = false, $skip = 0, $takes = 7)
 {
     $similar_images = array();
     $total_image = 0;
     if (count($arr_image_id)) {
         $arr_keyword = array();
         $keywords = VIImage::select(DB::raw(" GROUP_CONCAT(`keywords` SEPARATOR ',') as 'keyword' "))->whereIn('id', $arr_image_id)->get()->toArray();
         $keywords = explode(',', $keywords[0]['keyword']);
         foreach ($keywords as $key => $keyword) {
             if (!isset($arr_keyword[$keyword])) {
                 $arr_keyword[$keyword] = 1;
             } else {
                 $arr_keyword[$keyword] += 1;
             }
         }
         // pr($keywords);
         // pr(DB::getQueryLog());die;
         shuffle($arr_keyword);
         arsort($arr_keyword);
         $arr_keyword = array_keys($arr_keyword);
         $take = count($arr_keyword) < 5 ? count($arr_keyword) - 1 : 5;
         $query = VIImage::select('name', 'short_name', 'images.id')->withType('main');
         for ($i = 0; $i < $take; $i++) {
             $query->orWhereRaw("MATCH(images.name,images.description,images.keywords) AGAINST('" . $arr_keyword[$i] . "' IN BOOLEAN MODE)");
         }
         $query->whereNotIn('images.id', $arr_image_id);
         $query->groupBy('images.id');
         $total_image = $query->get()->count();
         $similar_images = $query->skip($skip)->take($takes)->get()->toArray();
     }
     $arrSimilarImages = array();
     foreach ($similar_images as $value) {
         $arrSimilarImages[$i]['image_id'] = $value['id'];
         $arrSimilarImages[$i]['name'] = $value['name'];
         $arrSimilarImages[$i]['short_name'] = $value['short_name'];
         $arrSimilarImages[$i]['width'] = $value['width'];
         $arrSimilarImages[$i]['height'] = $value['height'];
         //$arrSimilarImages[$i]['path'] = $value['path'];
         $arrSimilarImages[$i]['path'] = '/pic/small-thumb/' . $value['short_name'] . '-' . $value['id'] . '.jpg';
         $i++;
     }
     if (Request::ajax()) {
         $html = View::make('frontend.images.similar-images')->with(['arrSimilarImages' => $arrSimilarImages, 'id_image' => isset($arr_image_id[0]) ? $arr_image_id[0] : 0])->render();
         $arrReturn = ['status' => 'ok', 'message' => '', 'html' => $html];
         $response = Response::json($arrReturn);
         $response->header('Content-Type', 'application/json');
         return $response;
     }
     if (!$viewAll) {
         return View::make('frontend.images.similar-images')->with(['arrSimilarImages' => $arrSimilarImages, 'id_image' => isset($arr_image_id[0]) ? $arr_image_id[0] : 0])->render();
     }
     return ['arrSimilarImages' => $arrSimilarImages, 'total_image' => $total_image];
 }
 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);
 }