<?php

namespace Modules\Menu\Models;

use CodeIgniter\Model;

class MenusModel extends Model
{

  protected $table = 'menu';
    protected $primaryKey = 'id';

    protected $allowedFields = ['title', 'alias', 'photo', 'category_id', 'menutype_id', 'menu_type', 'description', 'parent_id', 'ordering', 'status', 'path', 'post_id', 'post_cat', 'link', 'level',  'created_by', 'updated_at', 'updated_by'];

    protected $useTimestamps  = true;
    protected $useSoftDeletes = true;
    
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    private $menu = "usc_menu";
    public function __construct()
    {
        parent::__construct();
        $this->db = db_connect();
        //ini_set('max_execution_time', 44540);
        //ini_set('memory_limit', '2048M');
       
    }

    //Get all Menus
    public function getAll()
    {
      $builder = $this->db->table('menu');
      $builder->where('deleted_at', null);
      $builder->orderBy('id', 'DESC');
		  $result = $builder->get()->getResult();
		  return $result;
    }

    //Get Single Menu Item
    public function singleMenuItem($item)
    {
      $builder = $this->db->table('menu as m');
      $builder->select('m.*, mt.name as menu_category, mt.alias as category_alias');
      $builder->join('menu_type as mt', 'mt.id=m.category_id');
      $builder->where('m.id', $item);
      return $result = $builder->get()->getRow();
    }

    //Get Menu Group Items
    public function getGroupMenu($id)
    {
      $builder = $this->db->table('menu as m');
      $builder->select('m.*, mt.name as menu_category, mt.alias as category_alias, pm.title as parent_item');
      $builder->join('menu as pm', 'pm.id=m.parent_id', 'Left');
      $builder->join('menu_type as mt', 'mt.id=m.category_id');
      $builder->where('m.deleted_at', null);
      $builder->where('m.category_id', $id);
      $builder->orWhere('mt.alias', $id);
		  $result = $builder->get()->getResult();
		  return $result;
    }



  

}
