Have been ill for some time, but now I’m alright. I have now updated MPTtree to version 0.1.6 (see top post) and
I have also made a simple wiki as an example (quite hastily done, can contain errors):
controllers/wiki.php:
<?php
class Wiki extends Controller{
function Wiki(){
parent::Controller();
$this->load->database();
$this->load->MPTT('wiki_tree', 'pages');
$this->load->helper('url');
}
function _remap($method){
switch ($method) {
case 'add':
$this->add();
break;
case 'edit':
$this->edit();
break;
case 'del':
$this->del();
break;
default:
$this->index();
break;
}
}
function index(){
// load the segments and remove the "wiki" one
$segs = $this->uri->segment_array();
array_shift($segs);
// find the page with xpath
$page = $this->pages->xpath($segs);
if($page != false){
$this->_show_page($page,$segs);
}
else{
if(count($segs) == 0){
// no root, add one
$new_node->set('title','Default title');
$new_node->set('contents','Default Text');
$new_node->set('date',time());
$new_node->insert_as_root();
$this->_show_page($parent->get_all(),$segs);
return;
}
// page does not exists
$data['title'] = '404 Error: Page does not exist';
$data['contents'] = 'The page you requested cannot be found';
$this->load->view('wiki',$data);
}
}
function edit(){
if($this->input->post('edit') == true && $this->input->post('page') !== false){
$data['contents'] = $this->input->post('contents');
$data['title'] = $this->input->post('title');
$data['date'] = time();
// commit changes
$this->db->where('id',$this->input->post('page'));
$this->db->update('wiki_tree',$data);
// load data to display edited page
$page = $this->pages->get_ORM_byid($this->input->post('page'));
$this->_show_page($page->get_all(),$page->path());
}
else{
// display edit form
$page = $this->pages->get_node($this->uri->segment(3));
$page['edit'] = true;
$this->load->view('wiki',$page);
}
}
function add(){
$parent = $this->pages->get_ORM($this->uri->segment(3));
$new_node = $this->pages->new_ORM(); // creates a new empty object
// save data
$new_node->set('title','Default title');
$new_node->set('contents','Default Text');
$new_node->set('date',time());
$new_node->insert_as_first_child_of($parent);
// load data to display parent page
$this->_show_page($parent->get_all(),$parent->path());
}
function del(){
$page = $this->pages->get_ORM($this->uri->segment(3));
if($page){
$page->delete();
}
redirect('/wiki');
}
function _show_page($page,$segs = array()){
$page['edit_link'] = array('wiki','edit',$page['lft']);
$page['del_link'] = array('wiki','del',$page['lft']);
$page['children'] = $this->pages->get_children($page['lft'], $page['rgt']);
$page['path'] = array_merge(array('wiki'),$segs);
$page['deleteable'] = true;
// just pass the result to the view
$this->load->view('wiki',$page);
}
}
?>
views/wiki.php:
<html>
<head>
<title>Wiki: <?php echo htmlentities($title) ?></title>
</head>
<body>
<h1><?php echo htmlentities($title) ?></h1>
<?php if (isset($children) && count($children)): ?>
<p>Children:<?php foreach ($children as $child): ?>
<?php echo anchor(array_merge($path,array($child['title'])),$child['title']) ?>
<?php endforeach ?></p>
<?php endif ?>
<p><?php echo anchor(array('wiki','add',$lft),'Add Child') ?>
<hr />
<?php if (isset($edit) && $edit == true): ?>
<form method="POST" action="<?php echo site_url(array('wiki','edit')) ?>">
<input type="hidden" name="edit" value="true" />
<input type="hidden" name="page" value="<?php echo $id ?>" />
Title:<br />
<input type="text" name="title" value="<?php echo $title ?>"><br />
Content: <br />
<textarea name="contents" rows="20" cols="60"><?php echo htmlentities($contents) ?></textarea>
<br />
<input type="submit" value="Save" />
</form>
<?php else: ?>
<?php echo $contents; ?>
<?php endif ?>
<hr />
<?php if(isset($date)): ?>
<em>Last Modified: <?php echo date('Y-m-d H:i:s',$date) ?></em>
<?php endif ?>
<?php echo isset($edit_link) ? ' - ' . anchor($edit_link,'Edit Page') : '' ?>
- <?php echo isset($deleteable) && $deleteable == true ? anchor($del_link,'Delete Page') : '' ?> - <a href="<?php echo site_url(array('wiki')) ?>">Home</a>
</body>
</html>
Database table:
wiki_tree
columns:
id unsigned int auto increment
lft unsigned int
rgt unsigned int
title varchar 45
contents text
date unsigned int