I've got a piece of code and if I place a die statement in that code at the bottom at the normal return location, it seems to work (before dying of course!). Without the die, the code seems not to work. Anybody have any ideas.
Thanks 'yall!

        if ( $last_month<=$date and $date<$this_month ){
                pegevent_set_term_entry( $nid, $terms[6] );
        } elseif ( $this_month<=$date and $date<$next_month ){
                pegevent_set_term_entry( $nid, $terms[7] );
        } elseif ( $next_month<=$date and $date<$n_n_month ){
                pegevent_set_term_entry( $nid, $terms[8] );
        }
        if($dbg){ die( "dbg>> pegevent_set_time_terms called w/ nid $nid, post clear." ); }
} // end of function


function pegevent_set_term_entry( $nid, $tid ){
        # check to see if already marked: avoid duplicate error
        $qs = "select * from term_node where nid = $nid and tid = $tid";
        $qr = db_query( $qs );
        while( $row = mysql_fetch_array( $qr )){
                return; # already marked
        }
        #$qs = "insert into term_node set nid=$nid, tid=$tid";
        $qs = "INSERT INTO term_node (nid, tid) VALUES ($nid, $tid)";
        return( db_query( $qs ) );
}

Comments

nevets’s picture

More context needed, what invokes the code on top and what does "the code seems not to work" mean?

joegml’s picture

See more detail below. "Code seems not to work" means term_node table is not modified as intended.

If I put in the die statement, term_node gets modified, without the die, no modification in term_node.

Thanks.
--
Joe Golden
www.triangul.us : People, Ideas, Connections

criznach’s picture

This may or may not be the problem, but you probably should bracket your table names with {} because that's how db_query handles table prefixing. Like the other person said, we need more context to help. Try placing some print statements in your code to see what the variables are. Anything strange in the logs?

joegml’s picture

I'm testing w/ table names in brackets. That's not doing the trick.
Print statements will not be shown, perhaps because of the calling from hook_nodeapi. I had to put in a die because I was pretty sure the code was being accessed, but wasn't seeing debug print messages. I think the call from hook_nodeapi is important.

