/var/www/(Del)gepgroup.hk/common/ActiveRecordModel.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
require_once( realpath(dirname(__FILE__)) . '/connection.php');
class 
ActiveRecordModelBase extends ActiveRecord\Model
{
    static 
$fckeditor_attributes = array();
    
    public function 
delete()
    {
        
// Initial this->errors, for add errors
        
require_once( realpath(dirname(__FILE__)) . '/../php-activerecord/lib/Validations.php');

        
$validator = new ActiveRecord\Validations($this);
        
$this->errors $validator->get_record();
        
        return 
parent::delete();
    }
    
    public function 
safe_set_attributes($attributes$values=array(), $attribute_formatters=array())
    {
        
$table = static::table();
        
$formatters = array();
        
        
$formatters[] = new IgnoreAttributeFormatter(array('id'));
        
$formatters[] = new FCKEditorAttributeFormatter(static::$fckeditor_attributes);
        foreach (
$attribute_formatters as $formatter)
            
$formatters[] = $formatter;
        
$formatters[] = new DefaultAttributeFormatter();
        
        foreach (
$attributes as $attribute => $value)
        {
            if (
array_key_exists($attribute$table->columns))
            {
                foreach (
$formatters as $formatter)
                {
                    if (
$formatter->is_support($attribute))
                    {
                        if (
$formatter->is_update($attribute))
                            
$values[$attribute] = $formatter->format($attribute$value);
                        break;
                    }
                }
            }
        }
        
$this->set_attributes($values);
    }
    
    
// This method source code is copy from update_all()
    // options support joins
    
public static function mysql_update_all($options=array())
    {
        
$table = static::table();
        
$conn = static::connection();
        
        
$table_with_joins $table->get_fully_qualified_table_name();
        if (isset(
$options['joins']) && ($joins $options['joins']))
            
$table_with_joins .= ' ' $joins;
        
        
$sql = new ActiveRecord\SQLBuilder($conn$table_with_joins);

        
// Following code no changed
        
$sql->update($options['set']);

        if (isset(
$options['conditions']) && ($conditions $options['conditions']))
        {
            if (
is_array($conditions) && !ActiveRecord\is_hash($conditions))
                
call_user_func_array(array($sql'where'), $conditions);
            else
                
$sql->where($conditions);
        }

        if (isset(
$options['limit']))
            
$sql->limit($options['limit']);

        if (isset(
$options['order']))
            
$sql->order($options['order']);

        
$values $sql->bind_values();
        
$ret $conn->query(($table->last_sql $sql->to_s()), $values);
        return 
$ret->rowCount();
    }
}

//class MemberActiveRecordModel extends ActiveRecordModelBase
//{
//    static $before_create = array('fill_before_create');
//    static $before_update = array('fill_before_update');
//    
//    static $belongs_to = array(
//        array('create_user', 'class_name' => 'Member', 'foreign_key' => 'create_by', 'readonly' => true),
//        array('modify_user', 'class_name' => 'Member', 'foreign_key' => 'modify_by', 'readonly' => true),
//    );
//    
//    public function fill_before_create()
//    {
//        $now = date("Y-m-d H:i:s");
//        //$now = $this->updated_at->format('Y-m-d H:i:s');
//        $this->create_on = $now;
//        $this->create_by = $_SESSION['member']['id'];
//        $this->modify_on = $now;
//        $this->modify_by = $_SESSION['member']['id'];
//    }
//    
//    public function fill_before_update()
//    {
//        $now = date("Y-m-d H:i:s");
//        //$now = $this->updated_at->format('Y-m-d H:i:s');
//        $this->modify_on = $now;
//        $this->modify_by = $_SESSION['member']['id'];
//    }
//}

class ActiveRecordModel extends ActiveRecordModelBase
{
    static 
$before_create = array('fill_before_create');
    static 
$before_update = array('fill_before_update');
    
    static 
$belongs_to = array(
        array(
'create_user''class_name' => 'SysCmsUser''foreign_key' => 'create_by''readonly' => true),
        array(
'modify_user''class_name' => 'SysCmsUser''foreign_key' => 'modify_by''readonly' => true),
    );
    
    public function 
fill_before_create()
    {
        
$now date("Y-m-d H:i:s");
        
//$now = $this->updated_at->format('Y-m-d H:i:s');
        
$this->create_on $now;
        
$this->create_by $_SESSION['webadmin']['id'];
        
$this->modify_on $now;
        
$this->modify_by $_SESSION['webadmin']['id'];
    }
    
    public function 
fill_before_update()
    {
        
$now date("Y-m-d H:i:s");
        
//$now = $this->updated_at->format('Y-m-d H:i:s');
        
$this->modify_on $now;
        
$this->modify_by $_SESSION['webadmin']['id'];
    }
    
    public function 
assign_sort_if_empty($options=array())
    {
        if (!
strlen($this->sort) || $this->sort == '0')
        {
            
// Get max sort
            
if (!isset($options['select']))
                
$options['select'] = 'MAX(sort) AS max_sort';
            
$all = static::all($options);
            
$model $all[0];
            
$max_sort strlen($model->max_sort) ? $model->max_sort 0;
            
            
// Assign sort
            
$this->sort $max_sort 1;
        }
    }
}

abstract class 
AttributeFormatter
{
    abstract public function 
is_support($attribute);
    abstract public function 
is_update($attribute);
    abstract public function 
format($attribute$value);
}

class 
CustomAttributeFormatter extends AttributeFormatter
{
    private 
$is_support_closure;
    private 
$is_update_closure;
    private 
$format_closure;
    
    function 
__construct($is_support_closure$is_update_closure$format_closure)
    {
        
$this->is_support_closure $is_support_closure;
        
$this->is_update_closure $is_update_closure;
        
$this->format_closure $format_closure;
    }
    
    public function 
is_support($attribute)
    {
        
$closure $this->is_support_closure;
        return 
$closure($attribute);
    }
    
    public function 
is_update($attribute)
    {
        
$closure $this->is_update_closure;
        return 
$closure($attribute);
    }
    
    public function 
format($attribute$value)
    {
        
$closure $this->format_closure;
        return 
$closure($value);
    }
}

class 
IgnoreAttributeFormatter extends AttributeFormatter
{
    private 
$support_attributes;
    
    function 
__construct($support_attributes)
    {
        
$this->support_attributes = empty($support_attributes) ? array() : $support_attributes;
    }
    
    public function 
is_support($attribute)
    {
        return 
in_array($attribute$this->support_attributes);
    }
    
    public function 
is_update($attribute)
    {
        return 
false;
    }
    
    public function 
format($attribute$value)
    {
        throw new 
Exception('Not support format.');
    }
}

class 
FCKEditorAttributeFormatter extends AttributeFormatter
{
    private 
$support_attributes;
    
    function 
__construct($support_attributes)
    {
        
$this->support_attributes = empty($support_attributes) ? array() : $support_attributes;
    }
    
    public function 
is_support($attribute)
    {
        return 
in_array($attribute$this->support_attributes);
    }
    
    public function 
is_update($attribute)
    {
        return 
true;
    }
    
    public function 
format($attribute$value)
    {
        return 
preg_replace("/'/""\'"$value);
    }
}

class 
DefaultAttributeFormatter extends AttributeFormatter
{
    public function 
is_support($attribute)
    {
        return 
true;
    }
    
    public function 
is_update($attribute)
    {
        return 
true;
    }
    
    public function 
format($attribute$value)
    {
        return 
strlen($value) ? htmlspecialchars($valueENT_QUOTES) : NULL;
    }
}