Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
selection.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset_attribute.inc';
19 
47 {
48 
49 
57  function Asset_Attribute_Selection($attributeid=0, $value=NULL)
58  {
59  $this->Asset_Attribute($attributeid, $value);
60 
61  // set default parameters
62  if (!isset($this->_params['options'])) {
63  $this->_params['options'] = Array();
64  }
65  if (!isset($this->_params['multiple'])) {
66  $this->_params['multiple'] = FALSE;
67  }
68  if (!isset($this->_params['allow_empty'])) {
69  $this->_params['allow_empty'] = TRUE;
70  }
71  if (!isset($this->_params['allow_other'])) {
72  $this->_params['allow_other'] = FALSE;
73  }
74 
75  // set default edit parameters
76  $this->_edit_params['style'] = 'list';
77  $this->_edit_params['type'] = 'table';
78  $this->_edit_params['height'] = '';
79  $this->_edit_params['columns'] = '1';
80  $this->_edit_params['empty_text'] = '';
81  $this->_edit_params['extras'] = '';
82 
83  }//end constructor
84 
85 
95  function setEditParams(&$node)
96  {// TODO TOF TEST
97  if (!parent::setEditParams($node)) return FALSE;
98 
99  foreach (Array('style', 'height', 'columns', 'empty_text', 'extras') as $param) {
100  if (isset($node->attributes()->{$param})) {
101  $this->_edit_params[$param] = (string)$node->attributes()->{$param};
102  }
103  }
104 
105  return TRUE;
106 
107  }//end setEditParams()
108 
109 
119  function paintEditParams($prefix, $write_access=FALSE)
120  {
121  $searchStyles = Array();
122  $searchStyles['list'] = 'List';
123  $searchStyles['checkbox'] = 'Checkbox';
124  $searchTypes['table'] = 'Table';
125  $searchTypes['list'] = 'Unordered List';
126 
127  if (!empty($this->name)) {
128  echo '<b>Selection Attribute " '.ucwords(str_replace('_', ' ', $this->name)).' " ('.$this->name.')</b><br />';
129  }
130  ?>
131  <table class="sq-backend-table">
132  <tr>
133  <td class="sq-backend-section-heading" style="width: 100px;"><?php echo translate('style'); ?></td>
134  <td class="sq-backend-table-cell">
135  <?php
136  if ($write_access) {
137  combo_box($prefix.'_style', $searchStyles, FALSE, $this->_edit_params['style']);
138  } else {
139  echo $this->_edit_params['style'];
140  }
141  ?>
142  </td>
143  </tr>
144  <tr>
145  <td class="sq-backend-section-heading" style="width: 100px;">Type</td>
146  <td class="sq-backend-table-cell">
147  <?php
148  if ($write_access) {
149  combo_box($prefix.'_type', $searchTypes, FALSE, $this->_edit_params['type']);
150  } else {
151  echo $this->_edit_params['type'];
152  }
153  ?>
154  </td>
155  </tr>
156  <tr>
157  <td class="sq-backend-section-heading" style="width: 100px;"><?php echo translate('height'); ?></td>
158  <td class="sq-backend-table-cell">
159  <?php
160  if ($write_access) {
161  text_box($prefix.'_height', $this->_edit_params['height']);
162  } else {
163  echo $this->_edit_params['height'];
164  }
165  ?>
166  </td>
167  </tr>
168  <tr>
169  <td class="sq-backend-section-heading" style="width: 100px;"><?php echo translate('columns'); ?></td>
170  <td class="sq-backend-table-cell">
171  <?php
172  if ($write_access) {
173  text_box($prefix.'_columns', $this->_edit_params['columns']);
174  } else {
175  echo $this->_edit_params['columns'];
176  }
177  ?>
178  </td>
179  </tr>
180  <?php
181  if ($this->_params['allow_empty']) {
182  ?>
183  <tr>
184  <td class="sq-backend-section-heading" style="width: 100px;"><?php echo translate('empty_text'); ?></td>
185  <td class="sq-backend-table-cell">
186  <?php
187  if ($write_access) {
188  text_box($prefix.'_empty', $this->_edit_params['empty_text']);
189  } else {
190  echo $this->_edit_params['empty_text'];
191  }
192  ?>
193  </td>
194  </tr>
195  <?php
196  }
197  ?>
198  </table>
199  <?php
200 
201  }//end paintEditParams()
202 
203 
212  function processEditParams($prefix)
213  {
214  if (!empty($_POST[$prefix.'_style'])) {
215  $this->_edit_params['style'] = $_POST[$prefix.'_style'];
216  }
217 
218  if (!empty($_POST[$prefix.'_type'])) {
219  $this->_edit_params['type'] = $_POST[$prefix.'_type'];
220  }
221 
222  if (!empty($_POST[$prefix.'_height'])) {
223  $this->_edit_params['height'] = $_POST[$prefix.'_height'];
224  }
225 
226  if (!empty($_POST[$prefix.'_columns'])) {
227  $this->_edit_params['columns'] = $_POST[$prefix.'_columns'];
228  }
229 
230  if (!empty($_POST[$prefix.'_empty'])) {
231  $this->_edit_params['empty_text'] = $_POST[$prefix.'_empty'];
232  }
233 
234  $values = Array();
235  $values['style'] = $this->_edit_params['style'];
236  $values['type'] = $this->_edit_params['type'];
237  $values['height'] = $this->_edit_params['height'];
238  $values['columns'] = $this->_edit_params['columns'];
239  $values['empty_text'] = $this->_edit_params['empty_text'];
240  $values['extras'] = $this->_edit_params['extras'];
241 
242  return $values;
243 
244 }//end processEditParams()
245 
246 
255 function _ar2str($array)
256 {
257  return implode('|', array_intersect(array_keys($this->_params['options']), $array));
258 
259 }//end _ar2str()
260 
261 
270 function _str2ar($string)
271 {
272  return array_intersect(array_keys($this->_params['options']), explode('|', $string));
273 
274 }//end _str2ar()
275 
276 
286 function paint($prefix, $read_only=FALSE)
287 {
288  $options = $this->_params['options'];
289  if ($this->_params['allow_empty'] && !$this->_params['multiple']) {
290  if (empty($this->_edit_params['empty_text'])) {
291  $this->_edit_params['empty_text'] = '-- Leave Empty --';
292  }
293  $options = Array('' => $this->_edit_params['empty_text']) + $options;
294  }
295  if (!empty($this->_params['multiple']) && !is_array($this->value)) {
296  $value = $this->_str2ar($this->value);
297  } else {
298  $value = $this->value;
299  }
300 
301  if ($read_only) {
302  if (empty($this->_params['multiple'])) {
303  if (!isset($options[$value])) {
304  // may be an "other" choice
305  if ($this->_params['allow_other']) {
306  echo translate('other').': '.$value;
307  }
308  // may have a value left over from when we
309  // were a mutiple selection field
310  $value = $this->_str2ar($value);
311  if (empty($value) || !isset($options[$value[0]])) {
312  return;
313  }
314  echo $options[$value[0]];
315  } else {
316  echo $options[$value];
317  }
318 
319  } else {
320  echo '<ul>';
321  foreach ($value as $k) {
322  if (!isset($options[$k])) continue; // "other" not allowed with multiple
323  echo '<li class="sq-backend-data">'.htmlspecialchars($options[$k]).'</li>';
324  }
325  echo '</ul>';
326 
327  }
328  return;
329  }
330 
331  require_once SQ_LIB_PATH.'/html_form/html_form.inc';
332 
333  // just have this so that we can check whether this field was posted or not
334  // because checkboxes don't have an entry in the $_POST array when they are not selected
335  hidden_field($prefix.'_posted', '1');
336 
337  if ($this->_edit_params['style'] == 'list') {
338  if (empty($this->_params['multiple']) && $this->_params['allow_other']) {
339  $options['__other__'] = translate('other');
340  $onchange_code = 'document.getElementById(\''.$prefix.'_other\').style.visibility = (this.options[this.selectedIndex].value == \'__other__\') ? \'visible\' : \'hidden\'';
341  $onchange_pos = strpos(strtolower($this->_edit_params['extras']), 'onchange=');
342  if (FALSE === $onchange_pos) {
343  $this->_edit_params['extras'] .= ' onchange="'.$onchange_code.'"';
344  } else {
345  $this->_edit_params['extras'] = substr($this->edit_params['extras'], 0, $onchange_pos+10)
346  .$onchange_code.'; '
347  .substr($this->edit_params['extras'], $onchange_pos+11);
348  }
349  }
350  combo_box($prefix, $options, !empty($this->_params['multiple']), $this->hasUnlistedValue() ? '__other__' : $value, $this->_edit_params['height'], $this->_edit_params['extras']);
351  if (array_get_index($this->_params, 'allow_other')) {
352  echo '&nbsp;';
353  text_box($prefix.'_other', $this->hasUnlistedValue() ? $this->value : '', '', '', FALSE, $this->hasUnlistedValue() ? '' : 'style="visibility: hidden"');
354  }
355  } else {
356  if (empty($this->_params['multiple']) && $this->_params['allow_other']) {
357  $options['__other__'] = translate('other');
358  $onclick_pos = strpos(strtolower($this->_edit_params['extras']), 'onchange=');
359  if (FALSE === $onclick_pos) {
360  $this->_edit_params['extras'] .= ' onclick="%on_click%"';
361  } else {
362  $this->_edit_params['extras'] = substr($this->edit_params['extras'], 0, $onclick_pos+10)
363  .'%on_click%; '
364  .substr($this->edit_params['extras'], $onclick_pos+11);
365  }
366  }
367 
368  if ($this->_edit_params['type'] == 'table') {
369  // begin painting table
370  ?>
371  <table cellpadding="0" cellspacing="0" border="0">
372  <tr>
373  <?php
374  $i = 0;
375  while (list($key, $val) = each($options)) {
376  if (empty($this->_params['multiple'])) {
377  ?>
378  <td>&nbsp;
379  <?php
380  $selected = (($key == '__other__') && $this->_params['allow_other']) && $this->hasUnlistedValue() || ($key == $value);
381  $onclick = 'document.getElementById(\''.$prefix.'_other\').style.visibility = \''.(($key == '__other__') ? 'visible' : 'hidden').'\'';
382  $extras = str_replace('%on_click%', $onclick, $this->_edit_params['extras']);
383  $id = $prefix.'_'.str_replace(' ', '_', $key);
384  $extras .= ' id="'.$id.'"';
385  radio_button($prefix, $key, $selected, '', $extras);
386  label($val, $id);
387  if (($key == '__other__') && $this->_params['allow_other']) {
388  echo '&nbsp;';
389  text_box($prefix.'_other', $this->hasUnlistedValue() ? $this->value : '', '', '', FALSE, $selected ? '' : 'style="visibility: hidden"');
390  }
391  ?>
392  </td>
393  <?php
394  } else {
395  $id = $prefix.'_'.str_replace(' ', '_', $key);
396  $extras = $this->_edit_params['extras'].' id="'.$id.'"';
397  ?>
398  <td>&nbsp;
399  <?php
400  check_box($prefix.'['.$i.']', $key, (is_array($value) && in_array($key, $value)), '', $extras);
401  label($val, $id);
402  ?>
403  </td>
404  <?php
405 
406  }
407  if ((($i++) % $this->_edit_params['columns']) == ($this->_edit_params['columns'] - 1)) {
408  echo '</tr><tr>';
409  }
410  }//end while
411  ?>
412  <td></td></tr>
413  </table>
414  <?php
415  // end painting table
416  } else {
417  ?>
418  <ul>
419  <?php
420  $i = 0;
421  while (list($key, $val) = each($options)) {
422  ?><li><?php
423  if (empty($this->_params['multiple'])) {
424  $selected = (($key == '__other__') && $this->_params['allow_other']) && $this->hasUnlistedValue() || ($key == $value);
425  $onclick = 'document.getElementById(\''.$prefix.'_other\').style.visibility = \''.(($key == '__other__') ? 'visible' : 'hidden').'\'';
426  $extras = str_replace('%on_click%', $onclick, $this->_edit_params['extras']);
427  $id = $prefix.'_'.str_replace(' ', '_', $key);
428  $extras .= ' id="'.$id.'"';
429  radio_button($prefix, $key, $selected, '', $extras);
430  label($val, $id);
431  if (($key == '__other__') && $this->_params['allow_other']) {
432  echo '&nbsp;';
433  text_box($prefix.'_other', $this->hasUnlistedValue() ? $this->value : '', '', '', FALSE, $selected ? '' : 'style="visibility: hidden"');
434  }
435  } else {
436  $id = $prefix.'_'.str_replace(' ', '_', $key);
437  $extras = $this->_edit_params['extras'].' id="'.$id.'"';
438  check_box($prefix.'['.$i.']', $key, (is_array($value) && in_array($key, $value)), '', $extras);
439  label($val, $id);
440  }
441  ?></li><?php
442  $i++;
443  }
444  ?>
445  </ul>
446  <?php
447  }//end else
448 
449  }//end else style is not list
450  }//end paint()
451 
452 
461  function process($prefix)
462  {
463  require_once SQ_FUDGE_PATH.'/general/general.inc';
464  if (!isset($_REQUEST[$prefix.'_posted'])) return FALSE;
465  $value = (isset($_REQUEST[$prefix])) ? $_REQUEST[$prefix] : '';
466  if ($this->_params['allow_other'] && ($value == '__other__')) {
467  $value = $_REQUEST[$prefix.'_other'];
468  }
469 
470  if (is_array($value)) $value = $this->_ar2str($value);
471 
472  if ($this->setValue($value)) {
473  $this->processed = TRUE;
474  } else {
475  $this->processed = FALSE;
476  }
477 
478  }//end process()
479 
480 
489  function validateValue(&$value)
490  {
491  $options = $this->_params['options'];
492  if (is_array($value)) $value = $this->_ar2str($value);
493  $value = trim((string) $value);
494  if ($value == '') {
495  return $this->_params['allow_empty'];
496  }
497 
498  // special case for multiple values
499  if (empty($this->_params['multiple'])) {
500  if (!isset($options[$value])) {
501  return $this->_params['allow_other'];
502  }
503  } else {
504  $new_value = $this->_str2ar($value);
505  if (!is_array($new_value)) return FALSE;
506  $return_value = Array();
507  foreach ($new_value as $key) {
508  if (isset($options[$key])) $return_value[] = $key;
509  }
510  $value = $this->_ar2str($return_value);
511  }
512 
513  return TRUE;
514 
515  }//end validateValue()
516 
517 
524  function getOptions()
525  {
526  return $this->_params['options'];
527 
528  }//end getOptions()
529 
530 
538  function getValueArray()
539  {
540  $value = $this->_str2ar($this->value);
541  $value_arr = Array();
542  foreach ($value as $k) {
543  $value_arr[$k] = $this->_params['options'][$k];
544  }
545  return $value_arr;
546 
547  }//end getValueArray()
548 
549 
556  function hasUnlistedValue()
557  {
558  return $this->_params['allow_other'] && !isset($this->_params['options'][$this->value]);
559 
560  }//end hasUnlistedValue()
561 
562 
569  function exportContentsToXML()
570  {
571 
572  $value_xml = '';
573  foreach ($this->getValueArray() as $value => $display_name) {
574  $value_xml .= '<selection displayname="'.$display_name.'"><![CDATA['.$value.']]></selection>';
575  }
576  $xml = '<value>'.$value_xml.'</value>';
577 
578  return $xml;
579 
580  }//end exportContentsToXML()
581 
582 
583 
584 
585 
586 }//end class
587 
588 ?>