Skip to content

Commit

Permalink
Make documentation links more visible
Browse files Browse the repository at this point in the history
  • Loading branch information
zoglo committed Feb 17, 2023
1 parent da51ef3 commit 0cf9843
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 104 deletions.
188 changes: 93 additions & 95 deletions src/Resources/contao/classes/GooglePlacesApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,113 +28,111 @@ class GooglePlacesApi extends Frontend
{
public function getGoogleReviews(?array $arrIds = null, bool $manualSync = false)
{
// Check if method is called by cronjob
$blnCron = false;
if(null === $arrIds)
// Check if method is called by cronjob
$blnCron = false;

if (null === $arrIds)
{
$recTable = RecommendationArchiveModel::getTable();
$recTable = RecommendationArchiveModel::getTable();

$objRecommendationArchives = RecommendationArchiveModel::findBy([
$recTable . ".syncWithGoogle=?"
],[1]);
if (!$manualSync)
{
$blnCron = true;
}
$recTable . ".syncWithGoogle=?"
], [1]);

if (!$manualSync)
{
$blnCron = true;
}
}
else
{
$objRecommendationArchives = RecommendationArchiveModel::findMultipleByIds($arrIds);
}
else
{
$objRecommendationArchives = RecommendationArchiveModel::findMultipleByIds($arrIds);
}

if (null === $objRecommendationArchives)
return;


foreach($objRecommendationArchives as $objRecommendationArchive)
foreach ($objRecommendationArchives as $objRecommendationArchive)
{
$strSyncUrl = 'https://maps.googleapis.com/maps/api/place/details/json?reviews_sort=newest&language=' . ($objRecommendationArchive->syncLanguage ?? '') . '&place_id='.$objRecommendationArchive->googlePlaceId . '&fields=reviews&key=' . $objRecommendationArchive->googleApiToken;
$client = HttpClient::create();
$arrContent = $client->request('POST', $strSyncUrl)->toArray();
$objContent = (object) $arrContent;
System::loadLanguageFile('tl_recommendation');
$strSyncUrl = 'https://maps.googleapis.com/maps/api/place/details/json?reviews_sort=newest&language=' . ($objRecommendationArchive->syncLanguage ?? '') . '&place_id=' . $objRecommendationArchive->googlePlaceId . '&fields=reviews&key=' . $objRecommendationArchive->googleApiToken;
$client = HttpClient::create();
$arrContent = $client->request('POST', $strSyncUrl)->toArray();
$objContent = (object)$arrContent;

System::loadLanguageFile('tl_recommendation');

if ($objContent && $objContent->status !== 'OK')
{
$logger = System::getContainer()->get('monolog.logger.contao');
$logger->log(
LogLevel::ERROR,
'Recommendations for Archive with ID ' . $objRecommendationArchive->id . ' could not be synced - Reason: '. ($objContent->error_message ?? $objContent->status ?? 'Connection with Google Api could not be established.') ,
array('contao' => new ContaoContext(__METHOD__, TL_ERROR))
);
// Display an error if api call was not successful
if(!$blnCron)
{
Message::addError(sprintf($GLOBALS['TL_LANG']['tl_recommendation']['archiveSyncFailed'], $objRecommendationArchive->id, ($objContent->error_message ?? $objContent->status ?? 'Connection with Google Api could not be established.')));
}
continue;
$logger = System::getContainer()->get('monolog.logger.contao');
$logger->log(
LogLevel::ERROR,
'Recommendations for Archive with ID ' . $objRecommendationArchive->id . ' could not be synced - Reason: ' . ($objContent->error_message ?? $objContent->status ?? 'Connection with Google Api could not be established.'),
['contao' => new ContaoContext(__METHOD__, TL_ERROR)]
);

// Display an error if api call was not successful
if (!$blnCron)
{
Message::addError(sprintf($GLOBALS['TL_LANG']['tl_recommendation']['archiveSyncFailed'], $objRecommendationArchive->id, ($objContent->error_message ?? $objContent->status ?? 'Connection with Google Api could not be established.')));
}

continue;
}

if ($objContent && $objContent->result && (is_array($arrReviews = $objContent->result['reviews']) ?? null))
{
$time = time();
$objRecommendations = RecommendationModel::findByPid($objRecommendationArchive->id);
$time = time();

$objRecommendations = RecommendationModel::findByPid($objRecommendationArchive->id);

foreach ($arrReviews as $review)
{
// Skip if author url or text is empty or record already exists
if (!$review['author_url'] || !$review['text'] || $this->recordExists($objRecommendations, $review['author_url']))
continue;


// Prepare the record
$arrData = array
(
'tstamp' => $time,
'pid' => $objRecommendationArchive->id,
'author' => $review['author_name'],
'date' => $review['time'],
'time' => $review['time'],
'text' => '<p>' . $review['text'] . '</p>',
'rating' => $review['rating'],
'imageUrl' => $review['profile_photo_url'],
'googleAuthorUrl' => $review['author_url'],
'published' => 1
);
// Prepare the record
$arrData = [
'tstamp' => $time,
'pid' => $objRecommendationArchive->id,
'author' => $review['author_name'],
'date' => $review['time'],
'time' => $review['time'],
'text' => '<p>' . $review['text'] . '</p>',
'rating' => $review['rating'],
'imageUrl' => $review['profile_photo_url'],
'googleAuthorUrl' => $review['author_url'],
'published' => 1
];

$objRecommendation = new RecommendationModel();
$objRecommendation->setRow($arrData)->save();
}

// Sync happened successfully
if(!$blnCron) {
Message::addInfo(sprintf($GLOBALS['TL_LANG']['tl_recommendation']['archiveSyncSuccess'], $objRecommendationArchive->id));
}

//Invalidate archive tag
$this->invalidateRecommendationArchiveTag($objRecommendationArchive);

// Sync happened successfully
if (!$blnCron)
{
Message::addInfo(sprintf($GLOBALS['TL_LANG']['tl_recommendation']['archiveSyncSuccess'], $objRecommendationArchive->id));
}

//Invalidate archive tag
$this->invalidateRecommendationArchiveTag($objRecommendationArchive);
}
}
}
/**
* Sync all archives manually
*/
public function syncAllArchives()
{
$this->getGoogleReviews(null, true);
$this->redirect($this->getReferer());
}
/**
* Sync selected archive with Google
*/

