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

Migrationshilfe - Fragmente und Beispiel #50

Merged
merged 4 commits into from
Dec 31, 2023
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea
/composer.lock
/.php-cs-fixer.cache
/vendor
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"redaxo/php-cs-fixer-config": "^2.0",
"friendsofphp/php-cs-fixer": "^3.14"
},
"replace": {
"psr/log": "*",
"psr/container": "*"
},
"scripts": {
"cs-dry": "php-cs-fixer fix -v --ansi --dry-run --config=.php-cs-fixer.dist.php",
"cs-fix": "php-cs-fixer fix -v --ansi --config=.php-cs-fixer.dist.php"
Expand Down
46 changes: 46 additions & 0 deletions docs/01_d_example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Beispiel und Fragmente

Wurde Neues mit dem URL-Addon installiert und entsprechend der [Anleitung](/redaxo/index.php?page=neues/docs&mdfile=06_url) konfiguriert, kannst du dir eine Beispielvorlage für die Ausgabe im Template ausgeben lassen.

Die Ausgabe erfolgt über Fragment-Dateien. Diese findest du im Ordner `fragments/neues`. Die Fragmente können nach Belieben angepasst werden. Weitere Informationen zu Fragmenten findest du in der [Redaxo-Dokumentation](https://redaxo.org/doku/main/fragmente).

Im Template, in dem du die Ausgabe von Neues realisieren möchtest, fügst du folgenden Code ein:

```php
<?php
// Voraussetzung: URL-Addon ist installiert und konfiguriert
$manager = Url\Url::resolveCurrent();

if($manager) {
// Ausgabe eines einzelnen Datensatzes
$postId = $manager->getDatasetId();
echo neues::getEntry($postId);
} else {
// Ausgabe einer Liste
echo neues::getList();
}
?>
```


## Methoden

### getEntry(int $postId)

Gibt einen einzelnen Datensatz aus. Benötigt wird die ID des Datensatzes.

```php
$entry = neues::getEntry(1);
```

### getList(int $rowsPerPage = 10, string $pageCursor = 'page')

Gibt eine Liste aller Datensätze als HTML aus.

- rowsPerPage: Anzahl der Datensätze pro Seite. Standard: `10`
- pageCursor: Name des GET-Parameters, der die aktuelle Seite enthält. Standard: `page`


```php
$list = neues::getList();
```
78 changes: 78 additions & 0 deletions fragments/neues/entry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/** @var rex_fragment $this */

/** @var neues_entry $post */
$post = $this->getVar('post');
?>

<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<article class="post">

<!-- Headline -->
<?php if ('' !== $post->getName()) : ?>
<h1 class="mb-2 fw-bold"><?= htmlspecialchars($post->getName()) ?></h1>
<?php endif ?>

<!-- Date -->
<?php if ($post->getPublishDate()) : ?>
<p class="blog-post-meta">
<?= $post->getFormattedPublishDate() ?>

<!-- Author -->
<?php if ($post->getAuthor()) : ?>
<?php if ($post->getAuthor()->getName()) : ?>
von <span><?= htmlspecialchars($post->getAuthor()->getName()) ?></span>
<?php elseif($post->getAuthor()->getNickname()): ?>
von <span><?= htmlspecialchars($post->getAuthor()->getNickname()) ?></span>
<?php endif ?>
<?php endif ?>

</p>
<?php endif ?>

<!-- Post Image -->
<?php if ('' !== $post->getImage()) : ?>
<?php
$media = rex_media::get($post->getImage());
?>
<?php if ($media) : ?>
<div class="ratio ratio-16x9 mb-3 mt-4">
<img src="<?= $media->getUrl() ?>" alt="<?= htmlspecialchars($media->getTitle()) ?>" class="h-100 object-fit-cover" width="200"/>
</div>
<?php endif ?>
<?php endif ?>


<!-- Post Content -->
<?php if ($post->getDescription()) : ?>
<div class="mt-5">
<?= $post->getDescription() ?>
</div>
<?php endif ?>

<!-- Post Images/Gallery -->
<?php if ($post->getImages()) : ?>
<div class="mt-5 row g-3">
<?php foreach ($post->getImages() as $image) : ?>
<?php
$media = rex_media::get($image);
$mediaUrl = rex_media_manager::getUrl('rex_media_medium', $image);
?>
<?php if ($media) : ?>
<div class="col-md-4">
<a href="<?= $media->getUrl() ?>" class="d-inline-flex ratio ratio-16x9 h-100">
<img src="<?= $mediaUrl ?>" alt="<?= htmlspecialchars($media->getTitle()) ?>" class="h-100 object-fit-cover" width="200"/>
</a>
</div>
<?php endif ?>
<?php endforeach ?>
</div>
<?php endif ?>

</article>

</div>
</div>
</div>
52 changes: 52 additions & 0 deletions fragments/neues/list-entry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/** @var rex_fragment $this */

/** @var neues_entry $post */
$post = $this->getVar('post');
?>

<div class="row g-0 border rounded overflow-hidden flex-md-row mb-4 shadow-sm h-md-250 position-relative h-100">
<div class="col p-4 d-flex flex-column position-static">

<!-- Categories -->
<?php if ($post->getCategories()) : ?>
<p class="d-inline-block mb-2 text-primary-emphasis">
<?= htmlspecialchars(implode(', ', $post->getCategories()->toKeyValue('id', 'name'))) ?>
</p>
<?php endif ?>

<!-- Title -->
<?php if ('' !== $post->getName()) : ?>
<h3 class="mb-0">
<a href="<?= rex_getUrl('', '', ['neues-entry-id' => $post->getId()]) ?>" class="stretched-link"><?= $post->getName() ?></a>
</h3>
<?php endif ?>

<!-- Date -->
<?php if ($post->getPublishDate()) : ?>
<div class="mb-2 text-body-secondary">
<?= $post->getFormattedPublishDate() ?>
</div>
<?php endif ?>

<!-- Teaser -->
<?php if ('' !== $post->getTeaser()) : ?>
<p class="card-text mb-0">
<?= htmlspecialchars($post->getTeaser()) ?>
</p>
<?php endif ?>
</div>

<!-- Image -->
<?php if ('' !== $post->getImage()) : ?>
<?php
$media = rex_media::get($post->getImage());
$mediaUrl = rex_media_manager::getUrl('rex_media_medium', $post->getImage());
?>
<?php if ($media) : ?>
<div class="col-auto d-none d-lg-block">
<img src="<?= $mediaUrl ?>" alt="<?= htmlspecialchars($media->getTitle()) ?>" class="h-100 object-fit-cover" width="200"/>
</div>
<?php endif ?>
<?php endif ?>
</div>
31 changes: 31 additions & 0 deletions fragments/neues/list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/** @var rex_fragment $this */

/** @var rex_pager $pager */
$pager = $this->getVar('pager');

/** @var rex_yform_manager_collection $posts */
$posts = $this->getVar('posts');
?>

<!-- Entry list -->
<div class="container">
<div class="row row-cols-1 row-cols-md-2 g-3">
<?php foreach ($posts as $post) : ?>
<div class="col">
<?php
$fragment = new rex_fragment();
$fragment->setVar('post', $post);
echo $fragment->parse('neues/list-entry.php');
?>
</div>
<?php endforeach ?>
</div>
</div>

<!-- Pagination -->
<?php
$fragment = new rex_fragment();
$fragment->setVar('pager', $pager);
echo $fragment->parse('neues/pagination.php');
?>
72 changes: 72 additions & 0 deletions fragments/neues/pagination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/** @var rex_fragment $this */

/** @var rex_pager $pager */
$pager = $this->getVar('pager');

$currentPage = $pager->getCurrentPage();
$prevPage = $pager->getPrevPage();
$firsPage = $pager->getFirstPage();
$nextPage = $pager->getNextPage();
$lastPage = $pager->getLastPage();
$articleLink = rex_article::getCurrent()->getUrl();
?>

<?php
/**
* The pagination snippet is based on the Bootstrap 5 pagination component.
* See https://getbootstrap.com/docs/5.2/components/pagination/.
*
* The pagination will only be displayed if there is more than one page.
*/
?>

<?php if ($pager->getPageCount() > 1) : $page = 0 ?>
<nav class="mt-5">
<ul class="pagination justify-content-center">
<!-- Previous -->
<li class="page-item <?= $prevPage === $currentPage ? 'disabled' : '' ?>">
<?php if (0 === $prevPage) : ?>
<a class="page-link d-inline-flex align-items-center h-100" href="<?= $prevPage === $currentPage ? '#' : $articleLink ?>">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M11.354 1.646a.5.5 0 0 1 0 .708L5.707 8l5.647 5.646a.5.5 0 0 1-.708.708l-6-6a.5.5 0 0 1 0-.708l6-6a.5.5 0 0 1 .708 0z"/>
</svg>
</a>
<?php else: ?>
<a class="page-link d-inline-flex align-items-center h-100" href="<?= $prevPage === $currentPage ? '#' : '?page=' . $prevPage ?>">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M11.354 1.646a.5.5 0 0 1 0 .708L5.707 8l5.647 5.646a.5.5 0 0 1-.708.708l-6-6a.5.5 0 0 1 0-.708l6-6a.5.5 0 0 1 .708 0z"/>
</svg>
</a>
<?php endif ?>
</li>

<!-- Pages -->
<?php for($i = 0; $i < $pager->getPageCount(); ++$i): ?>
<li class="page-item">
<?php if (0 === $page) : ?>
<a class="page-link <?= $currentPage === $i ? 'active' : '' ?>"
href="<?= $articleLink ?>">
<?= $page + 1 ?>
</a>
<?php else: ?>
<a class="page-link <?= $currentPage === $i ? 'active' : '' ?>"
href="?page=<?= $page ?>">
<?= $page + 1 ?>
</a>
<?php endif ?>
</li>
<?php $page = $i + 1; endfor ?>

<!-- Next -->
<li class="page-item <?= $nextPage === $currentPage ? 'disabled' : '' ?>">
<a class="page-link d-inline-flex align-items-center h-100"
href="<?= $nextPage === $currentPage ? '#' : '?page=' . $nextPage ?>">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M4.646 1.646a.5.5 0 0 1 .708 0l6 6a.5.5 0 0 1 0 .708l-6 6a.5.5 0 0 1-.708-.708L10.293 8 4.646 2.354a.5.5 0 0 1 0-.708z"/>
</svg>
</a>
</li>
</ul>
</nav>
<?php endif ?>
1 change: 1 addition & 0 deletions lang/de_de.lang
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ neues_author_be_user_id = REDAXO-Benutzer:in
neues_docs_a_intro = Einleitung
neues_docs_b_import_export = Import/Export
neues_docs_c_migration = Migration
neues_docs_d_example = Beispiel
neues_docs_settings = Einstellungen
neues_docs_neues_entry = Einträge
neues_docs_neues_category = Kategorien
Expand Down
1 change: 1 addition & 0 deletions lang/en_gb.lang
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ neues_author_be_user_id = REDAXO User
neues_docs_a_intro = Introduction
neues_docs_b_import_export = Import/Export
neues_docs_c_migration = Migration
neues_docs_d_example = Example
neues_docs_settings = Settings
neues_docs_neues_entry = Entries
neues_docs_neues_category = Categories
Expand Down
48 changes: 47 additions & 1 deletion lib/neues.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
<?php

class neues {}
class neues
{
/**
* Gibt eine HTML-Liste mit den Einträgen zurück.
* Diese Liste ist paginiert.
* Die Einträge werden nach dem Veröffentlichungsdatum absteigend sortiert.
* Der Output wird über ein Fragment erzeugt, diese können nach Belieben angepasst werden.
* Die Beispiel-Fragmente bauen auf Bootstrap 5 auf.
*
* @param int $rowsPerPage Anzahl der Einträge pro Seite
* @param string $pageCursor Name des GET-Parameters für die Seitennummer
* @return string HTML-Liste mit den Einträgen
*
* Beispiel / Example:
* echo neues::getList(2);
*/
public static function getList(int $rowsPerPage = 10, string $pageCursor = 'page'): string
{
$query = neues_entry::query()->orderBy('publishdate', 'desc');
$pager = new rex_pager($rowsPerPage, $pageCursor);
$posts = $query->paginate($pager);

$fragment = new rex_fragment();
$fragment->setVar('posts', $posts);
$fragment->setVar('pager', $pager);
return $fragment->parse('neues/list.php');
}

/**
* Gibt einen einzelnen Eintrag zurück.
* Der Output wird über ein Fragment erzeugt, diese können nach Belieben angepasst werden.
* Die Beispiel-Fragmente bauen auf Bootstrap 5 auf.
*
* @param int $postId ID des Eintrags
* @return string HTML des Eintrags
*
* Beispiel / Example:
* echo neues::getEntry(2);
*/
public static function getEntry(int $postId): string
{
$post = neues_entry::get($postId);
$fragment = new rex_fragment();
$fragment->setVar('post', $post);
return $fragment->parse('neues/entry.php');
}
}
4 changes: 2 additions & 2 deletions lib/neues_entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public function setName(string $name): self
*/
public function getAuthor(): ?neues_author
{
if ($this->getRelatedDataset('author')) {
return neues_author::get($this->getRelatedDataset('author')->getId());
if ($this->getRelatedDataset('author_id')) {
return neues_author::get($this->getRelatedDataset('author_id')->getId());
}
return null;
}
Expand Down