Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] retrieve l18n Description from currentWeather by openweatherapi #51

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Classes/Domain/Model/CurrentWeather.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class CurrentWeather extends AbstractEntity
*/
protected $name = '';

/**
* @var string
*/
protected $description = '';

/**
* @var \DateTime
*/
Expand Down Expand Up @@ -98,6 +103,16 @@ public function setName(string $name): void
$this->name = $name;
}

public function getDescription(): string
{
return $this->description;
}

public function setDescription(string $description): void
{
$this->description = $description;
}

public function getMeasureTimestamp(): ?\DateTime
{
return $this->measureTimestamp;
Expand Down
73 changes: 58 additions & 15 deletions Classes/Task/OpenWeatherMapTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MailUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
use TYPO3\CMS\Core\Site\SiteFinder;

/**
* OpenWeatherMapTask Class for Scheduler
Expand Down Expand Up @@ -55,6 +56,11 @@ class OpenWeatherMapTask extends WeatherAbstractTask
*/
public $apiKey = '';

/**
* @var string $apiKey
*/
public $languages = '';

/**
* Comma seperated list of page UIDs to clear cache
*
Expand Down Expand Up @@ -126,12 +132,49 @@ public function execute(): bool

$this->removeOldRecordsFromDb();

$this->url = sprintf(
'https://api.openweathermap.org/data/2.5/weather?q=%s,%s&units=%s&APPID=%s',
//Check if languages were set in scheduler task
//if there are languages set, we need to find the twoLetterIsoCode for each language to receive the request in the desired language
//No Language was default until now, so we set the parameter -1 in sys_language_uid so we cann access this data in all languages and prevent breaking changes
if($this->languages != ''){
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
$site = $siteFinder->getSiteByPageId($this->recordStoragePage);
$languages = explode(",",$this->languages);
foreach ($languages as $langId) {
$langId = (int)$langId;
$langCode = $site->getLanguageById($langId)->getTwoLetterIsoCode();
$this->getWeatherData($langCode);
$this->saveCurrentWeatherInstanceForResponseClass($this->responseClass, $langId);
}
}else{
$this->getWeatherData();
$this->saveCurrentWeatherInstanceForResponseClass($this->responseClass);
}

if (!empty($this->clearCache)) {
$cacheService = $this->getCacheService();
$cacheService->clearPageCache(GeneralUtility::intExplode(',', $this->clearCache));
}

return true;
}

/**
* Fetches data from openweathermap api
*
* @param string $langCode
* @return void
*/
private function getWeatherData(string $langCode = 'en'){
$parameters = [
urlencode($this->city),
urlencode($this->country),
'metric',
$this->apiKey
$this->apiKey,
$langCode
];
$this->url = sprintf(
'https://api.openweathermap.org/data/2.5/weather?q=%s,%s&units=%s&APPID=%s&lang=%s',
...$parameters
);
try {
$response = $this->getRequestFactory()->request($this->url);
Expand All @@ -150,16 +193,6 @@ public function execute(): bool

$this->responseClass = json_decode((string)$response->getBody());
$this->logger->info(sprintf('Response class: %s', json_encode($this->responseClass)));

// Changing the data save to query builder
$this->saveCurrentWeatherInstanceForResponseClass($this->responseClass);

if (!empty($this->clearCache)) {
$cacheService = $this->getCacheService();
$cacheService->clearPageCache(GeneralUtility::intExplode(',', $this->clearCache));
}

return true;
}

/**
Expand Down Expand Up @@ -215,13 +248,20 @@ private function checkResponseCode(ResponseInterface $response): bool
}
}

public function saveCurrentWeatherInstanceForResponseClass(\stdClass $responseClass): int
/**
* Saves the current weather instance to the database
*
* @param \stdClass $responseClass
* @param int $langId
* @return int
*/
public function saveCurrentWeatherInstanceForResponseClass(\stdClass $responseClass, int $langId = -1): int
{
$weatherObjectArray = [
'pid' => $this->recordStoragePage,
'name' => $this->name,
'sys_language_uid' => $langId,
];

if (isset($responseClass->main->temp)) {
$weatherObjectArray['temperature_c'] = (double) $responseClass->main->temp;
}
Expand Down Expand Up @@ -260,6 +300,9 @@ public function saveCurrentWeatherInstanceForResponseClass(\stdClass $responseCl
if (isset($responseClass->weather[0]->icon)) {
$weatherObjectArray['icon'] = $responseClass->weather[0]->icon;
}
if (isset($responseClass->weather[0]->description)) {
$weatherObjectArray['description'] = $responseClass->weather[0]->description;
}
if (isset($responseClass->weather[0]->id)) {
$weatherObjectArray['condition_code'] = $responseClass->weather[0]->id;
}
Expand Down
9 changes: 9 additions & 0 deletions Classes/Task/OpenWeatherMapTaskAdditionalFieldProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class OpenWeatherMapTaskAdditionalFieldProvider extends AbstractAdditionalFieldP
'city',
'country',
'apiKey',
'languages',
'clearCache',
'errorNotification',
'emailSenderName',
Expand Down Expand Up @@ -165,6 +166,13 @@ public function getAdditionalFields(
'label' => 'LLL:EXT:weather2/Resources/Private/Language/locallang_scheduler_openweatherapi.xlf:api_key',
];

$fieldID = 'languages';
$fieldCode = '<input type="text" class="form-control" name="tx_scheduler[languages]" id="' . $fieldID . '" value="' . $taskInfo['languages'] . '" size="120" />';
$additionalFields[$fieldID] = [
'code' => $fieldCode,
'label' => 'LLL:EXT:weather2/Resources/Private/Language/locallang_scheduler_openweatherapi.xlf:languages',
];

$fieldID = 'clearCache';
$fieldCode = '<input type="text" class="form-control" name="tx_scheduler[clearCache]" id="' . $fieldID . '" value="' . $taskInfo['clearCache'] . '" size="120" />';
$additionalFields[$fieldID] = [
Expand Down Expand Up @@ -313,6 +321,7 @@ public function saveAdditionalFields(array $submittedData, AbstractTask $task):
$task->recordStoragePage = (int)($submittedData['recordStoragePage'] ?? 0);
$task->country = $submittedData['country'] ?? '';
$task->apiKey = $submittedData['apiKey'] ?? '';
$task->languages = $submittedData['languages'] ?? '';
$task->clearCache = $submittedData['clearCache'] ?? '0';
$task->errorNotification = $submittedData['errorNotification'] ?? '';
$task->emailSenderName = $submittedData['emailSenderName'] ?? '';
Expand Down
45 changes: 44 additions & 1 deletion Configuration/TCA/tx_weather2_domain_model_currentweather.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
'crdate' => 'crdate',
'rootLevel' => -1,
'delete' => 'deleted',
'languageField' => 'sys_language_uid',
'transOrigPointerField' => 'l10n_parent',
'transOrigDiffSourceField' => 'l10n_diffsource',
'translationSource' => 'l10n_source',
'enablecolumns' => [
'disabled' => 'hidden',
'starttime' => 'starttime',
Expand All @@ -23,9 +27,39 @@
],
],
'types' => [
'1' => ['showitem' => 'name, measure_timestamp, temperature_c, pressure_hpa, humidity_percentage, min_temp_c, max_temp_c, wind_speed_m_p_s, wind_direction_deg, pop_percentage, rain_volume, snow_volume, clouds_percentage, icon, serialized_array, --div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.access, starttime, endtime'],
'1' => ['showitem' => 'name, description, measure_timestamp, temperature_c, pressure_hpa, humidity_percentage, min_temp_c, max_temp_c, wind_speed_m_p_s, wind_direction_deg, pop_percentage, rain_volume, snow_volume, clouds_percentage, icon, serialized_array, --div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.access, starttime, endtime'],
],
'columns' => [
'sys_language_uid' => [
'exclude' => true,
'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.language',
'config' => [
'type' => 'language',
],
],
'l10n_parent' => [
'displayCond' => 'FIELD:sys_language_uid:>:0',
'label' => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.l18n_parent',
'config' => [
'type' => 'group',
'allowed' => 'tx_weather2_domain_model_currentweather',
'size' => 1,
'maxitems' => 1,
'minitems' => 0,
'default' => 0,
],
],
'l10n_source' => [
'config' => [
'type' => 'passthrough',
],
],
'l10n_diffsource' => [
'config' => [
'type' => 'passthrough',
'default' => '',
],
],
'starttime' => [
'exclude' => 1,
'label' => 'EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.starttime',
Expand Down Expand Up @@ -65,6 +99,15 @@
'eval' => 'trim',
],
],
'description' => [
'exclude' => 1,
'label' => 'LLL:EXT:weather2/Resources/Private/Language/locallang_db.xlf:tx_weather2_domain_model_currentweather.description',
'config' => [
'type' => 'input',
'size' => 30,
'eval' => 'trim',
],
],
'measure_timestamp' => [
'exclude' => 1,
'label' => 'LLL:EXT:weather2/Resources/Private/Language/locallang_db.xlf:tx_weather2_domain_model_currentweather.measure_timestamp',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<source>API-Key</source>
<target>API-Key</target>
</trans-unit>
<trans-unit id="languages">
<source>Language IDs (comma separated list with IDs)</source>
<target>Sprach IDs (kommaseparierte Liste mit IDs)</target>
</trans-unit>
<trans-unit id="clear_cache">
<source>Clear cache for pages (comma separated list with IDs)</source>
<target>Leere Cache für Seiten (kommaseparierte Liste mit IDs)</target>
Expand Down
3 changes: 3 additions & 0 deletions Resources/Private/Language/locallang_db.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<trans-unit id="tx_weather2_domain_model_currentweather.name">
<source>Name</source>
</trans-unit>
<trans-unit id="tx_weather2_domain_model_currentweather.description">
<source>Description</source>
</trans-unit>
<trans-unit id="tx_weather2_domain_model_currentweather.measure_timestamp">
<source>Measure Timestamp</source>
</trans-unit>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<trans-unit id="api_key">
<source>API-Key</source>
</trans-unit>
<trans-unit id="languages">
<source>Language IDs (comma separated list with IDs)</source>
</trans-unit>
<trans-unit id="clear_cache">
<source>Clear cache for pages (comma separated list with IDs)</source>
</trans-unit>
Expand Down
1 change: 1 addition & 0 deletions ext_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#
CREATE TABLE tx_weather2_domain_model_currentweather (
name varchar(255) DEFAULT '' NOT NULL,
description varchar(255) DEFAULT '' NOT NULL,
measure_timestamp int(11) DEFAULT '0' NOT NULL,
temperature_c double(4,2) DEFAULT '0.0' NOT NULL,
pressure_hpa double(4,2) DEFAULT '0' NOT NULL,
Expand Down