<?php // ************************************************************ // Very simple to-do list // John Watson // john {at} flagrantdisregard {dotcom} // ************************************************************ // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // ' // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // ************************************************************ // $Id: verysimpletodo.php,v 1.3 2005/09/21 21:19:01 John Exp $ // ************************************************************ if (!defined('PmWiki')) exit(); /* This recipe works by creating input forms that know how to modify the content of the current page by adding or removing a single item. Multiple forms on a single page are supported by identifying each form with a unique name (:todo-add-<uniquename> <above|below> <symbol>:) When items are added to the page, they are either added above or below the form and are prefixed with the symbols given in the last argument. For example: (:todo-add-list1 above *:) creates an input form that adds new lines above it prefixed with *, creating a bulletted list entry form. The <uniquename> can only contain letters and numbers. When an item is added using the form, markup is added as a suffix (:todo-delete-<id>:) that is rendered as a link. When clicked, the link will remove the item from the page. The <id> is generated as an md5 hash of the line being added but can be anything as long as it is unique and contains only letters nad numbers. This allows one to add the (:todo-delete-<id>:) markup to existing lines by hand if desired. To use this recipe, add the following lines to your local/config.php: // Very simple todo lists //$TodoDeleteText = "delete"; //$TodoAddText = "New item:"; include_once("$FarmD/cookbook/verysimpletodo.php"); $TodoDeleteText is the string used as the text of the delete link. $TodoAddText is the label placed in front of the input box for new items. Additionally, you can style the components using the classes "todoform" for the input form and "tododelete" for the delete link. For example: .todoform { color: #999; font-size: 80%; margin: 0; padding: 0; } .todoform input { font-weight: normal; border: 0; border-bottom: 1px solid #eee; width: 400px; } a.tododelete { color: #999 !important; text-transform: lowercase; font-size: 70%; text-decoration: none !important; } a:hover.tododelete { color: #999 !important; text-transform: lowercase; font-size: 70%; text-decoration: underline !important; } */ Markup("todo", "block", "/\(:todo-add-(\w+?) (above|below) (.+?):\)/", "-><form id='todoform$1' class='todoform' action='$ScriptUrl' method='post'>" .(isset($TodoAddText)?$TodoAddText:'New item:')." " ."<input type='hidden' name='todoform' value='$1' />" ."<input type='hidden' name='n' value='$pagename' />" ."<input type='hidden' name='action' value='todo-add' />" ."<input type='text' id='tododata' name='tododata' />" ."<script type='text/javascript'>document.getElementById('tododata').focus();</script>" ."</form>"); Markup("todo-delete", "block", "/\(:todo-delete-(.*?):\)/", "<a class='tododelete' href='$ScriptUrl?n=$pagename&todoitem=$1&action=todo-delete'>".(isset($TodoDeleteText)?$TodoDeleteText:'Delete')."</a>"); $HandleActions['todo-add'] = 'HandleAddTodo'; $HandleActions['todo-delete'] = 'HandleDeleteTodo'; // Add an item to a page function HandleAddTodo() { global $pagename; if (isset($_REQUEST['tododata'])) { $todoform = $_REQUEST['todoform']; $tododata = $_REQUEST['tododata']; $tododata = stripmagic($tododata); // Escape dollar signs in the data or you end up with weird preg_replace() issues $tododata = preg_replace('/\$/', '\\\$', $tododata); $page = RetrieveAuthPage($pagename, 'edit', true); $deletetag = '(:todo-delete-'.md5($tododata).':)'; if (!$page) Abort('?cannot read $pagename'); $new = $page; if (preg_match("/\(:todo-add-$todoform above (.+?):\)/", $page['text'])) { $new['text'] = preg_replace("/\(:todo-add-$todoform (above|below) (.+?):\)/", "$2 $tododata $deletetag\n(:todo-add-$todoform $1 $2:)", $page['text'], 1); } else if (preg_match("/\(:todo-add-$todoform below (.+?):\)/", $page['text'])) { $new['text'] = preg_replace("/\(:todo-add-$todoform (above|below) (.+?):\)/", "(:todo-add-$todoform $1 $2:)\n$2 $tododata $deletetag", $page['text'], 1); } PostPage($pagename, $page, $new); Redirect($pagename); } } // Remove an item from a page function HandleDeleteTodo() { global $pagename; if (isset($_REQUEST['todoitem'])) { $todoitem = $_REQUEST['todoitem']; $todoitem = stripmagic($todoitem); $page = RetrieveAuthPage($pagename, 'edit', true); if (!$page) Abort('?cannot read $pagename'); $new = $page; $new['text'] = preg_replace("/^.*?\(:todo-delete-$todoitem:\).*?\n/m", "", $page['text'], 1); PostPage($pagename, $page, $new); Redirect($pagename); } } ?>