diff -urp ajax_checklist.orig/ajax_checklist.js ajax_checklist/ajax_checklist.js --- ajax_checklist.orig/ajax_checklist.js 2010-02-25 11:10:16.000000000 -0500 +++ ajax_checklist/ajax_checklist.js 2010-02-25 11:10:28.000000000 -0500 @@ -1,11 +1,10 @@ -$(document).ready( - function() { +jQuery(function($) { $("form.ajaxchecklist input.ajaxchecklist-nid").each( function () { // get the node id from where we left it in the header var nodeid=$(this).val(); // load the current state of the checkboxes once on loading page - $.getJSON("/ajaxchecklist/loadnid/"+nodeid, + $.getJSON(Drupal.settings.basePath + "ajaxchecklist/loadnid/"+nodeid, function(json) { for( var i=0; i < json.length; i++ ) { $(".form-item #"+json[i].qid).removeAttr("disabled"); @@ -25,7 +24,7 @@ $(document).ready( ); // setup an onclick for each checkbox that writes it state back to the database // when toggled. The label text is turned red while writing to the db. - $.getJSON("/ajaxchecklist/update-access", + $.getJSON(Drupal.settings.basePath + "ajaxchecklist/update-access", function(json) { $.ajaxchecklist_access=json.update; $("form.ajaxchecklist label.form-item :input").each( @@ -39,14 +38,14 @@ $(document).ready( var colorbefore=thislabel.css("color"); if ( $(this).attr("checked") === false || !$(this).attr("checked") ) { $(thislabel).css("color","red"); - $.get("/ajaxchecklist/save/"+nodeid+"/"+$(this).attr("id")+"/0", + $.get(Drupal.settings.basePath + "ajaxchecklist/save/"+nodeid+"/"+$(this).attr("id")+"/0", function() { $(thislabel).css("color",colorbefore); } ); } else { $(this).parent().css("color","red"); - $.get("/ajaxchecklist/save/"+nodeid+"/"+$(this).attr("id")+"/1", + $.get(Drupal.settings.basePath + "ajaxchecklist/save/"+nodeid+"/"+$(this).attr("id")+"/1", function() { $(thislabel).css("color",colorbefore); } @@ -59,5 +58,4 @@ $(document).ready( ); } ); - } -); +}); diff -urp ajax_checklist.orig/ajax_checklist.module ajax_checklist/ajax_checklist.module --- ajax_checklist.orig/ajax_checklist.module 2010-02-25 11:48:43.000000000 -0500 +++ ajax_checklist/ajax_checklist.module 2010-02-25 11:44:17.000000000 -0500 @@ -29,12 +29,33 @@ function ajax_checklist_menu() { // include the .js file in nodes function ajax_checklist_nodeapi(&$node, $op, $a3 = NULL, $a4 =NULL) { - if ($op="prepare") { - // This will currently load the js for all pages. This could be improved. - $js_path = drupal_get_path('module', 'ajax_checklist'); - drupal_add_js($js_path . '/ajax_checklist.js'); - //TODO: this won't work on CCK fields in views - $node->body=str_replace('%AJAXCHECKLISTNID',$node->nid,$node->body); + switch ($op) { + case 'alter': + if (!(strpos($node->body, '!ajax_checklist_nid') === FALSE)) { + $js_path = drupal_get_path('module', 'ajax_checklist'); + drupal_add_js($js_path . '/ajax_checklist.js'); + } + break; + + case 'delete': + db_query('DELETE FROM {ajax_checklist} WHERE nid = %d', $node->nid); + break; + } +} + +function ajax_checklist_user($op, &$edit, &$account, $category = NULL) { + if ($op == 'delete') { + db_query('DELETE FROM {ajax_checklist} WHERE user = %d', $account->uid); + } +} + +/** + * Substitute the token !ajax_checklist_nid for the actual node id. + * We do this in the preprocess level to keep CCK and other modules from overriding this. + */ +function ajax_checklist_preprocess_node(&$vars) { + if (!(strpos($vars['content'], 'ajax_checklist_nid') === FALSE)) { + $vars['content'] = strtr($vars['content'], array('!ajax_checklist_nid' => $vars['nid'])); } } @@ -53,10 +74,10 @@ function ajax_checklist_loadnid ($nid) { $dcvals = array(); // any checkbox id that starts with user- we remember the current user's settings // any other id is global and we use user=0 - $result=db_query("select qid,state from ajax_checklist ". + $result=db_query("select qid,state from {ajax_checklist} ". " where nid=%d and qid not like 'user-%' and user=0 ". " union ". - "select qid,state from ajax_checklist ". + "select qid,state from {ajax_checklist} ". "where nid=%d and qid like 'user-%' and user=%d ",$nid,$nid,$uid); while ($data=db_fetch_object($result)) { $dcvals[] = array (qid => $data->qid, state => $data->state, access => $access); @@ -74,16 +95,16 @@ function ajax_checklist_save ($nid,$qid, $uid=0; } - $existing=db_result(db_query("select count(state) from ajax_checklist ". + $existing=db_result(db_query("select count(state) from {ajax_checklist} ". "where nid=%d and user=%d and qid='%s'",$nid,$uid,$qid)); if ($existing == 0) { - db_query("insert into ajax_checklist (nid,user,qid,state) ". + db_query("insert into {ajax_checklist} (nid,user,qid,state) ". "values (%d,%d,'%s',%d)",$nid,$uid,$qid,$state); } elseif ( $current != $state ) { - $current=db_result(db_query("select state from ajax_checklist ". + $current=db_result(db_query("select state from {ajax_checklist} ". " where nid=%d and user=%d and qid='%s'",$nid,$uid,$qid)); if ($current != $state) { - db_query("update ajax_checklist ". + db_query("update {ajax_checklist} ". "set state=%d where nid=%d and user=%d and qid='%s'",$state,$nid,$uid,$qid); } } @@ -91,17 +112,16 @@ function ajax_checklist_save ($nid,$qid, exit(); } -// implementation of hool_filter to create the checkbox filter +// implementation of hook_filter to create the checkbox filter function ajax_checklist_filter($op, $delta = 0, $format = -1, $text = '' ) { global $ajax_checklist_matchcount; $ajax_checklist_matchcount = 0; switch($op) { case 'list': - return array (0 => t('Ajax Checkbox filter')); + return array (0 => t('AJAX checkbox filter')); case 'description': - return t('Allows checkboxes in nodes with [cb someid "Example?"]'. - 'that saves their state with AJAX to the database'); + return t('Allows checkboxes in nodes defined as [cb unique_checkbox_id Check this!], whose state is saved with AJAX to the database.'); case 'settings': break; case 'no cache': @@ -115,10 +135,11 @@ function ajax_checklist_filter($op, $del return $text; } else { - $output = "
\n"; - $output .= "\n"; + // Changing the node id token to standard form seems to allow checkboxes to work on CCK fields. + $output = '' . "\n"; + $output .= '' . "\n"; $output .= preg_replace_callback("|\[cb ([^ ]*)([^]]*)\]|i",'ajax_checklist_docheckbox',$text); - $output .="
"; + $output .= ""; return $output; } break; @@ -127,6 +148,13 @@ function ajax_checklist_filter($op, $del } } +/** + * Implementation of hook_filter_tips(). + */ +function ajax_checklist_filter_tips($delta, $format, $long = FALSE) { + return t('AJAX checkboxes can be added to this post, defined like this: [cb unique_checkbox_id Check this!].'); +} + // helper function for preg_replace_callback to generate the html for each checkbox // Form API not used as that would float all the checkboxes to the top/bottom of the page. function ajax_checklist_docheckbox ($matches) {