/home/mip/mip/app/Modules/AbstractRepository.php
<?php

namespace QxCMS\Modules;

abstract class AbstractRepository
{
    public function findById($id)
    {
        return $this->model->findOrFail($id);
    }

    public function select($columns = ['*'])
    {
       return $this->model->select($columns);
    }

    public function all()
    {
        return $this->model->all();
    }

    public function where($column, $operator = null, $value = null, $boolean = 'and')
    {
        return $this->model->where($column, $operator, $value, $boolean);
    }

    public function lists($name = 'name', $id = '', $orderBy = array('name', 'asc'))
    {
        if($id != '') return $this->model->orderBy($orderBy[0], $orderBy[1])->lists($name, $id)->all();
        else return $this->model->orderBy($orderBy[0], $orderBy[1])->lists($name)->all();
    }

    public function pluck($name = 'name', $id = '')
    {
        if($id != '') return $this->model->pluck($name, $id);
        else return $this->model->pluck($name);
    }

    public function with($eagers = '')
    {
        return $this->model->with($eagers);
    }

    public function getAjaxResponse($type, $message)
    {
        return  response(
            array('message'=>$message,'type'=>$type)
        )->header('Content-Type', 'application/json');
    }

    public function getModuleId()
    {
        return $this->model->module_id;
    }

    public function setConfig($client)
    {
        \Config::set('database.connections.client', array(
            'driver'    => 'mysql',
            'host'      => $client->database_host,
            'database'  => env('DB_PREFIX', 'qxcms_').$client->database_name,
            'username'  => $client->database_username,
            'password'  => $client->database_password,
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ));
        return config('database.connections.client');
    }

    public function getUserLogs($module_id, $id = null, $sub_menu = null)
    {
        if(($id == null) && ($sub_menu == null)) return $this->log->where('module_id', $module_id)->latest()->take(3)->with('user')->get();
        else if($sub_menu == null) return $this->log->where('module_id', $module_id)->where('data_id', $id)->where('sub_menu', '')->latest()->take(3)->with('user')->get();
        else if(($id == null) && ($sub_menu != null)) return $this->log->where('module_id', $module_id)->where('sub_menu', $sub_menu)->latest()->take(3)->with('user')->get();
        else return $this->log->where('module_id', $module_id)->where('data_id', $id)->where('sub_menu', $sub_menu)->latest()->take(3)->with('user')->get();
    }
}