Skip to content

Commit

Permalink
Merge pull request #74 from alexplusde/rexstan
Browse files Browse the repository at this point in the history
rexstan lvl 6/8
  • Loading branch information
alxndr-w authored Sep 25, 2024
2 parents f7c8ef2 + b829a3b commit cbcc003
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 43 deletions.
2 changes: 1 addition & 1 deletion boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
/**
* nur im Backend.
*/
if (in_array(rex_be_controller::getCurrentPagePart(1), ['yform', 'wenns_sein_muss'])) {
if (in_array(rex_be_controller::getCurrentPagePart(1), ['yform', 'wenns_sein_muss'], true)) {
rex_extension::register('YFORM_DATA_LIST', Service::epYformDataList(...));
rex_extension::register('YFORM_DATA_LIST', Group::epYformDataList(...));
rex_extension::register('YFORM_DATA_LIST', Protocol::epYformDataList(...));
Expand Down
4 changes: 2 additions & 2 deletions fragments/wsm.js.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
consentModal: {
layout: '<?= Wsm::getConfig('consent_settings_layout', 'string', '') ?>',
position: '<?= Wsm::getConfig('consent_modal_position', 'string', '') ?>',
equalWeightButtons: <?= Wsm::getConfig('consent_modal_equal_weight_buttons', 'bool') ? 'true' : 'false' ?>,
flipButtons: <?= Wsm::getConfig('consent_modal_swap_buttons', 'bool') ? 'true' : 'false' ?>,
equalWeightButtons: <?= Wsm::getConfig('consent_modal_equal_weight_buttons', 'bool', null) ? 'true' : 'false' ?>,
flipButtons: <?= Wsm::getConfig('consent_modal_swap_buttons', 'bool', null) ? 'true' : 'false' ?>,
},
preferencesModal: {
layout: '<?= Wsm::getConfig('consent_settings_layout', 'string', '') ?>',
Expand Down
23 changes: 16 additions & 7 deletions lib/Api/ImThumbnail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Alexplusde\Wsm;

use InvalidArgumentException;
use rex_api_function;
use rex_file;
use rex_logger;
Expand Down Expand Up @@ -33,15 +34,17 @@ public function execute()
if ($response->isOk()) {
// liest die Informationen aus der Datei
$body = json_decode($response->getBody());
if (is_array($body) && array_key_exists(0, $body) && property_exists($body[0], 'thumbnail_large')) {
// TODO: Prüfen, ob noch notwendig
// $url = self::getImgFromVimeo($id, $body[0]->thumbnail_large);
rex_response::sendFile(rex_path::addonData('wenns_sein_muss', self::generateFileName('youtube', $id)), 'image/jpeg');
}
}
if (is_array($body) && array_key_exists(0, $body) && property_exists($body[0], 'thumbnail_large')) {
$url = self::getImgFromVimeo($id, $body[0]->thumbnail_large);
rex_response::sendFile(rex_path::addonData('wenns_sein_muss', self::generateFileName('youtube', $id)), 'image/jpeg');
}
}
}

if ('youtube' === $service) {
$url = self::getImgFromYoutube($id);
// TODO: Prüfen, ob noch notwendig
// $url = self::getImgFromYoutube($id);
rex_response::sendFile(rex_path::addonData('wenns_sein_muss', self::generateFileName('youtube', $id)), 'image/jpeg');
}

Expand Down Expand Up @@ -94,14 +97,20 @@ private static function getImgFromVimeo(string $id, string $url): string
return '';
}

/**
* @api
*/
private static function generateFileName(string $service, string $id, string $filetype = '.jpg'): string
{
return rex_string::normalize($service . '_' . $id) . $filetype;
}

