From 1b6b2ea140c9f5802c32808a5b4ecc3cf7dcffeb Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 22 May 2020 15:45:06 +0300 Subject: [PATCH] pdf: google maps --- .../Espo/Core/TemplateHelpers/GoogleMaps.php | 194 ++++++++++++++++++ .../metadata/app/templateHelpers.json | 3 + 2 files changed, 197 insertions(+) create mode 100644 application/Espo/Core/TemplateHelpers/GoogleMaps.php create mode 100644 application/Espo/Resources/metadata/app/templateHelpers.json diff --git a/application/Espo/Core/TemplateHelpers/GoogleMaps.php b/application/Espo/Core/TemplateHelpers/GoogleMaps.php new file mode 100644 index 0000000000..6c05279c3c --- /dev/null +++ b/application/Espo/Core/TemplateHelpers/GoogleMaps.php @@ -0,0 +1,194 @@ +get('language'); + + if (strpos($size, 'x') === false) { + $size = $size .'x' . $size; + } + + if ($field && $metadata->get(['entityDefs', $entityType, 'fields', $field, 'type']) !== 'address') { + $GLOBALS['log']->warning("Template helper _googleMapsImage: Specified field is not of address type."); + return null; + } + + if ( + !$field && + !array_key_exists('street', $hash) && + !array_key_exists('city', $hash) && + !array_key_exists('country', $hash) && + !array_key_exists('state', $hash) && + !array_key_exists('postalCode', $hash) + ) { + $field = ($entityType === 'Account') ? 'billingAddress' : 'address'; + } + + if ($field) { + $street = $data[$field . 'Street'] ?? null; + $city = $data[$field . 'City'] ?? null; + $country = $data[$field . 'Country'] ?? null; + $state = $data[$field . 'State'] ?? null; + $postalCode = $data[$field . 'postalCode'] ?? null; + } else { + $street = $hash['street'] ?? null; + $city = $hash['city'] ?? null; + $country = $hash['country'] ?? null; + $state = $hash['state'] ?? null; + $postalCode = $hash['postalCode'] ?? null; + } + + $address = ''; + if ($street) { + $address .= $street; + } + if ($city) { + if ($address != '') { + $address .= ', '; + } + $address .= $city; + } + if ($state) { + if ($address != '') { + $address .= ', '; + } + $address .= $state; + } + if ($postalCode) { + if ($state || $city) { + $address .= ' '; + } else { + if ($address) { + $address .= ', '; + } + } + $address .= $postalCode; + } + if ($country) { + if ($address != '') { + $address .= ', '; + } + $address .= $country; + } + + $address = urlencode($address); + + $apiKey = $config->get('googleMapsApiKey'); + + if (!$apiKey) { + $GLOBALS['log']->error("Template helper _googleMapsImage: No Google Maps API key."); + return null; + } + + if (!$address) { + $GLOBALS['log']->debug("Template helper _googleMapsImage: No address to display."); + return null; + } + + $format = 'jpg;'; + + $url = "https://maps.googleapis.com/maps/api/staticmap?" . + 'center=' . $address . + 'format=' . $format . + '&size=' . $size . + '&key=' . $apiKey; + + if ($zoom) { + $url .= '&zoom=' . $zoom; + } + if ($language) { + $url .= '&language=' . $language; + } + + $GLOBALS['log']->debug("Template helper _googleMapsImage: URL: {$url}."); + + $image = \Espo\Core\TemplateHelpers\GoogleMaps::getImage($url); + + if (!$image) { + return null; + } + + + + $filePath = tempnam(sys_get_temp_dir(), 'google_maps_image'); + file_put_contents($filePath, $image); + + list($width, $height) = explode('x', $size); + + $tag = ""; + + return new LightnCandy\SafeString($tag); + } + + public static function getImage(string $url) + { + $headers = []; + $headers[] = 'Accept: image/jpeg, image/pjpeg'; + $headers[] = 'Connection: Keep-Alive'; + + $agent = 'Mozilla/5.0'; + + $c = curl_init(); + + curl_setopt($c, \CURLOPT_URL, $url); + curl_setopt($c, \CURLOPT_HTTPHEADER, $headers); + curl_setopt($c, \CURLOPT_HEADER, 0); + curl_setopt($c, \CURLOPT_USERAGENT, $agent); + curl_setopt($c, \CURLOPT_TIMEOUT, 10); + curl_setopt($c, \CURLOPT_RETURNTRANSFER, 1); + curl_setopt($c, \CURLOPT_FOLLOWLOCATION, 1); + curl_setopt($c, \CURLOPT_BINARYTRANSFER, 1); + + + $raw = curl_exec($c); + curl_close($c); + + return $raw; + } +} diff --git a/application/Espo/Resources/metadata/app/templateHelpers.json b/application/Espo/Resources/metadata/app/templateHelpers.json new file mode 100644 index 0000000000..82ec336b60 --- /dev/null +++ b/application/Espo/Resources/metadata/app/templateHelpers.json @@ -0,0 +1,3 @@ +{ + "_googleMapsImage": "\\Espo\\Core\\TemplateHelpers\\GoogleMaps::image" +}