Pejman Moghadam / PHP

CakePHP 1.3 - CakeTooDoo task list example

Public domain


Preparing directory and files

  1. Prepare cakephp directory :

    cd /var/www/htdocs
    tar xf ~/cakephp-cakephp-1.3.12-0-g96a8d97.tar.gz
    mv cakephp-cakephp-8236c7e/ CakeTooDoo
    
  2. Change app/tmp owner and all it's subdirectories :

    chown -R apache CakeTooDoo/app/tmp
    
  3. Change Security.salt and Security.cipherSeed values in app/config/core.php:

    Configure::write('Security.salt', 
                     'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi');
    Configure::write('Security.cipherSeed', 
                     '76859309657453542496749683645');
    

Preparing Database

  1. Create a database :

    CREATE DATABASE caketoodoo;
    
  2. Create a table with plural name and a primary key named 'id' :

    USE caketoodoo;
    
    
    CREATE TABLE tasks (
    id int(10) unsigned NOT NULL auto_increment,
    title varchar(255) NOT NULL,
    done tinyint(1) default NULL,
    created datetime default NULL,
    modified datetime default NULL,
    PRIMARY KEY (id)
    );
    
  3. Rename database.php.default to database.php :

    mv app/config/database.php{.default,}
    
  4. edit app/config/database.php , remove $test array and change $default array:

    class DATABASE_CONFIG {
        var $default = array(
          'driver' => 'mysql',
          'persistent' => false,
          'host' => 'localhost',
          'login' => 'root',
          'password' => '',
          'database' => 'caketoodoo',
          'prefix' => '',
          //'encoding' => 'utf8',
        );
    }
    
  5. Check for errors

    http://localhost/CakeTooDoo
    

Writing Model

  1. Create a file, named same as database table name, but singular, in app/models/task.php :

    <?php
    class Task extends AppModel {
        var $name = 'Task';
    }
    ?>
    

Writing Controller

  1. Create a file, named same as database table name (plural) with controller.php, in app/controllers/taskscontroller.php :

    <?php
    class TasksController extends AppController {
        var $name = 'Tasks';
    }
    ?>
    

Writing View

  1. Add a method named index to TasksController in app/controllers/tasks_controller.php :

    <?php
    class TasksController extends AppController {
        var $name = 'Tasks';
        function index() {
            $this->set('tasks', $this->Task->find('all'));
        }
    }
    ?>
    
  2. Create a directory, named same as database table (plural) in app/views/ :

    mkdir app/views/tasks
    
  3. Create a file, named same as method name in controller with .ctp extension (Cake Template Page), in app/views/tasks/index.ctp:

    <h2>Tasks</h2>
    <?php if(empty($tasks)): ?>
        There are no tasks in this list <br>
    <?php else: ?>
    <table>
      <tr>
        <th>Title</th>
        <th>Status</th>
        <th>Created</th>
        <th>Modified</th>
        <th>Actions</th>
      </tr>
      <?php foreach ($tasks as $task): ?>
      <tr>
        <td>
        <?php echo $task['Task']['title'] ?>
        </td>
        <td>
        <?php
        if($task['Task']['done']) echo "Done";
        else echo "Pending";
        ?>
        </td>
        <td>
        <?php echo $task['Task']['created'] ?>
        </td>
        <td>
        <?php echo $task['Task']['modified'] ?>
        </td>
        <td>
        <!-- actions on tasks will be added later -->
        </td>
      </tr>
      <?php endforeach; ?>
    </table>
    <?php endif; ?>
    
  4. Check output

    http://localhost/CakeTooDoo/tasks/index
    

Adding a New Task

  1. Add a new action and $helper variabe to app/controllers/tasks_controller.php:

    var $helpers = array('Html', 'Form');
    
    
    function add() {
      if (!empty($this->data)) {
        $this->Task->create();
        if ($this->Task->save($this->data)) {
          $this->Session->setFlash('The Task has been saved');
          $this->redirect(array('action'=>'index'), null, true);
        } else {
          $this->Session->setFlash('Task not saved. Try again.');
        }
      }
    }
    
  2. Create a new file in app/views/tasks/add.ctp :

    <?php echo $form->create('Task');?>
    <fieldset>
    <legend>Add New Task</legend>
    <?php
      echo $form->input('title');
      echo $form->input('done');
    ?>
    </fieldset>
    <?php echo $form->end('Add Task');?>
    <?php echo $html->link('List All Tasks', array('action'=>'index')); ?>
    
  3. At the end of app/views/tasks/index.ctp , add :

    <?php echo $html->link('Add Task', array('action'=>'add')); ?>
    
  4. Check output

    http://localhost/CakeTooDoo/tasks/add
    

