Skip to content

Commit

Permalink
Auto-add redirects (WIP under flag)
Browse files Browse the repository at this point in the history
  • Loading branch information
krazzer committed Sep 6, 2023
1 parent d6ea8b2 commit f0e1b69
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Classes/Phalcon/ConfigGroups/ApplicationConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @property bool storeMailForms
* @property bool pageCache
* @property bool hideServiceMailBranding
* @property bool autoredirect
*/
class ApplicationConfig extends Config
{
Expand Down
46 changes: 44 additions & 2 deletions src/Models/PageLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace KikCMS\Models;

use KikCMS\Classes\Phalcon\IniConfig;
use KikCMS\Objects\Redirect\RedirectService;
use KikCMS\Services\Pages\PageLanguageService;
use KikCMS\Services\Pages\UrlService;
use KikCmsCore\Classes\Model;

/**
Expand Down Expand Up @@ -42,15 +46,51 @@ public static function getById($id)
*/
public function beforeDelete()
{
$this->getDI()->get('pageLanguageService')->removeCache($this);
/** @var PageLanguageService $pageLanguageService */
$pageLanguageService = $this->getDI()->get('pageLanguageService');

$pageLanguageService->removeCache($this);
}

/**
* Create URL if needed
*/
public function beforeSave()
{
$this->getDI()->get('pageLanguageService')->checkAndUpdateSlug($this);
/** @var PageLanguageService $pageLanguageService */
$pageLanguageService = $this->getDI()->get('pageLanguageService');

$pageLanguageService->checkAndUpdateSlug($this);
}

/**
* Check if the slug has changed
*/
public function beforeUpdate()
{
/** @var IniConfig $config */
$config = $this->getDI()->get('config');

if( ! $config->application->autoredirect){
return;
}

if ( ! $this->hasChanged(self::FIELD_SLUG)) {
return;
}

$previousPageLanguage = self::getById($this->getId());

/** @var UrlService $urlService */
$urlService = $this->getDI()->get('urlService');

/** @var RedirectService $redirectService */
$redirectService = $this->getDI()->get('redirectService');

$previousUrlPath = $urlService->createUrlPathByPageLanguage($previousPageLanguage);
$urlPath = $urlService->createUrlPathByPageLanguage($this);

$redirectService->add($previousUrlPath, $urlPath, $this->getId());
}

/**
Expand All @@ -60,6 +100,8 @@ public function initialize()
{
parent::initialize();

$this->keepSnapshots(true);

$pageClass = $this->getDI()->get('websiteSettings')->getPageClass() ?: Page::class;

$this->belongsTo("page_id", $pageClass, "id", ["alias" => "page"]);
Expand Down
24 changes: 24 additions & 0 deletions src/Objects/Redirect/Redirect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace KikCMS\Objects\Redirect;

use KikCmsCore\Classes\Model;

class Redirect extends Model
{
public const TABLE = 'cms_redirect';
public const ALIAS = 'r';

public const FIELD_ID = 'id';
public const FIELD_PATH_FROM = 'path_from';
public const FIELD_PATH_TO = 'path_to';
public const FIELD_PAGE_LANGUAGE_ID = 'page_language_id';

/**
* Initialize relations
*/
public function initialize()
{
parent::initialize();
}
}
24 changes: 24 additions & 0 deletions src/Objects/Redirect/RedirectForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace KikCMS\Objects\Redirect;

use KikCMS\Classes\WebForm\DataForm\DataForm;

class RedirectForm extends DataForm
{
/**
* @inheritdoc
*/
public function getModel(): string
{
return Redirect::class;
}

/**
* @inheritdoc
*/
protected function initialize()
{
// add form code...
}
}
44 changes: 44 additions & 0 deletions src/Objects/Redirect/RedirectList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types=1);

namespace KikCMS\Objects\Redirect;

use KikCmsCore\Classes\ObjectList;

class RedirectList extends ObjectList
{
/**
* @inheritdoc
* @return Redirect|false
*/
public function current()
{
return parent::current();
}

/**
* @inheritdoc
* @return Redirect|false
*/
public function get($key)
{
return parent::get($key);
}

/**
* @inheritdoc
* @return Redirect|false
*/
public function getFirst()
{
return parent::getFirst();
}

/**
* @inheritdoc
* @return Redirect|false
*/
public function getLast()
{
return parent::getLast();
}
}
44 changes: 44 additions & 0 deletions src/Objects/Redirect/RedirectMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types=1);

namespace KikCMS\Objects\Redirect;

use KikCmsCore\Classes\ObjectMap;

class RedirectMap extends ObjectMap
{
/**
* @inheritdoc
* @return Redirect|false
*/
public function current()
{
return parent::current();
}

/**
* @inheritdoc
* @return Redirect|false
*/
public function get($key)
{
return parent::get($key);
}

/**
* @inheritdoc
* @return Redirect|false
*/
public function getFirst()
{
return parent::getFirst();
}

/**
* @inheritdoc
* @return Redirect|false
*/
public function getLast()
{
return parent::getLast();
}
}
46 changes: 46 additions & 0 deletions src/Objects/Redirect/RedirectService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types=1);

namespace KikCMS\Objects\Redirect;

use KikCMS\Classes\Phalcon\Injectable;
use Phalcon\Mvc\Model\Query\Builder;

class RedirectService extends Injectable
{
/**
* @param string $previousUrlPath
* @param string $urlPath
* @param int $pageLanguageId
* @return void
*/
public function add(string $previousUrlPath, string $urlPath, int $pageLanguageId)
{
$redirectMap = $this->getByPageLanguageId($pageLanguageId);

$redirect = new Redirect();

$redirect->path_from = $previousUrlPath;
$redirect->path_to = $urlPath;
$redirect->page_language_id = $pageLanguageId;

$redirect->save();

foreach ($redirectMap as $redirect) {
$redirect->path_to = $urlPath;
$redirect->save();
}
}

/**
* @param int $pageLanguageId
* @return RedirectMap
*/
public function getByPageLanguageId(int $pageLanguageId): RedirectMap
{
$query = (new Builder)
->from(Redirect::class)
->inWhere(Redirect::FIELD_PAGE_LANGUAGE_ID, [$pageLanguageId]);

return $this->dbService->getObjectMap($query, RedirectMap::class);
}
}
53 changes: 53 additions & 0 deletions src/Objects/Redirect/Redirects.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types=1);

namespace KikCMS\Objects\Redirect;

use KikCMS\Classes\DataTable\DataTable;

class Redirects extends DataTable
{
/**
* @inheritdoc
*/
public function getFormClass(): string
{
return RedirectForm::class;
}

/**
* @inheritdoc
*/
public function getLabels(): array
{
return ['redirect', 'redirects'];
}

/**
* @inheritdoc
*/
public function getModel(): string
{
return Redirect::class;
}

/**
* @inheritdoc
*/
public function getTableFieldMap(): array
{
return [
Redirect::FIELD_ID => 'Id',
Redirect::FIELD_PATH_FROM => 'Path_from',
Redirect::FIELD_PATH_TO => 'Path_to',
Redirect::FIELD_PAGE_LANGUAGE_ID => 'Page_language_id',
];
}

/**
* @inheritdoc
*/
public function initialize()
{
// nothing here...
}
}

0 comments on commit f0e1b69

Please sign in to comment.