| 1 |
<?php
|
| 2 |
// $Id: todolist.module,v 1.8 2008/07/30 05:23:04 marvil07 Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_node_info().
|
| 6 |
*/
|
| 7 |
function todolist_node_info() {
|
| 8 |
return array(
|
| 9 |
'todolist' => array(
|
| 10 |
'name' => t('Todolist'),
|
| 11 |
'module' => 'todolist',
|
| 12 |
'description' => t('Create a new todolist.'),
|
| 13 |
)
|
| 14 |
);
|
| 15 |
}
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Implementation of hook_perm().
|
| 19 |
*/
|
| 20 |
function todolist_perm() {
|
| 21 |
return array('create todolist', 'edit own todolist', 'create tasks', 'complete tasks', 'uncomplete tasks', 'reorder tasks', 'delete tasks', 'edit own tasks', 'edit tasks');
|
| 22 |
}
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Implementation of hook_menu().
|
| 26 |
*/
|
| 27 |
function todolist_menu($may_cache) {
|
| 28 |
$items = array();
|
| 29 |
|
| 30 |
if ($may_cache) {
|
| 31 |
$items[] = array(
|
| 32 |
'path' => 'node/add/todolist',
|
| 33 |
'title' => t('Todolist'),
|
| 34 |
'access' => user_access('create todolist'),
|
| 35 |
);
|
| 36 |
$items[] = array(
|
| 37 |
'path' => 'todolist/create_task',
|
| 38 |
'callback' => 'todolist_create_task',
|
| 39 |
'access' => user_access('create tasks'),
|
| 40 |
'type' => MENU_CALLBACK,
|
| 41 |
);
|
| 42 |
$items[] = array(
|
| 43 |
'path' => 'todolist/edit_task',
|
| 44 |
'callback' => 'todolist_edit_task',
|
| 45 |
'access' => user_access('edit tasks'),
|
| 46 |
'type' => MENU_CALLBACK,
|
| 47 |
);
|
| 48 |
$items[] = array(
|
| 49 |
'path' => 'todolist/toggle_task',
|
| 50 |
'callback' => 'todolist_toggle_task',
|
| 51 |
'access' => TRUE,
|
| 52 |
'type' => MENU_CALLBACK,
|
| 53 |
);
|
| 54 |
$items[] = array(
|
| 55 |
'path' => 'todolist/reorder_task',
|
| 56 |
'callback' => 'todolist_reorder_task',
|
| 57 |
'access' => user_access('reorder tasks'),
|
| 58 |
'type' => MENU_CALLBACK,
|
| 59 |
);
|
| 60 |
$items[] = array(
|
| 61 |
'path' => 'todolist/delete_task',
|
| 62 |
'callback' => 'todolist_delete_task',
|
| 63 |
'access' => user_access('delete tasks'),
|
| 64 |
'type' => MENU_CALLBACK,
|
| 65 |
);
|
| 66 |
}
|
| 67 |
|
| 68 |
return $items;
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
* Implementation of hook_access().
|
| 73 |
*/
|
| 74 |
function todolist_access($op, $node) {
|
| 75 |
global $user;
|
| 76 |
|
| 77 |
if ($op == 'create') {
|
| 78 |
return user_access('create todolist');
|
| 79 |
}
|
| 80 |
|
| 81 |
if ($op == 'update' || $op == 'delete') {
|
| 82 |
if (user_access('edit own todolist') && ($user->uid == $node->uid)) {
|
| 83 |
return TRUE;
|
| 84 |
}
|
| 85 |
}
|
| 86 |
}
|
| 87 |
|
| 88 |
/**
|
| 89 |
* Implementation of hook_form().
|
| 90 |
*/
|
| 91 |
function todolist_form(&$node) {
|
| 92 |
return array(
|
| 93 |
'title' => array(
|
| 94 |
'#type' => 'textfield',
|
| 95 |
'#title' => t('Title'),
|
| 96 |
'#required' => TRUE,
|
| 97 |
'#default_value' => $node->title,
|
| 98 |
'#weight' => -4,
|
| 99 |
),
|
| 100 |
'body' => array(
|
| 101 |
'#type' => 'textarea',
|
| 102 |
'#title' => t('Description'),
|
| 103 |
'#required' => FALSE,
|
| 104 |
'#default_value' => $node->body,
|
| 105 |
),
|
| 106 |
);
|
| 107 |
}
|
| 108 |
|
| 109 |
/**
|
| 110 |
* Add todolist JavaScript library to the current page.
|
| 111 |
*/
|
| 112 |
function todolist_add_js() {
|
| 113 |
static $done;
|
| 114 |
if (!isset($done)) {
|
| 115 |
drupal_add_js(drupal_get_path('module', 'todolist') .'/todolist.js');
|
| 116 |
$done = TRUE;
|
| 117 |
}
|
| 118 |
}
|
| 119 |
|
| 120 |
/**
|
| 121 |
* Add todolist JavaScript library to the current page.
|
| 122 |
*/
|
| 123 |
function todolist_add_css() {
|
| 124 |
static $done;
|
| 125 |
if (!isset($done)) {
|
| 126 |
drupal_add_js(drupal_get_path('module', 'todolist') .'/todolist.js');
|
| 127 |
$done = TRUE;
|
| 128 |
}
|
| 129 |
}
|
| 130 |
|
| 131 |
/**
|
| 132 |
* Implementation of hook_view().
|
| 133 |
*/
|
| 134 |
function todolist_view(&$node, $teaser = FALSE, $page = FALSE) {
|
| 135 |
if ($page) {
|
| 136 |
jquery_interface_add();
|
| 137 |
todolist_add_js();
|
| 138 |
drupal_add_css(drupal_get_path('module', 'todolist') .'/todolist.css', 'module');
|
| 139 |
|
| 140 |
$node->content['todolist'] = array('#value' => theme('todolist', $node));
|
| 141 |
}
|
| 142 |
|
| 143 |
return node_prepare($node, $teaser);
|
| 144 |
}
|
| 145 |
|
| 146 |
/**
|
| 147 |
* Generate a Todolist.
|
| 148 |
*/
|
| 149 |
function theme_todolist(&$node) {
|
| 150 |
$results = db_query("SELECT tid, status, task FROM {todolist_task} WHERE nid = %d ORDER BY status, sort;", $node->nid);
|
| 151 |
while ($task = db_fetch_object($results)) {
|
| 152 |
$tasks[($task->status? 'complete' : 'incomplete')][] = theme('todolist_task', $task);
|
| 153 |
}
|
| 154 |
$output .= theme('item_list', $tasks['incomplete'], NULL, 'ul', array('id' => 'todolist-'. $node->nid, 'class' => 'todolist incomplete'));
|
| 155 |
$output .= drupal_get_form('todolist_add_task_form', $node);
|
| 156 |
$output .= theme('item_list', $tasks['complete'], NULL, 'ul', array('class' => 'todolist complete'));
|
| 157 |
|
| 158 |
return $output;
|
| 159 |
}
|
| 160 |
|
| 161 |
/**
|
| 162 |
* Generate a Todolist Task.
|
| 163 |
*/
|
| 164 |
function theme_todolist_task(&$task) {
|
| 165 |
return array(
|
| 166 |
'data' => '<input type="checkbox" name="task-'. $task->tid .'"'. ($task->status? 'checked' : '') .' /> '. ($task->status? '<a href="javascript:void(0)" title="Delete this task" class="delete" onclick="return TodoList.delete_task(\'task-'. $task->tid .'\')">Delete</a> <span>'. date('M d', $task->status) .'</span> ' : '') . $task->task,
|
| 167 |
'class' => 'task',
|
| 168 |
'id' => 'task-'. $task->tid,
|
| 169 |
);
|
| 170 |
}
|
| 171 |
|
| 172 |
/**
|
| 173 |
* Generate an Add Task form.
|
| 174 |
*
|
| 175 |
* @return Array $form
|
| 176 |
* Forms API form array.
|
| 177 |
*/
|
| 178 |
function todolist_add_task_form(&$node) {
|
| 179 |
return array(
|
| 180 |
'#action' => url('todolist/create_task'),
|
| 181 |
array(
|
| 182 |
'#type' => 'fieldset',
|
| 183 |
'#title' => t('Add new task'),
|
| 184 |
'#collapsible' => TRUE,
|
| 185 |
'#collapsed' => TRUE,
|
| 186 |
'task' => array(
|
| 187 |
'#type' => 'textfield',
|
| 188 |
'#title' => t('Task'),
|
| 189 |
'#default_value' => '',
|
| 190 |
'#maxlength' => 255,
|
| 191 |
'#required' => TRUE,
|
| 192 |
),
|
| 193 |
'submit' => array(
|
| 194 |
'#type' => 'submit',
|
| 195 |
'#value' => t('Add this task'),
|
| 196 |
),
|
| 197 |
'nid' => array(
|
| 198 |
'#type' => 'hidden',
|
| 199 |
'#value' => $node->nid,
|
| 200 |
),
|
| 201 |
),
|
| 202 |
);
|
| 203 |
}
|
| 204 |
|
| 205 |
/**
|
| 206 |
* Generate an Edit Task form.
|
| 207 |
*
|
| 208 |
* @return Array $form
|
| 209 |
* Forms API form array.
|
| 210 |
*/
|
| 211 |
function todolist_edit_task_form(&$task) {
|
| 212 |
return array(
|
| 213 |
'#action' => url('todolist/edit_task'),
|
| 214 |
array(
|
| 215 |
'task' => array(
|
| 216 |
'#type' => 'textarea',
|
| 217 |
'#default_value' => $task->task,
|
| 218 |
'#required' => TRUE,
|
| 219 |
),
|
| 220 |
'submit' => array(
|
| 221 |
'#type' => 'submit',
|
| 222 |
'#value' => t('Save this task'),
|
| 223 |
),
|
| 224 |
'tid' => array(
|
| 225 |
'#type' => 'hidden',
|
| 226 |
'#value' => $task->tid,
|
| 227 |
),
|
| 228 |
),
|
| 229 |
);
|
| 230 |
}
|
| 231 |
|
| 232 |
/**
|
| 233 |
* Notify the browser of a bad request.
|
| 234 |
*
|
| 235 |
* The request could not be understood by the server due to malformed syntax.
|
| 236 |
* The client SHOULD NOT repeat the request without modifications.
|
| 237 |
*/
|
| 238 |
function todolist_bad_request() {
|
| 239 |
drupal_set_header('Status:400 Bad Request');
|
| 240 |
exit;
|
| 241 |
}
|
| 242 |
|
| 243 |
/**
|
| 244 |
* Output JavaScript Content-Type headers.
|
| 245 |
*/
|
| 246 |
function todolist_ok_js() {
|
| 247 |
drupal_set_header('Status:200 OK');
|
| 248 |
drupal_set_header('Content-Type:text/javascript');
|
| 249 |
}
|
| 250 |
|
| 251 |
/**
|
| 252 |
* Escape single-quotes in a string.
|
| 253 |
*
|
| 254 |
* @param String $txt
|
| 255 |
* A string to be used in JavaScript.
|
| 256 |
* @return String
|
| 257 |
* An escaped string.
|
| 258 |
*/
|
| 259 |
function _todolist_js_safe($txt) {
|
| 260 |
$txt = str_replace("'", "\'", $txt);
|
| 261 |
$txt = preg_replace('/[\r\n]+/', '', $txt);
|
| 262 |
return $txt;
|
| 263 |
}
|
| 264 |
|
| 265 |
/**
|
| 266 |
* MENU_CALLBACK function for /todolist/create_task.
|
| 267 |
*/
|
| 268 |
function todolist_create_task() {
|
| 269 |
if (user_access('create tasks'))
|
| 270 |
if (isset($_REQUEST['nid']) && isset($_REQUEST['sort']) && isset($_REQUEST['task'])) {
|
| 271 |
db_query("INSERT INTO {todolist_task} SET nid = %d, `sort` = %d, task = '%s';", $_REQUEST['nid'], $_REQUEST['sort'], $_REQUEST['task']);
|
| 272 |
$id = 'task-'. db_result(db_query("SELECT tid FROM {todolist_task} ORDER BY tid DESC LIMIT 1;"));
|
| 273 |
todolist_ok_js(); ?>
|
| 274 |
TodoList.add_incomplete_list();
|
| 275 |
$('.todolist.incomplete').append('<li class="task" id="<?php print $id ?>"><input type="checkbox" name="<?php print $id ?>" /><?php print _todolist_js_safe($_POST['task']) ?></li>');
|
| 276 |
$('#<?php print $id ?>').each(TodoList.add_nub).each(TodoList.bind_task).Highlight(1000, '#ff0');
|
| 277 |
$('.todolist.incomplete').SortableAddItem($('#<?php print $id ?>').get(0));
|
| 278 |
<?php exit;
|
| 279 |
}
|
| 280 |
else todolist_bad_request();
|
| 281 |
}
|
| 282 |
|
| 283 |
/**
|
| 284 |
* MENU_CALLBACK function for /todolist/toggle_task.
|
| 285 |
*/
|
| 286 |
function todolist_toggle_task() {
|
| 287 |
if (isset($_REQUEST['id']) && isset($_REQUEST['sort']) && isset($_REQUEST['checked'])) {
|
| 288 |
$completed = time();
|
| 289 |
db_query("UPDATE {todolist_task} SET status = %d, `sort` = %d WHERE tid = %d", $_REQUEST['checked']? $completed : 0, $_REQUEST['sort'], substr($_REQUEST['id'], 5));
|
| 290 |
todolist_ok_js();
|
| 291 |
if ($_REQUEST['checked'] && user_access('complete tasks')): ?>
|
| 292 |
$('#<?php print $_REQUEST['id'] ?> input[@type=checkbox]').after('<a href="javascript:void(0)" title="Delete this task" class="delete" onclick="return TodoList.delete_task(\'<?php print $_REQUEST['id'] ?>\')">Delete</a> <span><?php print date('M d', $completed) ?></span> ');
|
| 293 |
if (!$('.todolist.complete').length) $('<ul class="todolist complete"></ul>').insertAfter('#todolist-add-task-form');
|
| 294 |
$('#<?php print $_REQUEST['id'] ?>').prependTo('.todolist.complete').each(TodoList.unbind_nub).Highlight(1000, '#ff0');
|
| 295 |
<?php elseif (user_access('uncomplete tasks')): ?>
|
| 296 |
$('#<?php print $_REQUEST['id'] ?> > a.delete').remove();
|
| 297 |
$('#<?php print $_REQUEST['id'] ?> span').remove();
|
| 298 |
TodoList.add_incomplete_list();
|
| 299 |
$('#<?php print $_REQUEST['id'] ?>').appendTo('.todolist.incomplete').each(TodoList.bind_nub).Highlight(1000, '#ff0');
|
| 300 |
<?php endif;
|
| 301 |
exit;
|
| 302 |
}
|
| 303 |
else todolist_bad_request();
|
| 304 |
}
|
| 305 |
|
| 306 |
/**
|
| 307 |
* MENU_CALLBACK function for /todolist/delete_task.
|
| 308 |
*/
|
| 309 |
function todolist_delete_task() {
|
| 310 |
if (user_access('delete tasks'))
|
| 311 |
if (isset($_REQUEST['id'])) {
|
| 312 |
db_query("DELETE FROM {todolist_task} WHERE tid = %d", substr($_REQUEST['id'], 5));
|
| 313 |
todolist_ok_js(); ?>
|
| 314 |
$('#<?php print $_REQUEST['id'] ?>').remove();
|
| 315 |
<?php exit;
|
| 316 |
}
|
| 317 |
else todolist_bad_request();
|
| 318 |
}
|
| 319 |
|
| 320 |
/**
|
| 321 |
* MENU_CALLBACK function for /todolist/reorder_task.
|
| 322 |
*/
|
| 323 |
function todolist_reorder_task() {
|
| 324 |
if (user_access('reorder tasks'))
|
| 325 |
foreach ($_REQUEST as $k => $tasks) {
|
| 326 |
if (substr($k, 0, 9) == 'todolist-') {
|
| 327 |
foreach ($tasks as $order => $task) {
|
| 328 |
$sql .= 'WHEN '. substr($task, 5) .' THEN '. $order .' ';
|
| 329 |
}
|
| 330 |
}
|
| 331 |
}
|
| 332 |
if ($sql) {
|
| 333 |
db_query("UPDATE {todolist_task} SET `sort` = CASE tid ". $sql ." END;");
|
| 334 |
todolist_ok_js();
|
| 335 |
exit;
|
| 336 |
}
|
| 337 |
else todolist_bad_request();
|
| 338 |
}
|
| 339 |
|
| 340 |
/**
|
| 341 |
* MENU_CALLBACK function for /todolist/edit_task.
|
| 342 |
*/
|
| 343 |
function todolist_edit_task() {
|
| 344 |
if (isset($_REQUEST['tid']) && isset($_REQUEST['task'])) {
|
| 345 |
db_query("UPDATE {todolist_task} SET task = '%s' WHERE tid = %d", $_REQUEST['task'], $_REQUEST['tid']);
|
| 346 |
$task = db_fetch_object(db_query("SELECT tid, status, task FROM {todolist_task} WHERE tid = %d", $_REQUEST['tid']));
|
| 347 |
$field = theme('todolist_task', $task);
|
| 348 |
todolist_ok_js(); ?>
|
| 349 |
$('#task-<?php print $_REQUEST['tid'] ?>').unbind('mouseover').unbind('mouseout').find('form').remove();
|
| 350 |
$('#task-<?php print $_REQUEST['tid'] ?>').html('<?php print _todolist_js_safe($field['data']) ?>').each(TodoList.bind_nub).Highlight(1000, '#ff0');
|
| 351 |
$('.todolist.incomplete').SortableAddItem($('#task-<?php print $_REQUEST['tid'] ?>').get(0));
|
| 352 |
<?php exit;
|
| 353 |
}
|
| 354 |
else if (isset($_REQUEST['id'])) {
|
| 355 |
$task = db_fetch_object(db_query("SELECT tid, task FROM {todolist_task} WHERE tid = %d", substr($_REQUEST['id'], 5)));
|
| 356 |
$html = drupal_get_form('todolist_edit_task_form', $task);
|
| 357 |
todolist_ok_js(); ?>
|
| 358 |
$('#<?php print $_REQUEST['id'] ?>').DraggableDestroy();
|
| 359 |
$('#<?php print $_REQUEST['id'] ?>').unbind('mouseover').unbind('mouseout').text('').find('input').remove();
|
| 360 |
$('#<?php print $_REQUEST['id'] ?>').append('<?php print _todolist_js_safe($html) ?>');
|
| 361 |
$('#<?php print $_REQUEST['id'] ?> form').submit(TodoList.edit_task).find('textarea').focus();
|
| 362 |
<?php exit;
|
| 363 |
}
|
| 364 |
else todolist_bad_request();
|
| 365 |
}
|