<?php pravin_get_todo(); ?> in your template.
Version: 0.2
Author: Pravin Paratey
Author URI: http://www.DustyAnt.com
*/
/* Copyright 2007 Pravin Paratey (pravinp@gmail.com)
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
(at your option) 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
*/
class pravin {
// --------------------------------------------------------------------
// Responsible for installing the plugin
// --------------------------------------------------------------------
function todo_install() {
global $wpdb;
$table_name = $wpdb->prefix . 'pravin_todo';
// Check if the table is already present
if($wpdb->get_var("SHOW TABLES LIKE '" . $table_name ."'") != $table_name) {
$sql = "CREATE TABLE $table_name (
task_id bigint(20) not null auto_increment,
task_desc text not null default '',
task_owner bigint(20) not null,
assigned_by bigint(30) not null,
date_due bigint(11) not null default '0',
date_created bigint(11) not null default '0',
priority smallint(3) not null default '1',
notes text not null default '',
status tinyint(1) not null default '0',
unique key id(task_id)
);";
$results = $wpdb->query($sql);
$table_name = $wpdb->prefix . 'pravin_todo_options';
$sql = "CREATE TABLE $table_name (
option_id smallint(3) not null,
show_limit smallint(3) default '1',
show_completed tinyint(1) default '0',
show_duetoday tinyint(1) default '0',
show_spectrum tinyint(1) default '1',
hot_color tinytext,
cold_color tinytext,
format_date_due tinytext,
format_date_creat tinytext,
sort_field1 tinytext,
sort_order1 tinytext,
sort_field2 tinytext,
sort_order2 tinytext,
show_task_id tinyint(1) default '0',
show_task_owner tinyint(1) default '1',
show_assigned_by tinyint(1) default '0',
show_date_due tinyint(1) default '1',
show_date_creat tinyint(1) default '0',
show_priority tinyint(1) default '1',
show_notes tinyint(0) default '0',
column_order text default '',
timezone_offset tinytext,
unique key id(option_id)
);";
$results = $wpdb->query($sql);
// Add initial data
$sql = "INSERT INTO `$table_name` (show_limit, sort_field1, sort_order1, sort_field2, sort_order2) " .
" VALUES ('5', 'status', 'DESC', 'date_due', 'DESC')";
$results = $wpdb->query($sql);
}
}
// --------------------------------------------------------------------
// Adds the ToDo page under Manage
// --------------------------------------------------------------------
function todo_addpages() {
add_management_page('Manage your ToDo list', 'ToDo', 8, 'todo', array('pravin', 'todo_addoption'));
}
// --------------------------------------------------------------------
// Responsible for rendering the ToDo page under Manage
// --------------------------------------------------------------------
function todo_addoption()
{
global $wpdb;
$output_html = '
ToDo List
<$ToDoList$>
Add a ToDo
Display Options
Advanced
If you are upgrading from v0.1 to v0.2, use this to DROP your table. You will lost all ToDo data and settings! After you press the button, you will get an error. It is expected. Deactivate the plugin and activate it again to recreate your tables.
';
$table_name = $wpdb->prefix . 'pravin_todo';
$sql = "select * from $table_name order by status ASC, date_due ASC ";
$results = $wpdb->get_results($sql);
$alt = 0;
$todolist = '
';
}
$assigned_by = $wpdb->get_var("SELECT user_nicename FROM `$wpdb->users` WHERE ID = $result->assigned_by LIMIT 1");
$assigned_to = $wpdb->get_var("SELECT user_nicename FROM `$wpdb->users` WHERE ID = $result->task_owner LIMIT 1");
// 'F jS, Y @ H:i'
$todolist .= '
' . gmdate('m/d/y H:i', $result->date_due) . '
'.
'
' .$result->task_desc . '
' .
$assigned_to . '
' .
$result->priority . '
' .
'
' . $assigned_by . '
' .
'
';
}
}
$output_html = str_replace('<$ToDoList$>', $todolist, $output_html);
$month_array = array('January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December');
$cur_month = date('n');
$month_html = '';
for($i=1; $i < 13; $i++)
{
$month_html .= '';
}
$month_html = '' .
' , ' .
' @ ' .
' : ' .
' hrs';
$output_html = str_replace('<$ToDoAdd$>', $month_html, $output_html);
$users = $wpdb->get_results("SELECT * FROM `$wpdb->users`");
$userlist = '';
foreach($users as $user)
{
$userlist .= '';
}
$output_html = str_replace('<$UserList$>', $userlist, $output_html);
// Get Show Completed
$table_name = $wpdb->prefix . 'pravin_todo_options';
$op = $wpdb->get_var("SELECT show_limit FROM $table_name LIMIT 1");
$output_html = str_replace('<$ShowLimit$>', $op, $output_html);
$op = $wpdb->get_var("SELECT show_completed FROM $table_name LIMIT 1");
$option_html = '';
if(1 == $op)
{
$option_html = '';
}
else
{
$option_html = '';
}
$output_html = str_replace('<$ShowCompleted$>', $option_html, $output_html);
// Get Due Today
$op = $wpdb->get_var("SELECT show_duetoday FROM $table_name LIMIT 1");
$option_html = '';
if(1 == $op)
{
$option_html = '';
}
else
{
$option_html = '';
}
$output_html = str_replace('<$ShowDueToday$>', $option_html, $output_html);
// Get sort field 1
$op = $wpdb->get_var("SELECT sort_field1 FROM $table_name LIMIT 1");
$option_html = '';
if('date_due' == $op)
{
$option_html = '';
}
else if('priority' == $op)
{
$option_html = '';
}
else
{
$option_html = '';
}
$output_html = str_replace('<$SortBy1$>', $option_html, $output_html);
// Get sort field 2
$op = $wpdb->get_var("SELECT sort_field2 FROM $table_name LIMIT 1");
$option_html = '';
if('date_due' == $op)
{
$option_html = '';
}
else if('priority' == $op)
{
$option_html = '';
}
else
{
$option_html = '';
}
$output_html = str_replace('<$SortBy2$>', $option_html, $output_html);
// Get Sort Order 1
$op = $wpdb->get_var("SELECT sort_order1 FROM $table_name LIMIT 1");
$option_html = '';
if('asc' == $op)
{
$option_html = '';
}
else
{
$option_html = '';
}
$output_html = str_replace('<$SortOrder1$>', $option_html, $output_html);
// Get Sort Order 2
$op = $wpdb->get_var("SELECT sort_order2 FROM $table_name LIMIT 1");
$option_html = '';
if('asc' == $op)
{
$option_html = '';
}
else
{
$option_html = '';
}
$output_html = str_replace('<$SortOrder2$>', $option_html, $output_html);
echo $output_html;
}
}
// --------------------------------------------------------------------
// Widgetize!
// --------------------------------------------------------------------
function widget_pravin_todo() {
function widget_todo($args) {
// $args is an array of strings that help widgets to conform to
// the active theme: before_widget, before_title, after_widget,
// and after_title are the array keys. Default tags: li and h2.
extract($args);
// Each widget can store its own options. We keep strings here.
$title = $options['title'];
echo $before_widget . $before_title . 'ToDo' . $after_title;
echo pravin_get_todo();
echo $after_widget;
}
// This registers our widget so it appears with the other available
// widgets and can be dragged and dropped into any active sidebars.
register_sidebar_widget(array('ToDo Plugin', 'widgets'), 'widget_todo');
}
// --------------------------------------------------------------------
// Called when user clicks activate in the plugin menu
// --------------------------------------------------------------------
if (isset($_GET['activate']) && $_GET['activate'] == 'true') {
if (defined('WPINC') && strpos($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME']) > 0) {
add_action('init', array('pravin', 'todo_install'));
}
}
// --------------------------------------------------------------------
// Insert the mt_add_pages() sink into the plugin hook list for 'admin_menu'
// --------------------------------------------------------------------
add_action('admin_menu', array('pravin', 'todo_addpages'));
// Run our code later in case this loads prior to any required plugins.
add_action('widgets_init', 'widget_pravin_todo');
// --------------------------------------------------------------------
// Handle any add/delete/update requests
// --------------------------------------------------------------------
$name = $_POST["operation"];
if('add' == $name) {
global $wpdb;
// I don't know how to get current user :(
// wp-includes/pluggable-functions line 30 looks promising
$assigned_by = '1';
$table_name = $wpdb->prefix . 'pravin_todo';
$date_due = mktime($_POST["hour"], $_POST["minute"], 0, $_POST["month"], $_POST["day"], $_POST["year"]);
$sql = "INSERT INTO `$table_name` (task_desc, task_owner, assigned_by, date_due, date_created, priority, notes) VALUES( '" .
$wpdb->escape($_POST["task"]) . "' , '" .
$_POST['taskowner'] . "' , '" .
$assigned_by . "', '" .
$date_due . "', '" .
time() . "', '" .
$_POST['priority'] . "', '" .
$wpdb->escape($_POST['notes']) . "')";
$results = $wpdb->query($sql);
}
else if('update' == $name) {
global $wpdb;
$table_name = $wpdb->prefix . 'pravin_todo';
$sql = '';
$dowhat = $_POST["dowhat"];
$id = $_POST["id"];
if('delete' == $dowhat)
{
$sql = "DELETE FROM $table_name WHERE task_id=$id";
}
else if('done' == $dowhat)
{
$sql = "UPDATE $table_name SET status='1' WHERE task_id=$id";
}
else
{
$sql = "UPDATE $table_name SET status='0' WHERE task_id=$id";
}
$results = $wpdb->query($sql);
}
else if('drop' == $name) {
global $wpdb;
$table_name = $wpdb->prefix . 'pravin_todo';
$table_options = $table_name . '_options';
$wpdb->query("DROP TABLE `$table_name`, `$table_options`;");
}
else if('options' == $name) {
global $wpdb;
$table_name = $wpdb->prefix . 'pravin_todo_options';
$wpdb->query("UPDATE $table_name SET show_limit='" . $_POST['limit'] .
"', sort_field1='" . $_POST['sortby1'] .
"', sort_order1='" . $_POST['sortorder1'] .
"', sort_field2='" . $_POST['sortby2'] .
"', sort_order2='" . $_POST['sortorder2'] .
"', show_completed='" . $_POST['showcompleted'] .
"', show_duetoday='" . $_POST['showduetoday'] . "'");
}
function pravin_get_todo()
{
global $wpdb;
$table_name = $wpdb->prefix . 'pravin_todo';
$table_name_options = $wpdb->prefix . 'pravin_todo_options';
$sql = "SELECT * from `$table_name_options` LIMIT 1";
$options = $wpdb->get_results($sql);
$option = $options[0];
$where_clause = '';
if($option->show_completed == '0')
{
$where_clause = " WHERE status='0' ";
}
$sql = "SELECT * from `$table_name` $where_clause ORDER BY $option->sort_field1 $option->sort_order1, $option->sort_field2 $option->sort_order2 LIMIT $option->show_limit";
$results = $wpdb->get_results($sql);
$output_html = '