1
0

BxBaseSearchExtendedForm.php 6.6 KB

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