Commit 9c224a8a authored by Yassine Doghri's avatar Yassine Doghri
Browse files

feat: add pages table to store custom instance pages (eg. legal-notice, cookie policy, etc.)

- add pages  migration, model and entity
- add page controllers
- update routes config to input page forms and page view in public
- fix markdow editor focus area
- show pages links in public side footer

closes #24
parent a1a28de7
Loading
Loading
Loading
Loading
+32 −14
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ $routes->setAutoRoute(false);
 */

$routes->addPlaceholder('podcastName', '[a-zA-Z0-9\_]{1,191}');
$routes->addPlaceholder('episodeSlug', '[a-zA-Z0-9\-]{1,191}');
$routes->addPlaceholder('slug', '[a-zA-Z0-9\-]{1,191}');

/**
 * --------------------------------------------------------------------
@@ -53,15 +53,6 @@ $routes->group(config('App')->installGateway, function ($routes) {
    ]);
});

// Public routes
$routes->group('@(:podcastName)', function ($routes) {
    $routes->get('/', 'Podcast/$1', ['as' => 'podcast']);
    $routes->get('(:episodeSlug)', 'Episode/$1/$2', [
        'as' => 'episode',
    ]);
    $routes->get('feed.xml', 'Feed/$1', ['as' => 'podcast_feed']);
});

// Route for podcast audio file analytics (/stats/podcast_id/episode_id/podcast_folder/filename.mp3)
$routes->add('stats/(:num)/(:num)/(:any)', 'Analytics::hit/$1/$2/$3', [
    'as' => 'analytics_hit',
@@ -80,10 +71,6 @@ $routes->group(
            'as' => 'admin',
        ]);

        $routes->get('my-podcasts', 'Podcast::myPodcasts', [
            'as' => 'my-podcasts',
        ]);

        // Podcasts
        $routes->group('podcasts', function ($routes) {
            $routes->get('/', 'Podcast::list', [
@@ -201,6 +188,27 @@ $routes->group(
            });
        });

        // Pages
        $routes->group('pages', function ($routes) {
            $routes->get('/', 'Page::list', ['as' => 'page-list']);
            $routes->get('new', 'Page::create', [
                'as' => 'page-create',
            ]);
            $routes->post('new', 'Page::attemptCreate');

            $routes->group('(:num)', function ($routes) {
                $routes->get('/', 'Page::view/$1', ['as' => 'page-view']);
                $routes->get('edit', 'Page::edit/$1', [
                    'as' => 'page-edit',
                ]);
                $routes->post('edit', 'Page::attemptEdit/$1');

                $routes->add('delete', 'Page::delete/$1', [
                    'as' => 'page-delete',
                ]);
            });
        });

        // Users
        $routes->group('users', function ($routes) {
            $routes->get('/', 'User::list', [
@@ -294,6 +302,16 @@ $routes->group(config('App')->authGateway, function ($routes) {
    $routes->post('reset-password', 'Auth::attemptReset');
});

// Public routes
$routes->group('@(:podcastName)', function ($routes) {
    $routes->get('/', 'Podcast/$1', ['as' => 'podcast']);
    $routes->get('(:slug)', 'Episode/$1/$2', [
        'as' => 'episode',
    ]);
    $routes->get('feed.xml', 'Feed/$1', ['as' => 'podcast_feed']);
});
$routes->get('/(:slug)', 'Page/$1', ['as' => 'page']);

/**
 * --------------------------------------------------------------------
 * Additional Routing
+1 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ class Validation
        \CodeIgniter\Validation\FormatRules::class,
        \CodeIgniter\Validation\FileRules::class,
        \CodeIgniter\Validation\CreditCardRules::class,
        \App\Validation\Rules::class,
        \Myth\Auth\Authentication\Passwords\ValidationRules::class,
    ];

+111 −0
Original line number Diff line number Diff line
<?php

/**
 * @copyright  2020 Podlibre
 * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
 * @link       https://castopod.org/
 */

namespace App\Controllers\Admin;

use App\Models\PageModel;

class Page extends BaseController
{
    /**
     * @var \App\Entities\Page|null
     */
    protected $page;

    public function _remap($method, ...$params)
    {
        if (count($params) > 0) {
            if (!($this->page = (new PageModel())->find($params[0]))) {
                throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
            }
        }

        return $this->$method();
    }

    function list()
    {
        $data = [
            'pages' => (new PageModel())->findAll(),
        ];

        return view('admin/page/list', $data);
    }

    function view()
    {
        return view('admin/page/view', ['page' => $this->page]);
    }

    function create()
    {
        helper('form');

        return view('admin/page/create');
    }

    function attemptCreate()
    {
        $page = new \App\Entities\Page([
            'title' => $this->request->getPost('title'),
            'slug' => $this->request->getPost('slug'),
            'content' => $this->request->getPost('content'),
        ]);

        $pageModel = new PageModel();

        if (!$pageModel->save($page)) {
            return redirect()
                ->back()
                ->withInput()
                ->with('errors', $pageModel->errors());
        }

        return redirect()
            ->route('page-list')
            ->with(
                'message',
                lang('Page.messages.createSuccess', [
                    'pageTitle' => $page->title,
                ])
            );
    }

    function edit()
    {
        helper('form');

        replace_breadcrumb_params([0 => $this->page->title]);
        return view('admin/page/edit', ['page' => $this->page]);
    }

    function attemptEdit()
    {
        $this->page->title = $this->request->getPost('title');
        $this->page->slug = $this->request->getPost('slug');
        $this->page->content = $this->request->getPost('content');

        $pageModel = new PageModel();

        if (!$pageModel->save($this->page)) {
            return redirect()
                ->back()
                ->withInput()
                ->with('errors', $pageModel->errors());
        }

        return redirect()->route('page-list');
    }

    public function delete()
    {
        (new PageModel())->delete($this->page->id);

        return redirect()->route('page-list');
    }
}
+6 −13
Original line number Diff line number Diff line
@@ -31,22 +31,15 @@ class Podcast extends BaseController
        return $this->$method();
    }

    public function myPodcasts()
    public function list()
    {
        if (!has_permission('podcasts-list')) {
            $data = [
                'podcasts' => (new PodcastModel())->getUserPodcasts(user()->id),
            ];

        return view('admin/podcast/list', $data);
    }

    public function list()
    {
        if (!has_permission('podcasts-list')) {
            return redirect()->route('my-podcasts');
        }

        } else {
            $data = ['podcasts' => (new PodcastModel())->findAll()];
        }

        return view('admin/podcast/list', $data);
    }
@@ -155,7 +148,7 @@ class Podcast extends BaseController

        $db->transComplete();

        return redirect()->route('podcast-list');
        return redirect()->route('podcast-view', [$newPodcastId]);
    }

    public function edit()
+45 −0
Original line number Diff line number Diff line
<?php

/**
 * @copyright  2020 Podlibre
 * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
 * @link       https://castopod.org/
 */

namespace App\Controllers;

use App\Models\PageModel;

class Page extends BaseController
{
    /**
     * @var \App\Entities\Page|null
     */
    protected $page;

    public function _remap($method, ...$params)
    {
        if (count($params) > 0) {
            if (
                !($this->page = (new PageModel())
                    ->where('slug', $params[0])
                    ->first())
            ) {
                throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
            }
        }

        return $this->$method();
    }

    public function index()
    {
        // The page cache is set to a decade so it is deleted manually upon page update
        $this->cachePage(DECADE);

        $data = [
            'page' => $this->page,
        ];
        return view('page', $data);
    }
}
Loading