/**
* Sync all archives manually
*/
public function syncAllArchives()
{
$this->getGoogleReviews(null, true);
$this->redirect($this->getReferer());
}

/**
* Sync selected archive with Google
*/
public function syncWithGoogle()
{
$this->getGoogleReviews([Input::get('id')]);
Expand All @@ -145,27 +143,27 @@ public function syncWithGoogle()
* Check if a record exists
*
* @param RecommendationModel $objRecommendations
* @param string $authorUrl
* @param string $authorUrl
*
* @return boolean
*/
protected function recordExists($objRecommendations, $authorUrl): bool
{
if (null === $objRecommendations)
return false;

$arrUrls = $objRecommendations->fetchEach('googleAuthorUrl');

return in_array($authorUrl, $arrUrls);

$arrUrls = $objRecommendations->fetchEach('googleAuthorUrl');

return in_array($authorUrl, $arrUrls);
}

/**
* Invalidates the recommendation cache tag
*/
public function invalidateRecommendationArchiveTag($objRecommendationArchive)
{
/** @var FOS\HttpCacheBundle\CacheManager $cacheManager */
$cacheManager = System::getContainer()->get('fos_http_cache.cache_manager');
$cacheManager->invalidateTags(['contao.db.tl_recommendation_archive.' . $objRecommendationArchive->id]);
}

/**
* Invalidates the recommendation cache tag
*/
public function invalidateRecommendationArchiveTag($objRecommendationArchive)
{
/** @var FOS\HttpCacheBundle\CacheManager $cacheManager */
$cacheManager = System::getContainer()->get('fos_http_cache.cache_manager');
$cacheManager->invalidateTags(array('contao.db.tl_recommendation_archive.' . $objRecommendationArchive->id));
}
}
12 changes: 6 additions & 6 deletions src/Resources/contao/languages/de/tl_recommendation_archive.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@
<target>Google API Token</target>
</trans-unit>
<trans-unit id="tl_recommendation_archive.googleApiToken.1">
<source>Please enter your Google API Token &lt;a href=&quot;https://cloud.google.com/docs/authentication/api-keys&quot; title=&quot;Google API keys&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot;&gt;(Using API keys)&lt;/a&gt;.</source>
<target>Bitte geben Sie Ihren Google API Token ein &lt;a href=&quot;https://cloud.google.com/docs/authentication/api-keys&quot; title=&quot;Google API keys&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot;&gt;(API-Schlüssel verwenden)&lt;/a&gt;.</target>
<source>Please enter your Google API Token &lt;a href=&quot;https://cloud.google.com/docs/authentication/api-keys&quot; title=&quot;Google API keys&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot; style=&quot;text-decoration:underline;&quot;&gt;(Using API keys)&lt;/a&gt;.</source>
<target>Bitte geben Sie Ihren Google API Token ein &lt;a href=&quot;https://cloud.google.com/docs/authentication/api-keys&quot; title=&quot;Google API keys&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot; style=&quot;text-decoration:underline;&quot;&gt;(API-Schlüssel verwenden)&lt;/a&gt;.</target>
</trans-unit>
<trans-unit id="tl_recommendation_archive.googlePlaceId.0">
<source>Google Places ID</source>
<target>Google Places ID</target>
</trans-unit>
<trans-unit id="tl_recommendation_archive.googlePlaceId.1">
<source>Please enter the Google Places ID &lt;a href=&quot;https://developers.google.com/maps/documentation/places/web-service/place-id&quot; title=&quot;Place IDs&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot;&gt;(Place IDs)&lt;/a&gt;.</source>
<target>Bitte geben Sie die Google Places ID ein &lt;a href=&quot;https://developers.google.com/maps/documentation/places/web-service/place-id&quot; title=&quot;Place IDs&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot;&gt;(Place IDs)&lt;/a&gt;.</target>
<source>Please enter the Google Places ID &lt;a href=&quot;https://developers.google.com/maps/documentation/places/web-service/place-id&quot; title=&quot;Place IDs&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot; style=&quot;text-decoration:underline;&quot;&gt;(Place IDs)&lt;/a&gt;.</source>
<target>Bitte geben Sie die Google Places ID ein &lt;a href=&quot;https://developers.google.com/maps/documentation/places/web-service/place-id&quot; title=&quot;Place IDs&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot; style=&quot;text-decoration:underline;&quot;&gt;(Place IDs)&lt;/a&gt;.</target>
</trans-unit>
<trans-unit id="tl_recommendation_archive.syncLanguage.0">
<source>Language</source>
<target>Sprache</target>
</trans-unit>
<trans-unit id="tl_recommendation_archive.syncLanguage.1">
<source>Please select the import language &lt;a href=&quot;https://developers.google.com/maps/faq#languagesupport&quot; title=&quot;Machine translated&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot;&gt;(Machine translation by Google)&lt;/a&gt;.</source>
<target>Bitte wählen Sie die Importsprache &lt;a href=&quot;https://developers.google.com/maps/faq#languagesupport&quot; title=&quot;Maschinelle Übersetzung&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot;&gt;(Maschinelle Übersetzung durch Google)&lt;/a&gt;.</target>
<source>Please select the import language &lt;a href=&quot;https://developers.google.com/maps/faq#languagesupport&quot; title=&quot;Machine translated&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot; style=&quot;text-decoration:underline;&quot;&gt;(Machine translation by Google)&lt;/a&gt;.</source>
<target>Bitte wählen Sie die Importsprache &lt;a href=&quot;https://developers.google.com/maps/faq#languagesupport&quot; title=&quot;Maschinelle Übersetzung&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot; style=&quot;text-decoration:underline;&quot;&gt;(Maschinelle Übersetzung durch Google)&lt;/a&gt;.</target>
</trans-unit>
</body>
</file>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
<source>Google API Token</source>
</trans-unit>
<trans-unit id="tl_recommendation_archive.googleApiToken.1">
<source>Please enter your Google API Token &lt;a href=&quot;https://cloud.google.com/docs/authentication/api-keys&quot; title=&quot;Google API keys&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot;&gt;(Using API keys)&lt;/a&gt;.</source>
<source>Please enter your Google API Token &lt;a href=&quot;https://cloud.google.com/docs/authentication/api-keys&quot; title=&quot;Google API keys&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot; style=&quot;text-decoration:underline;&quot;&gt;(Using API keys)&lt;/a&gt;.</source>
</trans-unit>
<trans-unit id="tl_recommendation_archive.googlePlaceId.0">
<source>Google Places ID</source>
</trans-unit>
<trans-unit id="tl_recommendation_archive.googlePlaceId.1">
<source>Please enter the Google Places ID &lt;a href=&quot;https://developers.google.com/maps/documentation/places/web-service/place-id&quot; title=&quot;Place IDs&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot;&gt;(Place IDs)&lt;/a&gt;.</source>
<source>Please enter the Google Places ID &lt;a href=&quot;https://developers.google.com/maps/documentation/places/web-service/place-id&quot; title=&quot;Place IDs&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot; style=&quot;text-decoration:underline;&quot;&gt;(Place IDs)&lt;/a&gt;.</source>
</trans-unit>
<trans-unit id="tl_recommendation_archive.syncLanguage.0">
<source>Language</source>
</trans-unit>
<trans-unit id="tl_recommendation_archive.syncLanguage.1">
<source>Please select the import language &lt;a href=&quot;https://developers.google.com/maps/faq#languagesupport&quot; title=&quot;Machine translated&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot;&gt;(Machine translation by Google)&lt;/a&gt;.</source>
<source>Please select the import language &lt;a href=&quot;https://developers.google.com/maps/faq#languagesupport&quot; title=&quot;Machine translated&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot; style=&quot;text-decoration:underline;&quot;&gt;(Machine translation by Google)&lt;/a&gt;.</source>
</trans-unit>
</body>
</file>
Expand Down

0 comments on commit 0cf9843

Please sign in to comment.