function pegevent_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
        if  ( ( $op=="submit" ) and ($node->type == "content_program") ){
                # rename all showings to correspond to program title
                pegevent_rename_showings_of_program( $node->nid );
               # mark as produced last week, this month, etc.
                pegevent_set_time_terms( $node->nid );
        }
}
function pegevent_set_time_terms( $nid ){
# set terms for program production timeframe (yesterday, this week, next month, etc.)
$dbg=1;
# debug statement printed at this point will NOT be seen

#production_yesterday = 200;
#production_today = 201;
# etc.

# order: yest, today, tomorrow, last week, this week, next week
#       last month, this month, next month
$terms = array ( 200, 201, 202, 188, 197, 189, 190, 198, 199 );

foreach ($terms as $term){
        pegevent_clear_term_entry( $nid, $term); # clear all preexisitng marks
}

if ( pegevent_is_program( $nid ) ){
        $pdate = pegevent_get_pdate( $nid );
        # date is pdate
        $date = strtotime( $pdate );
        if($dbg){ print "date is $date<br>";}

} else {
        die(" In pegevent_set_time_terms passed nid $nid. Not a program.<br>");
}
        ### boundary times as unixtimes ##
        ## these should respect DST boundaries ##
        $day_before_yesterday = strtotime("yesterday -1 day");
        $yesterday = strtotime("yesterday");
        $today = strtotime("today");
        $tomorrow = strtotime("tomorrow");

        $last_week = strtotime("Sunday 2 weeks ago");
        $this_week = strtotime("Sunday 1 weeks ago");
        $next_week = strtotime("Sunday");
        $n_n_week = strtotime("Sunday next week");

        $last_month = mktime(0, 0, 0, date("m")-1, 1,   date("Y"));
        $this_month = mktime(0, 0, 0, date("m"), 1,   date("Y"));
        $next_month = mktime(0, 0, 0, date("m")+1, 1,   date("Y"));
        $n_n_month = mktime(0, 0, 0, date("m")+2, 1,   date("Y"));
        ###################################

        # days
        if($dbg){
                print "dbg>> you are in pegevent_set_time_terms: yesterday is $yesterday, tomorrow is $tomorrow, date is $date<br>";
        }
        if ( $day_before_yesterday<=$date and $date<$yesterday){
                pegevent_set_term_entry( $nid, $terms[0] );
        } elseif ( $yesterday<=$date and $date<$today){
                pegevent_set_term_entry( $nid, $terms[1] );
        } elseif ( $today<=$date and $date<$tomorrow){
                pegevent_set_term_entry( $nid, $terms[2] );
        }
        # weeks
        if ( $last_week<=$date and $date<$this_week){
                pegevent_set_term_entry( $nid, $terms[3] );
        } elseif ( $this_week<=$date and $date<$next_week){
                pegevent_set_term_entry( $nid, $terms[4] );
        } elseif ( $next_week<=$date and $date<$n_n_week){
                pegevent_set_term_entry( $nid, $terms[5] );
        }
        # months
        if ( $last_month<=$date and $date<$this_month ){
                pegevent_set_term_entry( $nid, $terms[6] );
        } elseif ( $this_month<=$date and $date<$next_month ){
                pegevent_set_term_entry( $nid, $terms[7] );
        } elseif ( $next_month<=$date and $date<$n_n_month ){
                pegevent_set_term_entry( $nid, $terms[8] );
        }
        if($dbg){ die( "dbg>> pegevent_set_time_terms called w/ nid $nid, post clear." ); }
        # works w/ die, doesn't work without die
}
function pegevent_clear_term_entry( $nid, $tid ){
        # clear entry from term_node for nid, tid
        $qs = "delete from {term_node} where nid = $nid and tid = $tid";
        db_query( $qs );
        # nothing to return
}
function pegevent_set_term_entry( $nid, $tid ){
        # check to see if already marked: avoid duplicate error
        $qs = "select * from {term_node} where nid = $nid and tid = $tid";
        $qr = db_query( $qs );
        while( $row = mysql_fetch_array( $qr )){
                return; # already marked
        }       
        #$qs = "insert into term_node set nid=$nid, tid=$tid";
        $qs = "INSERT INTO {term_node} (nid, tid) VALUES ($nid, $tid)";
        return( db_query( $qs ) );
}

Thanks a lot for any insight or ideas. Hope this is sufficient detail.
--
Joe Golden
www.triangul.us : People, Ideas, Connections

nevets’s picture

Problem is you are modifying the term_node table directly (generally not approriate) and in this the die "short circtuits" other code which may allow your code to work but could cause problems.

So to change how it works, change

pegevent_set_time_terms( $nid )

to

pegevent_set_time_terms( &$node )

Note you will need to change the call(s) to pegevent_set_time_terms() and in the function change any uses of $nid to $node->nid.

Then change

foreach ($terms as $term){
        pegevent_clear_term_entry( $nid, $term); # clear all preexisitng marks
}

to

foreach ($terms as $term){
        unset($node->taxonomy[$term]);
}

Now change each line like (there are several)

 pegevent_set_term_entry( $nid, $terms[8] );

to

$node->taxonomy[$terms[8] ] = $terms[8] ;
joegml’s picture

I'm trying this out and parts seem to work and others don't. The code is firing at the correct time. I'm only testing at node modification at present.

I'm getting a persistent entry in term_node for term 199, that doesn't seem to be cleared by

foreach ($terms as $term){
        unset($node->taxonomy[$term]);
}

Also, the date recognition seems off now, whereas before, it was fine. I'm not sure where this is coming from.

Thanx.
--
Joe Golden : www.triangul.us : People, Ideas, Connections

nevets’s picture

Do other tems clear correctly?

As for the date recognition, did you change

pegevent_is_program( $nid )

to

pegevent_is_program( $node->nid )

and change

pegevent_get_pdate( $nid );

to

pegevent_get_pdate( $node->nid );
joegml’s picture

