ich habe gerade in meinem aktuellen Projekt ein ToDo-Modell ohne Relationen hinzugefügt.
Ist sehr praktisch, finde ich. Kann man in jedes Projekt schnell einkopieren und auch mal Solo benutzen.
Ist nur ne quick & dirty-Lösung, aber hilfreich. Ich hab's angehangen.
Hier der Code:
SQL:
- Code: Alles auswählen
CREATE TABLE IF NOT EXISTS `todos` (
`id` int(10) NOT NULL auto_increment,
`name` varchar(48) NOT NULL,
`link` varchar(255) NOT NULL,
`description` text NOT NULL,
`parent_id` int(10) unsigned NOT NULL,
`lft` int(10) unsigned NOT NULL,
`rght` int(10) unsigned NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`status` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Todo-Liste';
In "link" kann man den Pfad angeben, auf die die Aufgabe zutrifft (z.B. "/users/register/").
Status habe ich nur für 0 oder 1 vorgesehen.
Modell:
- Code: Alles auswählen
Controller:
- Code: Alles auswählen
- <?
class TodosController extends AppController {
var $name = 'Todos';
var $scaffold;
function index() {
$this->set('todos', $this->Todo->find('all', array('order' => 'lft ASC')));
}
function add() {
if (!empty($this->data)) {
$this->data['Todo']['name'] = nl2br($this->data['Todo']['name']);
$this->data['Todo']['description'] = nl2br($this->data['Todo']['description']);
if ($this->Todo->save($this->data)) {
$this->redirect(array('controller' => 'todos', 'action' => 'index'));
}
}
else {
// Ersten eintrag in Auswahl-Liste künstlich erzeugen
$all_todos = $this->Todo->generatetreelist(null, null, null, '-');
$all_todos[0] = 'Neuer Aufgabenzweig';
$this->set('todos', $all_todos);
}
}
function edit($id = NULL) {
if (!empty($this->data)) {
$this->data['Todo']['name'] = nl2br($this->data['Todo']['name']);
$this->data['Todo']['description'] = nl2br($this->data['Todo']['description']);
if ($this->Todo->save($this->data)) {
$this->redirect(array('controller' => 'todos', 'action' => 'index'));
}
else {
pr ($this->data);
}
}
else {
$this->data = $this->Todo->read();
$all_todos = $this->Todo->generatetreelist(null, null, null, '-');
$all_todos[0] = 'Neuer Aufgabenzweig';
$this->set('todos', $all_todos); }
}
function getDepth($id) {
$depth = $this->Todo->getpath($id);
return (sizeof($depth));
}
}
?>
Die Funktion "getDepth" geht auch bestimmt, in dem man das Array aus dem find() in der Funktion index() auswertet. Kann ja wer machen und den Code hier reinstellen
index.ctp:
- Code: Alles auswählen
- <!-- File: /app/views/todos/index.ctp -->
<?php
//pr ($todos);
?>
<h1>Aufgaben</h1>
<table>
<tr>
<th>Name</th>
<th>Link</th>
<th>Beschreibung</th>
</tr>
<?php
foreach ($todos as $todo) {
// todo: das hier eleganter machen, ohne zusätzliche Abfragen
$depth = $this->requestAction('/todos/getDepth/'.$todo['Todo']['id']);
// hintergrundfarbe errechnen
$color = dechex(255/$depth);
?>
<tr>
<td style="text-align: left;padding-left: <?php echo $depth*15; ?>px; background: #00<?php echo $color; ?>00;">
<?php echo $html->link($todo['Todo']['name'],"/todos/edit/".$todo['Todo']['id']); ?>
</td>
<td><?php echo $html->link($todo['Todo']['link'],$todo['Todo']['link']); ?></td>
<td>
<?php
echo $todo['Todo']['description'];
?>
</td>
</tr>
<?php
}
?>
</table>
<?php echo $html->link('Aufgabe hinzufügen',array('controller' => 'todos', 'action' => 'add'))?>
Ich habe den Baum eingerückt und farbig dargestellt. Geht auch bestimmt eleganter - tut aber seinen Dienst.
add.ctp:
- Code: Alles auswählen
- <!-- File: /app/views/todos/add.ctp -->
<h1>Aufgabe hinzufügen</h1>
<?php
echo $form->create('Todo');
echo $form->input('name');
echo $form->input('description');
echo $form->input('parent_id', array('options' => $todos, 'label' => 'Unteraufgabe von?'));
echo $form->input('status', array('type' => 'checkbox', 'label' => 'Erledigt?'));
echo $form->end('Aufgabe erstellen');
?>
edit.ctp:
- Code: Alles auswählen
- <!-- File: /app/views/todos/edit.ctp -->
<h1>Aufgabe ändern</h1>
<?php
echo $form->create('Todo');
echo $form->input('id', array('type' => 'hidden'));
echo $form->input('name');
echo $form->input('description');
echo $form->input('parent_id', array('options' => $todos, 'label' => 'Unteraufgabe von?'));
echo $form->input('status', array('type' => 'checkbox', 'label' => 'Erledigt?'));
echo $form->end('Aufgabe speichern');
?>
Viel Spaß damit,
Dogo