Skip to content

Commit

Permalink
Merge pull request #16 from tbreuss/feat-dynamic-pages
Browse files Browse the repository at this point in the history
Feat dynamic pages
  • Loading branch information
tbreuss authored Jun 22, 2023
2 parents bd767a6 + 327ecbc commit 4a7dca9
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 9 deletions.
43 changes: 43 additions & 0 deletions components/PageUrlRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace app\components;

use yii\base\BaseObject;
use yii\web\Request;
use yii\web\UrlManager;
use yii\web\UrlRuleInterface;

final class PageUrlRule extends BaseObject implements UrlRuleInterface
{
/**
* @param UrlManager $manager
* @param Request $request
* @return array|bool
* @phpstan-return array<int, array<string, string>|string>|bool
* @throws \yii\base\InvalidConfigException
*/
public function parseRequest($manager, $request)
{
$pathInfo = $request->getPathInfo();
if ($pathInfo !== '') {
return ['site/page', ['url' => '/' . $pathInfo]];
}
return false; // this rule does not apply
}

/**
* @param UrlManager $manager
* @param string $route
* @param array<string, mixed> $params
* @return bool|string
*/
public function createUrl($manager, $route, $params)
{
if ($route === 'site/page') {
if (!empty($params['url'])) {
return ltrim($params['url'], '/');
}
}
return false; // this rule does not apply
}
}
4 changes: 3 additions & 1 deletion config/rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
'admin/<controller>' => 'admin/<controller>/index',
'admin' => 'admin',
// Pages
// '<view:[a-z0-9-/]+>' => 'page/index',
[
'class' => 'app\components\PageUrlRule',
],
'' => 'site/index',
];
15 changes: 15 additions & 0 deletions controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use app\models\Comment;
use app\models\Log404;
use app\models\Page;
use app\models\Rating;
use app\models\Redirect;
use app\models\Website;
Expand All @@ -12,6 +13,7 @@
use yii\web\Controller;
use app\models\ContactForm;
use app\models\Search;
use yii\web\NotFoundHttpException;
use yii\web\Response;

final class SiteController extends Controller
Expand Down Expand Up @@ -127,6 +129,19 @@ public function actionFeed(): Response
return $this->redirect(['feed/rss'], 301);
}

public function actionPage(string $url, int $preview = 0): string
{
$page = Page::findByUrl($url);

if ($page->deleted && empty($preview)) {
throw new NotFoundHttpException();
}

return $this->render('page', [
'page' => $page,
]);
}

public function actionError(): Response|string
{
try {
Expand Down
10 changes: 6 additions & 4 deletions models/Advertisement.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,12 @@ public function validateCurrency(string $attribute): void

public function validateBlacklist(string $attribute): void
{
$blacklistCsv = trim($_ENV['ADVERTISEMENT_BLACKLIST'] ?? '');
$blacklist = !empty($blacklistCsv) ? array_map('trim', explode(',', $blacklistCsv)) : [];
if (in_array($this->email, $blacklist)) {
$this->addError($attribute, 'E-Mail steht in unserer Blacklist.');
foreach (AdvertisementBadEmail::findRegexPatterns() as $regexPattern) {
$pattern = '/^' . $regexPattern . '$/i';
if (preg_match($pattern, $this->email)) {
$this->addError($attribute, 'E-Mail steht in unserer Blacklist.');
break;
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions models/AdvertisementBadEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace app\models;

use yii\db\ActiveRecord;

final class AdvertisementBadEmail extends ActiveRecord
{
public static function tableName()
{
return '{{advertisement_bad_email}}';
}

/**
* @return string[]
*/
public static function findRegexPatterns(): array
{
return self::find()
->select('regex_pattern')
->where(['deleted' => null])
->column();
}
}
22 changes: 22 additions & 0 deletions models/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace app\models;

use yii\db\ActiveRecord;

/**
* @property int $id
* @property string $url
* @property string $title
* @property string $abstract
* @property string $content
*/
final class Page extends ActiveRecord
{
public static function findByUrl(string $url): ?self
{
return self::find()
->where(['url' => $url])
->one();
}
}
1 change: 1 addition & 0 deletions models/Rating.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ final class Rating extends ActiveRecord
'fingering',
'glossar',
'lesson',
'page',
'website',
'video',
'joke',
Expand Down
2 changes: 2 additions & 0 deletions models/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ public function getContextText(): string
return 'Bass-Lehrbuch';
case 'lesson':
return 'Bass-Lektion';
case 'page':
return 'Seite';
case 'website':
return 'Bass-Website';
case 'video':
Expand Down
3 changes: 1 addition & 2 deletions modules/admin/models/LoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ public function rules(): array
/**
* Validates the password.
* This method serves as the inline validation for password.
* @phpstan-param array<string, string> $params
*/
public function validatePassword(string $attribute, array $params): void
public function validatePassword(string $attribute): void
{
if (!$this->hasErrors()) {
$user = $this->getUser();
Expand Down
2 changes: 1 addition & 1 deletion views/_partials/meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
foreach ($categories as $category) {
echo $delim;
echo app\helpers\Html::a($category['label'], $category['url'], ['class' => 'meta__link']);
$delim = ', ';
$delim = ' &gt; ';
}
?>
</div>
Expand Down
29 changes: 29 additions & 0 deletions views/site/page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* @var yii\web\View $this
* @var app\models\Page $page
*/

use yii\helpers\Markdown;
use app\widgets\Comments;
use app\widgets\Hits;
use app\widgets\Rating;
use app\widgets\SocialBar;

$this->title = $page->title;

// TODO breadcrumbs

?>

<h1><?= $page->title ?></h1>
<div class="markdown"><?= Markdown::process($page->content) ?></div>

<?= Rating::widget(["tableName" => "page", "tableId" => $page->id]) ?>

<?= SocialBar::widget(["id" => $page->id, "text" => $page->title]) ?>

<?= Comments::widget(["tableName" => "page", "tableId" => $page->id]) ?>

<?= Hits::widget(["tableName" => "page", "tableId" => $page->id]) ?>
2 changes: 1 addition & 1 deletion widgets/Hits.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
final class Hits extends Widget
{
/** @var string[] */
protected static array $ALLOWED = ['advertisement', 'album', 'blog', 'catalog', 'fingering', 'glossar', 'lesson', 'website', 'video'];
protected static array $ALLOWED = ['advertisement', 'album', 'blog', 'catalog', 'fingering', 'glossar', 'lesson', 'page', 'website', 'video'];
public string $tableName = '';
public int $tableId = 0;

Expand Down

0 comments on commit 4a7dca9

Please sign in to comment.