StickyForms.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Elgg\Forms;
  3. /**
  4. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  5. *
  6. * @package Elgg.Core
  7. * @subpackage Forms
  8. * @since 1.10.0
  9. *
  10. * @access private
  11. */
  12. class StickyForms {
  13. /**
  14. * Load all the GET and POST variables into the sticky form cache
  15. *
  16. * Call this from an action when you want all your submitted variables
  17. * available if the submission fails validation and is sent back to the form
  18. *
  19. * @param string $form_name Name of the sticky form
  20. *
  21. * @return void
  22. */
  23. public function makeStickyForm($form_name) {
  24. elgg_clear_sticky_form($form_name);
  25. $session = _elgg_services()->session;
  26. $data = $session->get('sticky_forms', array());
  27. $req = _elgg_services()->request;
  28. // will go through XSS filtering in elgg_get_sticky_value()
  29. $vars = array_merge($req->query->all(), $req->request->all());
  30. $data[$form_name] = $vars;
  31. $session->set('sticky_forms', $data);
  32. }
  33. /**
  34. * Clear the sticky form cache
  35. *
  36. * Call this if validation is successful in the action handler or
  37. * when they sticky values have been used to repopulate the form
  38. * after a validation error.
  39. *
  40. * @param string $form_name Form namespace
  41. *
  42. * @return void
  43. */
  44. function clearStickyForm($form_name) {
  45. $session = _elgg_services()->session;
  46. $data = $session->get('sticky_forms', array());
  47. unset($data[$form_name]);
  48. $session->set('sticky_forms', $data);
  49. }
  50. /**
  51. * Has this form been made sticky?
  52. *
  53. * @param string $form_name Form namespace
  54. *
  55. * @return boolean
  56. */
  57. function isStickyForm($form_name) {
  58. $session = _elgg_services()->session;
  59. $data = $session->get('sticky_forms', array());
  60. return isset($data[$form_name]);
  61. }
  62. /**
  63. * Get a specific sticky variable
  64. *
  65. * @param string $form_name The name of the form
  66. * @param string $variable The name of the variable
  67. * @param mixed $default Default value if the variable does not exist in sticky cache
  68. * @param boolean $filter_result Filter for bad input if true
  69. *
  70. * @return mixed
  71. *
  72. * @todo should this filter the default value?
  73. */
  74. function getStickyValue($form_name, $variable = '', $default = null, $filter_result = true) {
  75. $session = _elgg_services()->session;
  76. $data = $session->get('sticky_forms', array());
  77. if (isset($data[$form_name][$variable])) {
  78. $value = $data[$form_name][$variable];
  79. if ($filter_result) {
  80. // XSS filter result
  81. $value = filter_tags($value);
  82. }
  83. return $value;
  84. }
  85. return $default;
  86. }
  87. /**
  88. * Get all the values in a sticky form in an array
  89. *
  90. * @param string $form_name The name of the form
  91. * @param bool $filter_result Filter for bad input if true
  92. *
  93. * @return array
  94. */
  95. function getStickyValues($form_name, $filter_result = true) {
  96. $session = _elgg_services()->session;
  97. $data = $session->get('sticky_forms', array());
  98. if (!isset($data[$form_name])) {
  99. return array();
  100. }
  101. $values = $data[$form_name];
  102. if ($filter_result) {
  103. foreach ($values as $key => $value) {
  104. // XSS filter result
  105. $values[$key] = filter_tags($value);
  106. }
  107. }
  108. return $values;
  109. }
  110. /**
  111. * Clear a specific sticky variable
  112. *
  113. * @param string $form_name The name of the form
  114. * @param string $variable The name of the variable to clear
  115. *
  116. * @return void
  117. */
  118. function clearStickyValue($form_name, $variable) {
  119. $session = _elgg_services()->session;
  120. $data = $session->get('sticky_forms', array());
  121. unset($data[$form_name][$variable]);
  122. $session->set('sticky_forms', $data);
  123. }
  124. }