<?php
/**
 * Routes configuration
 *
 * In this file, you set up routes to your controllers and their actions.
 * Routes are very important mechanism that allows you to freely connect
 * different URLs to chosen controllers and their actions (functions).
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

use Cake\Core\Plugin;
use Cake\Routing\Router;

/**
 * The default class to use for all routes
 *
 * The following route classes are supplied with CakePHP and are appropriate
 * to set as the default:
 *
 * - Route
 * - InflectedRoute
 * - DashedRoute
 *
 * If no call is made to `Router::defaultRouteClass()`, the class used is
 * `Route` (`Cake\Routing\Route\Route`)
 *
 * Note that `Route` does not do any inflections on URLs which will result in
 * inconsistently cased URLs when used with `:plugin`, `:controller` and
 * `:action` markers.
 *
 */

Router::defaultRouteClass('DashedRoute');

Router::scope('/', function ($routes) {


    $routes->setExtensions(['json']);
    $routes->resources('Files');
    /**
     * Here, we are connecting '/' (base path) to a controller called 'Pages',
     * its action called 'display', and we pass a param to select the view file
     * to use (in this case, src/Template/Pages/home.ctp)...
     */

    $url = $_SERVER['REQUEST_URI'];
    $exploded_url = explode("/", $url);
    $params_array = [];
    $cmsuser_id = 0;
    $client_id = 0;
    if(!empty($exploded_url)){
        foreach ($exploded_url as $ex_value){
            if($ex_value > 0){
                $params_array[] = $ex_value;
            }
        }
    }

    if(isset($params_array[0]) && $params_array[0] > 0){
       $cmsuser_id =  $params_array[0];
    }


    if($cmsuser_id > 0 && $client_id == 0){
        $routes->connect('/:pass', ['controller' => 'Cmsusers', 'action' => 'login']);
    }
    else if($cmsuser_id > 0 && $client_id > 0){
        $routes->connect('/:pass/:client_id', ['controller' => 'Cmsusers', 'action' => 'login']);
    }


    $routes->connect('/login', ['controller' => 'Cmsusers', 'action' => 'login']);
    $routes->connect('/', ['controller' => 'Learninghub', 'action' => 'index']);

    $routes->connect('/admin', ['controller' => 'Cmsusers', 'action' => 'login']);
    $routes->connect('/reset_password/*', ['controller' => 'Cmsusers', 'action' => 'resetPassword']);

    /**
     * ...and connect the rest of 'Pages' controller's URLs.
     */
    $routes->connect('/users/logout/', ['controller' => 'Cmsusers', 'action' => 'logout']);
    $routes->connect('/users/edit-profile/', ['controller' => 'Cmsusers', 'action' => 'editProfile']);
    $routes->connect('/users/add/', ['controller' => 'Cmsusers', 'action' => 'add']);
    $routes->connect('/forgot_password', ['controller' => 'Cmsusers', 'action' => 'forgotPassword']);
    $routes->connect('/users/bulk-action', ['controller' => 'Cmsusers', 'action' => 'bulkAction']);
    $routes->connect('/users/allowaction/*', ['controller' => 'Cmsusers', 'action' => 'allowaction']);
    $routes->connect('/users/revokeaction/*', ['controller' => 'Cmsusers', 'action' => 'revokeaction']);
    $routes->connect('/users/revokePassword/*', ['controller' => 'Cmsusers', 'action' => 'revokePassword']);
    $routes->connect('/users/edit/*', ['controller' => 'Cmsusers', 'action' => 'edit']);
    $routes->connect('/users/view/*', ['controller' => 'Cmsusers', 'action' => 'view']);
    $routes->connect('/users/addmembership/*', ['controller' => 'Cmsusers', 'action' => 'addmembership']);
    $routes->connect('/users/revokemembership/*', ['controller' => 'Cmsusers', 'action' => 'revokemembership']);
    $routes->connect('/users/*', ['controller' => 'Cmsusers']);

    $routes->connect('/pages/*', ['controller' => 'Pages']);
    $routes->connect('/pages/edit/*', ['controller' => 'Pages', 'action' => 'edit']);
    $routes->connect('/pages/add', ['controller' => 'Pages', 'action' => 'add']);
    $routes->connect('/pages/delete/*', ['controller' => 'Pages', 'action' => 'delete']);
    $routes->connect('/pages/bulk-action', ['controller' => 'Pages', 'action' => 'bulkAction']);

    $routes->connect('/submodules/*', ['controller' => 'SubModules']);
    $routes->connect('/submodules/add', ['controller' => 'SubModules', 'action' => 'add']);
    $routes->connect('/submodules/edit/*', ['controller' => 'SubModules', 'action' => 'edit']);
    $routes->connect('/submodules/delete/*', ['controller' => 'SubModules', 'action' => 'delete']);
    $routes->connect('/submodules/bulk-action', ['controller' => 'SubModules', 'action' => 'bulkAction']);


    $routes->connect('/companies/*', ['controller' => 'Companies']);
    $routes->connect('/companies/add', ['controller' => 'Companies', 'action' => 'add']);
    $routes->connect('/companies/edit/*', ['controller' => 'Companies', 'action' => 'edit']);
    $routes->connect('/companies/delete/*', ['controller' => 'Companies', 'action' => 'delete']);
    $routes->connect('/companies/view/*', ['controller' => 'Companies', 'action' => 'view']);
    $routes->connect('/companies/bulk-action', ['controller' => 'Companies', 'action' => 'bulkAction']);
    $routes->connect('/companies/uploadPicture', ['controller' => 'Companies', 'action' => 'uploadPicture']);
    $routes->connect('/companies/companyActive', ['controller' => 'Companies', 'action' => 'companyActive']);
    $routes->connect('/companies/trnsToaws/*', ['controller' => 'Companies', 'action' => 'trnsToaws']);

    $routes->connect('/settings/edit/*', ['controller' => 'Settings', 'action' => 'edit']);


    $routes->connect('/session', ['controller' => 'Session', 'action' => 'index']);
    $routes->connect('/session/add', ['controller' => 'Session', 'action' => 'add']);
    $routes->connect('/session/edit/*', ['controller' => 'Session', 'action' => 'edit']);
    $routes->connect('/session/delete/*', ['controller' => 'Session', 'action' => 'delete']);
    $routes->connect('/session/view/*', ['controller' => 'Session', 'action' => 'view']);

    $routes->connect('/course', ['controller' => 'Course', 'action' => 'index']);
    $routes->connect('/course/add', ['controller' => 'Course', 'action' => 'add']);
    $routes->connect('/course/edit/*', ['controller' => 'Course', 'action' => 'edit']);
    $routes->connect('/course/delete/*', ['controller' => 'Course', 'action' => 'delete']);
    $routes->connect('/course/view/*', ['controller' => 'Course', 'action' => 'view']);


    $routes->connect('/student', ['controller' => 'Student', 'action' => 'index']);
    $routes->connect('/student/add', ['controller' => 'Student', 'action' => 'add']);
    $routes->connect('/student/edit/*', ['controller' => 'Student', 'action' => 'edit']);
    $routes->connect('/student/delete/*', ['controller' => 'Student', 'action' => 'delete']);


    $routes->connect('/about', ['controller' => 'Learninghub', 'action' => 'about']);
    $routes->connect('/acreditation', ['controller' => 'Learninghub', 'action' => 'acreditation']);
    $routes->connect('/accomodation', ['controller' => 'Learninghub', 'action' => 'accomodation']);
    $routes->connect('/application-process', ['controller' => 'Learninghub', 'action' => 'applicationProcess']);

    $routes->connect('/apply-form', ['controller' => 'Learninghub', 'action' => 'applyForm']);
    $routes->connect('/contact', ['controller' => 'Learninghub', 'action' => 'contact']);
    $routes->connect('/courses', ['controller' => 'Learninghub', 'action' => 'courses']);
    $routes->connect('/courses/:slug', ['controller' => 'Learninghub', 'action' => 'viewCourses'], [
        'pass' => ['slug'],
        'slug' => '[a-z0-9_\-]+'
    ]);
    $routes->connect('/verification', ['controller' => 'Learninghub', 'action' => 'verification']);
    $routes->connect('/agencies', ['controller' => 'Learninghub', 'action' => 'agent']);
    $routes->connect('/courses/english', ['controller' => 'Learninghub', 'action' => 'viewEnglishCourses']);


    /**
     * Connect catchall routes for all controllers.
     *
     * Using the argument `DashedRoute`, the `fallbacks` method is a shortcut for
     *    `$routes->connect('/:controller', ['action' => 'index'], ['routeClass' => 'DashedRoute']);`
     *    `$routes->connect('/:controller/:action/*', [], ['routeClass' => 'DashedRoute']);`
     *
     * Any route class can be used with this method, such as:
     * - DashedRoute
     * - InflectedRoute
     * - Route
     * - Or your own route class
     *
     * You can remove these routes once you've connected the
     * routes you want in your application.
     */
    $routes->fallbacks('DashedRoute');
});

/**
 * Load all plugin routes.  See the Plugin documentation on
 * how to customize the loading of plugin routes.
 */
Plugin::routes();
