BxDolLocationFieldGoogle.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. class BxDolLocationFieldGoogle extends BxDolLocationField
  10. {
  11. protected $_sApiKey;
  12. protected $_sEndpoint;
  13. protected function __construct($aObject)
  14. {
  15. parent::__construct($aObject);
  16. $this->_sApiKey = trim(getParam('sys_maps_api_key'));
  17. $this->_sEndpoint = 'https://maps.googleapis.com/maps/api/place/';
  18. }
  19. public function getAutocomplete($sValue)
  20. {
  21. $sEndpoint = bx_append_url_params($this->_sEndpoint . 'autocomplete/json', [
  22. 'key' => $this->_sApiKey,
  23. 'input' => urlencode($sValue)
  24. ]);
  25. $sResponse = bx_file_get_contents($sEndpoint);
  26. if(empty($sResponse))
  27. return false;
  28. $aResponse = json_decode($sResponse, true);
  29. if(empty($aResponse) || !is_array($aResponse))
  30. return false;
  31. return $aResponse;
  32. }
  33. public function getPlace($sPlaceId, $aFields = [])
  34. {
  35. if(!$aFields)
  36. $aFields = ['geometry', 'formatted_address', 'address_component'];
  37. $sEndpoint = bx_append_url_params($this->_sEndpoint . 'details/json', [
  38. 'key' => $this->_sApiKey,
  39. 'place_id' => $sPlaceId,
  40. 'fields' => implode(',', $aFields)
  41. ]);
  42. $sResponse = bx_file_get_contents($sEndpoint);
  43. if(empty($sResponse))
  44. return false;
  45. $aResponse = json_decode($sResponse, true);
  46. if(empty($aResponse) || !is_array($aResponse) || empty($aResponse['result']))
  47. return false;
  48. return $aResponse['result'];
  49. }
  50. public function genInputLocation (&$aInput, $oForm)
  51. {
  52. $aVars = $this->_getInputLocationVars($aInput, $oForm);
  53. return $oForm->getTemplate()->parseHtmlByName('location_field_google.html', $aVars);
  54. }
  55. protected function _getInputLocationVars(&$aInput, $oForm)
  56. {
  57. $isManualInput = (int)(isset($aInput['manual_input']) && $aInput['manual_input']);
  58. $sIdStatus = $oForm->getInputId($aInput) . '_status';
  59. $sIdInput = $oForm->getInputId($aInput) . '_location';
  60. $aVars = array (
  61. 'key' => $this->_sApiKey,
  62. 'lang' => bx_lang_name(),
  63. 'name' => $aInput['name'],
  64. 'id_status' => $sIdStatus,
  65. 'id_input' => $sIdInput,
  66. 'manual_input' => $isManualInput,
  67. 'bx_if:manual_input' => array(
  68. 'condition' => $isManualInput,
  69. 'content' => array(),
  70. ),
  71. 'bx_if:auto_input' => array(
  72. 'condition' => !$isManualInput,
  73. 'content' => array(
  74. 'id_status' => $sIdStatus,
  75. 'location_string' => _t('_sys_location_field_label'),
  76. ),
  77. ),
  78. 'api_field_name_short' => 'short_name',
  79. 'api_field_name_long' => 'long_name',
  80. 'api_field_name_2_length' => json_encode(array()),
  81. );
  82. $aLocationIndexes = BxDolForm::$LOCATION_INDEXES;
  83. foreach ($aLocationIndexes as $sKey)
  84. $aVars[$sKey] = $this->getLocationVal($aInput, $sKey, $oForm);
  85. if ($isManualInput) {
  86. $aAttrs = empty($aInput['attrs']) ? array() : $aInput['attrs'];
  87. $aInput2 = $aInput;
  88. $aInput2['type'] = 'text';
  89. $aInput2['attrs']['id'] = $sIdInput;
  90. $aInput2['attrs'] = array_merge($aAttrs, $aInput2['attrs']);
  91. $aVars['input'] = $oForm->genInputStandard($aInput2);
  92. }
  93. else {
  94. if ($this->getLocationVal($aInput, 'lat', $oForm) && $this->getLocationVal($aInput, 'lng', $oForm))
  95. $aInput['checked'] = true;
  96. else
  97. $aInput['checked'] = $oForm->getCleanValue($aInput['name'] . '_lat') && $oForm->getCleanValue($aInput['name'] . '_lng') ? 1 : 0;
  98. $aVars['input'] = $oForm->genInputSwitcher($aInput);
  99. $sLocationString = _t($aInput['checked'] ? '_sys_location_undefined' : '_sys_location_field_label');
  100. if ($aVars['country']) {
  101. $aCountries = BxDolFormQuery::getDataItems('Country');
  102. $sLocationString = ($aVars['street_number'] ? $aVars['street_number'] . ', ' : '') . ($aVars['street'] ? $aVars['street'] . ', ' : '') . ($aVars['city'] ? $aVars['city'] . ', ' : '') . ($aVars['state'] ? $aVars['state'] . ', ' : '') . $aCountries[$aVars['country']];
  103. }
  104. $aVars['bx_if:auto_input']['content']['location_string'] = $sLocationString;
  105. }
  106. return $aVars;
  107. }
  108. }
  109. /** @} */