This repository has been archived by the owner on Dec 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnection.php
226 lines (190 loc) · 6.47 KB
/
connection.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
<?php
date_default_timezone_set('UTC');
define('EMAIL_CONTACT', '[email protected]');
define('AUTHOR', 'bitSnaps');
define('DEBUG_MODE', true);
//alternative to json_encode for PHP < 5.4 (fix utf8 encoding issue)
function raw_json_encode($input) {
return preg_replace_callback(
'/\\\\u([0-9a-zA-Z]{4})/',
function ($matches) {
return mb_convert_encoding(pack('H*',$matches[1]),'UTF-8','UTF-16');
},
json_encode($input)
);
}
require_once __DIR__.'/phpar/ActiveRecord.php';
if (DEBUG_MODE){
$db_host = "127.0.0.1";
$db_user = "root";
$db_pass = "";
$db_name = "mysql";
} else {
$db_host = "your.hosting.com";
$db_user = "your_db_user";
$db_pass = "your_db_password";
$db_name = "your_db_name";
}
//setup connections
$connections = array(
'development' => "mysql://$db_user:$db_pass@$db_host/$db_name;charset=utf8",
'production' => "mysql://$db_user:$db_pass@$db_host/$db_name;charset=utf8"
);
//initialize ActiveRecord
ActiveRecord\Config::initialize(function($cfg) use ($connections)
{
$cfg->set_model_directory('.');
$cfg->set_connections($connections);
$cfg->set_default_connection('production');
});
//Abstract Model
abstract class BaseModel extends ActiveRecord\Model {
//explicit connection ([production|development])
//static $connection = 'production';
//explicit database name
// static $db = 'my_database_name';
private $searchField;
private $primaryKey;
private $labels = array();
private $fields = array();
public static $no_result = '{
"current": 0,
"rowCount": 0,
"rows":[]
,
"total": 0
}';
public function getJson($id = '1'){
$searchPhrase = (isset($_POST['searchPhrase']) && !empty($_POST['searchPhrase']))?$_POST['searchPhrase']:'';
//get query parameters
$sort = (isset($_POST['sort']) && !empty($_POST['sort']))?$_POST['sort']:array();
$rowCount = (isset($_POST['rowCount']) && !empty($_POST['rowCount']))?$_POST['rowCount']:0;
$current = (isset($_POST['current']) && !empty($_POST['current']))?$_POST['current']-1:0;
//order by clause
$orderClause = '';
$i = 0;
if (count($sort) > 0){
foreach ($sort as $field => $order)
$orderClause.= ($i++>0?',':'').$field.' '.$order;
}
//filter condition
$conditions = '';
if (!empty($searchPhrase)){
$conditions.=$this->getSearchField().' LIKE "%'.addslashes($searchPhrase).'%"';
}
$parameters = array(
// 'joins'=>array('servers'), //not yet supported
'select'=>empty($this->fields)?'*':join($this->fields,','),
'conditions'=>array($this->getPK().' >= ?'.(empty($conditions)?'':' and ('.$conditions.')'), $id),
'order'=>$orderClause
);
$allRecords = $this::all($parameters);
if ($rowCount > 0){
$parameters['limit']=$rowCount;
$parameters['offset']=$current*$rowCount;
}
$records = $this::all($parameters);
$totalRows = count($allRecords);
$rows = array();
foreach ($records as $s)
array_push($rows, $s->attributes());
return '{
"current": '.($current+1).',
"rowCount": '.$rowCount.',
"rows":'.( raw_json_encode($rows) ).',
"total": '.$totalRows.'
}';
} //getJson()
public function getName(){
return get_class($this);
}
protected function prettyName($value){
return ucwords(str_replace('_',' ',$value));
}
public function setLabels($l){
foreach ($l as $field => $label){
if (is_int($field))
$this->labels[$label] = $this->prettyName($label);
else
$this->labels[$field] = ($label == '*') ?$this->prettyName($field).'*':$label;
} //foreach
$this->fields = array_keys($this->labels);
}
private function fillFields(){
$attributes = array();
foreach ($this->getAttributes() as $k => $v)
$attributes[$k] = $this->prettyName($k);
$this->setLabels($attributes);
}
public function getLabels($withoutOptions = false){
if (count($this->labels) == 0){
$this->fillFields();
}
$l = array();
if ($withoutOptions){
foreach ($this->labels as $field => $label){
$l[$field] = str_replace('*', '', $label);
}
return $l;
}
return $this->labels;
}
public function getFields(){
return $this->fields;
}
public function setSearchField($field_name){
$this->searchField = $field_name;
}
public function getSearchField(){
if (empty($this->searchField)){
$this->searchField = $this->getFirstField();
}
return $this->searchField;
}
public function getAttributes(){
return $this->attributes();
}
public function setPK($pk){
$this->primaryKey = $pk;
}
public function getPK(){
if (empty($this->primaryKey)){
$this->primaryKey = $this->getFirstField();
}
return $this->primaryKey;
}
protected function getFirstField(){
$f = $this->labels;
reset($f);
return key($f);
}
public function update($values){
$model = $this::find($values[$this->getPK()]);
if (!empty($model)){
foreach ($model->attributes() as $k => $v){
//you could also use property_exists() to check in different way
if (array_key_exists($k, $values))
$model->$k = htmlentities($values[$k], ENT_COMPAT, 'UTF-8'); //need some security enhancements...
}
return $model;
}
}
public function insert($values){
$model = new $this();
foreach ($model->attributes() as $k => $v){
if (array_key_exists($k, $values))
$model->$k = htmlentities($values[$k], ENT_COMPAT, 'UTF-8'); //need some security enhancements...
}
return $model;
}
public function remove($rid){
$model = $this::find($rid);
$model->delete();
}
} //BaseModel
/* Models */
class User extends BaseModel{
//explicit table name
static $table_name = 'user';
} //User
?>