<nephtali>

A simple, flexible, fast, and security-focused PHP framework
Nephtali documentation

Basic to-dos admin

Build a little admin that manages and displays a list of to-do items using SQLite as the database.

Code

Markup in todos.php

<h2>Current to-dos</h2>
<p>To-dos ordered by priority grouping (high priorities listed first), and then alphabetically.</p>
<!--pipe:todo-->
<!--view:status-->
<p class="status_update"><!--data-->{message}<!--data--></p>
<!--view:status-->
<!--view:error-->
<p class="status_error">There was an error processing your request.  Please try again.</p>
<!--view:error-->
<!--view:feedback-->
<div class="status_error" id="suggestion_feedback">
   <h3>Some fields in your submission were invalid:</h3>
   <ul class="standard_list">
      <!--data-->
      <li>{message}</li>
      <!--data-->
   </ul>
</div>
<!--view:feedback-->
<!--pipe:todo-->
<!--pipe:todos-->
<!--view:default-->
<table>
   <thead>
      <tr>
         <th>&nbsp;</th>
         <th>To-do</th>
         <th>Priority</th>
      </tr>
   </thead>
   <tbody>
      <!--data-->
      <tr>
         <td><a href="todos-example.php?did={id|url}">Delete</a></td>
         <td>{todo|html}</td>
         <td><a href="todos-example.php?toggle_priority={priority|url}&amp;id={id|url}" title="Toggle priority for '{todo|attr}' todo item">{priority|html} (toggle)</a></td>
      </tr>
      <!--data-->
   </tbody>
</table>
<!--view:default-->
<!--view:error-->
<p>There was an error retrieving the current listing of to-dos.</p>
<!--view:error-->
<!--view:empty-->
<p>Currently there are no to-dos.</p>
<!--view:empty-->
<!--pipe:todos-->
<h2>Add a to-do item</h2>
<form action="/todos-example.php" method="post" class="validation">
   <ul>
      <li>
         <label for="todo">To-do</label>
         <input type="text" id="todo" name="todo" maxlength="50" placeholder="To-do" />
      </li>
      <li>
         <label for="priority">Priority</label>
         <select id="priority" name="priority">
            <option>High</option>
            <option>Low</option>
         </select>
      </li>
      <li>
        <input type="submit" value="Add to-do item" style="float:none;" />
      </li>
   </ul>
</form>

PHP in nsite/todos.php

n\port\register_bulk($ports = array(
   'id' => array(
      'min_value' => 1,
      'max_value' => 10000000
   ),
   'todo' => array(
      'max_length' => 200,
      'filter' => n\constant\FILTER_TEXT
   ),
   'priority' => array(
      'whitelist' => array('High','Low')
   ),
   'toggle_priority' => array(
      'whitelist' => array('High','Low'),
      'formatter' => function($val) {
         // toggle value
         return ($val == 'High') ? "Low" : "High";
      }
   ),
   'did' => array(
      'min_value' => 1,
      'max_value' => 10000000
   )
));

n\pipe\register_pure_action($name = 'todo', $actions = array(
   'insert' => array(
      'io_func' => function($port_vals) {
         return n\sql\action\insert($table_name = 'todos', $inputs = $port_vals);
      },
      'signature' => array('todo','priority'),
      'message' => 'To-do item added.'
   ),
   'toggle_todo' => array(
      'io_func' => function($port_vals) {
         return n\sql\action\query($query = 'UPDATE todos SET priority = :toggle_priority WHERE id = :id', $inputs = $port_vals);
      },
      'signature' => array('id','toggle_priority'),
      'message' => 'To-do item priority toggled.'
   ),
   'delete' => array(
      'io_func' => function($port_vals) {
         return n\sql\action\delete($table_name = 'todos', $id = $port_vals['did']);
      },
      'signature' => array('did'),
      'message' => 'To-do item deleted.'
   )	
));

n\pipe\register_pure_display(
   $name = 'todos',
   $rows_func = function($port_vals) {
      return n\sql\source\query($query = 'SELECT id, todo, priority FROM todos ORDER by priority, todo');
   }
);
blog comments powered by Disqus