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