| Server IP : 210.245.233.93 / Your IP : 216.73.216.226 Web Server : Apache/2.2.15 (CentOS) System : Linux webserver2.onesolution.com.hk 2.6.32-754.35.1.el6.x86_64 #1 SMP Sat Nov 7 12:42:14 UTC 2020 x86_64 User : apache ( 48) PHP Version : 5.3.3 Disable Function : exec, shell_exec, system, passthru, popen, proc_open, pcntl_exec MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/hkosl.com/b2b2c/webadmin/models/ |
Upload File : |
<?php
use Illuminate\Database\Eloquent\Model as Eloquent;
use Core\Session as Session;
class BaseModel extends Eloquent
{
const ORDER_BY = 'id';
const ORDER_BY_SORTING = 'ASC';
protected $guarded = array();
/**
* The name of the "created at" column.
*
* @var string
*/
const CREATED_AT = 'createdate';
/**
* The name of the "updated at" column.
*
* @var string
*/
const UPDATED_AT = 'lastupdate';
/**
* Overrides Illuminate\Database\Eloquent\Model 's query()
* for showing records deleted=0 by default
*
* @var boolean
*/
protected static $_allowDeleted = false;
protected static $_relations = array();
public static $orderBy = 'id';
/**
* Setup model event for manipulating
* createby and lastupby field when updating database
*/
public static function boot()
{
parent::boot();
static::creating( function( $model ) {
if ( $_SESSION['cmsloginid'] ) {
$model->createby = $_SESSION['cmsloginid'];
$model->lastupby = $_SESSION['cmsloginid'];
}else {
$model->createby = '0';
$model->lastupby = '0';
}
} );
static::updating( function( $model ) {
if ( $_SESSION['cmsloginid'] ) {
$model->lastupby = $_SESSION['cmsloginid'];
}else {
$model->lastupby = '0';
}
} );
}
/**
*
*
* @param unknown $excludeDeleted (optional)
* @return unknown
*/
public function newQuery( $excludeDeleted = true )
{
// dd(get_class($this));
$query = parent::newQuery( $excludeDeleted );
if ( ! static::$_allowDeleted ) {
$query->where( $this->table . '.deleted', 0 );
}
else {
static::$_allowDeleted = false;
}
return $query;
}
/**
* call this if you need deleted record as well
*
* @return unknown
*/
public static function allowDeleted() {
static::$_allowDeleted = true;
return new static;
}
public function creator(){
return $this->belongsTo('SysCmsLogin', 'createby', 'cmsloginid');
}
public function updater(){
return $this->belongsTo('SysCmsLogin', 'lastupby', 'cmsloginid');
}
public function creator_name(){
return $this->creator->cmsusername ?: 'N/A';
}
public function updater_name(){
return $this->updater->cmsusername ?: 'N/A';
}
public function scopeID($query, $id)
{
return $query->where('id', $id);
}
public function scopeCompanyId($query, $year_id)
{
return $query->where('company_id', $year_id);
}
public function scopeYearId($query, $year_id)
{
return $query->where('year_id', $year_id);
}
public function scopeIsDeleted($query, $val='1')
{
return $query->where('deleted', $val);
}
public function delete(){
$this->deleted = 1;
return $this;
}
public function file()
{
return $this->morphMany('File', 'fileable')->orderBy(static::ORDER_BY, static::ORDER_BY_SORTING);
}
public function image()
{
return $this->morphMany('Image', 'imageable')->orderBy(static::ORDER_BY, static::ORDER_BY_SORTING);
}
public static $labels = array();
/**
* translate given string to the Model's label
*
* @param string $key array key in labels array
* @return String translated label
*/
public static function label( $key, $lang=false )
{
if(!$lang){
$lang = Session::getDefaultSegment()->get('language');
}
if ( array_key_exists( $lang, static::$labels ) ) {
if ( array_key_exists( $key, static::$labels[$lang] ) ) {
return static::$labels[$lang][$key];
}
}
if ( array_key_exists( $key, static::$labels ) ) {
return static::$labels[$key];
}
return $key;
}
/**
* models will override this function to modify query builder
* based on filters provided
* @param query_builder $query the query of the model
* @param array $filters array of filters to be applied to the model
* @return query_builder modified query builder
*/
public static function filter($query, $filters = array())
{
return $query;
}
// tell twig the fields exist
public function __isset($name)
{
if (in_array($name, static::$_relations) ) {
// var_dump("found {$name}");
return true;
}
// var_dump("called parent isset {$name}");
return parent::__isset($name);
// return false;
}
}