/var/www/hkosl.com/littleark/webadmin/models/BaseSimpleModel.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
<?php

use Illuminate\Database\Eloquent\Model as Eloquent;
use 
Core\Session as Session;

class 
BaseSimpleModel extends Eloquent
{
    const 
ORDER_BY 'id';
    const 
ORDER_BY_SORTING 'ASC';

    protected 
$guarded  = array();
    public 
$timestamps false;

    
/**
     * 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();

    }

    
/**
     *
     *
     * @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 
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;
    
}

}