/var/www/hkosl.com/ifapc/html_20210817/IFAPC2021/application/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
196
197
198
199
200
201
202
203
204
205
206
<?php
use Illuminate\Database\Eloquent\Model as Eloquent;

class 
BaseModel extends Eloquent
{
    const 
ORDER_BY 'id';
    const 
ORDER_BY_SORTING 'ASC';
    
//const CREATED_AT = 'created_at';
    //const UPDATED_AT = 'lastupdate';
    //const DELETED_AT = 'deleted_at';

    
protected $dateFormat 'Y-m-d H:i:s';

    protected 
$guarded = [];
    
//protected $hidden = ['created_at', 'created_by', 'lastupdate', 'updated_by', 'deleted_at', 'deleted_by'];

    
protected static $_allowDeleted false;

    
//use SoftDeletes;

    /**
     * Setup model event for manipulating
     * created_by and updated_by field when updating database
     */
    /*public static function boot()
    {
    parent::boot();

    //Eloquent models fire several events, allowing you to hook into various points in the model's lifecycle using the following methods: creating, created, updating, updated, saving,  saved, deleting, deleted, restoring, restored.

    static::creating(function ($model) {
    $model->created_at = date("Y-m-d H:i:s");
    $model->updated_at = date("Y-m-d H:i:s");
    if ($_SESSION['login_id']) {
    $model->created_by = $_SESSION["login_id"];
    $model->updated_by = $_SESSION['login_id'];
    } else {
    $model->created_by = '0';
    $model->updated_by = '0';
    }
    });

    static::updating(function ($model) {
    $model->updated_at = date("Y-m-d H:i:s");
    if ($_SESSION['login_id']) {
    $model->updated_by = $_SESSION['login_id'];
    } else {
    $model->updated_by = '0';
    }
    $model->updated_by = '111';
    });

    static::deleting(function ($model) {
    $model->deleted_at = date("Y-m-d H:i:s");
    if ($_SESSION['login_id']) {
    $model->deleted_by = $_SESSION['login_id'];
    } else {
    $model->deleted_by = '0';
    }
    });

    static::saving(function ($model) {
    $model->updated_at = date("Y-m-d H:i:s");
    if ($_SESSION['login_id']) {
    $model->updated_by = $_SESSION['login_id'];
    } else {
    $model->updated_by = '0';
    }
    $model->updated_by = '111';

    var_dump($model);
    exit;
    });
    }*/
    
public static function setAllowDeleted() {
        static::
$_allowDeleted true;
    }

    public function 
newQuery($excludeDeleted true)
    {
        
// dd(get_class($this));
        
$query parent::newQuery($excludeDeleted);
        if (!static::
$_allowDeleted) {
            
$query->where($this->table '.deleted'0);
        }
        return 
$query;
    }

    public function 
delete()
    {
        
$this->deleted 1;
        return 
$this;
    }

    protected static 
$sort_field true;

    public static function 
get_fields_of_data_by_fields($fields$via 'home')
    {
        
// $fields can be string or array()
        
$model self::select($fields)->where('deleted'0);
        if (
self::$sort_field) {
            
$model->orderBy('sort''asc');
        }
        
$model->orderBy('created_at''desc')->orderBy('id''desc');

        if (
$via == 'webadmin') {
            return 
$model->get();
        } else if (
$via == 'home') {
            return 
$model->where('status'1)->get();
        }
    }

    public static function 
get_data($via 'home')
    {
        
$model self::where('deleted'0);
        if (
self::$sort_field) {
            
$model->orderBy('sort''asc');
        }
        
$model->orderBy('created_at''desc')->orderBy('id''desc');

        if (
$via == 'webadmin') {
            return 
$model->get();
        } else if (
$via == 'home') {
            return 
$model->where('status'1)->get();
        }
    }

    public static function 
get_certain($id$via 'home')
    {
        
$model self::where('deleted'0);
        if (
$via == 'webadmin') {
            return 
$model->find($id);
        } else if (
$via == 'home') {
            return 
$model->where('status'1)->find($id);
        }
    }

    public static function 
get_index_data_by_paginate_and_searching($page$per_page$searches$via 'home')
    {
        
$model self::where('deleted'0);
        if (
self::$sort_field) {
            
$model->orderBy('sort''asc');
        }
        
$model->orderBy('created_at''desc')->orderBy('id''desc');
        if (
$via == 'webadmin') {
            
$languages Master_type_code_model::get_data_by_type_code('LANGUAGE');

            foreach (
$searches as $key => $value) {
                if (!empty(
$value) || $value != null) {
                    if (
$key == 'status' || strpos($key'_id') !== false) {
                        
$model->where($key$value);
                    } else if (
substr($key, -1) == '_') {
                        
$whereRaw '(';
                        foreach (
$languages as $lang) {
                            
$whereRaw .= $key $lang['code'] . ' LIKE \'%' $value '%\'' ' OR ';
                        }
                        
$whereRaw substr($whereRaw0, -strlen(' OR ')) . ')';

                        
$model->whereRaw($whereRaw);
                    } else {
                        
$model->where($key'like''%' $value '%');
                    }
                }
            }
            
$temp_model['total'] = $model->count();
            
$temp_model['model'] = $model->forPage($page$per_page)->get();
            return 
$temp_model;
        }
    }

    public static function 
get_data_by_field_with_value($data$via 'home')
    {
        
$model self::where('deleted'0);
        foreach (
$data as $field => $value) {
            
$model->where($field$value);
        }
        if (
self::$sort_field) {
            
$model->orderBy('sort''asc');
        }
        if (
$via == 'webadmin') {
            return 
$model->get();
        } else {
            return 
$model->where('status'1)->get();
        }
    }

    public static function 
get_latest_sort($data$via 'home')
    {
        
// $data for override get data

        
if (self::$sort_field) {
            if (
$via == 'webadmin') {
                return 
self::max('sort') + 1;
            } else {
                return 
self::where('status'1)->max('sort') + 1;
            }

        }
    }

    protected static 
$fields_list = array();
    public static function 
get_fields_list()
    {
        return 
self::$fields_list;
    }
}