Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Knuth authored and Christian Knuth committed Mar 22, 2013
1 parent 1aea584 commit 101d282
Show file tree
Hide file tree
Showing 135 changed files with 13,649 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea/
10 changes: 10 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Enable rewrite engine and route requests to framework
RewriteEngine On

RewriteCond %{REQUEST_URI} \.ini$
RewriteRule \.ini$ - [R=404]

RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA]
16 changes: 16 additions & 0 deletions app/layout/model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Layout;

class Model extends \DB\Jig\Mapper {

/** @var \Base */
protected $f3;

public function __construct()
{
$this->f3 = \Base::instance();
parent::__construct($this->f3->get('DB'), 'layout.json');
}

}
41 changes: 41 additions & 0 deletions app/navigation/model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
Navigation Model
**/

namespace Navigation;

class Model extends \DB\Jig\Mapper {

/** @var \Base */
protected $f3;

public function __construct() {
$this->f3 = \Base::instance();
parent::__construct($this->f3->get('DB'),'navigation.json');
}

/**
* return a set of pages that belongs to a given menu
* @param $menu
* @return array
*/
public function getPages($menu) {
$this->load(array('@name = ?', $menu));
if(!$this->dry()) {
$page = new \Page\Model();
$pages = array();
foreach ($this->pages as $item) {
// $page->load(array('@_id = ? AND ( isset(@deleted) && @deleted = 0)', $item));
$page->load(array('@_id = ?', $item));
if (!$page->dry())
$pages[] = array('title' => $page->title, 'slug' => $page->slug);
$page->reset();
}
} else {
trigger_error('menu not found');
}
$this->reset();
return $pages;
}
}
35 changes: 35 additions & 0 deletions app/navigation/view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
Navigation Controller
**/
namespace Navigation;

class View {

static public function render($menu,$tmpl = 'navigation_main.html') {
$f3 = \Base::instance();
$nav = new Model();

$f3->set('menu', $nav->getPages($menu));
$f3->set('current_page_path',$f3->get('PARAMS.page'));
$content = \Template::instance()->render($f3->get('TMPL').$tmpl);
$f3->clear('menu');
return $content;
}

static public function renderTag($args)
{
$attr = $args['@attrib'];
$tmp = \Template::instance();
foreach ($attr as &$att)
$att = $tmp->token($att);
if (array_key_exists('menu', $attr)) {
$nav_code = '\Navigation\View::render(\''.$attr['menu'].'\');';
if (array_key_exists('tmpl', $attr))
$nav_code = "\Navigation\View::render('".$attr['menu']."','".$attr['tmpl']."');";
return '<?php echo '.$nav_code.' ?>';
}
}


}
256 changes: 256 additions & 0 deletions app/page/controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
<?php
/**
Page Controller
The contents of this file are subject to the terms of the GNU General
Public License Version 3.0. You may not use this file except in
compliance with the license. Any of the license terms and conditions
can be waived if you get permission from the copyright holder.
**/
namespace Page;

