BxDolLocationFieldNominatim.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * Plain fields with Nomination service geocoding
  11. */
  12. class BxDolLocationFieldNominatim extends BxDolLocationField
  13. {
  14. protected $_sEndpoint;
  15. protected function __construct($aObject)
  16. {
  17. parent::__construct($aObject);
  18. $this->_sEndpoint = bx_append_url_params($this->getNominatimServer() . '/search', [
  19. 'email' => $this->getNominatimEmail(),
  20. 'addressdetails' => 1
  21. ]);
  22. }
  23. public function getLocation($aAddress, $sFormat = 'json', $iLimit = 1)
  24. {
  25. $sEndpoint = bx_append_url_params($this->_sEndpoint, array_merge([
  26. 'format' => $sFormat,
  27. 'limit' => $iLimit
  28. ], $aAddress));
  29. $sResults = bx_file_get_contents($sEndpoint);
  30. if(empty($sResults))
  31. return false;
  32. $aResults = json_decode($sResults, true);
  33. if(empty($aResults) || !is_array($aResults))
  34. return false;
  35. $aLocations = [];
  36. foreach($aResults as $aResult) {
  37. if(empty($aResult) || !is_array($aResult))
  38. continue;
  39. $aLocations[] = [
  40. 'lat' => $aResult['lat'],
  41. 'lon' => $aResult['lon'],
  42. 'address' => $aResult['address'],
  43. ];
  44. }
  45. if(empty($aLocations))
  46. return false;
  47. return $iLimit == 1 ? array_shift($aLocations) : $aLocations;
  48. }
  49. public function genInputLocation (&$aInput, $oForm)
  50. {
  51. $isManualInput = (int)(isset($aInput['manual_input']) && $aInput['manual_input']);
  52. if (!$isManualInput) {
  53. $aVars = array();
  54. $aInputField = $aInput;
  55. $aLocationIndexes = BxDolForm::$LOCATION_INDEXES;
  56. foreach ($aLocationIndexes as $sKey)
  57. $aVars[$sKey] = $this->getLocationVal($aInput, $sKey, $oForm);
  58. $aInputField['caption'] = _t('_sys_location_field_label');
  59. if ($this->getLocationVal($aInput, 'lat', $oForm) && $this->getLocationVal($aInput, 'lng', $oForm))
  60. $aInputField['checked'] = true;
  61. else
  62. $aInputField['checked'] = $oForm->getCleanValue($aInput['name'] . '_lat') && $oForm->getCleanValue($aInput['name'] . '_lng') ? 1 : 0;
  63. $sLocationString = _t($aInputField['checked'] ? '_sys_location_undefined' : '_sys_location_field_label');
  64. if ($aVars['country']) {
  65. $aCountries = BxDolFormQuery::getDataItems('Country');
  66. $sLocationString = ($aVars['street_number'] ? $aVars['street_number'] . ', ' : '') . ($aVars['street'] ? $aVars['street'] . ', ' : '') . ($aVars['city'] ? $aVars['city'] . ', ' : '') . ($aVars['state'] ? $aVars['state'] . ', ' : '') . $aCountries[$aVars['country']];
  67. }
  68. elseif ($aVars['lat'] || $aVars['lng']) {
  69. $sLocationString = $aVars['lat'] . ', ' . $aVars['lng'];
  70. }
  71. $aVars['form_id'] = $oForm->id;
  72. $aVars['name'] = $aInput['name'];
  73. $aVars['input'] = $oForm->genInputSwitcher($aInputField);
  74. $aVars['id_status'] = $oForm->getInputId($aInput) . '_status';
  75. $aVars['location_string'] = $sLocationString;
  76. $aVars['nominatim_server'] = $this->getNominatimServer();
  77. $aVars['nominatim_email'] = $this->getNominatimEmail();
  78. $sRet = $oForm->getTemplate()->parseHtmlByName('location_field_plain_auto.html', $aVars);
  79. }
  80. else {
  81. $bMinimized = isset($aInput['minimized']) && $aInput['minimized'];
  82. $aFields = array(
  83. 'lat' => array('type' => 'hidden'),
  84. 'lng' => array('type' => 'hidden'),
  85. 'street_number' => array('type' => 'text', 'ph' => _t('_sys_location_ph_number')),
  86. 'street' => array('type' => 'text', 'ph' => _t('_sys_location_ph_street')),
  87. 'city' => array('type' => 'text', 'ph' => _t('_sys_location_ph_city')),
  88. 'state' => array('type' => 'text', 'ph' => _t('_sys_location_ph_state')),
  89. 'zip' => array('type' => 'text', 'ph' => _t('_sys_location_ph_zip')),
  90. 'country' => array('type' => 'select'),
  91. );
  92. if($bMinimized)
  93. unset($aFields['street_number'], $aFields['street'], $aFields['zip']);
  94. $sInputs = '';
  95. foreach ($aFields as $sKey => $a) {
  96. $aInputField = $aInput;
  97. $aInputField['name'] = $aInput['name'] . '_' . $sKey;
  98. $aInputField['type'] = $a['type'];
  99. $aInputField['value'] = $this->getLocationVal($aInput, $sKey, $oForm);
  100. if (!isset($aInputField['attrs']) || !is_array($aInputField['attrs']))
  101. $aInputField['attrs'] = [];
  102. $aInputField['attrs']['placeholder'] = empty($a['ph']) ? '' : _t($a['ph']);
  103. if (isset($aInputField['attrs']['class']))
  104. $aInputField['attrs']['class'] .= ' bx-form-input-location-' . $sKey;
  105. else
  106. $aInputField['attrs']['class'] = 'bx-form-input-location-' . $sKey;
  107. if ('country' == $sKey) {
  108. $aCountries = BxDolFormQuery::getDataItems('Country');
  109. array_unshift($aCountries, array('key' => '', 'value' => _t('_sys_please_select')));
  110. $aInputField['values'] = $aCountries;
  111. $sInputs .= $oForm->genInputSelect($aInputField);
  112. }
  113. else {
  114. $sInputs .= $oForm->genInputStandard($aInputField);
  115. }
  116. }
  117. $sRet = $oForm->getTemplate()->parseHtmlByName('location_field_plain.html', array(
  118. 'form_id' => $oForm->id,
  119. 'name' => $aInput['name'],
  120. 'minimized' => $bMinimized ? 'minimized' : '',
  121. 'nominatim_server' => $this->getNominatimServer(),
  122. 'nominatim_email' => $this->getNominatimEmail(),
  123. 'inputs' => $sInputs,
  124. 'normalize_names' => getParam('sys_location_normalize_names') ? 1 : 0,
  125. ));
  126. }
  127. return $sRet;
  128. }
  129. protected function getNominatimServer ()
  130. {
  131. return trim(trim(getParam('sys_nominatim_server')), '/');
  132. }
  133. protected function getNominatimEmail ()
  134. {
  135. return trim(getParam('sys_nominatim_email'));
  136. }
  137. }
  138. /** @} */