You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

115 lines
3.9 KiB

  1. <?php
  2. /**
  3. * Class helper_plugin_bureaucracy_fieldfieldset
  4. *
  5. * Creates a new set of fields, which optional can be shown/hidden depending on the value of another field above it.
  6. */
  7. class helper_plugin_bureaucracy_fieldfieldset extends helper_plugin_bureaucracy_field {
  8. protected $mandatory_args = 1;
  9. /** @var array with zero, one entry (fieldname) or two entries (fieldname and match value) */
  10. public $depends_on = array();
  11. /**
  12. * Arguments:
  13. * - cmd
  14. * - label (optional)
  15. * - field name where switching depends on (optional)
  16. * - match value (optional)
  17. *
  18. * @param array $args The tokenized definition, only split at spaces
  19. */
  20. public function initialize($args) {
  21. // get standard arguments
  22. $this->opt = array('cmd' => array_shift($args));
  23. if (count($args) > 0) {
  24. $this->opt['label'] = array_shift($args);
  25. $this->opt['display'] = $this->opt['label'];
  26. $this->depends_on = $args;
  27. }
  28. }
  29. /**
  30. * Render the top of the fieldset as XHTML
  31. *
  32. * @param array $params Additional HTML specific parameters
  33. * @param Doku_Form $form The target Doku_Form object
  34. * @param int $formid unique identifier of the form which contains this field
  35. */
  36. function renderfield($params, Doku_Form $form, $formid) {
  37. $form->startFieldset(hsc($this->getParam('display')));
  38. if (!empty($this->depends_on)) {
  39. $dependencies = array_map('hsc',(array) $this->depends_on);
  40. if (count($this->depends_on) > 1) {
  41. $msg = 'Only edit this fieldset if ' .
  42. '“<span class="bureaucracy_depends_fname">%s</span>” '.
  43. 'is set to “<span class="bureaucracy_depends_fvalue">%s</span>”.';
  44. } else {
  45. $msg = 'Only edit this fieldset if ' .
  46. '“<span class="bureaucracy_depends_fname">%s</span>” is set.';
  47. }
  48. $form->addElement('<p class="bureaucracy_depends">' . vsprintf($msg, $dependencies) . '</p>');
  49. }
  50. }
  51. /**
  52. * Handle a post to the fieldset
  53. *
  54. * When fieldset is closed, set containing fields to hidden
  55. *
  56. * @param null $value field value of fieldset always empty
  57. * @param helper_plugin_bureaucracy_field[] $fields (reference) form fields (POST handled upto $this field)
  58. * @param int $index index number of field in form
  59. * @param int $formid unique identifier of the form which contains this field
  60. * @return bool Whether the passed value is valid
  61. */
  62. public function handle_post($value, &$fields, $index, $formid) {
  63. if(empty($this->depends_on)) {
  64. return true;
  65. }
  66. // search the field where fieldset depends on in fields before fieldset
  67. $hidden = false;
  68. for ($n = 0 ; $n < $index; ++$n) {
  69. $field = $fields[$n];
  70. if ($field->getParam('label') != $this->depends_on[0]) {
  71. continue;
  72. }
  73. if(count($this->depends_on) > 1) {
  74. $hidden = $field->getParam('value') != $this->depends_on[1];
  75. } else {
  76. $hidden = !$field->isSet_();
  77. }
  78. break;
  79. }
  80. // mark fields after this fieldset as hidden
  81. if ($hidden) {
  82. $this->hidden = true;
  83. for ($n = $index + 1 ; $n < count($fields) ; ++$n) {
  84. $field = $fields[$n];
  85. if ($field->getFieldType() === 'fieldset') {
  86. break;
  87. }
  88. $field->hidden = true;
  89. }
  90. }
  91. return true;
  92. }
  93. /**
  94. * Get an arbitrary parameter
  95. *
  96. * @param string $name
  97. * @return mixed|null
  98. */
  99. function getParam($name) {
  100. if($name === 'value') {
  101. return null;
  102. } else {
  103. return parent::getParam($name);
  104. }
  105. }
  106. }