<?php if(!defined('access') or !access) die('This file cannot be directly accessed.'); ?>
<?php G\Render\include_theme_header(); ?>
<?php CHV\Render\show_banner('explore_after_top'); ?>
<div class="content-width">
   
    <?php
       
        // Use CHV magic params
        $list_params = CHV\Listing::getParams();
       
        $type = 'images';
        $category = function_exists('get_category') ? $category : NULL;
       
        try {
            global $list;
            $list = new CHV\Listing;
            $list->setType($type); // images | users | albums
            $list->setOffset($list_params['offset']);
            $list->setLimit($list_params['limit']); // how many results?
            $list->setItemsPerPage($list_params['items_per_page']); // must
            $list->setSortType($list_params['sort'][0]); // date | size | views
            $list->setSortOrder($list_params['sort'][1]); // asc | desc
            if($category) {
                $list->setCategory($category['id']);
            }
            $list->setRequester(CHV\Login::getUser());
            $list->exec();
        } catch(Exception $e) {
            G\exception_to_error($e);
        }
    ?>
   
    <div class="header header-tabs margin-bottom-10 follow-scroll">
        <h1><strong><?php echo $category ? $category['name'] : _s('Explore'); ?></strong></h1>
        <?php
            global $tabs; // Define it as a global to bind it on the included tab.php
            $tabs = array(
                0 => array(
                    'list'        => true,
                    'tools'        => true,
                    'label'        => _s('Most recent'),
                    'id'        => 'list-most-recent',
                    'params'    => 'list=images&sort=date_desc&page=1',
                    'current'    => $_REQUEST['sort'] == 'date_desc' or !$_REQUEST['sort'] ? true : false,
                ),
                1 => array(
                    'list'        => true,
                    'tools'        => true,
                    'label'        => _s('Oldest'),
                    'id'        => 'list-most-oldest',
                    'params'    => 'list=images&sort=date_asc&page=1',
                    'current'    => $_REQUEST['sort'] == 'date_asc',
                ),
                2 => array(
                    'list'        => true,
                    'tools'        => true,
                    'label'        => _s('Most viewed'),
                    'id'        => 'list-most-viewed',
                    'params'    => 'list=images&sort=views_desc&page=1',
                    'current'    => $_REQUEST['sort'] == 'views_desc',
                )
            );
           
            $current = false;
            foreach($tabs as $k => $v) {
               
                if($category) {
                    $tabs[$k]['params_hidden'] .= 'category_id=' . $category['id'];
                }
               
                if($v['current']) {
                    $current = true;
                }
                $tabs[$k]['type'] = 'images';
                $route_path = G\get_route_name();
                if($category) {
                    $route_path .= '/' . $category['url_key'];
                }
                $tabs[$k]['url'] = G\get_base_url($route_path . '/?' . $tabs[$k]['params']);
            }
           
            if(!$current) {
                $tabs[0]['current'] = true;
            }
           
        ?>
       
        <?php G\Render\include_theme_file("snippets/tabs"); ?>
       
        <?php
            if(is_admin()) {
                global $user_items_editor;
                $user_items_editor = false;
                G\Render\include_theme_file("snippets/user_items_editor");
        ?>
        <div class="header-content-right phone-float-none">
            <?php G\Render\include_theme_file("snippets/listing_tools_editor"); ?>
        </div>
        <?php
            }
        ?>
       
    </div>
   
    <div id="content-listing-tabs" class="tabbed-listing">
        <div id="tabbed-content-group">
            <?php
                G\Render\include_theme_file("snippets/listing");
            ?>
        </div>
    </div>
   
</div>
<?php G\Render\include_theme_footer(); ?>