Dates don't clear correctly. The 199 doesn't seem to be sticking anymore, but the date recognition is confused. The *old version* (prior to your recommended changes) of the function works correctly when called as a php page (not in the nodeapi context). So I'm pretty sure the date recognition logic is good.

<?php
print "running pegevent_set_time_terms( 49776 );<br>";
pegevent_set_time_terms( 49776 );
?>

The new version ... I'm not sure.
Yes I changed to $node->nid in both places you mentioned.
When I change the date to 2007, which should match NO terms for pdate, I get

mysql> select * from term_node where nid = 49776;
+-------+-----+
| nid   | tid |
+-------+-----+
| 49776 | 198 |
| 49776 | 197 |
| 49776 | 202 |
| 49776 | 189 |
| 49776 | 230 |
+-------+-----+
5 rows in set (0.00 sec)

mysql> # above is for pdate 2007: should only be term 230 > live show

Is there a way to check that the term clearing is working?

I'm gonna try a direct call via php to the new version and see if that works smoothly. That might help isolate the nodeapi part.
--
Joe Golden : www.triangul.us : People, Ideas, Connections

nevets’s picture

Right after the loop that is suppose to clear any terms add a return statement. Edit a node and save, it should not have terms from the ones you have listed.

joegml’s picture

My previous clearing code works correctly for this test, but I know it may be too brutish ;-) *wait*, no it doesn't. the 198 seems to keep dangling there w/ pegevent_clear_term_entry( $node->nid, $term ); code as well.

mysql> select * from term_node where nid = 49776;
+-------+-----+
| nid   | tid |
+-------+-----+
| 49776 | 198 |
| 49776 | 197 |
| 49776 | 230 |
+-------+-----+
3 rows in set (0.00 sec)

mysql> select * from term_node where nid = 49776;
+-------+-----+
| nid   | tid |
+-------+-----+
| 49776 | 198 |
| 49776 | 230 |
+-------+-----+
2 rows in set (0.00 sec)

mysql> # return added post unset loop: seems to leave one term dangling, the 230 should always be there
joegml’s picture

The problem could be related to the taxo term assignment, as this seems to be confused rather than the date recognition code.

Any links for how to assign the taxo terms as the node is built or modified?
--
Joe Golden
www.triangul.us : People, Ideas, Connections

nevets’s picture

What does that part of the code look like after your changes?

nevets’s picture

Change

foreach ($terms as $term){
        unset($node->taxonomy[$term]);
}

to

drupal_set_message('BEFORE<pre>' . print_r($node->taxonomy, TRUE) . '</pre>');
foreach ($terms as $term){
        unset($node->taxonomy[$term]);
}
drupal_set_message('AFTER<pre>' . print_r($node->taxonomy, TRUE) . '</pre>');
return;

What to the before and after results look like?

joegml’s picture

both the same:

Array                           
(                               
    [31] =>                     
    [37] =>                     
    [47] => Array               
        (                       
        )                       
                                
    [46] => Array               
        (                       
        )                       
                                
    [39] =>                     
    [tags] => Array
        (
            [34] => 
        )

    [38] => Array
        (
            [253] => 0
            [254] => 0
            [255] => 0
            [256] => 0
            [257] => 0
        )

    [35] => Array
        (
            [312] => 0
            [221] => 0
            [222] => 0
            [223] => 0
            [224] => 0
            [parent] => 0
            [305] => 0
            [306] => 0
            [304] => 0
        )

    [36] => Array
        (
            [229] => 0
            [230] => 230
            [231] => 0
        )

    [23] => 190
)
joegml’s picture

Almost nailed it. Crux of the solution shown below for general edification. Thanx Nevets!

# clear vocabulary 23
$node->taxonomy[23] = array( 200=>0, 201=>0, 202=>0, 188=>0, 197=>0, 189=>0, 190=>0, 198=>0, 199=>0 );
# ...
# mark w/ correct term based on time match
#yesterday
        if ( $yesterday<=$date and $date<$today){
                $node->taxonomy[23][$terms[0] ] = $terms[0] ;
# etc.