<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * 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
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */

App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package     app.Controller
 * @link        http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
public $uses = array('Inventory_stockin','Inventory_stockin_item','Sync_error','Order_orderhistory_json','Offer','Order_split_orderhistory','Order_split_transaction','Order_split_tax','Order_split_payment','Account_imprest','Catalog_product_branches','Catalog_category_branches','User_location','Catalog_unit_mapping','Inventory_linking','Kds_track','Kds_users_station_mapping','Kds_user','Inventory_semifinished_semifinished_linking','Inventory_damage_item','Inventory_damage','Inventory_indent_items','Inventory_indent','Inventory_subcategory','Inventory_category','Catalog_product_attribute','Order_customers','Order_modifier','Inventory_modifier_semifinished_linking','Inventory_modifier_sku_linking','Catalog_product_modifier_group_linkings','Catalog_modifier_group_linkings','Catalog_modifier_group','Catalog_modifier','Online_order','Temp_audit_stock','Temp_audit_stock_items','Audit_stock','Audit_stock_items','Stock_ledger','Catalog_package','Order_table_tracking','Stock_transfer','Stock_transfer_item','User_track','Order_waiter','Inventory_semifinished_stock','Inventory_sku_stock','Inventory_purchase_order','Inventory_purchase_order_item','Inventory_internal_indent_items','Inventory_internal_indent','Department','User','Card','Catalog_unit','Inventory_unit','Cardtracking','User_module','Catalog_category','Catalog_subcategory','Catalog_tax','Catalog_product','Catalog_product_addon','Order_orderhistory','Order_table','Order_transaction','Catalog_product_tax','Order_payment','Order_tax','Coupon','Location','Pos_printer','Online_booking','Inventory_sku','Inventory_tax','Inventory_sku_tax','Inventory_vendor','Inventory_vendor_sku_served','Inventory_vendor_service_area','Inventory_semifinished','Inventory_semifinished_tax','Inventory_semifinished_sku_linking','Inventory_product_semifinished_linking','Inventory_product_sku_linking');
public $helpers = array('Form', 'Html', 'Js', 'Time','Cache','Session', 'Flash', 'Time');
public $components = array('Auth', 'Session','Cookie','RequestHandler','DebugKit.Toolbar', 'Flash');

//    public function isAuthorized($user) {
//        // Admin can access every action
//        if (isset($user['role']) && $user['role'] === 'admin') {
//            return true;
//        }
//
//
//        // Default deny
//        return false;
//    }
function isAuthorized() {
    if (!empty($this->params['prefix']) && $this->params['prefix'] == 'admin') {
        if ($this->Auth->user('role') != 'admin') {
            return false;
        }
    }
    return true;
}
    public function beforeFilter() {
        $this->Auth->allow('login');
        
        
         if ($this->request->controller == 'dashboard') {
            AuthComponent::$sessionKey = 'Auth.User'; 
            $this->Auth->loginAction = array( 'controller' => 'dashboard', 'action' => 'login' );
            $this->Auth->loginRedirect = array('controller' => 'dashboard', 'action' => 'home');
            $this->Auth->logoutRedirect = array( 'controller' => 'dashboard', 'action' => 'login' );
            //,'passwordHasher' => 'Blowfish' 
            $this->Auth->authenticate = array('Form' => array( 'userModel' => 'User', 'scope' => array('User.status' =>1) ));        
            $this->Auth->authorize = 'Controller';
        } 
        
        else {
            //$this->Auth->allow();           
        }
        
        
        
        
        
            // if($this->Session->check('Auth.User')){
            //     if (isset($this->params['prefix']) && $this->params['prefix'] == 'admin' && $this->Session->read('Auth.User.role')!= "admin" ) {
            //         $this->redirect(array('controller'=>'dashboard', 'action'=>'index',  'admin'=>FALSE ));
            //     }
            //     if (isset($this->params['prefix']) && $this->params['prefix'] == 'manager' && $this->Session->read('Auth.User.role')!= "manager" ) {
            //         $this->redirect(array('controller'=>'dashboard', 'action'=>'index',  'admin'=>TRUE ));
            //     }
            //     if (!isset($this->params['prefix']) && $this->Session->read('Auth.User.role')== "admin" )             {
            //         $this->redirect(array('controller'=>'dashboard', 'action'=>'index',  'admin'=>TRUE ));
            //     }else {
            //         return true;
                   
            //     }
            // }
    }
    
    public function trackUser($ref_id,$users, $type, $remarks = [])
    {
        $this->loadModel('User_track');
    
        $this->User_track->create();
        $this->User_track->save([
            'reference_id'  => $ref_id ?? 0,
            'user_id'       => $users['User']['id'] ?? 0,
            'location_id'   => $users['User']['location_id'] ?? 0,
            'location_name' => $users['User']['location_name'] ?? '',
            'type'          => $type,
            'remarks'       => json_encode($remarks)
        ]);
    }
}