class Controller
{
protected
$data, // page meta data
$include, // template include
$content; // page contents

public function __construct() {
$this->data = array (
'title' => '',
'template' => ''
);
$this->include = '';
$this->displayEdit = false;
}

/*
* check if we run this from our own dev machine
* this wiki is not intended to be edited on the live server
*/
protected function checkEnviroment($f3,$params)
{
if(strtolower($f3->get('HOST')) == $f3->get('DOMAIN')) {

if( array_key_exists('marker',$params) && !empty($params['marker'])) {
$edit_link = $f3->get('REPO').'/edit/master/'.
$f3->get('MDCONTENT').$params['page'].'/'.$params['marker'].'.md';
} else {
$edit_link = $f3->get('REPO').'/'.$f3->get('MDCONTENT').$params['page'];
}
$f3->set('edit_link',$edit_link);
$this->include = $f3->get('TMPL').'fork.html';
return false;
}
return true;
}

/**
* display page form
* @param $f3
* @param $params
*/
public function edit($f3, $params)
{
$page = '';
if(array_key_exists('page',$params) && !empty($params['page'])) {
$page = $params['page'];
}

if(!$this->checkEnviroment($f3,$params))
return;

$layout = new \Layout\Model();
$model = new Model();

if(!empty($page)) {
$model->loadExistingPage($page);
if(!$model->dry()) {
$this->data = $model->cast();

$layout->load(array('@file = ?', $model->template));
if (!$layout->dry()) {
// set markdown file paths
$marker = array();
foreach ($layout->marker as $mk) {
$path = $f3->get('MDCONTENT').$page.'/'.$mk.'.md';
$marker[$mk] = (file_exists($path)) ? $f3->read($path) : '';
}
$f3->mset(array('layout' => $marker));
} else {
$f3->error('500', 'Layout not found');
}

}
}
$title = (array_key_exists('title',$this->data)) ? $this->data['title'] : '';
$this->data['title'] = 'Edit Page'.(($title)?': '.$title:'');

$f3->set('FORM_title', $title);
$f3->set('ACTION','edit/'.$page);
if(!$model->dry()) {
$f3->set('backend_layout',$f3->get('TMPL').'backend/'.$model->template);
$f3->set('REQUEST.template', $model->template);
}
$layout->reset();
// load template list
$templates = array();
foreach($layout->find() as $item)
$templates[$item->file] = $item->name;
$f3->set('templates', $templates);

$this->include = $f3->get('TMPL').'edit.html';
}

/**
* save submitted page data
* @param $f3
* @param $params
* @return bool
*/
public function save($f3, $params)
{
if(!$this->checkEnviroment($f3,$params))
return;

$web = \Web::instance();
$model = new Model();
$new = true;
if (empty($params['page'])) {
$slug = $web->slug($f3->get('POST.title'));
$model->load(array('@slug = ?', $slug));
if (!$model->dry())
$f3->error(500, 'Another page with same title already exists.');
} else {
$slug = $web->slug($params['page']);
$model->load(array('@slug = ?', $slug));
if (!$model->dry()) {
$new = false;
if ($model->slug != $web->slug($f3->get('POST.title'))) {
if (!$this->rename())
return false;
}
}
}

// find layout marker
$layout = new \Layout\Model();
$layout->load(array('@file = ?', $f3->get('POST.template')));
if (!$layout->dry()) {
// write markdown files
if (!is_writable($path = $f3->get('MDCONTENT')))
trigger_error('data folder is not writable: '.$f3->get('MDCONTENT'));
@mkdir($f3->get('MDCONTENT').$slug.'/');
$marker = array();
foreach ($layout->marker as $mk) {
$path = $f3->get('MDCONTENT').$params['page'].'/'.$mk.'.md';
$val = $f3->get('POST.'.$mk);
if ($f3->exists('POST.'.$mk) && !empty($val))
$f3->write($path, $val);
}
} else {
$f3->error('500', 'Layout not found');
}

// save page config
$model->title = $f3->get('POST.title');
$model->template = $f3->get('POST.template');
$model->slug = $slug;
$model->lang = 'en'; // TODO: support multilanguage
$model->save();

$this->data['title'] = 'page saved successfully';
$this->include = $f3->get('TMPL').'saved.html';
}

/**
* display page content
* @param $f3
* @param $params
*/
public function view($f3, $params)
{
$model = new \Page\Model();
$model->loadExistingPage($params['page']);

if ($model->dry())
$f3->error('404', 'The requested Page does not exist.');

$this->data = $model->cast();
// sub-template / page layout
if (
array_key_exists('template', $this->data) &&
!empty($this->data['template'])
) {
$layout = new \Layout\Model();
$layout->load(array('@file = ?', $model->template));
if(!$layout->dry()) {
// set markdown file paths
$marker = array();
foreach ($layout->marker as $mk)
$marker[$mk] = $f3->get('MDCONTENT').$params['page'].'/'.$mk.'.md';
$f3->mset(array('layout'=>$marker));
} else {
$f3->error('500', 'Layout not found');
}
$this->include = $f3->get('TMPL').'layout/'.$model->template;
} else {
$f3->error('500', 'No page layout template selected');
}
}

/**
* rename a page
*/
public function rename()
{
if(!$this->checkEnviroment($f3,$params))
return;

// TODO: check if page could be renamed
// check if new name has already been taken
// scan all other pages for links, that should be repaired and do it

// extra points: make it possible to remain a page
// that redirects with a 301 Error (Moved Permanently) to the new URI
\Base::instance()->error('501','renaming a page is not available.');
return false;
}

/**
* delete a page
*/
public function delete($f3,$params)
{
if(!$this->checkEnviroment($f3,$params))
return;

$model = new Model();
$model->loadExistingPage($params['page']);
if(!$model->dry()) {
$model->erase();
// TODO: delete directory contents
}
$this->content = '<div class="alert">Page has been deleted</div>';
$this->data['title'] = 'Deleting page '.$params['page'];
}

/**
* render view
*/
public function afterroute()
{
$f3 = \Base::instance();
$data['page'] = $this->data;

if (!empty($this->include))
$data['include'] = $this->include;
if (!empty($this->content))
$data['content'] = $this->content;

$view = new View();
echo $view->render($data, $f3->get('TMPL').'layout.html');
}

}
28 changes: 28 additions & 0 deletions app/page/model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
Page Model
The contents of this file are subject to the terms of the GNU General
Public License Version 3.0. You may not use this file except in
compliance with the license. Any of the license terms and conditions
can be waived if you get permission from the copyright holder.
**/
namespace Page;

class Model extends \DB\Jig\Mapper {

/**
* sync with table
*/
public function __construct()
{
parent::__construct(\Base::instance()->get('DB'), 'pages.json');
}

public function loadExistingPage($slug) {
// $this->load(array('@slug = ? AND (isset(@deleted) && @deleted = ?)', $slug, 0));
$this->load(array('@slug = ?', $slug));
}

}
Loading

0 comments on commit 101d282

Please sign in to comment.