forked from Sigsiu-NET/SobiPro
/
chbxgroup.php
271 lines (259 loc) · 8.28 KB
/
chbxgroup.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
<?php
/**
* @package: SobiPro Component for Joomla!
* @author
* Name: Sigrid Suski & Radek Suski, Sigsiu.NET GmbH
* Email: sobi[at]sigsiu.net
* Url: http://www.Sigsiu.NET
* @copyright Copyright (C) 2006 - 2015 Sigsiu.NET GmbH (https://www.sigsiu.net). All rights reserved.
* @license GNU/GPL Version 3
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as published by the Free Software Foundation, and under the additional terms according section 7 of GPL v3.
* See http://www.gnu.org/licenses/gpl.html and https://www.sigsiu.net/licenses.
* 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 General Public License for more details.
*/
defined( 'SOBIPRO' ) || exit( 'Restricted access' );
SPLoader::loadClass( 'opt.fields.radio' );
/**
* @author Radek Suski
* @version 1.0
* @created 27-Nov-2009 17:10:15
*/
class SPField_ChbxGr extends SPField_Radio implements SPFieldInterface
{
/** * @var string */
protected $cssClass = 'spClassCheckbox';
/** * @var string */
protected $cssClassView = 'spClassViewCheckbox';
/** * @var string */
protected $cssClassEdit = 'spClassEditCheckbox';
/** * @var string */
protected $cssClassSearch = 'spClassSearchCheckbox';
/**
* @var bool
*/
protected $multi = true;
/**
* @var string
*/
protected $dType = 'predefined_multi_data_multi_choice';
/** * @var string */
protected $metaSeparator = ' ';
/**
* Shows the field in the edit entry or add entry form
* @param bool $return return or display directly
* @return string
*/
public function field( $return = false )
{
if ( !( $this->enabled ) ) {
return false;
}
$class = $this->required ? $this->cssClass . ' required' : $this->cssClass;
$params = array( 'class' => $class . ' checkbox' );
$values = array();
if ( count( $this->options ) ) {
foreach ( $this->options as $option ) {
$values[ $option[ 'id' ] ] = $option[ 'label' ];
}
}
$selected = $this->getRaw();
if ( is_array( $selected ) && !( is_string( $selected ) ) && count( $selected ) ) {
$selected = array_merge( $selected, array_keys( $selected ) );
}
if (($selected == null) && ($this->defaultValue)) {
$selected = explode(',', $this->defaultValue);
$selected = array_map('trim', $selected);
}
$list = SPHtml_Input::checkBoxGroup( $this->nid, $values, $this->nid, $selected, $params, $this->labelSite, true );
$field = null;
if ( count( $list ) ) {
$c = 0;
foreach ( $list as $box ) {
$field .= '<div class="spFieldCheckbox"';
if ( $this->optWidth ) {
$field .= ' style="width:' . $this->optWidth . 'px;"';
}
$field = $field . '>' . $box . '</div>';
$field .= "\n";
if ( $this->optInLine ) {
if ( !( ( ++$c ) % $this->optInLine ) ) {
$field .= "\n<div class=\"clearfix\"></div>\n";
}
}
}
$field = "<div id=\"{$this->nid}\" class=\"{$class}\">{$field}\n<div class=\"clearfix\"></div>\n</div>";
}
if ( !$return ) {
echo $field;
}
else {
return $field;
}
}
/**
* Returns meta description
*/
public function metaDesc()
{
return $this->metaKeys();
}
/**
* Returns meta keys
*/
public function metaKeys()
{
$data = $this->getRaw();
return ( $this->addToMetaKeys && is_array( $data ) && count( $data ) ) ? implode( ', ', $this->getRaw() ) : null;
}
/**
* Get field specific values if these are in an other table
* @param $sid - id of the entry
* @param $fullData - the database row form the spdb_field_data table
* @param $rawData - raw data of the field content
* @param $fData - full formatted data of the field content
* @return void
*/
public function loadData( $sid, &$fullData, &$rawData, &$fData )
{
/* @var SPdb $db */
$db =& SPFactory::db();
static $lang = null;
if ( !( $lang ) ) {
$lang = Sobi::Lang( false );
}
$table = $db->join(
array(
array( 'table' => 'spdb_field_option_selected', 'as' => 'sdata', 'key' => 'fid' ),
array( 'table' => 'spdb_field_data', 'as' => 'fdata', 'key' => 'fid' ),
array( 'table' => 'spdb_language', 'as' => 'ldata', 'key' => array( 'sdata.optValue', 'ldata.sKey' ) ),
)
);
try {
$db->select(
'*, sdata.copy as scopy',
$table,
array(
'sdata.fid' => $this->id,
'sdata.sid' => $sid,
'fdata.sid' => $sid,
'ldata.oType' => 'field_option',
'ldata.fid' => $this->id,
),
'scopy', 0, 0, true /*, 'sdata.optValue' */
);
$data = $db->loadObjectList();
if ( $data && count( $data ) ) {
$order = SPFactory::cache()->getVar( 'order_' . $this->nid );
if ( !( $order ) ) {
$db->select( 'optValue', 'spdb_field_option', array( 'fid' => $this->id ), 'optPos' );
$order = $db->loadResultArray();
SPFactory::cache()->addVar( $order, 'order_' . $this->nid );
}
$rawData = array();
$sRawData = array();
$copied = false;
// check which version the user may see
$copy = $this->checkCopy();
foreach ( $data as $selected ) {
// if there was at least once copy
if ( $selected->scopy ) {
$copied = true;
}
}
// check what we should show
$remove = ( int )$copied && $copy;
foreach ( $data as $selected ) {
if ( $selected->scopy == $remove ) {
if ( !( isset( $rawData[ $selected->optValue ] ) ) || $selected->language == $lang ) {
$rawData[ $selected->optValue ] = $selected->sValue;
}
}
}
foreach ( $order as $opt ) {
if ( isset( $rawData[ $opt ] ) ) {
$sRawData[ ] = $rawData[ $opt ];
}
}
$fData = implode( "</li>\n\t<li>", $sRawData );
$fData = "<ul id=\"{$this->nid}\" class=\"{$this->cssClass}\">\n\t<li>{$fData}</li>\n</ul>\n";
$fullData->baseData = $fData;
}
} catch ( SPException $x ) {
Sobi::Error( __CLASS__, SPLang::e( 'CANNOT_GET_SELECTED_OPTION', $x->getMessage() ), SPC::WARNING, 0, __LINE__, __FILE__ );
}
}
/* (non-PHPdoc)
* @see Site/opt/fields/SPField_Select#fetchData($request)
*/
protected function fetchData( $data )
{
if ( is_array( $data ) && count( $data ) ) {
$selected = array();
foreach ( $data as $opt ) {
/* check if such option exist at all */
if ( !( isset( $this->optionsById[ $opt ] ) ) ) {
throw new SPException( SPLang::e( 'FIELD_NO_SUCH_OPT', $opt, $this->name ) );
}
$selected[ ] = preg_replace( '/^[a-z0-9]\.\-\_/ei', null, $opt );
}
return $selected;
}
else {
return array();
}
}
/**
* @return array
*/
public function struct()
{
$baseData = $this->getRaw();
$list = array();
$struct = array();
$order = SPFactory::cache()->getVar( 'order_' . $this->nid );
if ( !( $order ) ) {
$order = SPFactory::db()->select( 'optValue', 'spdb_field_option', array( 'fid' => $this->id ), 'optPos' )->loadResultArray();
SPFactory::cache()->addVar( $order, 'order_' . $this->nid );
}
if ( is_array( $baseData ) && count( $baseData ) ) {
$this->cssClass = ( strlen( $this->cssClass ) ? $this->cssClass : 'spFieldsData' );
$this->cssClass = $this->cssClass . ' ' . $this->nid;
$this->cleanCss();
foreach ( $order as $opt ) {
if ( isset( $baseData[ $opt ] ) ) {
$list[ ] = array( '_tag' => 'li', '_value' => SPLang::clean( $baseData[ $opt ] ), '_class' => $opt, /*'_id' => trim( $this->nid.'_'.strtolower( $opt ) )*/ );
}
}
foreach ( $this->options as $opt ) {
$struct[ ] = array(
'_complex' => 1,
'_data' => $opt[ 'label' ],
'_attributes' => array( 'selected' => ( isset( $baseData[ $opt[ 'id' ] ] ) ? 'true' : 'false' ), 'id' => $opt[ 'id' ], 'position' => $opt[ 'position' ] )
);
}
$data = array(
'ul' => array(
'_complex' => 1,
'_data' => $list,
'_attributes' => array( /* 'id' => $this->nid, */
'class' => $this->cssClass ) )
);
}
if ( count( $list ) ) {
return array(
'_complex' => 1,
'_data' => $data,
'_attributes' => array( 'lang' => $this->lang, 'class' => $this->cssClass ),
'_options' => $struct,
);
}
}
/**
* Returns the parameter list
* @return array
*/
protected function getAttr()
{
return array( 'optInLine', 'labelSite', 'optWidth', 'searchMethod', 'itemprop', 'metaSeparator', 'cssClassView', 'cssClassSearch', 'cssClassEdit', 'showEditLabel', 'defaultValue' );
}
}