/**
* @api
*/
public static function getThumbUrl(string $service, string $id, string $filetype = '.jpg'): string
{
$filename = self::generateFilename($service, $id, $filetype);
$filename = self::generateFileName($service, $id, $filetype);
$file = rex_path::addonData('wenns_sein_muss', $filename);

$timestamp = filemtime($file);
Expand Down
3 changes: 1 addition & 2 deletions lib/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Entry extends rex_yform_manager_dataset
{
/**
* @return array<array>
* @return array<string,string>[]
*/
public static function findEntriesArray(int $service_id): array
{
Expand All @@ -18,7 +18,6 @@ public static function findEntriesArray(int $service_id): array
$entries = self::query()->where('service_id', $service_id)->find();
$return = [];
foreach ($entries as $entry) {
/** @var Entry $entry */
$e = [];
$e['name'] = $entry->getName();
$e['service'] = $service->getName();
Expand Down
15 changes: 14 additions & 1 deletion lib/Lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

namespace Alexplusde\Wsm;

use InvalidArgumentException;
use LogicException;
use rex_article;
use rex_exception;
use rex_sql_exception;
use RuntimeException;

use function count;

Expand All @@ -11,7 +16,9 @@
class Lang
{
/* Erhalte das passende JSON für die Ausgabe der Sprache Frontend */

/**
* @return array<string,mixed>[]
*/
private static function getLangAsArray(): array
{
$return = [];
Expand Down Expand Up @@ -88,6 +95,9 @@ public static function getLangAsJson(): string|false
return @json_encode(self::getLangAsArray(), JSON_PRETTY_PRINT);
}

/**
* @return array<string,string>
*/
private static function getConsentModal(): array
{
$consentModal = [];
Expand Down Expand Up @@ -117,6 +127,9 @@ private static function getConsentModal(): array
return $consentModal;
}

/**
* @return array<string,string>
*/
private static function getPreferencesModal(): array
{
$preferencesModal = [];
Expand Down
14 changes: 6 additions & 8 deletions lib/Protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,22 +165,20 @@ static function ($a) {
$accepted_services = $a['list']->getValue('accepted_services');
$rejected_services = $a['list']->getValue('rejected_services');

$accepted_services = json_decode($accepted_services, true) ?? [];
$rejected_services = json_decode($rejected_services, true) ?? [];
$accepted_services = array_filter(json_decode($accepted_services, true) ?? [], fn (string $value): bool => strlen($value) > 0);
$rejected_services = array_filter(json_decode($rejected_services, true) ?? [], fn (string $value): bool => strlen($value) > 0);

$output = '';
$output .= '✅<br>';
// TODO: Überprüfen, was $services enthält und ggf. Tags / Badges daraus machen
foreach ($accepted_services as $category => $services) {
if (!empty($services)) {
$output .= '<small>' . $category . ': ' . implode(', ', $services) . '</small><br>';
}
$output .= '<small>' . $category . ': ' . $services . '</small><br>';
}

$output .= '❌<br>';
// TODO: Überprüfen, was $services enthält und ggf. Tags / Badges daraus machen
foreach ($rejected_services as $category => $services) {
if (!empty($services)) {
$output .= '<small>' . $category . ': ' . implode(', ', $services) . '</small><br>';
}
$output .= '<small>' . $category . ': ' . $services . '</small><br>';
}

return $output;
Expand Down
8 changes: 5 additions & 3 deletions lib/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public function getName(): string
}

/**
* @return rex_yform_manager_collection<self>
* @return rex_yform_manager_collection<static>
*/
public static function findScripts(): rex_yform_manager_collection
{
return self::query()->whereRaw('`status` = 1 AND `script` != "" AND (FIND_IN_SET(0, `rex_domain`) || FIND_IN_SET(' . Wsm::getDomainId() . ', `rex_domain`))')->joinRelation('group', 'g')->select('g.name', 'group_name')->find();
}

/**
* @return rex_yform_manager_collection<self>
* @return rex_yform_manager_collection<static>
*/
public static function findServices(int $group_id): rex_yform_manager_collection
{
Expand Down Expand Up @@ -131,7 +131,9 @@ public function setRexDomain(int $value): self
}

/* Cookies, LocalStorage und Tracking-Bilder */
/** @api */
/**
* @return rex_yform_manager_collection<Entry>
* @api */
public function getEntry(): ?rex_yform_manager_collection
{
return $this->getRelatedCollection('entry_ids');
Expand Down
65 changes: 46 additions & 19 deletions lib/Wsm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@

namespace Alexplusde\Wsm;

use InvalidArgumentException;
use LogicException;
use rex;
use rex_addon;
use rex_clang;
use rex_config;
use rex_exception;
use rex_extension_point;
use rex_file;
use rex_formatter;
use rex_i18n;
use rex_path;
use rex_sql_exception;
use rex_string;
use rex_type;
use rex_url;
use rex_view;
use rex_yrewrite;
use RuntimeException;
use ZipArchive;

use function count;
Expand All @@ -26,7 +31,7 @@

class Wsm
{
public const TABLES = [
protected const TABLES = [
'service' => 'rex_wenns_sein_muss_service',
'group' => 'rex_wenns_sein_muss_group',
'entry' => 'rex_wenns_sein_muss_entry',
Expand All @@ -39,13 +44,16 @@ public static function getDomainId(): int
$domain_id = 0; // default

if (rex_addon::get('yrewrite')->isAvailable() && !rex::isSafeMode()) {
$domain_id = rex_yrewrite::getCurrentDomain()->getId() ?: 0;
$domain_id = rex_yrewrite::getCurrentDomain()->getId() ?? 0;
}
return $domain_id;
}

/* Erhalte das passende JSON für die Ausgabe der Drittanbieter-Services im Frontend */

/**
* @return array<string,mixed>[]
*/
private static function getServicesAsArray(): array
{
$sections = [];
Expand Down Expand Up @@ -87,7 +95,10 @@ public static function getServicesAsJson(): string|false
return json_encode(self::getServicesAsArray(), JSON_PRETTY_PRINT);
}

/** @api */
/**
* @return array<string,mixed>
* @api
*/
public static function getIframeServicesAsArray(): array
{
$return = [];
Expand Down Expand Up @@ -119,7 +130,9 @@ public static function getIframeServicesAsJson(): string
}

/* Auswahl-Liste an Gruppen und deren Services */

/**
* @return array<string,mixed>
*/
private static function getCategoriesAsArray(): array
{
$categories = [];
Expand Down Expand Up @@ -174,18 +187,27 @@ public static function getRevisionTimestamp(): string
return self::getConfig('revision');
}

/**
* @api
*/
public static function getLastChangeTimestamp(): string
{
return self::getConfig('lastchange');
}

/**
* @api
*/
public static function newRevision(): void
{
if(self::backupRevision()) {
self::setConfig('revision', date('Y-m-d H:i:s'));
}
}

/**
* @api
*/
public static function newChange(): void
{
self::setConfig('lastchange', date('Y-m-d H:i:s'));
Expand All @@ -199,7 +221,7 @@ public static function yformDataChanged(rex_extension_point $ep): void
$table = $ep->getParam('table');
$table_name = $table->getTableName();
/* @var \rex_yform_manager_table $table */
if (in_array($table_name, self::TABLES)) {
if (in_array($table_name, self::TABLES, true)) {
self::newChange();
/* Link to Backend Page to update the revision: page=wenns_sein_muss/revision */
echo rex_view::success(rex_i18n::rawMsg('wsm_success_yform_data_changed', rex_url::currentBackendPage(['page' => 'wenns_sein_muss/revision'])));
Expand All @@ -220,13 +242,18 @@ public static function getConfig(string $key, string $vartype = 'string', mixed
return $default;
}

/**
* @api
*/
public static function setConfig(string $key, mixed $value): bool
{
return rex_config::set('wenns_sein_muss', $key, $value);
}

/* Sprog */

/**
* @api
*/
public static function getConfigText(string $key, string $lang_code = 'de'): string
{
$clang_id = 1;
Expand Down Expand Up @@ -264,34 +291,31 @@ public static function InitOnFirstBoot(): void
if (rex_addon::get('yrewrite')->isAvailable()) {
$domains = rex_yrewrite::getDomains();
foreach ($domains as $domain) {
/** @var rex_yrwewrite_domain $domain */
/** @var \rex_yrewrite_domain $domain */
$wsm_domain = Domain::create();
$wsm_domain->setDomainId($domain->getId());
$wsm_domain->setDomainId($domain->getId() ?? 0);
$wsm_domain->setPrivacyPolicyId($domain->getStartId());
$wsm_domain->setImprintId($domain->getStartId());
$wsm_domain->save();
}
}
}

/**
* @api
*/
public static function backupRevision(): bool
{
$group = Group::query()->find();
$groups = Group::query()->find();

$return = [];

foreach($group as $group) {
foreach($groups as $group) {
$return[$group->getName()] = $group->getData();
$services = Service::query()->where('group', $group->getId())->find();
if(empty($services)) {
continue;
}

foreach($services as $service) {
$return[$group->getName()]['service'][$service->getName()] = $service->getData();
$entries = Entry::query()->where('service_id', $service->getId())->find();
if(empty($entries)) {
continue;
}
foreach($entries as $entry) {
$return[$group->getName()]['service'][$service->getName()]['entry'][$entry->getName()] = $entry->getData();
}
Expand All @@ -304,7 +328,10 @@ public static function backupRevision(): bool
mkdir($backupFolder);
}
$backupFile = $backupFolder . '/' . $revisionNumber . '.json';
rex_file::put($backupFile, json_encode($return, JSON_PRETTY_PRINT));
$content = json_encode($return, JSON_PRETTY_PRINT);
if(false !== $content) {
rex_file::put($backupFile, $content);
}

$backupFile = $backupFolder . '/' . $revisionNumber . '.json';
$zipFile = $backupFolder . '/' . $revisionNumber . '.zip';
Expand All @@ -313,7 +340,7 @@ public static function backupRevision(): bool
$zip->addFile($backupFile, basename($backupFile));
$zip->close();
unlink($backupFile);
return $zipFile;
return true;
} else {
return false;
}
Expand Down

0 comments on commit cbcc003

Please sign in to comment.