Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_type_select_edit_fns.inc
1 <?php
18 require_once dirname(__FILE__).'/../../form_question/form_question_edit_fns.inc';
19 
32 {
33 
34 
40  {
41  $this->Form_Question_Edit_Fns();
42 
43  }//end constructor
44 
45 
56  function paintDefault(&$asset, &$o, $prefix)
57  {
58  if (!$asset->writeAccess('attributes')) return FALSE;
59  $options = $asset->getOptions();
60  if (count($options) > 0) {
61  foreach ($options as $key => $value) {
62  hidden_field($prefix.'_old_options['.$key.']', $value, 'id='.$prefix.'_old_options['.htmlspecialchars($key).']');
63  }
64  }
65  $multiple = ($asset->attr('multiple')) ? TRUE : FALSE;
66  return combo_box($prefix.'_default', $options, $multiple, $asset->attr('default'));
67 
68  }//end paintDefault()
69 
70 
81  function processDefault(&$asset, &$o, $prefix)
82  {
83  if (isset($_POST[$prefix.'_default'])) {
84 
85  $default = $_POST[$prefix.'_default'];
86  if (!is_array($default)) $default = Array($default);
87 
88  $old_options = array_get_index($_POST, $prefix.'_old_options', Array());
89 
90  if (!empty($old_options)) {
91  $new_options = $asset->getOptions();
92  $new_values = Array();
93  $new_default_vals = Array();
94  foreach ($default as $str_key) {
95  if ($str_key === '') {
96  trigger_error('Key text for the selected default option(s) cannot be empty string', E_USER_WARNING);
97  continue;
98  }
99  // neaten up any numeric keys passing through
100  if (is_numeric($str_key)) {
101  $str_key = (int) $str_key;
102  }
103  $new_default_vals[] = $old_options[$str_key];
104  }
105 
106  $new_default = Array();
107  foreach ($new_default_vals as $val) {
108  if (($idx = array_search($val, $new_options)) !== FALSE) {
109  $new_default[] = $idx;
110  }
111  }
112  if (!empty($new_default)) {
113  $default = $new_default;
114  }
115  }
116 
117  if (!$asset->setAttrValue('default', $default)) {
118  return FALSE;
119  }
120 
121  }//end if (isset($_POST[$prefix.'_default']))
122  return TRUE;
123 
124  }//end processDefault()
125 
126 
137  public function paintOptionUpload(Form_Question_Type_Select $asset, Backend_Outputter $o, $prefix)
138  {
139  $write_access = $asset->writeAccess();
140  file_upload($prefix.'_option_upload');
141  return $write_access;
142 
143  }//end paintOptionUpload()
144 
145 
157  {
158  if (isset($_FILES[$prefix.'_option_upload'])) {
159  $file = $_FILES[$prefix.'_option_upload'];
160 
161  if ($file['error'] === UPLOAD_ERR_OK) {
162  $asset->importOptionsFromCSV($file['tmp_name']);
163  }
164  }
165 
166  }//end processOptionUpload()
167 
168 
179  function paintOptions(&$asset, &$o, $prefix)
180  {
181  // Check for write access
182  if ($asset->writeAccess('attributes')) {
183  $read_only = FALSE;
184  } else {
185  $read_only = TRUE;
186  }
187 
188  $prefix = str_replace(':','_',$prefix); // handle shadow assets
189  $value = $asset->attr('options');
190 
191  if (!$read_only) {
192  ?>
193  <div id="option-list-<?php echo $prefix; ?>">
194  <?php
195  $order = 0;
196  if (count($value) > 0) {
197  foreach ($value as $k => $v) {
198  $k = html_entity_decode($k, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
199  $v = html_entity_decode($v, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
200  $printup = ($order < 1) ? FALSE : TRUE;
201  $this->_printOptionField($k, $v, $order, $prefix, 20, $printup);
202  $order++;
203  }
204  }
205  for ($i = 0; $i < 2; $i++) {
206  $k = '';
207  $v = '';
208  $printdown = ($i < 1) ? TRUE : FALSE;
209  $this->_printOptionField($k, $v, $order, $prefix, 20, TRUE, $printdown);
210  $order++;
211  }
212  ?>
213  </div>
214  <script type="text/javascript">
215  <?php
216  // Yeesh... this is hackish but this essentially (attempts to)
217  // correct the lack of delete-button class in front-end
218  // asset builder creation
219  if (!SQ_IN_BACKEND && !SQ_IN_LIMBO) {
220  ?>
221  buttons = document.getElementsByTagName('BUTTON');
222  for (i = 0; i < buttons.length; i++) {
223  if (buttons[i].className == 'delete-button') {
224  with (buttons[i].style) {
225  backgroundImage = 'url(<?php echo sq_web_path('lib') ?>/web/images/icons/delete.png)';
226  backgroundPosition = '50% 50%';
227  backgroundRepeat = 'no-repeat';
228  width = '16px';
229  height = '16px';
230  backgroundColor = 'transparent';
231  border = '0px';
232  cursor = 'pointer';
233  }
234  }
235  }
236  <?php
237  }
238  ?>
239  // Some variables
240  var optionItemPrefix = '<?php echo $prefix; ?>';
241 
242  // Functions for option list attribute
243  var doExpandList = new Function('doExpandOptionList(this)');
244  var doDeleteRow = new Function('doDeleteOptionListRow(this); return false;');
245  var doMoveDn = new Function('sListMoveDown(this); return false;');
246  var doMoveUp = new Function('sListMoveUp(this); return false;');
247 
248  function sListMoveDown(obj) {
249  var currentOrder = 0;
250 
251  var list = document.getElementById('option-list-<?php echo $prefix; ?>');
252  var inputs = list.getElementsByTagName('INPUT');
253  for (var i=0 ; i < inputs.length; i++) {
254  if (obj.id == inputs[i].id) {
255  currentOrder = i;
256  break;
257  }
258  }
259 
260  if (currentOrder == inputs.length) return;
261 
262  // If the input is disabled, don't respond
263  if (inputs[currentOrder].disabled) return;
264 
265  // Do the move
266  if (currentOrder > 2) {
267  sListMoveMe(currentOrder, currentOrder-2);
268  }
269  }
270 
271  function sListMoveUp(obj) {
272  var optionList = document.getElementById('option-list-<?php echo $prefix; ?>');
273  var currentOrder = 0;
274 
275  var inputs = optionList.getElementsByTagName('INPUT');
276  for (var i=0 ; i < inputs.length; i++) {
277  if (obj.id == inputs[i].id) {
278  currentOrder = i;
279  break;
280  }
281  }
282 
283  if (currentOrder == inputs.length) return;
284 
285  // If the input is disabled, don't respond
286  if (inputs[currentOrder].disabled) return;
287 
288  // Do the move
289  if (currentOrder > 0) {
290  sListMoveMe(currentOrder-2, currentOrder);
291  }
292  }
293 
294  function doExpandOptionList(input)
295  {
296  // abort if we are not the last input in the lit
297  var nextInput = input.nextSibling;
298  while (nextInput !== null) {
299  if (nextInput.tagName == 'INPUT') {
300  return;
301  }
302  nextInput = nextInput.nextSibling;
303  }
304 
305  // abort if we and the second-last input are both empty
306  var lastInput = input.previousSibling;
307  var last2Input = lastInput.previousSibling;
308 
309  while (lastInput !== null) {
310  if (lastInput.tagName == 'INPUT') {
311  last2Input = lastInput.previousSibling;
312  while (last2Input !== null) {
313  if (last2Input.tagName == 'INPUT') {
314  if (last2Input.value == '') {
315  return;
316  }
317  break;
318  }
319  last2Input = last2Input.previousSibling;
320  }
321  break;
322  }
323  lastInput = lastInput.previousSibling;
324  }
325 
326  var optionList = document.getElementById('option-list-<?php echo $prefix; ?>');
327  var inputs = optionList.getElementsByTagName('INPUT');
328 
329  // add move down button to the previous input
330  var moveDownButton = lastInput.previousSibling;
331  while (moveDownButton != null) {
332  moveDownButton = moveDownButton.previousSibling;
333  if (moveDownButton.tagName == 'A' && moveDownButton.name=="movedown") {
334  break;
335  }
336  }
337  var newMoveDownButton = moveDownButton.cloneNode(true);
338  newMoveDownButton.id = optionItemPrefix+'_options['+(inputs.length/2)+']';
339  newMoveDownButton.onclick = doMoveDn;
340 
341 
342  //If safari, we will remove the script for printing move up/down icon, it's causing document.write to overwrite the page in safari
343  var buttonScript = newMoveDownButton.getElementsByTagName("script")[0];
344  var browserAgent = navigator.userAgent.toLowerCase();
345  if ((browserAgent.indexOf("safari") != -1) && buttonScript != null) {
346  newMoveDownButton.removeChild(buttonScript);
347  }
348 
349 
350 
351  var brElements = lastInput.parentNode.getElementsByTagName('BR');
352  input.parentNode.removeChild(brElements[brElements.length-1]);
353  input.parentNode.appendChild(document.createTextNode(' '));
354  input.parentNode.appendChild(newMoveDownButton);
355  input.parentNode.appendChild(document.createElement('BR'));
356 
357  // add the extra field
358  var newInput = input.cloneNode(true);
359  var newKeyInput = lastInput.cloneNode(true);
360  var newId = (inputs.length/2);
361  var newKeyId = (inputs.length/2);
362  newKeyInput.onfocus = doExpandList;
363  newKeyInput.value = '';
364  newKeyInput.id = optionItemPrefix+'_options_keys['+newId+']';
365  input.parentNode.appendChild(newKeyInput);
366  input.parentNode.appendChild(document.createTextNode(' '));
367  newInput.onfocus = doExpandList;
368  newInput.value = '';
369  newInput.id = optionItemPrefix+'_options['+newId+']';
370  input.parentNode.appendChild(newInput);
371  input.parentNode.appendChild(document.createTextNode(' '));
372  var delButton = input.nextSibling;
373  while (delButton.tagName != 'BUTTON') {
374  delButton = delButton.nextSibling;
375  }
376  delButton = delButton.cloneNode(true);
377  delButton.onclick = doDeleteRow;
378  input.parentNode.appendChild(delButton);
379  input.parentNode.appendChild(document.createTextNode(' '));
380 
381  // add the move up button to the new input
382  var moveUpButton = input.nextSibling;
383  while (moveUpButton != null) {
384  if (moveUpButton.tagName == 'A' && moveUpButton.name == 'moveup') {
385  break;
386  }
387  moveUpButton = moveUpButton.nextSibling;
388  }
389  moveUpButton = moveUpButton.cloneNode(true);
390  moveUpButton.id = optionItemPrefix+'_options['+((inputs.length/2)-1)+']';
391  moveUpButton.onclick = doMoveUp;
392 
393  //If safari, we will remove the script for printing move up/down icon, it's causing document.write to overwrite the page in safari
394  var buttonScript = moveUpButton.getElementsByTagName("script")[0];
395  var browserAgent = navigator.userAgent.toLowerCase();
396  if ((browserAgent.indexOf("safari") != -1) && buttonScript != null) {
397  moveUpButton.removeChild(buttonScript);
398  }
399 
400  input.parentNode.appendChild(moveUpButton);
401  input.parentNode.appendChild(document.createTextNode(' '));
402  input.parentNode.appendChild(document.createElement('BR'));
403 
404  }
405 
406  function doDeleteOptionListRow(button)
407  {
408  var input = button.previousSibling;
409  while (input.tagName != 'INPUT') {
410  input = input.previousSibling;
411  }
412 
413  var input2 = input.previousSibling;
414  while (input2.tagName != 'INPUT') {
415  input2 = input2.previousSibling;
416  }
417 
418  if (input.value == '') return;
419 
420  // If the input is disabled, don't respond
421  if (input.disabled) return;
422 
423  // Don't let the option list get down to a single options. Clear the field
424  // instead, but leave the last two options
425 
426  var inputs = input.parentNode.getElementsByTagName('INPUT');
427  if (inputs.length <= 4) {
428  input.value = '';
429  input2.value = '';
430  return;
431  }
432 
433  var moveUpBut = button.nextSibling;
434  while (moveUpBut != null) {
435  if (moveUpBut.tagName == 'A' && moveUpBut.name == 'moveup') {
436  break;
437  }
438  moveUpBut = moveUpBut.nextSibling;
439  }
440  button.parentNode.removeChild(moveUpBut);
441 
442  var moveDownBut = button.nextSibling;
443  while (moveDownBut != null) {
444  if (moveDownBut.tagName == 'A' && moveDownBut.name == 'movedown') {
445  break;
446  }
447  moveDownBut = moveDownBut.nextSibling;
448  }
449  // If its the last option, it will not have down button
450  if (moveDownBut != null) {
451  button.parentNode.removeChild(moveDownBut);
452  }
453 
454 
455  var brTag = button.nextSibling;
456  while (brTag.tagName != 'BR') {
457  brTag = brTag.nextSibling;
458  }
459  button.parentNode.removeChild(input2);
460  button.parentNode.removeChild(input);
461  button.parentNode.removeChild(brTag);
462  button.parentNode.removeChild(button);
463  }
464 
465  // attach the event handlers
466  var optionList = document.getElementById('option-list-<?php echo $prefix; ?>');
467  var inputs = optionList.getElementsByTagName('INPUT');
468  for (var j=0; j < inputs.length; j++) {
469  inputs[j].onfocus = doExpandList;
470  }
471  var buttons = optionList.getElementsByTagName('BUTTON');
472  for (var j=0; j < buttons.length; j++) {
473  buttons[j].onclick = doDeleteRow;
474  }
475 
476  lastOrder = <?php echo $order; ?>;
477 
478  function sListMoveMe(first, second) {
479  var l = document.getElementById('option-list-<?php echo $prefix; ?>');
480  var f = l.getElementsByTagName('INPUT');
481  var tempkey = f[first-1].value;
482 
483  f[first-1].value = f[second-1].value;
484  f[second-1].value = tempkey;
485 
486  var temp = f[first].value;
487  f[first].value = f[second].value;
488  f[second].value = temp;
489  }
490 
491  </script>
492  <?php
493  } else {
494  if (count($value) > 0) {
495  echo '<ul><li>'.html_entity_decode(implode('</li><li>', $value), ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET).'</li></ul>';
496  }
497  }
498 
499 }//end paintOptions()
500 
501 
512 function processOptions(&$asset, &$o, $prefix)
513 {
514  $prefix = str_replace(':','_',$prefix); // handle shadow assets
515 
516  // Save the values
517  if (!isset($_REQUEST[$prefix.'_options'])) return;
518  $value = $_REQUEST[$prefix.'_options'];
519  foreach ($value as $i => $v) {
520  $value[$i] = htmlentities($v, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
521  if ($v == '') unset($value[$i]);
522  }
523 
524  // Save the key fields
525  if (!isset($_REQUEST[$prefix.'_options_keys'])) return;
526  $key_value = $_REQUEST[$prefix.'_options_keys'];
527  foreach ($key_value as $j => $k) {
528  $key_value[$j] = htmlentities($k, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
529  if (($k == '') && (isset($value[$j]) && !($value[$j] == ''))) {
530  $key_value[$j] = $value[$j];
531  } else if (($k == '') && (!isset($value[$j]) || ($value[$j] == ''))) {
532  unset($key_value[$j]);
533  }
534  }
535 
536  $key_value_count = count($key_value);
537  $value_count = count($value);
538  if($key_value_count != $value_count) return;
539  if ($key_value_count > 0) {
540  $values = array_combine($key_value, $value);
541  } else {
542  $values = Array();
543  }
544 
545  $processed_value = $values;
546  if ($asset->writeAccess('attributes')) {
547  $asset->setAttrValue('options', $processed_value);
548  $asset->saveAttributes();
549  }
550 
551 }//end processOptions()
552 
553 
563  function _printMoveDownButton($order, $prefix)
564  {
565  // We don't need to check order here because the check are in place for the buttons
566  $orderdn = $order + 2;
567  ?>
568  <a href="#" style="cursor:pointer;" onclick="sListMoveMe(<?php echo $order; ?>, <?php echo ($orderdn); ?>);return false;" tabindex="99999" name="movedown" id="<?php echo $prefix; ?>_options[<?php echo $order; ?>]"><script type="text/javascript">sq_print_icon("<?php echo sq_web_path('lib').'/web/images/icons/down_arrow.png' ?>", "16", "16", "Move Down");</script></a>
569  <?php
570 
571  }//end _printMoveDownButton()
572 
573 
583  function _printMoveUpButton($order, $prefix)
584  {
585  // We don't need to check order here because the check are in place for the buttons
586  $orderup = $order - 2;
587  ?>
588  <a href="#" style="cursor:pointer;" onclick="sListMoveMe(<?php echo $order; ?>, <?php echo ($orderup); ?>);return false;" tabindex="99999" name="moveup" id="<?php echo $prefix; ?>_options[<?php echo $order; ?>]"><script type="text/javascript">sq_print_icon("<?php echo sq_web_path('lib').'/web/images/icons/up_arrow.png' ?>", "16", "16", "Move Up");</script></a>
589  <?php
590 
591  }//end _printMoveUpButton()
592 
593 
608  function _printOptionField($key, $value, $order, $prefix, $size='10', $showup=TRUE, $showdown=TRUE)
609  {
610  // The order is key1=0, value1=1, key2=2, value2=3 etc. This is just the way they are ordered
611  $current_button = ($order * 2) + 1;
612 
613  // Cut the key field down to half the value field
614  $key_size = $size / 2;
615 
616  // Show the field
617  ?>
618  <input type="text" name="<?php echo $prefix; ?>_options_keys[]" value="<?php echo $key; ?>" id="<?php echo $prefix; ?>_options_keys[<?php echo $order; ?>]" size="<?=$key_size; ?>" />
619  <input type="text" name="<?php echo $prefix; ?>_options[]" value="<?php echo $value; ?>" id="<?php echo $prefix; ?>_options[<?php echo $order; ?>]" size="<?=$size; ?>" />
620  <button type="button" tabindex="99999" class="delete-button">&nbsp;</button>
621  <?php
622  if ($showup) {
623  $this->_printMoveUpButton($current_button, $prefix);
624  }
625  if ($showdown) {
626  $this->_printMoveDownButton($current_button, $prefix);
627  }
628  ?><br />
629  <?php
630 
631  }//end _printOptionField()
632 
633 
634 }//end class
635 ?>