1
0

BxBaseSearchExtendedForm.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. $aLocation = BxDolMetatags::locationsRetrieveFromForm($sName, $this);
  30. if ($aLocation) {
  31. return [
  32. 'string' => parent::getCleanValue($sName),
  33. 'array' => $aLocation,
  34. ];
  35. }
  36. }
  37. //--- Process field with 'Location Radius' type.
  38. if($bType && $this->aInputs[$sName]['type'] == 'location_radius') {
  39. $aLocation = BxDolMetatags::locationsRetrieveFromForm($sName, $this);
  40. if ($aLocation) {
  41. $aLocation[] = (int)$this->getLocationVal($this->aInputs[$sName], 'rad');
  42. return [
  43. 'string' => parent::getCleanValue($sName),
  44. 'array' => $aLocation,
  45. ];
  46. }
  47. }
  48. //--- Process field with 'Date Range Age' and 'Date-Time Range Age' type.
  49. if($bType && in_array($this->aInputs[$sName]['type'], array('datepicker_range_age', 'datetime_range_age'))) {
  50. $bTypeDateTime = $this->aInputs[$sName]['type'] == 'datetime_range_age';
  51. $sMethod = $this->aFormAttrs['method'];
  52. $sValue = self::getSubmittedValue($sName, $sMethod);
  53. if(!empty($sValue) && is_string($sValue)) {
  54. $aMatches = array();
  55. if(!preg_match("/^([0-9]+)-([0-9]+)$/i", $sValue, $aMatches))
  56. return array();
  57. $aArgs = array("%s-%s-%s", date('Y'), date('m'), date('d'));
  58. if($bTypeDateTime) {
  59. $aArgs[0] = "%s-%s-%s %s:%s:%s";
  60. $aArgs = array_merge($aArgs, array(date('H'), date('i'), date('s')));
  61. }
  62. $aArgsFrom = $aArgs;
  63. $aArgsFrom[1] -= ($aMatches[2] + 1);
  64. if($bTypeDateTime)
  65. $aArgsFrom[6] += 1;
  66. else
  67. $aArgsFrom[3] += 1;
  68. $aArgsTo = $aArgs;
  69. $aArgsTo[1] -= $aMatches[1];
  70. self::setSubmittedValue($sName, array(
  71. call_user_func_array('sprintf', $aArgsFrom),
  72. call_user_func_array('sprintf', $aArgsTo)
  73. ), $sMethod);
  74. }
  75. }
  76. return parent::getCleanValue($sName);
  77. }
  78. public function genInput(&$aInput)
  79. {
  80. switch($aInput['type'])
  81. {
  82. case 'text_range':
  83. case 'datepicker_range':
  84. case 'datetime_range':
  85. $bValue = !empty($aInput['value']) && is_array($aInput['value']);
  86. $aInput['name'] .= '[]';
  87. $aInput['type'] = str_replace('_range', '', $aInput['type']);
  88. if(!isset($aInput['attrs_wrapper']['class']))
  89. $aInput['attrs_wrapper']['class'] = '';
  90. $aInput['attrs_wrapper']['class'] .= ' range';
  91. $aSubInputs = array($aInput, $aInput);
  92. foreach($aSubInputs as $iKey => $aSubInput) {
  93. if($bValue && isset($aInput['value'][$iKey]))
  94. $aSubInput['value'] = $aInput['value'][$iKey];
  95. $aSubInputs[$iKey] = $this->genInputStandard($aSubInput);
  96. }
  97. return implode(' - ', $aSubInputs);
  98. case 'datepicker_range_age':
  99. case 'datetime_range_age':
  100. $iMin = $this->_iAgeMin;
  101. $iMax = $this->_iAgeMax;
  102. $aInput['type'] = 'doublerange';
  103. if(empty($aInput['value']))
  104. $aInput['value'] = $iMin . '-' . $iMax;
  105. else if(is_array($aInput['value'])) {
  106. $iCYear = (int)date('Y');
  107. $iCMonth = (int)date('n');
  108. $iCDay = (int)date('j');
  109. $aRange = array();
  110. foreach($aInput['value'] as $iIndex => $sDate) {
  111. $aDate = explode('-', $sDate);
  112. $aRange[$iIndex] = $iCYear - (int)$aDate[0];
  113. if($iCMonth < (int)$aDate[1] || ($iCMonth == (int)$aDate[1] && $iCDay < (int)$aDate[2]))
  114. $aRange[$iIndex] -= 1;
  115. }
  116. sort($aRange);
  117. $aInput['value'] = implode('-', $aRange);
  118. }
  119. if (!isset($aInput['attrs']['min']) && !isset($aInput['attrs']['max'])){
  120. $aAttrs = array('min' => $iMin, 'max' => $iMax, 'step' => 1);
  121. if(!empty($aInput['attrs']) && is_array($aInput['attrs']))
  122. $aInput['attrs'] = array_merge($aInput['attrs'], $aAttrs);
  123. else
  124. $aInput['attrs'] = $aAttrs;
  125. }
  126. return $this->genInputStandard($aInput);
  127. case 'location_radius':
  128. return $this->genInputLocationRadius($aInput);
  129. }
  130. return parent::genInput($aInput);
  131. }
  132. public function genInputLocation(&$aInput)
  133. {
  134. $aInput['manual_input'] = true;
  135. return parent::genInputLocation($aInput);
  136. }
  137. public function genInputLocationRadius(&$aInput)
  138. {
  139. $aInput['manual_input'] = true;
  140. $aInputRadius = $aInput;
  141. $aInputRadius['type'] = 'text';
  142. $aInputRadius['name'] = $aInputRadius['name'] . '_rad';
  143. $aInputRadius['value'] = $this->getLocationVal($aInput, 'rad');
  144. $aInputRadius['attrs']['placeholder'] = _t('_sys_form_input_location_radius_label');
  145. return $this->oTemplate->parseHtmlByName('form_field_location_radius.html', array (
  146. 'location_input' => parent::genInputLocation($aInput),
  147. 'radius_input' => parent::genInputStandard($aInputRadius),
  148. ));
  149. }
  150. protected function genCustomInputAuthor($aInput)
  151. {
  152. $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . 'searchExtended.php?action=get_authors';
  153. return $this->genCustomInputUsernamesSuggestions($aInput);
  154. }
  155. protected function genCustomInputLabels (&$aInput)
  156. {
  157. $aLabels = BxDolLabel::getInstance()->getLabels(['type' => 'values']);
  158. if(!empty($aLabels) && is_array($aLabels))
  159. $aInput['values'] = array_combine($aLabels, $aLabels);
  160. return $this->genInputCheckboxSet($aInput);
  161. }
  162. function addCssJsCore ()
  163. {
  164. parent::addCssJsCore();
  165. $this->_addCss('search_extended.css');
  166. }
  167. }
  168. /** @} */