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

findByCategoryIds() #81

Merged
merged 4 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 8 additions & 6 deletions boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use rex_be_controller;
use rex_config;
use rex_cronjob_manager;
use rex_cronjob_neues_publish;
use rex_cronjob_neues_sync;
use rex_csrf_token;
use rex_extension;
use rex_extension_point;
Expand All @@ -17,8 +19,8 @@
use rex_yform_manager_table;

if (rex_addon::get('cronjob')->isAvailable() && !rex::isSafeMode()) {
rex_cronjob_manager::registerType(\rex_cronjob_neues_publish::class);
rex_cronjob_manager::registerType(\rex_cronjob_neues_sync::class);
rex_cronjob_manager::registerType(rex_cronjob_neues_publish::class);
rex_cronjob_manager::registerType(rex_cronjob_neues_sync::class);
}

if (rex_addon::get('yform')->isAvailable() && !rex::isSafeMode()) {
Expand Down Expand Up @@ -94,7 +96,7 @@ static function ($a) {
$category_ids = array_filter(array_map('intval', explode(',', $a['value'])));

foreach ($category_ids as $category_id) {
/** @var $neues_category neues_category */
/** @var neues_category $neues_category */
$neues_category = Category::get($category_id);
if ($neues_category) {
$return[] = '<a href="' . rex_url::backendPage('neues/category', $params) . '">' . $neues_category->getName() . '</a>';
Expand All @@ -106,13 +108,13 @@ static function ($a) {
}
});

if (rex::isBackend() && \rex_addon::get('neues') && \rex_addon::get('neues')->isAvailable() && !rex::isSafeMode()) {
if (rex::isBackend() && rex_addon::get('neues') && rex_addon::get('neues')->isAvailable() && !rex::isSafeMode()) {
$addon = rex_addon::get('neues');
$pages = $addon->getProperty('pages');

if($_REQUEST) {
if ($_REQUEST) {
$_csrf_key = rex_yform_manager_table::get('rex_neues_entry')->getCSRFKey();

$token = rex_csrf_token::factory($_csrf_key)->getUrlParams();

$params = [];
Expand Down
6 changes: 3 additions & 3 deletions install.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use rex;
use rex_addon;
use rex_article;
use rex_config;
use rex_file;
use rex_media;
Expand All @@ -23,7 +24,6 @@
$sql->setQuery('UPDATE ' . rex::getTable('neues_entry') . ' SET uuid = uuid() WHERE uuid IS NULL OR uuid = ""');

require_once __DIR__ . '/install/update_scheme.php';

}

if (!rex_media::get('neues_entry_fallback_image.png')) {
Expand Down Expand Up @@ -53,12 +53,12 @@
if (false === rex_config::get('neues', 'url_profile', false)) {
$rex_neues_category = array_filter(rex_sql::factory()->getArray("SELECT * FROM rex_url_generator_profile WHERE `table_name` = '1_xxx_rex_neues_category'"));
if (!$rex_neues_category) {
$query = \str_replace("999999", \rex_article::getSiteStartArticleId(), rex_file::get(__DIR__ . '/install/rex_url_profile_neues_category.sql'));
$query = str_replace('999999', rex_article::getSiteStartArticleId(), rex_file::get(__DIR__ . '/install/rex_url_profile_neues_category.sql'));
rex_sql::factory()->setQuery($query);
}
$rex_neues_entry = array_filter(rex_sql::factory()->getArray("SELECT * FROM rex_url_generator_profile WHERE `table_name` = '1_xxx_rex_neues_entry'"));
if (!$rex_neues_entry) {
$query = \str_replace("999999", \rex_article::getSiteStartArticleId(), rex_file::get(__DIR__ . '/install/rex_url_profile_neues_entry.sql'));
$query = str_replace('999999', rex_article::getSiteStartArticleId(), rex_file::get(__DIR__ . '/install/rex_url_profile_neues_entry.sql'));
rex_sql::factory()->setQuery($query);
}
/* URL-Profile wurden bereits einmal installiert, daher nicht nochmals installieren und Entwickler-Einstellungen respektieren */
Expand Down
2 changes: 0 additions & 2 deletions install/update_scheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,3 @@
->ensureColumn(new rex_sql_column('category_id', 'int(10) unsigned'))
->ensureIndex(new rex_sql_index('entry_id_category_id', ['entry_id', 'category_id'], rex_sql_index::UNIQUE))
->ensure();

?>
4 changes: 0 additions & 4 deletions lib/neues.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

namespace FriendsOfRedaxo\Neues;

use rex;
use rex_fragment;
use rex_pager;
use rex_sql;
use rex_sql_column;
use rex_sql_index;
use rex_sql_table;

class Neues
{
Expand Down
32 changes: 32 additions & 0 deletions lib/neues_entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use rex_yform_manager_collection;
use rex_yform_manager_dataset;

use function is_string;

/**
* Class neues_entry.
*
Expand Down Expand Up @@ -430,6 +432,36 @@ public static function findByCategory(?int $category_id = null, int $status = 1)
return $query->find();
}

/**
* Findet Einträge nach mehreren Kategorien.
* Finds entries by multiple Categories.
*
* @param string|array|null $category_ids Die IDs der Kategorien als String oder Array. / The IDs of the Categories as a String or Array.
* @param int $status Der Status der Einträge. / The status of the entries.
* @return rex_yform_manager_collection|null Die gefundenen Einträge oder null, wenn keine Einträge gefunden wurden. / The found entries or null if no entries were found.
*
* Beispiel / Example:
* $entries = FriendsOfRedaxo\Neues\Entry::findByCategories('1,2', 1);
*
* @api
*/
public static function findByCategories(string|array|null $category_ids = null, int $status = 1): ?rex_yform_manager_collection
{
$query = self::query()->where('status', $status, '>=');

if ($category_ids) {
// Wenn es ein String ist, in ein Array umwandeln
if (is_string($category_ids)) {
$category_ids = explode(',', $category_ids);
}

// whereInList anwenden
$query->whereInList('category_ids', $category_ids);
}

return $query->find();
}

/**
* Gibt die URL des Eintrags zurück.
* Returns the URL of the entry.
Expand Down
21 changes: 10 additions & 11 deletions lib/neues_restful_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace FriendsOfRedaxo\Neues;

use rex_yform_rest_route;
use rex_yform_rest;
use rex_yform_rest_route;

class RestfulApi {

public static function init() :void {
class RestfulApi
{
public static function init(): void
{
$rex_neues_entry_route = new rex_yform_rest_route(
[
'path' => '/neues/entry/5.0.0/',
Expand Down Expand Up @@ -92,9 +93,9 @@ public static function init() :void {
],
],
);

rex_yform_rest::addRoute($rex_neues_entry_route);

/* YForm Rest API */
$rex_neues_category_route = new rex_yform_rest_route(
[
Expand Down Expand Up @@ -136,9 +137,9 @@ public static function init() :void {
],
],
);

rex_yform_rest::addRoute($rex_neues_category_route);

/* YForm Rest API */
$rex_neues_author_route = new rex_yform_rest_route(
[
Expand Down Expand Up @@ -183,9 +184,7 @@ public static function init() :void {
],
],
);

rex_yform_rest::addRoute($rex_neues_author_route);

}

}
4 changes: 2 additions & 2 deletions lib/rex_cronjob_neues_publish.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php


use FriendsOfRedaxo\Neues\Entry;

use function count;

Expand All @@ -9,7 +9,7 @@ class rex_cronjob_neues_publish extends rex_cronjob
public function execute()
{
/* Collection von Neues-Einträgen, die noch nicht veröffentlicht sind, aber es sein sollten. (geplant) */
$neues_entry_to_publish = FriendsOfRedaxo\Neues\Entry::query()->where('status', 0)->where('publishdate', date('Y-m-d'), '<')->find();
$neues_entry_to_publish = Entry::query()->where('status', 0)->where('publishdate', date('Y-m-d'), '<')->find();
$neues_entry_to_publish->setValue('status', 1);
if (!$neues_entry_to_publish->save()) {
$this->setMessage(sprintf(rex_i18n::msg('neues_entry_publish_error'), count($neues_entry_to_publish)));
Expand Down
25 changes: 13 additions & 12 deletions lib/rex_cronjob_neues_sync.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use FriendsOfRedaxo\Neues\Author;
use FriendsOfRedaxo\Neues\Category;
use FriendsOfRedaxo\Neues\Entry;

class rex_cronjob_neues_sync extends rex_cronjob
{
private $rest_urls = ['category' => '/rest/neues/category/5.0.0/',
Expand Down Expand Up @@ -28,15 +32,14 @@ public function execute()
$data[$type] = json_decode($response->getBody(), true);
}

if(isset($data['category']['data'])) {

if (isset($data['category']['data'])) {
foreach ($data['category']['data'] as $category) {
$category = $category['attributes'];

// Überprüfe, ob UUID bereits in der Datenbank vorhanden ist
$neues_category = FriendsOfRedaxo\Neues\Category::query()->where('uuid', $category['uuid'])->findOne();
$neues_category = Category::query()->where('uuid', $category['uuid'])->findOne();
if (null === $neues_category) {
$neues_category = FriendsOfRedaxo\Neues\Category::create();
$neues_category = Category::create();
}

$neues_category->setValue('uuid', $category['uuid']);
Expand All @@ -51,15 +54,14 @@ public function execute()
}
}

if(isset($data['author']['data'])) {

if (isset($data['author']['data'])) {
foreach ($data['author']['data'] as $author) {
$author = $author['attributes'];

// Überprüfe, ob UUID bereits in der Datenbank vorhanden ist
$neues_author = FriendsOfRedaxo\Neues\Author::query()->where('uuid', $author['uuid'])->findOne();
$neues_author = Author::query()->where('uuid', $author['uuid'])->findOne();
if (null === $neues_author) {
$neues_author = FriendsOfRedaxo\Neues\Author::create();
$neues_author = Author::create();
}

$neues_author->setValue('uuid', $author['uuid']);
Expand All @@ -73,12 +75,11 @@ public function execute()
foreach ($data['entry']['data'] as $entry) {
$entry = $entry['attributes'];
// Überprüfe, ob UUID bereits in der Datenbank vorhanden ist
$neues_entry = FriendsOfRedaxo\Neues\Entry::query()->where('uuid', $entry['uuid'])->findOne();
$neues_entry = Entry::query()->where('uuid', $entry['uuid'])->findOne();
if (null === $neues_entry) {
$neues_entry = FriendsOfRedaxo\Neues\Entry::create();
$neues_entry = Entry::create();
}


$neues_entry->setValue('uuid', $entry['uuid']);
$neues_entry->setValue('name', $entry['name']);
$neues_entry->setValue('teaser', $entry['teaser']);
Expand Down Expand Up @@ -127,6 +128,6 @@ public function getParamFields()
],
];

return $fields;
return $fields;
}
}