/var/www/hkosl.com/imusiccircle/webadmin/models/BaseModel.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?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');
        }
        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;
    
}

}