-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
537 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace Cuttlefish\Blog; | ||
|
||
use Cuttlefish\Controller; | ||
use Cuttlefish\Html; | ||
use Cuttlefish\App; | ||
|
||
class ControllerAdmin extends Controller | ||
{ | ||
public array $allowed_methods = array( | ||
'index' => 'Overview', | ||
'clearCache' => 'Clear cache', | ||
'generateSite' => 'Generate static site', | ||
'logout' => 'Logout', | ||
); | ||
|
||
// admin section does not use content files | ||
protected string $contents; | ||
|
||
protected function isAllowedMethod($action): bool | ||
{ | ||
return array_key_exists($action, $this->allowed_methods); | ||
} | ||
|
||
protected function showTasks(): string | ||
{ | ||
$output = '<ul>'; | ||
$am = $this->allowed_methods; | ||
array_shift($am); | ||
foreach ($am as $key => $value) : | ||
$Url = new Cuttlefish\Url("/admin/$key"); | ||
$output .= sprintf('<li><a href="%s">%s</a></li>', $Url->url_absolute, $value); | ||
endforeach; | ||
|
||
$output .= '</ul>'; | ||
|
||
return $output; | ||
} | ||
|
||
protected function showLogin(): string | ||
{ | ||
return App::getInstance()->Security->login(); | ||
} | ||
|
||
protected function clearCache() | ||
{ | ||
$App = App::getInstance(); | ||
$App->Security->maybeLoginRedirect(); | ||
|
||
return $App->Cache->clear(); | ||
} | ||
|
||
protected function generateSite(): void | ||
{ | ||
$App = App::getInstance(); | ||
$App->Security->maybeLoginRedirect(); | ||
echo $App->Cache->generateSite(); | ||
} | ||
|
||
|
||
/** | ||
* @return void | ||
*/ | ||
public function init() | ||
{ | ||
App::getInstance()->Cache->abort(); | ||
|
||
$action = ( isset($this->args[0]) ) ? $this->args[0] : 'index'; | ||
if ($this->isAllowedMethod($action)) { | ||
$this->contents = $this->$action(); | ||
} else { | ||
exit("Method $action is not allowed"); | ||
} | ||
|
||
parent::init(); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function view() | ||
{ | ||
parent::view(); | ||
|
||
$this->View = new Html($this->contents, array( | ||
'layout' => 'layout.php', | ||
'controller' => 'admin', | ||
'model' => 'page', | ||
)); | ||
} | ||
|
||
public function index(): string | ||
{ | ||
if (App::getInstance()->Security->isLoggedIn()) { | ||
return $this->showTasks(); | ||
} | ||
|
||
return $this->showLogin(); | ||
} | ||
|
||
public function logout() | ||
{ | ||
$App = App::getInstance(); | ||
$App->Security->maybeLoginRedirect(); | ||
|
||
return $App->Security->logout(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Cuttlefish\Blog; | ||
|
||
use Configuration; | ||
use Cuttlefish\Controller; | ||
use Cuttlefish\Files; | ||
use Cuttlefish\Html; | ||
|
||
class ControllerArchive extends Controller | ||
{ | ||
/** | ||
* @return void | ||
*/ | ||
public function records() | ||
{ | ||
$content_dir = Configuration::CONTENT_FOLDER . '/posts'; | ||
$Files = new Files(array( 'url' => $content_dir ), $this->ext); | ||
$this->records = $Files->files(); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function model() | ||
{ | ||
$this->Model = new ModelPost($this->records); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function view() | ||
{ | ||
parent::view(); | ||
$this->View = new Html($this->Model->contents, array( | ||
'layout' => 'layout.php', | ||
'controller' => 'archive', | ||
'model' => 'post', | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Cuttlefish\Blog; | ||
|
||
use Configuration; | ||
use Cuttlefish\Controller; | ||
use Cuttlefish\Filesystem; | ||
use Cuttlefish\Html; | ||
|
||
class ControllerError extends Controller | ||
{ | ||
// single errors page | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function records() | ||
{ | ||
$content_dir = Configuration::CONTENT_FOLDER . '/errors/'; | ||
$url = $content_dir . implode("/", $this->args) . '.' . $this->ext; | ||
$this->records = [ Filesystem::convertUrlToPath($url) ]; | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function model() | ||
{ | ||
$this->Model = new ModelPage($this->records); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function view() | ||
{ | ||
parent::view(); | ||
|
||
$this->View = new Html($this->Model->contents, array( | ||
'layout' => 'layout.php', | ||
'controller' => 'errors', | ||
'model' => 'page', | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Cuttlefish\Blog; | ||
|
||
use Configuration; | ||
use Cuttlefish\Controller; | ||
use Cuttlefish\Feed; | ||
use Cuttlefish\Files; | ||
|
||
class ControllerFeed extends Controller | ||
{ | ||
// single feed | ||
/** | ||
* @return void | ||
*/ | ||
public function records() | ||
{ | ||
$limit = Configuration::POSTS_HOMEPAGE; | ||
$content_dir = Configuration::CONTENT_FOLDER . '/posts'; | ||
$Records = new Files(array( 'url' => $content_dir ), $this->ext); | ||
$this->records = $Records->limit($limit + 5); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function model() | ||
{ | ||
$Model = new ModelPost($this->records); | ||
$this->Model = $Model->limit(10); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function view() | ||
{ | ||
parent::view(); | ||
$this->View = new Feed($this->Model->contents); | ||
} | ||
} |
Oops, something went wrong.