BxBaseSearchExtendedForm.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Extended Search Form.
  11. *
  12. * @see BxDolSearchExtended
  13. */
  14. class BxBaseSearchExtendedForm extends BxTemplFormView
  15. {
  16. protected $_iAgeMin;
  17. protected $_iAgeMax;
  18. public function __construct($aInfo, $oTemplate = false)
  19. {
  20. parent::__construct($aInfo, $oTemplate);
  21. $this->_iAgeMin = 1;
  22. $this->_iAgeMax = 75;
  23. }
  24. function getCleanValue($sName)
  25. {
  26. $bType = isset($this->aInputs[$sName]['type']);
  27. //--- Process field with 'Location' type.
  28. if($bType && $this->aInputs[$sName]['type'] == 'location')
  29. return array(
  30. 'string' => parent::getCleanValue($sName),
  31. 'array' => BxDolMetatags::locationsRetrieveFromForm($sName, $this)
  32. );
  33. if($bType && $this->aInputs[$sName]['type'] == 'location_radius'){
  34. $aTmp = BxDolMetatags::locationsRetrieveFromForm($sName, $this);
  35. $sValue = bx_get($sName . '_rad');
  36. $iValue = is_numeric($sValue) ? (int)$sValue : 0;
  37. array_push($aTmp, $iValue);
  38. return array(
  39. 'string' => parent::getCleanValue($sName),
  40. 'array' => $aTmp
  41. );
  42. }
  43. //--- Process field with 'Date Range Age' and 'Date-Time Range Age' type.
  44. if($bType && in_array($this->aInputs[$sName]['type'], array('datepicker_range_age', 'datetime_range_age'))) {
  45. $bTypeDateTime = $this->aInputs[$sName]['type'] == 'datetime_range_age';
  46. $sMethod = $this->aFormAttrs['method'];
  47. $sValue = self::getSubmittedValue($sName, $sMethod);
  48. if(!empty($sValue) && is_string($sValue)) {
  49. $aMatches = array();
  50. if(!preg_match("/^([0-9]+)-([0-9]+)$/i", $sValue, $aMatches))
  51. return array();
  52. $aArgs = array("%s-%s-%s", date('Y'), date('m'), date('d'));
  53. if($bTypeDateTime) {
  54. $aArgs[0] = "%s-%s-%s %s:%s:%s";
  55. $aArgs = array_merge($aArgs, array(date('H'), date('i'), date('s')));
  56. }
  57. $aArgsFrom = $aArgs;
  58. $aArgsFrom[1] -= ($aMatches[2] + 1);
  59. if($bTypeDateTime)
  60. $aArgsFrom[6] += 1;
  61. else
  62. $aArgsFrom[3] += 1;
  63. $aArgsTo = $aArgs;
  64. $aArgsTo[1] -= $aMatches[1];
  65. self::setSubmittedValue($sName, array(
  66. call_user_func_array('sprintf', $aArgsFrom),
  67. call_user_func_array('sprintf', $aArgsTo)
  68. ), $sMethod);
  69. }
  70. }
  71. return parent::getCleanValue($sName);
  72. }
  73. public function genInput(&$aInput)
  74. {
  75. switch($aInput['type'])
  76. {
  77. case 'datepicker_range':
  78. case 'datetime_range':
  79. $bValue = !empty($aInput['value']) && is_array($aInput['value']);
  80. $aInput['name'] .= '[]';
  81. $aInput['type'] = str_replace('_range', '', $aInput['type']);
  82. $aSubInputs = array($aInput, $aInput);
  83. foreach($aSubInputs as $iKey => $aSubInput) {
  84. if($bValue && isset($aInput['value'][$iKey]))
  85. $aSubInput['value'] = $aInput['value'][$iKey];
  86. $aSubInputs[$iKey] = $this->genInputStandard($aSubInput);
  87. }
  88. return implode(' - ', $aSubInputs);
  89. case 'datepicker_range_age':
  90. case 'datetime_range_age':
  91. $iMin = $this->_iAgeMin;
  92. $iMax = $this->_iAgeMax;
  93. $aInput['type'] = 'doublerange';
  94. if(empty($aInput['value']))
  95. $aInput['value'] = $iMin . '-' . $iMax;
  96. if (!isset($aInput['attrs']['min']) && !isset($aInput['attrs']['max'])){
  97. $aAttrs = array('min' => $iMin, 'max' => $iMax, 'step' => 1);
  98. if(!empty($aInput['attrs']) && is_array($aInput['attrs']))
  99. $aInput['attrs'] = array_merge($aInput['attrs'], $aAttrs);
  100. else
  101. $aInput['attrs'] = $aAttrs;
  102. }
  103. return $this->genInputStandard($aInput);
  104. case 'location_radius':
  105. return $this->genInputLocationRadius($aInput);
  106. }
  107. return parent::genInput($aInput);
  108. }
  109. public function genInputLocation(&$aInput)
  110. {
  111. $aInput['manual_input'] = true;
  112. return parent::genInputLocation($aInput);
  113. }
  114. protected function genCustomInputAuthor($aInput)
  115. {
  116. $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . 'searchExtended.php?action=get_authors';
  117. return $this->genCustomInputUsernamesSuggestions($aInput);
  118. }
  119. public function genInputLocationRadius(&$aInput)
  120. {
  121. $aInput['manual_input'] = true;
  122. $aInputRadius = $aInput;
  123. $aInputRadius['type'] = 'text';
  124. $aInputRadius['name'] = $aInputRadius['name'] . '_rad';
  125. $sValue = bx_get($aInputRadius['name']);
  126. $aInputRadius['value'] = is_numeric($sValue) ? $sValue : '';
  127. $aInputRadius['attrs']['placeholder'] = _t('_sys_form_input_location_radius_label');
  128. $aVars = array (
  129. 'location_input' => parent::genInputLocation($aInput),
  130. 'radius_input' => parent::genInputStandard($aInputRadius),
  131. );
  132. $this->_addCss('search_extended.css');
  133. return $this->oTemplate->parseHtmlByName('form_field_location_radius.html', $aVars);
  134. }
  135. }
  136. /** @} */