/[drupal]/contributions/modules/taxonomy_timer/taxonomy_timer.module
ViewVC logotype

Contents of /contributions/modules/taxonomy_timer/taxonomy_timer.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.12 - (show annotations) (download) (as text)
Tue Aug 26 01:03:34 2008 UTC (14 months, 4 weeks ago) by suydam
Branch: MAIN
CVS Tags: DRUPAL-5--1-8, DRUPAL-6--1-0-ALPHA1, DRUPAL-6--1-0-BETA1
Branch point for: DRUPAL-6--1
Changes since 1.11: +1 -2 lines
File MIME type: text/x-php
Fixing the watchdog still.
1 <?php
2 // $Id: taxonomy_timer.module,v 1.9 2007/11/02 15:04:02 suydam Exp $
3
4 /**
5 * @file
6 * Skip the cart review page and proceed directly to checkout after adding this item to my cart
7 */
8
9
10 // hook_menu()
11 function taxonomy_timer_menu($may_cache) {
12 $items = array();
13 if ( !$may_cache ) {
14 $items[] = array(
15 'path' => 'admin/settings/taxonomy_timer',
16 'title' => t('Taxonomy Timer Settings'),
17 'description' => t('Set system defaults for Taxonomy Timer module. This includes: which nodes are eligible for this functionality, which vocabs and terms can be timed, default expirations (for each term), which term gets applied when other terms expire....and more...maybe.'),
18 'callback' => 'taxonomy_timer_defaults_form',
19 'access' => user_access('administer taxonomy_timer settings')
20 );
21 $items[] = array(
22 'path' => 'admin/taxonomy_timer/delete',
23 'title' => t('Remove a taxonomy timer default entry'),
24 'callback' => '_remove_tt_default',
25 'access' => user_access('administer taxonomy_timer settings'),
26 'type' => MENU_CALLBACK
27 );
28 $items[] = array(
29 'path' => 'admin/taxonomy_timer/notify_settings',
30 'title' => 'Taxonomy Timer Email Settings',
31 'description' => t('Set the content for the email sent during notification'),
32 'callback' => 'taxonomy_timer_notify_form_display',
33 'access' => user_access('administer taxonomy_timer settings'),
34 'type' => MENU_CALLBACK
35 );
36 }
37 return $items;
38 }
39
40 function taxonomy_timer_defaults_form() {
41 return drupal_get_form('taxonomy_timer_defaults');
42 }
43
44 /* permissons for access_control */
45 function taxonomy_timer_perm() {
46 return array('administer taxonomy_timer settings','override taxonomy_timer settings on nodes','reset taxonomy_timer settings on nodes');
47 }
48
49 /*
50 * implementation of hook_cron()
51 */
52 function taxonomy_timer_cron() {
53 // cron does a couple things...
54 // first, it sends warning emails.
55 // second it expires nodes (performs our actions basedon the timers in tt nodes)
56 $t = getdate();
57 $today = $t[0];
58
59 $two_days_from_now = $today + 86400 + 86400;
60 watchdog('taxonomy_timer', "TODAY = $today");
61 /* watchdog('taxonomy_timer', "two days from now = $two_days_from_now");
62 watchdog('taxonomy_timer',"any node expiring between $today and $two_days_from_now shoudl get a warning email"); */
63
64 // select where expiration is sooner than two days from now but later than today
65 // email those people
66 $statement1 = "SELECT * FROM {taxonomy_timer_nodes} WHERE tt_expiration <= $two_days_from_now AND tt_expiration >= $today AND completed = 0 AND notified=0";
67 $statement2 = "SELECT * FROM {taxonomy_timer_nodes} WHERE tt_expiration <= $today AND completed = 0";
68 $statements = array( 'warning' => $statement1, 'expire' => $statement2 );
69 watchdog('taxonomy_timer','The "WARNING STATEMENT" is: ' . $statement1);
70 watchdog('taxonomy_timer','The "EXPIRATION STATEMENT" is: ' . $statement2);
71 $message = variable_get('taxonomy_timer_notification_content','%nodetitle is due to %action at %exp.');
72
73 foreach (array_keys( $statements ) as $stkey ) {
74 watchdog('taxonomy_timer',"$stkey: " . $statements [ $stkey ]);
75 $sth = db_query( $statements[ $stkey ] );
76 watchdog('taxonomy_timer',"yielded " . db_num_rows( $sth ) );
77
78 //watchdog('taxonomy_timer', "There are " . db_num_rows( $sth ) . " rows in the action list");
79 $watchdog_entries = array();
80
81 while ( $row = db_fetch_array( $sth ) ) {
82 $this_node = node_load( $row['nid'] );
83 // need to load up the user who owns the node for To:
84 $tmparr = array('uid' => $this_node->uid );
85 $node_author = user_load( $tmparr );
86 //drupal_set_message( "<PRE>" . sprint_r( $node_author ) . "</pre>");
87 if ( $row['tt_unpublish'] ) {
88 $watchdog_entries[] = 'should we UNPUBLISH node ' . $row['nid'] . "?";
89 // does the node have tid from the select statement?
90 if ( $this_node->taxonomy[ $row['tid'] ] ) {
91 if ( $stkey == 'warning' ) {
92 $watchdog_entries[] = 'Sending a warning email about unpublishing ' . $row['tid'];
93 $action = 'unpublish';
94 $exp = format_date( getdate( $row['tt_expiration'] ) );
95 $to = $node_author->mail;
96 $from = variable_get('taxonomy_timer_notification_from','test@test.com');
97 $subject = variable_get('taxonomy_timer_notification_subject','Node expiration warning');
98 $subject = preg_replace('/\%nodetitle/', $this_node->title, $subject );
99 $subject = preg_replace('/\%exp/', $exp, $subject );
100 $subject = preg_replace('/\%action/', $exp, $subject );
101 //drupal_mail($mailkey, $to, $subject, $body, $from = NULL, $headers = array())
102 $tmp_message = $message;
103 $tmp_message = preg_replace('/\%exp/', $exp, $tmp_message);
104 $tmp_message = preg_replace('/\%action/', $action, $tmp_message);
105 $tmp_message = preg_replace('/\%nodetitle/', $this_node->title, $tmp_message);
106 $tmp_message = preg_replace('/\%nodeeditlink/', l('edit ' . $this_node->title,'node/' . $this_node->nid . '/edit',NULL,NULL,NULL,TRUE,FALSE) , $tmp_message);
107 drupal_mail('ttnotificationkey',$to,$subject,$tmp_message,$from);
108 db_query( "UPDATE {taxonomy_timer_nodes} SET notified=1 WHERE ttid=" . $row['ttid'] );
109 }
110 elseif ( $stkey == 'expire' ) {
111 $watchdog_entries[] = 'UNPUBLISH node ' . $row['nid'];
112 db_query( "UPDATE {node} SET status=0 WHERE nid = " . $row['nid'] );
113 db_query( "UPDATE {taxonomy_timer_nodes} SET expired=1 WHERE ttid=" . $row['ttid'] );
114 }
115 }
116 else {
117 $watchdog_entries[] = "NO. Leave it. because it doesn't have term " . $row['tid'];
118 }
119 }
120 else {
121 $watchdog_entries[] = 'should we Assign ' . $row['tt_next_tid'] . ' to node ' . $row['nid'] . "?";
122 // does the node have tid from the select statement?
123 if ( $this_node->taxonomy[ $row['tid'] ] ) {
124 if ( $stkey == 'warning' ) {
125 $watchdog_entries[] = 'Sending a warning email about assignment to ' . $row['tt_next_tid'] . ' for node ' . $row['nid'];
126 $the_term = taxonomy_get_term( $row['tt_next_tid'] );
127 $the_term_name = $the_term->name;
128 $action = ' be reclassified as ' . $this_node->type . ' as ' . $the_term_name;
129 $a_date_test = getdate( $row['tt_expiration'] );
130 $exp = format_date( $a_date_test[0] );
131 $to = $node_author->mail;
132 $from = variable_get('taxonomy_timer_notification_from','test@test.com');
133 $subject = variable_get('taxonomy_timer_notification_subject','Node expiration warning');
134 $subject = preg_replace('/\%nodetitle/', $this_node->title, $subject );
135 $subject = preg_replace('/\%exp/', $exp, $subject );
136 $subject = preg_replace('/\%action/', $exp, $subject );
137 //drupal_mail($mailkey, $to, $subject, $body, $from = NULL, $headers = array())
138 $tmp_message = $message;
139 $tmp_message = preg_replace('/\%exp/', $exp, $tmp_message);
140 $tmp_message = preg_replace('/\%action/', $action, $tmp_message);
141 $tmp_message = preg_replace('/\%nodetitle/', $this_node->title, $tmp_message);
142 //$tmp_message = preg_replace('/\%nodeeditlink/', l('edit ' . $this_node->title,'node/' . $this_node->nid . '/edit') , $tmp_message);
143 $tmp_message = preg_replace('/\%nodeeditlink/', l('edit ' . $this_node->title,'node/' . $this_node->nid . '/edit',NULL,NULL,NULL,TRUE,FALSE) , $tmp_message);
144 drupal_mail('ttnotificationkey',$to,$subject,$tmp_message,$from);
145 db_query( "UPDATE {taxonomy_timer_nodes} SET notified=1 WHERE ttid=" . $row['ttid'] );
146
147 }
148 else if ( $stkey == 'expire' ) {
149 // insert new term
150 db_query("INSERT INTO {term_node} (nid,tid) VALUES ( " . $row['nid'] . "," . $row['tt_next_tid'] . ")" );
151
152 // remove old term
153 db_query("DELETE FROM {term_node} WHERE nid=" . $row['nid'] . " AND tid=" . $row['tid'] );
154
155 $watchdog_entries[] = "Added " . $row['tt_next_tid'] . " to " . $row['nid'];
156 db_query( "UPDATE {taxonomy_timer_nodes} SET completed=1 WHERE ttid=" . $row['ttid'] );
157 }
158 }
159 else {
160 $watchdog_entries[] = "NO. Leave it. because it doesn't have term " . $row['tid'];
161 }
162
163 // then we need to see if there are rules to create for the new tid too!
164 }
165 }
166
167 watchdog('taxonomy_timer', "HERE IS WHAT WE DID THIS GO-AROUND: <UL><LI>" . join("</li><LI>", $watchdog_entries ) . "</li></ul>");
168 }
169 }
170
171 // admin settings function
172 function taxonomy_timer_defaults() {
173 /* only allow configuration if they have taxonomy terms defined. */
174 $actions = $options = taxonomy_form_all();
175
176 /* actions is used to tell us what to do after expiration */
177 $actions['UNPUBLISH NODE'] = 'Unpublish this node on expiration';
178
179 /* drupal_set_message("<PRE>" . sprint_r( $actions ) . "</pre>"); */
180 if ( !$options ) {
181 drupal_set_message("Taxonomy Timer requries the creation of some vocabs and terms. Please " . l('create some terms','admin/content/taxonomy') . " before trying to configure this module.",'error');
182 }
183 else {
184 $yesno = array('1' => t('Yes'), '0' => t('No') );
185 $form['tt_intro'] = array(
186 '#value'=>'<P>Taxonomy Timer can be used to schedule the expiration of nodes that contain certain terms (tids). You have the choice to just unpublish that node, or to assign a new tid. This can all be configured here.</p>
187 <P>Be sure to visit ' . l('the access control page','admin/user/access') . ' to configure who can over-ride this stuff at the node level.</p>
188 <P><strong>' . l('Visit the email notification form','admin/taxonomy_timer/notify_settings') . ' to configure the email that gets sent as an expiration warning. We have placed that form on its own page so that we can add more email notifications in future versions.'
189 );
190 /* list all tids that are already enabled...or show some text */
191 $form['tt_defaults'] = array(
192 '#type' => 'fieldset',
193 '#access' => user_access('administer taxonomy_timer settings'),
194 '#title' => t('Taxonomy Timer Defaults'),
195 '#collapsible' => TRUE,
196 '#collapsed' => FALSE,
197 '#weight' => 0
198 );
199 $statement = "SELECT * FROM {taxonomy_timer_defaults} ORDER BY tid,tt_type";
200 $sth = db_query( $statement );
201 if ( db_num_rows($sth) ) {
202 // this really doesn't use the form...just stuffs an html table with text links for removing existing timers
203 $tt_output = "\n\n\n<table class='tt_defaults_table'>\n";
204 $tt_output .= "<TR>
205 <TH>Nodes of type...</th>
206 <TH>With term...</th>
207 <Th>wait...</th>
208 <TH>..and then do this:</th>
209 <Th>Delete this timer?</tH>
210 </tr>\n";
211
212 while ( $row = db_fetch_array( $sth )) {
213 $watch_term = taxonomy_get_term( $row['tid'] );
214 $action = '';
215 if ( $row['tt_next_tid'] ) {
216 $next_term = taxonomy_get_term( $row['tt_next_tid'] );
217 $action = "Assign " . $next_term->name . " (tid=" . $next_term->tid . ")";
218 }
219 elseif ( $row['tt_unpublish'] ) {
220 $action = "Unpublish Node";
221 }
222 else {
223 drupal_set_message("Taxonomy Timer: No tid and no unpublish bit found for " . sprint_r( $row ), 'error');
224 }
225
226 $tt_output .= "\n<TR>";
227 $tt_output .= "<TD>" . $row['tt_type'] . "</td>\n";
228 $tt_output .= "<TD>" . $watch_term->name . " (tid=" . $watch_term->tid . ")</td>\n";
229 $tt_output .= "<TD>" . $row['tt_duration'] . " days</td>\n";
230 $tt_output .= "<TD>" . $action . "</td>\n";
231
232 // send a confirmation window for delees
233 $attr = array( 'onClick' => 'return confirm("Are you sure you wish to delete this timer?")');
234 $tt_output .= "<TD>" . l('delete','admin/taxonomy_timer/delete/'. $row['ttid'], $attr);
235
236 $tt_output .= "</tr>\n";
237 }
238
239 $tt_output .= "</table>\n\n\n";
240 $form['tt_defaults']['happy_list'] = array(
241 '#value' => $tt_output
242 );
243 }
244 else {
245 $form['tt_defaults']['empty_text'] = array(
246 '#value' => '<P>There are no entries in the defaults table (yet). Please use the form below to start setting up your taxonomy expirations.'
247 );
248 }
249
250 /* list all terms... let them add more default setups. */
251 $form['tt_add'] = array(
252 '#type' => 'fieldset',
253 '#access' => user_access('administer taxonomy_timer settings'),
254 '#title' => t('Taxonomy Timer - Add New'),
255 '#collapsible' => TRUE,
256 '#collapsed' => FALSE,
257 '#weight' => '10'
258 );
259 $form['tt_add']['happy_text'] = array(
260 '#value' => '<P>Pick a term and a type to create a new default timer.</p>',
261 '#weight' => '-10'
262 );
263 $form['tt_add']['add_tid'] = array(
264 '#type' => 'select',
265 '#title' => 'Term',
266 '#options' => $options,
267 '#required' => TRUE,
268 '#description' => t('Select a term from this list to add the new timer.'),
269 '#weight' => '1'
270 );
271 $form['tt_add']['add_type'] = array(
272 '#type' => 'select',
273 '#title' => 'Node Type',
274 '#options' => node_get_types('names'),
275 '#required' => TRUE,
276 '#description' => t('Select a node type from this list to add the new timer.'),
277 '#weight' => '0'
278 );
279 $form['tt_add']['duration'] = array(
280 '#type' => 'textfield',
281 '#title' => 'Expire after',
282 '#description' => "...in days",
283 '#size' => 5,
284 '#maxlength' => 4,
285 '#required' => TRUE,
286 '#weight' => 5
287 );
288 $form['tt_add']['action'] = array(
289 '#type' => 'select',
290 '#title' => 'After expiration, assign this term (or unpublish node)',
291 '#description' => 'Select at least one of these, hold down ctrl to select multiple',
292 '#weight' => 6 ,
293 '#size' => 10,
294 '#multiple' => TRUE,
295 '#required' => TRUE,
296 '#options' => $actions
297 );
298 $form['tt_add']['submit'] = array(
299 '#weight' => '10',
300 '#type' => 'submit',
301 '#value' => t('Create')
302 );
303 }
304
305 $form['array_filter'] = array('#type' => 'hidden' );
306 return $form;
307
308 }
309
310 function taxonomy_timer_notify_form_submit($form_id, $form_values) {
311 //global $user;
312 variable_set('taxonomy_timer_notification_content',$form_values['email_content']);
313 variable_set('taxonomy_timer_notification_from',$form_values['from_address']);
314 variable_set('taxonomy_timer_notification_subject',$form_values['subject']);
315 drupal_set_message('Email notification settings were saved.');
316 /*drupal_set_message("<PRE>" . sprint_r( $user ) . "</pre>"); */
317
318 }
319
320 function taxonomy_timer_notify_form_display() {
321 return drupal_get_form('taxonomy_timer_notify_form');
322 }
323
324
325 function taxonomy_timer_notify_form( ) {
326 $form['tt_intro'] = array(
327 '#value'=>'<P>Taxonomy Timer sends emails when expiration nears. You can configure that message here.<P>As future versions arise, we plan to add more notifications here.'
328 );
329 $form['basics'] = array(
330 '#type' => 'fieldset',
331 '#title' => 'Taxonomy Timer Email Settings',
332 '#collapsible' => TRUE,
333 '#collapsed' => FALSE,
334 '#weight' => 0
335 );
336 $form['basics']['from_address'] = array(
337 '#type' => 'textfield',
338 '#title' => 'Emails from',
339 '#default_value' => variable_get('taxonomy_timer_notification_from',''),
340 '#required' => TRUE
341 );
342 $form['warning'] = array(
343 '#type' => 'fieldset',
344 '#title' => 'Taxonomy Timer Warning Email',
345 '#collapsible' => TRUE,
346 '#collapsed' => FALSE,
347 '#weight' => 10
348 );
349 $form['warning']['subject'] = array(
350 '#type' => 'textfield',
351 '#title' => 'Email Subject',
352 '#default_value' => variable_get('taxonomy_timer_notification_subject','%nodetitle due to %action on %exp'),
353 '#description' => t('You can variable subst. %ext (expiration date), %action (e.g. unpublish) and %nodetitle (node title) in the subject field.'),
354 '#required' => TRUE
355 );
356 $form['warning']['email_content'] = array(
357 '#type' => 'textarea',
358 '#default_value' => variable_get('taxonomy_timer_notification_content','%nodetitle is due to %action at %exp.'),
359 '#title' => 'Expiration Warning Email (plain text)',
360 '#required' => TRUE,
361 '#description' => t('You can variable subst. %exp (expiration date, e.g. "Sep 9, 2007"), %action (the action...adding a term or unpublish...just use the word "expire" if you wish), %nodetitle (node title), %nodeeditlink (shows the title of the node, linked to the edit page). Currently this email is sent')
362 );
363 $form['submit'] = array(
364 '#weight' => '100',
365 '#type' => 'submit',
366 '#value' => t('Save')
367 );
368
369 $form['array_filter'] = array('#type' => 'hidden' );
370 return $form;
371 }
372
373 function taxonomy_timer_defaults_validate( $form_id, $form_values ) {
374 //drupal_set_message("HERE's the form:<PRE>" . sprint_r( $form_values ) . "</pre>");
375
376 // two steps.... A) is this a plausible timer? That is, they picked a term to watch...can the node they select actually get that term?
377 $watch_term = taxonomy_get_term( $form_values['add_tid'] );
378 $watch_terms_vocab = taxonomy_get_vocabulary( $watch_term->vid );
379
380 //drupal_set_message("The term we want to watch is:<PRE> " . sprint_r($watch_term) . "</pre>");
381 //drupal_set_message("The vocab of that term is:<PRE> " . sprint_r($watch_terms_vocab) . "</pre>");
382
383 // is $form_values['add_type'] in $watch_terms_vocab->nodes ???
384 if( !in_array( $form_values['add_type'], $watch_terms_vocab->nodes ) ) {
385 form_set_error('add_type', t( '<I>' . $form_values['add_type'] . '</i> nodes are not allowed to have term "' . $watch_term->name . '" (as defined by ' . l('your Taxonomy setup','admin/content/taxonomy') . ') so the scenario you have constructed is not permitted..'));
386 }
387 else {
388 //drupal_set_message("Trying to add " . join( $form_values['action'] ) . " to nodes of type " . $form_values['add_type']);
389 // ok, what is/are the destination tid(s)?
390 $proceed = 1;
391 foreach ( array_keys( $form_values['action'] ) as $dest_tid ) {
392 if ( intval( $dest_tid ) ) {
393 $dest_term = taxonomy_get_term( $dest_tid );
394 $dest_term_vocab = taxonomy_get_vocabulary( $dest_term->vid );
395
396 //drupal_set_message("Can we set $dest_tid on " . $form_values['add_type'] . "?");
397 if( !in_array( $form_values['add_type'], $dest_term_vocab->nodes ) ) {
398 $proceed = 0;
399 drupal_set_message( '<I>' . $form_values['add_type'] . '</i> nodes are not allowed to have term "' . $dest_term->name . '" (as defined by ' . l('your Taxonomy setup','admin/content/taxonomy') . ')','error');
400 }
401 }
402 }
403 if ( !$proceed ) {
404 form_set_error('action',"Please correct the selections for <i>After expiration, assign this term (or unpublish node)</i>");
405 }
406 }
407 }
408
409 function taxonomy_timer_defaults_submit( $form_id, $form_values ) {
410 foreach ( array_keys( $form_values['action'] ) as $dest_tid ) {
411 if ( $dest_tid == 'UNPUBLISH NODE' ) {
412 $statement = "INSERT INTO {taxonomy_timer_defaults} (tid,tt_duration,tt_next_tid,tt_unpublish,tt_type) VALUES (
413 '" . $form_values['add_tid'] . "',
414 '" . $form_values['duration'] . "',
415 '0','1', '" . $form_values['add_type'] . "')";
416 }
417 else {
418 $statement = "INSERT INTO {taxonomy_timer_defaults} (tid,tt_duration,tt_next_tid,tt_unpublish,tt_type) VALUES (
419 '" . $form_values['add_tid'] . "',
420 '" . $form_values['duration'] . "',
421 '" . $dest_tid . "',
422 '0', '" . $form_values['add_type'] . "')";
423 }
424 if (! db_query( $statement ) ) {
425 drupal_set_message( db_error(),'error');
426 drupal_set_message( "There was an error with $statement ",'error');
427 }
428 else {
429 drupal_set_message("Taxonomy Timer: Success creating a new timer.");
430 }
431 }
432 }
433
434 function taxonomy_timer_taxonomy( $op, $type, $array=NULL ) {
435 if ( $op == 'delete' && $type == 'term') {
436 // need to remove taxonomy_timer defaults and node entries for this tid.
437 db_query("DELETE FROM {taxonomy_timer_defaults} WHERE tid=" . $array['tid']);
438 drupal_set_message("Taxonomy Timer: Deleted " . db_affected_rows() . " default timers");
439 db_query("DELETE FROM {taxonomy_timer_nodes} WHERE tid=" . $array['tid']);
440 drupal_set_message("Taxonomy Timer: Deleted " . db_affected_rows() . " specific node timers");
441 }
442 }
443
444 function _remove_tt_default() {
445 // get the details for what we're deleteing.
446 $statement = "SELECT * FROM {taxonomy_timer_defaults} WHERE ttid= " . arg(3);
447 $sth = db_query( $statement );
448 $row = db_fetch_array( $sth );
449 if ( ! db_query( "DELETE FROM {taxonomy_timer_defaults} WHERE ttid = " . arg(3))) {
450 drupal_set_message( db_error(),'error');
451 drupal_set_message( "There was an error with $statement ",'error');
452 }
453 else {
454 drupal_set_message("Taxonomy Timer: Success removing the existing timer default.");
455 $statement = "DELETE FROM {taxonomy_timer_nodes} WHERE tid = " . $row['tid'] . " AND tt_next_tid = " . $row['tt_next_tid'] . " AND tt_unpublish = " . $row['tt_unpublish'];
456 $sth = db_query( $statement );
457 drupal_set_message("Taxonomy Timer: Deleted " . db_affected_rows() . " specific node timers.");
458 }
459 drupal_goto('admin/settings/taxonomy_timer');
460 }
461
462 function _get_tt_data( $nid,$type ) {
463 $sth = db_query( "SELECT * FROM {taxonomy_timer_nodes} WHERE nid=$nid AND completed = 0");
464 if ( db_num_rows( $sth ) ) {
465 $returners = array();
466 while ( $row = db_fetch_array( $sth ) ) {
467 $returners[] = $row;
468 }
469 return $returners;
470 }
471 else {
472 return FALSE;
473 }
474 }
475 function _set_tt_defaults( $nid, $type ) {
476 // this function does 2 things.
477 // first, it clears out existing entries in tt nodes
478 // then it assigns defaults for this type to this nid.
479 db_query("DELETE FROM {taxonomy_timer_nodes} WHERE nid = $nid") or drupal_set_message("could not delete existing rows in _set_tt_defaults","error");
480 $this_statement = "SELECT * FROM {taxonomy_timer_defaults} WHERE tt_type = '$type'" ;
481 $sth = db_query( $this_statement );
482 $today = getdate();
483 $today_stamp = $today[0];
484
485 //drupal_set_message("$this_statement returned " . db_num_rows( $sth ) . " rows");
486
487 // set the defaults then call _get_tt_values to return them
488 while ( $row = db_fetch_array( $sth ) ) {
489 $expiration = $today_stamp + ( $row['tt_duration'] * 86400 );
490
491 // things are bad for the hook_form_alter if we don't have values...so create defaults if they don't exist.
492 // return the defaults too, obviously
493 $sth_new = db_query( "INSERT INTO {taxonomy_timer_nodes}
494 (nid, tid, tt_assigned, tt_expiration, tt_next_tid, tt_unpublish)
495 VALUES ($nid, " . $row['tid'] . ", $today_stamp, $expiration, " .
496 $row['tt_next_tid'] . "," . $row['tt_unpublish'] . ")"
497 ) or drupal_set_message("insert failed",'error');
498 }
499
500 $returners = _get_tt_data( $nid, $type );
501 //drupal_set_message("returners:<PRE>" . sprint_r( $returners) . "</pre>");
502 return $returners;
503
504 }
505
506 /*
507 * implementation of hook_form_alter()
508 */
509 function taxonomy_timer_form_alter( $form_id, &$form ) {
510 //drupal_set_message("form id = $form_id");
511 //drupal_set_message( "<h1>HERE's the form</h1><PRE>" . sprint_r( $form ) . "</pre>");
512 if ( preg_match("/_node_form/", $form_id ) && !preg_match("/node\/add/",$form['#action']) && isset( $form['nid']['#value'] ) && is_numeric( $form['nid']['#value'] ) ) {
513 //drupal_set_message("Node is of type " . $form['#node']->type);
514 /* drupal_set_message("Node is owned by..." . $form['#node']->uid);
515 $tmparr = array('uid' => $form['#node']->uid );
516 $node_author = user_load( $tmparr );
517 drupal_set_message( "<PRE>" . sprint_r( $node_author ) . "</pre>"); */
518 if ( user_access('override taxonomy_timer settings on nodes') ) {
519 //drupal_set_message("You have access to override taxonomy timer settings");
520 //drupal_set_message("<PRE>" . sprint_r( $form ) . "</pre>");
521
522 // only show our stuff if there are defaults for this node type
523 $sth = db_query( "SELECT COUNT(*) AS thecount FROM {taxonomy_timer_defaults} WHERE tt_type = '" . $form['#node']->type . "'" );
524 $row = db_fetch_array( $sth );
525 if ( $row['thecount'] ) {
526
527 // get all the taxonomy timer data for this node. if there isn't any, we'll set it in the
528 // following if statement (so then there will be from now on)
529 $tt_data = _get_tt_data( $form['#node']->nid, $form['#node']->type );
530 if ( !$tt_data ) {
531 drupal_set_message("tt data will be the defaults...");
532 $tt_data = _set_tt_defaults( $form['#node']->nid, $form['#node']->type );
533 }
534
535 //drupal_set_message("HERE IS TT DATA:<PRE>\n" . sprint_r( $tt_data ) . "</pre>");
536
537 // stuff this into the node edit form
538 $form['taxonomy_timer'] = array(
539 '#type' => 'fieldset',
540 '#title' => 'Taxonomy Timer Configuration',
541 '#collapsible' => TRUE,
542 '#collapsed' => TRUE,
543 '#weight' => 10
544 );
545 $form['taxonomy_timer']['intro'] = array(
546 '#value' => '<P>Taxonomy Timer enables automatic expiration, term changes and unpublishing of nodes after a given period of time. You have the opportunity to edit some (but not all) of the timers attached to this node.</p>',
547 '#weight' => '-10'
548 );
549
550 if ( is_array( $tt_data ) ) {
551 // step through each item in the taxonomy timer data that we fetched abvove
552 foreach ( array_keys( $tt_data) as $tt_entry ) {
553
554 //drupal_set_message("<PRE>" . sprint_r( $tt_data[$tt_entry] ) . "</pre>");
555
556 // determine if the tid in the rule is also assigned to this node.
557 // If it's NOT then don't bother scaring them with an expiration form.
558 if ( isset( $form['#node']->taxonomy[ $tt_data[$tt_entry]['tid'] ] ) ) {
559 // need a timestamp for today
560 $today = getdate();
561 $today_stamp = $today[0];
562
563 // when will this action take place?
564 //drupal_set_message("expiration date = " . $tt_data[$tt_entry]['tt_expiration'] );
565 $actiondate = getdate( $tt_data[$tt_entry]['tt_expiration']);
566 //drupal_set_message("action date = $actiondate");
567 $pretty_date = $actiondate['month'] . ' ' . $actiondate['mday'] . ', '. $actiondate['year'];
568 //drupal_set_message("pretty date = $pretty_date");
569 $expires_in_days = intval( ($tt_data[$tt_entry]['tt_expiration'] - $today_stamp) / 86400 );
570 //drupal_set_message("expires_in_days = $expires_in_days");
571 //drupal_set_message("<PRE>" . sprint_r( $tt_data[$tt_entry]) . '</pre>');
572
573 // what's gonna happen?
574 $tt_action = 'unpublish this ' . $form['#node']->type;
575 if ( $tt_data[$tt_entry]['tt_next_tid']) {
576 $next_term = taxonomy_get_term( $tt_data[$tt_entry]['tt_next_tid'] );
577 $tt_action = "assign the term <i>" . $next_term->name . "</i> to this " . $form['#node']->type;
578 }
579
580 // if there are at least 1 days until this happens, allow a mod-down (this form is only showing for users with mod-down perms)
581 $mod_options = array(
582 'no change' => 'Do not change this scheduled action',
583 'immediately' => 'Schedule Immediately'
584 );
585 if ( $expires_in_days > 0 ) {
586 for ($x=$expires_in_days; $x>=0; $x--) {
587 $mod_date = getdate( $tt_data[$tt_entry]['tt_expiration'] - ($x * 86400) );
588 // hide the date that's already scheduled (e.g. we were getting "Sept 26" in the drop
589 // down even though the event is already scheduled for that day)
590 $pretty_mod_date = $mod_date['month'] . ' ' . $mod_date['mday'] . ', '. $mod_date['year'];
591 $mod_options[$mod_date[0]] = 'Schedule for ' . $pretty_mod_date;
592 }
593 }
594 if ( user_access('reset taxonomy_timer settings on nodes') ) {
595 $mod_options['reset tt_assigned'] = 'Restart this timer';
596 }
597 $form['taxonomy_timer'][ 'tt_entry_' . $tt_data[$tt_entry]['ttid'] ] = array(
598 '#type' => "select",
599 '#options' => $mod_options,
600 '#title' => t("On $pretty_date we will automatically $tt_action"),
601 '#description' => t('Use this form to make it happen sooner.'),
602 '#default_value' => '0',
603 '#weight' => $tt_entry
604 );
605 }
606 }
607 }
608
609 /* $form['taxonomy_timer']['data_dump'] = array(
610 '#value' => '<PRE>' . sprint_r( $tt_data ) . '</pre>',
611 '#weight' => '300'
612 ); */
613 else {
614 //drupal_set_message("tt_data is not an array. this means that this node has no timers associated with it.<PRE>" . sprint_r( $form['#node']->taxonomy ) . "</pre>" );
615 }
616
617
618 }
619 /* else {
620 drupal_set_message("There are no tt defaults for nodes of type " . $form['#node']->type);
621 } */
622 }
623 /* else {
624 drupal_set_message("User doesn't have access to override tax_timer settings");
625 } */
626
627 }
628 /* elseif ( preg_match("/_node_form/", $form_id ) ) {
629 drupal_set_message("this is $form_id but we're not doing an edit...we're doing a " . $form['#action']);
630 } */
631 }
632
633 function _get_default_tt_expiration( $tid, $type, $assigned ) {
634 $statement = "SELECT * FROM {taxonomy_timer_defaults} WHERE tid = $tid and tt_type='$type'";
635 //drupal_set_message( $statement );
636 $sth = db_query($statement);
637 $row = db_fetch_array( $sth );
638
639 // $row['tt_duration'] tells us how long to go from assigned.
640 $new_tt_expiration = $assigned + ( 86400 * $row['tt_duration'] );
641 return $new_tt_expiration;
642
643 }
644
645 function _tt_parse_node_edit_values( $node ) {
646 //drupal_set_message("TT: Parsing edit form in search of tt_data");
647 // this function processes the edit form, if it had taxonomy time data in it.
648 // not all node edit forms will have that, but if they do, here's where we handle it.
649
650 // hunt out node items of key tt_entry_$ttid
651 foreach ( $node as $key => $value) {
652 $matches = array();
653 if ( preg_match("/tt_entry_(\d+)$/", $key, $matches ) ) {
654 // if they put "no change" then it means we're not changing this ttid
655 $ttid = $matches[1];
656 if ( $node->$key == 'no change' ) {
657 // what it says... :) no change
658 }
659 elseif ( $node->$key == 'reset tt_assigned' && user_access('reset taxonomy_timer settings on nodes') ) {
660 // set assigned to "today" and then use the default expiration for this pairing
661 // not everyone can reset expirations to default...but form_alter should have prevented them from
662 // even seeing this option...so the user_access test is just a fallback.
663 $today = getdate();
664 $tt_assigned = $today[0];
665 // need to reset tt_expiration to the default for this tid/type pairing.
666 // to do that, we have to actually fetch the entire fow from tt nodes
667 $tt_exp_statement = "SELECT * FROM {taxonomy_timer_nodes} WHERE ttid=$ttid";
668 $tt_exp_sth = db_query( $tt_exp_statement );
669 if ( db_num_rows( $tt_exp_sth ) ) {
670 $tt_exp_row = db_fetch_array( $tt_exp_sth );
671 $tt_expiration = _get_default_tt_expiration( $tt_exp_row['tid'], $node->type , $tt_assigned );
672
673 // now update using the new assigned and expiration values.
674 $statement = "UPDATE {taxonomy_timer_nodes} SET tt_expiration = $tt_expiration, tt_assigned = $tt_assigned WHERE ttid = $ttid ";
675 db_query( $statement );
676 //drupal_set_message( $statement );
677 }
678 else {
679 drupal_set_message("While doing a node update, we tried to $tt_exp_statement but that returned no rows so we had to skip this step.",'error');
680 }
681 }
682 else {
683 $tt_expiration = $node->$key;
684 if ( $node->$key == 'immediately' ) {
685 // over-ride tt_expiration assignment if they want immediately expiration
686 $today = getdate();
687 $tt_expiration = $today[0];
688 }
689 // prep an update statement
690 $statement = "UPDATE {taxonomy_timer_nodes} SET tt_expiration = $tt_expiration WHERE ttid = $ttid";
691 db_query( $statement );
692 //drupal_set_message( $statement );
693 }
694 }
695 }
696 }
697
698 function _tt_assure_defaults_exist( $node ) {
699 foreach( $node->taxonomy as $tid ) {
700 $defaults_statement = "SELECT * FROM {taxonomy_timer_defaults} WHERE tid = $tid";
701 //drupal_set_message("DEFAULTS: $defaults_statement");
702 $defaults_sth = db_query( $defaults_statement );
703 if ( db_num_rows( $defaults_sth ) ) {
704 //drupal_set_message("There are default tt entries for term $tid");
705 while ($default_row = db_fetch_array( $defaults_sth ) ) {
706
707 // now check tt nodes to see if we have records. For each one WITHOUT a record, stuff in the default
708 // the match is basically on tid, tt_next_tid and tt_unpublish.
709 // there should be a 1:1 correspondance between the two tables for that pairing
710 $check_sth_statement = "SELECT * FROM {taxonomy_timer_nodes} WHERE nid=" . $node->nid . " AND tid=" . $tid . " AND tt_next_tid=" . $default_row['tt_next_tid'] . " AND tt_unpublish=" . $default_row['tt_unpublish'];
711 $check_sth = db_query( $check_sth_statement );
712 //drupal_set_message("$check_sth_statement");
713 if ( !db_num_rows( $check_sth ) ) {
714 // no rows on taht statement, better insert a default
715
716 $today = getdate();
717 $tt_assigned = $today[0];
718 $tt_expiration = $tt_assigned + ( 86400 + $row['tt_duration'] );
719
720 //drupal_set_message("**** $nid NEEDS a row for $tid " . $default_row['tt_next_tid'] . " " . $default_row['tt_unpublish'] );
721 $insert_statement = "INSERT INTO {taxonomy_timer_nodes} (nid,tid,tt_assigned,tt_expiration,tt_next_tid,tt_unpublish,completed)
722 VALUES (
723 $node->nid,$tid,$tt_assigned,$tt_expiration," . $default_row['tt_next_tid'] . "," . $default_row['tt_unpublish'] . ",0
724 )";
725 //drupal_set_message($insert_statement);
726 db_query( $insert_statement );
727 }
728 /* else {
729 drupal_set_message("$nid has a row for $tid " . $default_row['tt_next_tid'] . " " . $default_row['tt_unpublish'] . " already, ignore.");
730 } */
731 }
732
733 }
734 else {
735 watchdog("taxonomy_timer","we can ignore $tid, no tt defaults");
736 }
737 }
738 }
739
740 function _tt_remove_superfluous_timers( $node ) {
741 // grab all entries in tt nodes and make sure
742 $nid = $node->nid;
743
744 // select only those timers not yet completed that are attached to this node.
745 $statement = "SELECT * FROM {taxonomy_timer_nodes} WHERE nid=$nid AND completed=0";
746 //drupal_set_message("removing superfluous timers for node $nid:<BR> $statement");
747 $sth = db_query( $statement );
748 //drupal_set_message("This statement returned " . db_num_rows( $sth ));
749
750 // loop the rows and for each row, check the defaults for a match on the trifecta (tid + tt_next_tid + tt_unpublish)
751 while ( $tt_node_row = db_fetch_array( $sth ) ) {
752 $superfluous =0;
753
754 // A) is there a corresponding row in tt defaults?
755 $tid = $tt_node_row['tid'];
756 $next = $tt_node_row['tt_next_tid'];
757 $unp = $tt_node_row['tt_unpublish'];
758 $statement = "SELECT COUNT(*) AS thecount FROM {taxonomy_timer_defaults} WHERE tid=$tid AND tt_next_tid=$next AND tt_unpublish=$unp";
759 $counter_sth = db_query($statement);
760 //drupal_set_message("counting: $statement");
761 $row = db_fetch_array( $counter_sth );
762 //drupal_set_message("Counting statement returned " . $row['thecount'] . " rows");
763 if ( !$row['thecount'] ) {
764 // this row is superfluous, delete it
765 // superfluous because there are no rows in defaults that describe this same action
766 $superfluous =1;
767 }
768 else {
769 // B) is the tid described in the row actually assigned to this node?
770 //drupal_set_message("the tt node row exists in tt defaults...check to see if the node has the terms described");
771 //drupal_set_message("TAXONOMY: <PRE>". sprint_r( $node->taxonomy ) . "</pre>");
772 $test_term = taxonomy_get_term( $tt_node_row['tid'] );
773 $test_vid = $test_term->vid;
774 //drupal_set_message("TERM " . $tt_node_row['tid'] . " looks like this:<BR><PRE>" . sprint_r( $test_term ) . "</pre>");
775
776 if ( $node->taxonomy[ "". $test_term->vid ] == $tt_node_row['tid'] ) {
777 // the tid in the tt node row is also a tid assigned to this node...keep this entry
778 //drupal_set_message("Keep this row.... it describes a tid that this node also has.");
779 }
780 else {
781 //drupal_set_message("looked for vid=" . $test_term->vid . " and tid=" . $tt_node_row['tid'] . " in node->tax but I didn't find it.<PRE>" . sprint_r( $node->taxonomy ) . "</pre>");
782 // this row is superfluous becuase it describes a timer, attached to a term that
783 // this node doesn't have, delete it
784 $superfluous =2;
785 }
786 }
787
788 if ( $superfluous ) {
789 $del_statement = "DELETE FROM {taxonomy_timer_nodes} WHERE ttid=" . $tt_node_row['ttid'];
790 db_query($del_statement);
791 //drupal_set_message( "SUPERFLUOUS ROW (s=$superfluous): $del_statement" );
792 }
793 }
794 }
795
796 /*
797 * Implementation of hook_nodeapi()
798 */
799 function taxonomy_timer_nodeapi( &$node, $op) {
800 // making the assumption that rows exist for this node.
801 // therefore, insert/update are totally different clauses.
802 switch($op) {
803 case 'insert':
804 //drupal_set_message("taxonomy timer isn't ready for node inserts yet. Nothing happened.",'error');
805 // take the tids and the nid, add all defaults.
806 $defaults = _set_tt_defaults( $node->nid, $node->type );
807 break;
808 case 'update':
809 // if they have a taxonomy term for which we have defaults, check tt nodes to see if there's data.
810 // if there's NOT data, we need to create a default entry.
811 _tt_assure_defaults_exist( $node );
812
813 // remove superfluous rows (if they removed term 2, and there are no actual
814 // timers for term 2, remove those rows from tt nodes)
815 _tt_remove_superfluous_timers( $node );
816
817 // this function processes the edit form, if it had taxonomy time data in it.
818 // not all node edit forms will have that, but if they do, here's where we handle it.
819 _tt_parse_node_edit_values( $node );
820 break;
821 case 'load':
822 //drupal_set_message("taxonomy timer isn't ready for node load yet. Nothing happened.",'error');
823 break;
824 case 'prepare':
825 //drupal_set_message("taxonomy timer isn't ready for node prepare yet. Nothing happened.",'error');
826 break;
827 case 'view':
828 //drupal_set_message("taxonomy timer isn't ready for node view yet. Nothing happened.",'error');
829 break;
830 case 'delete':
831 $statement = "DELETE FROM {taxonomy_timer_nodes} WHERE nid=" . $node->nid;
832 db_query( $statement );
833 //drupal_set_message($statement);
834 break;
835
836 }
837 }
838
839 // create the "should be part of PHP's core" sprint_r function...but only if I haven't
840 // already added it elsewhere...
841 if (!function_exists('sprint_r')) {
842 function sprint_r($var) {
843 ob_start();
844 print_r($var);
845 $ret = ob_get_contents();
846 ob_end_clean();
847 return $ret;
848 }
849 }
850 ?>

  ViewVC Help
Powered by ViewVC 1.1.2