/
extend_field_model.php
252 lines (206 loc) · 6.46 KB
/
extend_field_model.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
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Ionize
*
* @package Ionize
* @author Ionize Dev Team
* @license http://ionizecms.com/doc-license
* @link http://ionizecms.com
* @since Version 0.9.5
*/
// ------------------------------------------------------------------------
/**
* Ionize Extend Fields Model
*
* @package Ionize
* @subpackage Models
* @category Lang
* @author Ionize Dev Team
*
*/
class Extend_field_model extends Base_model
{
/**
* Constructor
*
* @access public
*/
public function __construct()
{
parent::__construct();
// Stores the extend fields definition
$this->set_table('extend_field');
$this->set_pk_name('id_extend_field');
$this->set_lang_table('extend_field_lang');
// Stores the extends fields instances
$this->elements_table = 'extend_fields';
}
function get_list($where = array())
{
$where['order_by'] = 'ordering ASC';
return parent::get_list($where);
}
function get_lang_list($where = array(), $lang)
{
$where['order_by'] = 'ordering ASC';
return parent::get_lang_list($where, $lang);
}
function get_label($id_extend_field){
if($id_extend_field != '') {
$this->db->select($this->lang_table . '.label');
$this->db->from($this->table);
$this->db->join($this->lang_table, $this->table . '.' . $this->pk_name . ' = ' . $this->lang_table . '.' . $this->pk_name, 'inner');
$this->db->where($this->lang_table . '.lang', Settings::get_lang());
$this->db->where($this->table . '.' . $this->pk_name, $id_extend_field);
$label = $this->db->get();
$label = $label->row_array();
return (!empty($label['label'])) ? $label['label'] : '';
}
return 'Need a "$id_extend_field"';
}
// ------------------------------------------------------------------------
/**
* Get the current extend fields and their values for one parent element
* Used by backend, as all the languages data are also got
*
* @param string parent name
* @param int parent ID
*
*/
function get_element_extend_fields($parent, $id_parent=null)
{
$data = array();
$extend_fields = array();
// Element extend fields
$where = array('parent'=>$parent);
$extend_fields = $this->get_list($where);
// Current element extend field
$this->{$this->db_group}->where(array('extend_field.parent'=>$parent, $this->elements_table.'.id_parent' => $id_parent));
$this->{$this->db_group}->join($this->elements_table, $this->elements_table.'.id_'.$this->table.' = ' .$this->table.'.id_'.$this->table, 'inner');
$query = $this->{$this->db_group}->get($this->table);
$result = array();
if ( $query->num_rows() > 0)
$result = $query->result_array();
$langs = Settings::get_languages();
$element_fields = $this->{$this->db_group}->list_fields($this->elements_table);
foreach($extend_fields as $k => &$extend_field)
{
// One not tranlated extend field...
if ($extend_field['translated'] != '1')
{
// fill the base data with empty values
$extend_field = array_merge(array_fill_keys($element_fields, ''), $extend_field);
foreach($result as $row)
{
if($row['id_extend_field'] == $extend_field['id_extend_field'])
{
$extend_field = array_merge($extend_field , $row);
}
}
}
else
{
foreach($langs as $language)
{
// Lang code
$lang = $language['lang'];
// Feed lang key with blank array
$extend_field[$lang] = array();
$extend_field[$lang]['content'] = '';
// Feeding of template languages elements
foreach($result as $row)
{
if($row['id_extend_field'] == $extend_field['id_extend_field'] && $row['lang'] == $lang)
{
$extend_field[$lang] = $row;
}
}
}
}
}
return $extend_fields;
}
// ------------------------------------------------------------------------
/**
* Saves extend fields data
*
* @param int Current parent element ID. Can be the page ID, the article ID...
* @param Array $_POST data array
*
*/
function save_data($parent, $id, $data)
{
// Get all extends fields with this element OR kind of parent
$extend_fields = (!empty($data['id_element_definition'])) ? $this->get_list(array('id_element_definition' => $data['id_element_definition'])) : $this->get_list(array('parent' => $parent));
foreach ($extend_fields as $extend_field)
{
// Link between extend_field and the current parent
$where = array(
$this->pk_name => $extend_field[$this->pk_name],
'id_parent' => $id
);
// Checkboxes : first clear values from DB as the var isn't in $_POST if no value is checked
if ($extend_field['type'] == '4')
{
$this->{$this->db_group}->where($where);
$this->{$this->db_group}->delete($this->elements_table);
}
// Get the value from _POST values and feed the data array
foreach ($_POST as $k => $value)
{
if (substr($k, 0, 2) == 'cf')
{
// Fill the extend field value with nothing : safe for checkboxes
$data = array();
$data['content'] = '';
$data['lang'] = '';
$data['id_parent'] = $id;
// $data['parent'] = $parent;
// id of the extend field
$key = explode('_', $k);
// if language code is set, use it in the query
if (isset($key[2]))
{
$where['lang'] = $data['lang'] = $key[2];
}
// If the extend field ID is set, we can safelly save...
if (isset($key[1]) && $key[1] == $extend_field['id_extend_field'])
{
// if value is an array...
if (is_array($value))
{
$value = implode(',', $value);
}
$data['content'] = $value;
// Update
if( $this->exists($where, $this->elements_table))
{
$this->{$this->db_group}->where($where);
$this->{$this->db_group}->update($this->elements_table, $data);
}
// Insert
else
{
// Set the extend field element field ID
$data[$this->pk_name] = $key[1];
$this->{$this->db_group}->insert($this->elements_table, $data);
}
}
}
}
}
}
// ------------------------------------------------------------------------
/**
* Delete all the extend fields elements corresponding to a extend field definition
* Can be very dangerous !
*
*/
function delete_extend_fields($id)
{
$this->{$this->db_group}->where('id_'.$this->table, $id);
return $this->{$this->db_group}->delete($this->elements_table);
}
}
/* End of file extend_field_model.php */
/* Location: ./application/models/extend_field_model.php */