Editing a Task

  1. add 'edit' action to controller in app/controllers/tasks_controller.php :

    function edit($id = null) {
            if (!$id) {
                    $this->Session->setFlash('Invalid Task');
                    $this->redirect(array('action'=>'index'), null, true);
            }
            if (empty($this->data)) {
                    $this->data = $this->Task->find(array('id' => $id));
            } else {
                    if ($this->Task->save($this->data)) {
                            $this->Session->setFlash(
                                    'The Task has been saved');
                            $this->redirect(
                                    array('action'=>'index'), null, true);
                    } else {
                            $this->Session->setFlash(
                         'The Task could not be saved.Please, try again.');
                    }
            }
    }
    
  2. Create a view for edit function in app/views/tasks/edit.ctp :

    <?php echo $form->create('Task');?>
    <fieldset>
    <legend>Edit Task</legend>
    <?php
            echo $form->hidden('id');
            echo $form->input('title');
            echo $form->input('done');
    ?>
    </fieldset>
    <?php echo $form->end('Save');?>
    <?php echo $html->link('Add Task', 
                     array('action'=>'add')); ?><br />
    <?php echo $html->link('List All Tasks', 
                     array('action'=>'index')); ?><br />
    
  3. add a link to 'edit' action in app/views/tasks/index.ctp :

    <?php echo $html->link('Edit', 
          array('action'=>'edit', $task['Task']['id'])); ?>
    

Data Validation

  1. Add data validation to model in app/models/task.php :

    var $validate = array(
            'title' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Title of a task cannot be empty'
            )
    );
    

Deleting a task

  1. Add 'delete' action to app/controllers/tasks_controller.php :

    function delete($id = null) {
            if (!$id) {
                    $this->Session->setFlash('Invalid id for Task');
                    $this->redirect(array('action'=>'index'), null, true);
            }
            if ($this->Task->delete($id)) {
                    $this->Session->setFlash('Task #'.$id.' deleted');
                    $this->redirect(array('action'=>'index'), null, true);
            }
    }
    
  2. Add a links to 'delete' action in app/views/tasks/index.ctp :

    <?php echo $html->link('Delete', array('action'=>'delete',
                                   $task['Task']['id']), null, 
             'Are you sure you want to delete this task?'); ?>
    

Viewing Completed and Pending Tasks

  1. Change 'index' action in app/controllers/tasks_controller.php :

    function index($status = null) {
            if ($status == 'done')
                    $tasks = $this->Task->find('all', 
                        array('conditions' => array('Task.done' => '1')));
            else if ($status == 'pending')
                    $tasks = $this->Task->find('all',
                        array('conditions' => array('Task.done' => '0')));
            else
                    $tasks = $this->Task->find('all');
            $this->set('tasks',$tasks);
            $this->set('status',$status);
    }
    
  2. Add to appropriate links to app/views/tasks/index.ctp :

    <?php if($status): ?>
            <?php echo $html->link('List All Tasks', 
                        array('action' => 'index')); ?><br />
    <?php endif;?>
    <?php if($status != 'done'): ?>
            <?php echo $html->link('List Done Tasks', 
                       array('action' => 'index', 'done')); ?><br />
    <?php endif;?>
    <?php if($status != 'pending'): ?>
            <?php echo $html->link('List Pending Tasks', 
                       array('action' => 'index', 'pending')); ?><br />
    <?php endif;?>
    
  3. Add to appropriate links to app/views/tasks/edit.ctp :

    <?php echo $html->link('List Done Tasks', 
                     array('action' => 'index', 'done')); ?><br />
    <?php echo $html->link('List Pending Tasks', 
                     array('action' => 'index', 'pending')); ?><br />
    

Formating Date and Time

  1. Add a 'Time' helper to app/controllers/tasks_controller.php:

    var $helpers = array('Html', 'Form', 'Time');
    
  2. change 'created' and 'modified' show sction in app/views/tasks/index.ctp :

    <td>
    <?php echo $time->niceShort($task['Task']['created']) ?>
    </td>
    <td>
    <?php echo $time->niceShort($task['Task']['modified']) ?>
    </td>
    

Creating Homepage

  1. Create a file in app/views/pages/home.ctp :

    <h1>Welcome to CakeTooDoo</h1>
    <p>CakeTooDoo is a simple but useful application to keep a record
    of all the things that you need to do. Using CakeTooDoo, you can:</p>
    <ul>
            <li><?php echo $html->link('List all your tasks',
                    array('controller' => 'tasks', 
                          'action'=>'index')); ?></li>
            <li><?php echo $html->link('List your completed tasks',
                    array('controller' => 'tasks', 
                          'action'=>'index','done')); ?></li>
            <li><?php echo $html->link('List your pending tasks',
                    array('controller' => 'tasks', 
                          'action'=>'index','pending')); ?></li>
            <li><?php echo $html->link('Add new Tasks', 
                    array('controller' => 'tasks', 
                          'action'=>'add')); ?></li>
            <li>Edit tasks</li>
            <li>Delete tasks</li>
    </ul>
    

BY: Pejman Moghadam
TAG: php, cakephp
DATE: 2011-11-04 11:05:35


Pejman Moghadam / PHP [ TXT ]