<?php
 // ini_set('display_startup_errors',1);
 // ini_set('display_errors',1);
 // error_reporting(-1);
/**
 *
 * @package  MediaLibrary
 * @copyright 2021 Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru); Rob de Cleen(rob@decleen.com);
 * Homepage: https://ordasoft.com
 * @version: 4.6 Free
 * @license GNU General Public license version 2 or later; see LICENSE.txt
 *
 */
if (!defined('_VALID_MOS') && !defined('_JEXEC')) die('Direct Access to ' . 
    basename(__FILE__) . ' is not allowed.');
JLoader::register('ContentHelper', JPATH_ADMINISTRATOR . 
    '/components/com_content/helpers/content.php');
JLoader::register('CategoryHelperAssociation', JPATH_ADMINISTRATOR . 
    '/components/com_categories/helpers/association.php');
abstract class BookLibraryHelperAssociation extends CategoryHelperAssociation {
    public static function getAssociations($id = 0, $view = null) {
        global $database;

        $task = trim(mosGetParam($_REQUEST, 'task', ""));

        if (isset($_REQUEST['view'])) {
            $view = protectInjectionWithoutQuote('view');
            if ((!isset($task) OR $task == '') AND isset($view)) $task = $view; 
        }

        if ((isset($_GLOBALS)) && ($_GLOBALS['task'] != "")) {
            $task = $_GLOBALS['task'];

        }


            $itemId = $_REQUEST['Itemid'];

            JLoader::import('helpers.route', JPATH_COMPONENT_SITE);
            if ($task == 'showCategory') {
                $catid = intval(mosGetParam($_REQUEST, 'catid', 0));
                $layout = intval(mosGetParam($_REQUEST, 'layout', 0));
                if (version_compare(JVERSION, '3.0', 'ge')) {
                    $menu = new JTableMenu($database);
                    $menu->load($Itemid);
                    $params = new JRegistry;
                    $params->loadString($menu->params);
                } else {
                    $menu = new mosMenu($database);
                    $menu->load($GLOBALS['Itemid']);
                    $params = new mosParameters($menu->params);
                }
                $layout = $params->get('categorylayout', '');
                if (empty($layout)) {
                    $layout = $params->get('singlecatlay', '');
                }
                if ($catid) {
                    $rederectUrlArr = BookLibraryHelperRoute::getBookCategoryRoute($catid /*, $itemId*/);
                    return $rederectUrlArr;
                } else {
                    if (version_compare(JVERSION, "1.6.0", "lt")) {
                        $catid = $params->get('catid');
                    } else if (version_compare(JVERSION, "1.6.0", "ge")) {
                        $single_category_id = ''; // for 1.6
                        $single_category_id = $params->get('single_category');
                        if ($single_category_id > 0) $catid = $single_category_id;
                        else {
                            $query = "SELECT * FROM #__booklibrary_main_categories";
                            $database->setQuery($query);
                            $a = $database->loadObjectList();
                            $catid = $a[0]->id;
                        }
                    }

                    $rederectUrlArr = BookLibraryHelperRoute::getBookCategoryRoute($catid /*, $itemId*/);
                    return $rederectUrlArr;
                }
                if ($layout == 'alonecategory') {
                    $rederectUrlArr = BookLibraryHelperRoute::getBookCategoryRoute($catid /*, $itemId*/);
                    return $rederectUrlArr;
                }
            }


            if ($task == 'view_bl' or $task == 'view' or $task == 'view_book') {
                $catid = intval(mosGetParam($_REQUEST, 'catid', 0));
                $id = intval(mosGetParam($_REQUEST, 'id', 0));
                if(!$id) {
                    if (version_compare(JVERSION, '3.0', 'ge')) {
                        
                        $menu = new JTableMenu($database);
                        $menu->load($Itemid);
                        $params = new JRegistry;
                        $params->loadString($menu->params);
                    } else {
                        $menu = new mosMenu($database);
                        $menu->load($GLOBALS['Itemid']);
                        $params = new mosParameters($menu->params);
                    }
                    if (version_compare(JVERSION, "1.6.0", "lt")) {
                        $id = $params->get('book');
                    } else if (version_compare(JVERSION, "1.6.0", "ge")) {
                        $view_book_id = ''; // for 1.6
                        $view_book_id = $params->get('book');
                        if ($view_book_id > 0) {
                            $id = $view_book_id;
                        } else {
                            $query = "SELECT id FROM #__booklibrary";
                            $database->setQuery($query);
                            $a = $database->loadObjectList();
                            $id = $a[0]->id;
                        }
                    }    

                }

                //--------------------------------------------
                if ($id) {
                    $rederectUrlArr = BookLibraryHelperRoute::getBookAssocRoute($id, $task, $catid /*, $itemId*/);
                    return $rederectUrlArr;
                }
            }

            //print_r($task . "</br>");
            //print_r($_REQUEST);
            //exit;

            if ($task == 'view_author') {
                $id = intval(mosGetParam($_REQUEST, 'id', 0));
                if(!$id) {
                    if (version_compare(JVERSION, '3.0', 'ge')) {        
                        $menu = new JTableMenu($database);
                        $menu->load($Itemid);
                        $params = new JRegistry;
                        $params->loadString($menu->params);
                    } else {
                        $menu = new mosMenu($database);
                        $menu->load($GLOBALS['Itemid']);
                        $params = new mosParameters($menu->params);
                    }
                    if (version_compare(JVERSION, "1.6.0", "lt")) {
                        $id = $params->get('author');
                    } else if (version_compare(JVERSION, "1.6.0", "ge")) {
                        $view_author_id = ''; // for 1.6
                        $view_author_id = $params->get('author');
                        if ($view_author_id > 0) {
                            $id = $view_author_id;
                        } else {
                            $query = "SELECT id FROM #__booklibrary_authors";
                            $database->setQuery($query);
                            $a = $database->loadObjectList();
                            $id = $a[0]->id;
                        }
                    }    

                }

                //--------------------------------------------
                if ($id) {
                    $rederectUrlArr = BookLibraryHelperRoute::getAuthorAssocRoute($id, $task /* $itemId*/);
                    return $rederectUrlArr;
                }
            }

            switch ($task) {
                case 'books':
                case 'all_authors':
                case 'all_category':
                case 'categories':
                case 'owners_list':
                case 'show_search_book':
                case 'show_my_books':
                case 'owner_books':
                case 'rent_history_books':
                case 'showmybooks':

                    $arr_params = array() ;
                    $arr_params['task'] = $task ;
                    $rederectUrlArr = BookLibraryHelperRoute::getBLOtherRoute($arr_params);
                    return $rederectUrlArr;
                    break;
                case 'lend_request_bl':
                case 'view_user_books':
                    $arr_params = array() ;
                    $arr_params['task'] = $task ;
                    $arr_params['tab'] = mosGetParam($_REQUEST, 'tab', "") ;
                    $arr_params['is_show_data'] = mosGetParam($_REQUEST, 'is_show_data', "") ;
                    if (isset($_REQUEST['name']) && !empty($_REQUEST['name']))  
                        $arr_params['name'] = mosGetParam($_REQUEST, 'name', "") ;
                    $rederectUrlArr = BookLibraryHelperRoute::getBLOtherRoute($arr_params);
                    return $rederectUrlArr;
                    break;

                default:
                    break;
            }

        return array();
    }
}
