forked from osclass/Osclass
/
Field.php
300 lines (265 loc) · 9.7 KB
/
Field.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php if ( !defined('ABS_PATH') ) exit('ABS_PATH is not loaded. Direct access is not allowed.') ;
/*
* OSCLass – software for creating and publishing online classified
* advertising platforms
*
* Copyright (C) 2010 OSCLASS
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Model database for Field table
*
* @package OSClass
* @subpackage Model
* @since unknown
*/
class Field extends DAO
{
/**
* It references to self object: Field.
* It is used as a singleton
*
* @access private
* @since unknown
* @var Field
*/
private static $instance ;
/**
* It creates a new Field object class ir if it has been created
* before, it return the previous object
*
* @access public
* @since unknown
* @return Field
*/
public static function newInstance()
{
if( !self::$instance instanceof self ) {
self::$instance = new self ;
}
return self::$instance ;
}
/**
* Set data related to t_meta_fields table
*/
function __construct()
{
parent::__construct();
$this->setTableName('t_meta_fields') ;
$this->setPrimaryKey('pk_i_id') ;
$this->setFields( array('pk_i_id', 's_name', 'e_type', 'b_required', 's_slug', 's_options') ) ;
}
/**
* Find a field by its id.
*
* @access public
* @since unknown
* @param int $id
* @return array Field information. If there's no information, return an empty array.
*/
public function findByPrimaryKey($id)
{
$this->dao->select() ;
$this->dao->from($this->getTableName()) ;
$this->dao->where('pk_i_id', $id) ;
$result = $this->dao->get() ;
if( $result == false ) {
return array() ;
}
return $result->row() ;
}
/**
* Find a field by its name
*
* @access public
* @since unknown
* @param string $id
* @return array Field information. If there's no information, return an empty array.
*/
public function findByCategory($id)
{
$this->dao->select('mf.*') ;
$this->dao->from(sprintf('%st_meta_fields mf, %st_meta_categories mc', DB_TABLE_PREFIX, DB_TABLE_PREFIX)) ;
$this->dao->where('mc.fk_i_category_id', $id) ;
$this->dao->where('mf.pk_i_id = mc.fk_i_field_id');
$result = $this->dao->get() ;
if( $result == false ) {
return array() ;
}
return $result->result() ;
}
/**
* Find fields from a category and an item
*
* @access public
* @since unknown
* @param string $id
* @return array Field information. If there's no information, return an empty array.
*/
public function findByCategoryItem($catId, $itemId)
{
if( !is_numeric($catId) || (!is_numeric($itemId) && $itemId != null) ) {
return array() ;
}
$result = $this->dao->query(sprintf("SELECT query.*, im.s_value as s_value FROM (SELECT mf.* FROM %st_meta_fields mf, %st_meta_categories mc WHERE mc.fk_i_category_id = %d AND mf.pk_i_id = mc.fk_i_field_id) as query LEFT JOIN %st_item_meta im ON im.fk_i_field_id = query.pk_i_id AND im.fk_i_item_id = %d", DB_TABLE_PREFIX, DB_TABLE_PREFIX, $catId, DB_TABLE_PREFIX, $itemId));
if( $result == false ) {
return array() ;
}
return $result->result() ;
}
/**
* Find a field by its name
*
* @access public
* @since unknown
* @param string $name
* @return array Field information. If there's no information, return an empty array.
*/
public function findByName($name)
{
$this->dao->select() ;
$this->dao->from($this->getTableName()) ;
$this->dao->where('s_name', $name) ;
$result = $this->dao->get() ;
if( $result == false ) {
return array() ;
}
return $result->row();
}
/**
* Find a field by its name
*
* @access public
* @since unknown
* @param string $slug
* @return array Field information. If there's no information, return an empty array.
*/
public function findBySlug($slug)
{
$this->dao->select() ;
$this->dao->from($this->getTableName()) ;
$this->dao->where('s_slug', $slug) ;
$result = $this->dao->get() ;
if( $result == false ) {
return array() ;
}
return $result->row();
}
/**
* Gets which categories are associated with that field
*
* @access public
* @since unknown
* @param string $id
* @return array
*/
public function categories($id)
{
$this->dao->select('fk_i_category_id') ;
$this->dao->from(sprintf('%st_meta_categories', DB_TABLE_PREFIX)) ;
$this->dao->where('fk_i_field_id', $id) ;
$result = $this->dao->get() ;
if( $result == false ) {
return array() ;
}
$categories = $result->result();
$cats = array();
foreach($categories as $k => $v) {
$cats[] = $v['fk_i_category_id'];
}
return $cats;
}
/**
* Insert a new field
*
* @access public
* @since unknown
* @param string $name
* @param string $type
* @param string $slug
* @param bool $required
* @param array $options
* @param array $categories
*/
public function insertField($name, $type, $slug, $required, $options, $categories = null) {
$this->dao->insert($this->getTableName(), array("s_name" => $name, "e_type" =>$type, "b_required" => $required, "s_slug" => $slug, 's_options' => $options));
$id = $this->dao->insertedId();
if($slug=='') {
$this->dao->update($this->getTableName(), array('s_slug' => $id), array('pk_i_id' => $id));
}
foreach($categories as $c) {
$this->dao->insert(sprintf('%st_meta_categories', DB_TABLE_PREFIX), array('fk_i_category_id' => $c, 'fk_i_field_id' =>$id));
}
}
/**
* Save the categories linked to a field
*
* @access public
* @since unknown
* @param int $id
* @param array $categories
* @return bool
*/
public function insertCategories($id, $categories = null) {
if($categories!=null) {
foreach($categories as $c) {
$res = $this->dao->insert(sprintf('%st_meta_categories', DB_TABLE_PREFIX), array('fk_i_category_id' => $c, 'fk_i_field_id' =>$id));
if(!$res) {
return $res;
}
}
return true;
}
}
/**
* Removes categories from a field
*
* @access public
* @since unknown
* @param int $id
* @return bool on success
*/
public function cleanCategoriesFromField($id) {
return $this->dao->delete(sprintf('%st_meta_categories', DB_TABLE_PREFIX), array('fk_i_field_id' =>$id));
}
/**
* Update a field value
*
* @access public
* @since unknown
* @param int $itemId
* @param int $field
* @param string $value
* @return mixed false on fail, int of num. of affected rows
*/
public function replace($itemId, $field, $value) {
return $this->dao->replace(sprintf('%st_item_meta', DB_TABLE_PREFIX), array('fk_i_item_id' => $itemId, 'fk_i_field_id' => $field, 's_value' => $value));
}
/**
* Delete a field and all information associated with it
*
* @access public
* @since unknown
* @param int $id
* @return bool on success
*/
public function deleteByPrimaryKey($id) {
$this->dao->delete(sprintf('%st_item_meta', DB_TABLE_PREFIX), array('fk_i_field_id' =>$id));
$this->dao->delete(sprintf('%st_meta_categories', DB_TABLE_PREFIX), array('fk_i_field_id' =>$id));
return $this->dao->delete($this->getTableName(), array('pk_i_id' =>$id));
}
}
/* file end: ./oc-includes/osclass/model/Field.php */
?>