Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/02/2009 07:54:21 PM (17 years ago)
Author:
apeatling
Message:

Merging 1.1 branch changes and syncing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-messages/bp-messages-classes.php

    r2020 r2077  
    11<?php
    22
    3 Class BP_Messages_Thread { 
     3Class BP_Messages_Thread {
    44        var $thread_id;
    55        var $message_ids;
    66        var $first_post_date;
    7        
     7
    88        var $last_post_date;
    99        var $last_sender_id;
    … …  
    1212        var $last_message_message;
    1313        var $last_message_date_sent;
    14        
     14
    1515        var $messages = null;
    1616        var $has_access = false;
    1717        var $unread_count = 0;
    1818        var $recipients = null;
    19        
     19
    2020        var $box;
    2121        var $get_all_messages;
    22        
     22
    2323        function bp_messages_thread( $id = null, $get_all_messages = false, $box = 'inbox' ) {
    2424                $this->box = $box;
    2525                $this->get_all_messages = $get_all_messages;
    26                
     26
    2727                if ( $id ) {
    2828                        $this->populate( $id );
    2929                }
    30                
     30
    3131                if ( $this->get_all_messages ) {
    3232                        $this->messages = $this->get_messages();
    … …  
    3434                }
    3535        }
    36        
    37         function populate( $id ) {     
     36
     37        function populate( $id ) {
    3838                global $wpdb, $bp;
    3939
    4040                $thread = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_threads} WHERE id = %d", $id ) );
    41                
     41
    4242                if ( $thread ) {
    43                        
     43
    4444                        // If we're only viewing a thread in a list of threads, don't bother using
    4545                        // up resources checking if we have access. Only check if we're viewing the full
    … …  
    5757                                // filter the users messages out
    5858                                if ( !$this->get_all_messages ) {
    59                                        
     59
    6060                                        // Flip the array to start from the newest message
    6161                                        $this->message_ids = array_reverse( $this->message_ids );
    62                                        
     62
    6363                                        foreach ( $this->message_ids as $key => $message_id ) {
    6464                                                if ( 'sentbox' == $this->box ) {
    … …  
    6767                                                        } else {
    6868                                                                break;
    69                                                         }                                               
     69                                                        }
    7070                                                } else {
    7171                                                        if ( messages_is_user_sender( $bp->loggedin_user->id, $message_id ) ) {
    … …  
    7373                                                        } else {
    7474                                                                break;
    75                                                         }                                                       
     75                                                        }
    7676                                                }
    7777                                        }
    … …  
    8686                                $this->first_post_date = $thread->first_post_date;
    8787                                $this->last_post_date = $thread->last_post_date;
    88                                
     88
    8989                                if ( !empty($this->message_ids) )
    9090                                        $this->message_ids = implode( ',', $this->message_ids );
    9191                                else
    9292                                        $this->message_ids = false;
    93                                
     93
    9494                                $this->unread_count = $this->get_unread();
    9595
    96                                 $last_message = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE id = %d", $this->last_message_id ) ); 
    97                                
     96                                $last_message = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_messages} WHERE id = %d", $this->last_message_id ) );
     97
    9898                                if ( $last_message ) {
    9999                                        $this->last_message_subject = $last_message->subject;
    … …  
    101101                                        $this->last_message_date_sent = $last_message->date_sent;
    102102                                }
    103                                
     103
    104104                                $this->recipients = $this->get_recipients();
    105105                        }
    106106                }
    107107        }
    108        
     108
    109109        function get_messages() {
    110110                global $wpdb, $bp;
    111                        
     111
    112112                if ( $this->message_ids)
    113113                        return $wpdb->get_results( "SELECT * FROM {$bp->messages->table_name_messages} WHERE id IN (" . $wpdb->escape($this->message_ids) . ")" );
    … …  
    121121                $sql = $wpdb->prepare( "SELECT unread_count FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d AND user_id = %d", $this->thread_id, $bp->loggedin_user->id );
    122122                $unread_count = $wpdb->get_var($sql);
    123                
     123
    124124                return $unread_count;
    125125        }
    126        
     126
    127127        function mark_read() {
    128128                BP_Messages_Thread::mark_as_read($this->thread_id);
    129129        }
    130        
     130
    131131        function mark_unread() {
    132132                BP_Messages_Thread::mark_as_unread($this->thread_id);
    133133        }
    134        
     134
    135135        function get_recipients() {
    136136                global $wpdb, $bp;
    … …  
    144144                                $recipient_ids[] = $recipient;
    145145                }
    146                
     146
    147147                return $recipient_ids;
    148148        }
    149        
     149
    150150        /** Static Functions **/
    151        
     151
    152152        function delete( $thread_id ) {
    153153                global $wpdb, $bp;
    154                
     154
    155155                $delete_for_user = $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET is_deleted = 1 WHERE thread_id = %d AND user_id = %d", $thread_id, $bp->loggedin_user->id ) );
    156                
     156
    157157                // Check to see if any more recipients remain for this message
    158158                // if not, then delete the message from the database.
    … …  
    163163                        $message_ids = $wpdb->get_var( $wpdb->prepare( "SELECT message_ids FROM {$bp->messages->table_name_threads} WHERE id = %d", $thread_id ) );
    164164                        $message_ids = unserialize($message_ids);
    165                        
     165
    166166                        // delete thread:
    167167                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_threads} WHERE id = %d", $thread_id ) );
    168                        
     168
    169169                        // delete messages:
    170170                        for ( $i = 0; $i < count($message_ids); $i++ ) {
    171171                                $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_messages} WHERE id = %d", $message_ids[$i] ) );
    172172                        }
    173                        
     173
    174174                        // delete the recipients
    175175                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_recipients} WHERE id = %d", $thread_id ) );
    … …  
    178178                return true;
    179179        }
    180        
     180
    181181        function get_current_threads_for_user( $user_id, $box = 'inbox', $limit = null, $page = null, $type = 'all' ) {
    182182                global $wpdb, $bp;
    … …  
    185185                if ( $limit && $page )
    186186                        $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
    187                
     187
    188188                if ( $type == 'unread' )
    189189                        $type_sql = $wpdb->prepare( " AND r.unread_count != 0 " );
    190190                else if ( $type == 'read' )
    191191                        $type_sql = $wpdb->prepare( " AND r.unread_count = 0 " );
    192                        
     192
    193193                $sql = $wpdb->prepare( "SELECT r.thread_id FROM {$bp->messages->table_name_recipients} r, {$bp->messages->table_name_threads} t WHERE t.id = r.thread_id AND r.is_deleted = 0 AND r.user_id = %d$exclude_sender $type_sql ORDER BY t.last_post_date DESC$pag_sql", $bp->loggedin_user->id );
    194194
    195195                if ( !$thread_ids = $wpdb->get_results($sql) )
    196196                        return false;
    197                
     197
    198198                $threads = false;
    199                
     199
    200200                for ( $i = 0; $i < count($thread_ids); $i++ ) {
    201201                        $threads[$i] = new BP_Messages_Thread( $thread_ids[$i]->thread_id, false, $box );
    … …  
    206206
    207207                // reset keys
    208                 return array_reverse( array_reverse( $threads ) ); 
    209         }
    210        
     208                return array_reverse( array_reverse( $threads ) );
     209        }
     210
    211211        function mark_as_read( $thread_id ) {
    212212                global $wpdb, $bp;
    213                
     213
    214214                $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 0 WHERE user_id = %d AND thread_id = %d", $bp->loggedin_user->id, $thread_id );
    215215                $wpdb->query($sql);
    216216        }
    217        
     217
    218218        function mark_as_unread( $thread_id ) {
    219219                global $wpdb, $bp;
    220                
     220
    221221                $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_recipients} SET unread_count = 1 WHERE user_id = %d AND thread_id = %d", $bp->loggedin_user->id, $thread_id );
    222222                $wpdb->query($sql);
    223223        }
    224        
     224
    225225        function get_total_threads_for_user( $user_id, $box = 'inbox', $type = 'all' ) {
    226226                global $wpdb, $bp;
    … …  
    229229                if ( $box != 'sentbox' )
    230230                        $exclude_sender = ' AND sender_only != 1';
    231                
     231
    232232                if ( $type == 'unread' )
    233233                        $type_sql = $wpdb->prepare( " AND unread_count != 0 " );
    … …  
    237237                return (int) $wpdb->get_var( $wpdb->prepare( "SELECT count(thread_id) FROM {$bp->messages->table_name_recipients} WHERE user_id = %d AND is_deleted = 0$exclude_sender $type_sql", $user_id ) );
    238238        }
    239        
     239
    240240        function user_is_sender($thread_id) {
    241241                global $wpdb, $bp;
    242                
    243                 $sender_ids = $wpdb->get_var( $wpdb->prepare( "SELECT sender_ids FROM {$bp->messages->table_name_threads} WHERE id = %d", $thread_id ) );       
    244                
     242
     243                $sender_ids = $wpdb->get_var( $wpdb->prepare( "SELECT sender_ids FROM {$bp->messages->table_name_threads} WHERE id = %d", $thread_id ) );
     244
    245245                if ( !$sender_ids )
    246246                        return false;
    247                        
     247
    248248                $sender_ids = unserialize($sender_ids);
    249                
     249
    250250                return in_array( $bp->loggedin_user->id, $sender_ids );
    251251        }
    … …  
    255255
    256256                $sql = $wpdb->prepare("SELECT last_sender_id FROM {$bp->messages->table_name_threads} WHERE id = %d", $thread_id);
    257        
     257
    258258                if ( !$sender_id = $wpdb->get_var($sql) )
    259259                        return false;
    260                
     260
    261261                return bp_core_get_userlink( $sender_id, true );
    262262        }
    263        
     263
    264264        function get_inbox_count() {
    265265                global $wpdb, $bp;
    … …  
    269269                if ( !$unread_counts = $wpdb->get_results($sql) )
    270270                        return false;
    271                
     271
    272272                $count = 0;
    273273                for ( $i = 0; $i < count($unread_counts); $i++ ) {
    274274                        $count += $unread_counts[$i]->unread_count;
    275275                }
    276                
     276
    277277                return $count;
    278278        }
    279        
     279
    280280        function check_access( $thread_id, $user_id = false ) {
    281281                global $wpdb, $bp;
    282                
     282
    283283                if ( !$user_id )
    284284                        $user_id = $bp->loggedin_user->id;
    285                
     285
    286286                return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d AND user_id = %d", $thread_id, $user_id ) );
    287287        }
    … …  
    289289        function is_valid( $thread_id ) {
    290290                global $wpdb, $bp;
    291                
     291
    292292                return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_threads} WHERE id = %d", $thread_id ) );
    293293        }
    294        
     294
    295295        function get_recipient_links($recipients) {
    296296                if ( count($recipients) >= 5 )
    … …  
    311311        var $message;
    312312        var $date_sent;
    313        
     313
    314314        var $thread_id;
    315315        var $recipients = false;
    … …  
    325325                }
    326326        }
    327        
     327
    328328        function populate( $id ) {
    329329                global $wpdb, $bp;
    330                
     330
    331331                $sql = $wpdb->prepare("SELECT * FROM {$bp->messages->table_name_messages} WHERE id = %d", $id);
    332332
    … …  
    340340
    341341        }
    342        
    343         function send() {       
    344                 global $wpdb, $bp;
    345                
     342
     343        function send() {
     344                global $wpdb, $bp;
     345
    346346                $this->sender_id = apply_filters( 'messages_message_sender_id_before_save', $this->sender_id, $this->id );
    347347                $this->subject = apply_filters( 'messages_message_subject_before_save', $this->subject, $this->id );
    348348                $this->message = apply_filters( 'messages_message_content_before_save', $this->message, $this->id );
    349                 $this->date_sent = apply_filters( 'messages_message_date_sent_before_save', $this->date_sent, $this->id ); 
     349                $this->date_sent = apply_filters( 'messages_message_date_sent_before_save', $this->date_sent, $this->id );
    350350
    351351                do_action( 'messages_message_before_save', $this );
    352                
     352
    353353                // First insert the message into the messages table
    354354                if ( !$wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_messages} ( sender_id, subject, message, date_sent ) VALUES ( %d, %s, %s, FROM_UNIXTIME(%d) )", $this->sender_id, $this->subject, $this->message, $this->date_sent ) ) )
    355355                        return false;
    356                        
     356
    357357                // Next, if thread_id is set, we are adding to an existing thread, if not, start a new one.
    358358                if ( $this->thread_id ) {
    … …  
    362362                        $message_ids[] = $wpdb->insert_id;
    363363                        $message_ids = serialize($message_ids);
    364                        
     364
    365365                        // We need this so we can return the new message ID.
    366366                        $message_id = $wpdb->insert_id;
    367                        
     367
    368368                        // Update the sender ids for the thread
    369369                        $sender_ids = unserialize($the_ids->sender_ids);
    … …  
    371371                        if ( !in_array( $this->sender_id, $sender_ids ) || !$sender_ids )
    372372                                $sender_ids[] = $this->sender_id;
    373                                
    374                         $sender_ids = serialize($sender_ids);                   
    375                        
     373
     374                        $sender_ids = serialize($sender_ids);
     375
    376376                        // Update the thread the message belongs to.
    377377                        $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_threads} SET message_ids = %s, sender_ids = %s, last_message_id = %d, last_sender_id = %d, last_post_date = FROM_UNIXTIME(%d) WHERE id = %d", $message_ids, $sender_ids, $wpdb->insert_id, $this->sender_id, $this->date_sent, $this->thread_id ) );
    378                                                
     378
    379379                        // Find the recipients and update the unread counts for each
    380380                        if ( !$this->recipients )
    381381                                $this->recipients = $this->get_recipients();
    382                        
     382
    383383                        for ( $i = 0; $i < count($this->recipients); $i++ ) {
    384384                                if ( $this->recipients[$i]->user_id != $bp->loggedin_user->id )
    … …  
    390390                        $serialized_message_id = serialize( array( (int)$message_id ) );
    391391                        $serialized_sender_id = serialize( array( (int)$bp->loggedin_user->id ) );
    392                        
    393                         $sql = $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_threads} ( message_ids, sender_ids, first_post_date, last_post_date, last_message_id, last_sender_id ) VALUES ( %s, %s, FROM_UNIXTIME(%d), FROM_UNIXTIME(%d), %d, %d )", $serialized_message_id, $serialized_sender_id, $this->date_sent, $this->date_sent, $message_id, $this->sender_id ); 
    394                        
     392
     393                        $sql = $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_threads} ( message_ids, sender_ids, first_post_date, last_post_date, last_message_id, last_sender_id ) VALUES ( %s, %s, FROM_UNIXTIME(%d), FROM_UNIXTIME(%d), %d, %d )", $serialized_message_id, $serialized_sender_id, $this->date_sent, $this->date_sent, $message_id, $this->sender_id );
     394
    395395                        if ( false === $wpdb->query($sql) )
    396396                                return false;
    397                        
     397
    398398
    399399                        $this->thread_id = $wpdb->insert_id;
    400                        
     400
    401401                        // Add a new entry for each recipient;
    402402                        for ( $i = 0; $i < count($this->recipients); $i++ ) {
    403403                                $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_recipients} ( user_id, thread_id, unread_count ) VALUES ( %d, %d, 1 )", $this->recipients[$i], $this->thread_id ) );
    404404                        }
    405                        
     405
    406406                        if ( !in_array( $this->sender_id, (array)$this->recipients ) ) {
    407407                                // Finally, add a recipient entry for the sender, as replies need to go to this person too.
    … …  
    409409                        }
    410410                }
    411                
     411
    412412                $this->id = $message_id;
    413413                messages_remove_callback_values();
    414414
    415415                do_action( 'messages_message_after_save', $this );
    416                
     416
    417417                return $this->thread_id;
    418418        }
    419        
     419
    420420        function get_recipients() {
    421421                global $bp, $wpdb;
    422                
     422
    423423                return $wpdb->get_results( $wpdb->prepare( "SELECT user_id FROM {$bp->messages->table_name_recipients} WHERE thread_id = %d", $this->thread_id ) );
    424424        }
    425        
     425
    426426        // Static Functions
    427        
     427
    428428        function get_recipient_ids( $recipient_usernames ) {
    429429                if ( !$recipient_usernames )
    430430                        return false;
    431        
     431
    432432                if ( is_array($recipient_usernames) ) {
    433433                        for ( $i = 0; $i < count($recipient_usernames); $i++ ) {
    … …  
    436436                        }
    437437                }
    438                
     438
    439439                return $recipient_ids;
    440440        }
    441        
     441
    442442        function get_last_sent_for_user( $thread_id ) {
    443443                global $wpdb, $bp;
    444                
     444
    445445                $message_ids = $wpdb->get_var( $wpdb->prepare( "SELECT message_ids FROM {$bp->messages->table_name_threads} WHERE id = %d", $thread_id ) );
    446446                $message_ids = implode( ',', unserialize($message_ids));
    … …  
    448448                $sql = $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_messages}  WHERE sender_id = %d AND id IN (" . $wpdb->escape($message_ids) . ") ORDER BY date_sent DESC LIMIT 1", $bp->loggedin_user->id );
    449449                return $wpdb->get_var($sql);
    450         }       
    451        
     450        }
     451
    452452        function is_user_sender( $user_id, $message_id ) {
    453453                global $wpdb, $bp;
    454454                return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_messages} WHERE sender_id = %d AND id = %d", $user_id, $message_id ) );
    455455        }
    456        
     456
    457457        function get_message_sender( $message_id ) {
    458458                global $wpdb, $bp;
    459                 return $wpdb->get_var( $wpdb->prepare( "SELECT sender_id FROM {$bp->messages->table_name_messages} WHERE id = %d", $message_id ) );             
     459                return $wpdb->get_var( $wpdb->prepare( "SELECT sender_id FROM {$bp->messages->table_name_messages} WHERE id = %d", $message_id ) );
    460460        }
    461461}
    … …  
    467467        var $date_sent;
    468468        var $is_active;
    469        
     469
    470470        function bp_messages_notice($id = null) {
    471471                if ( $id ) {
    … …  
    474474                }
    475475        }
    476        
     476
    477477        function populate() {
    478478                global $wpdb, $bp;
    479                
     479
    480480                $notice = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_notices} WHERE id = %d", $this->id ) );
    481                
     481
    482482                if ( $notice ) {
    483483                        $this->subject = $notice->subject;
    … …  
    487487                }
    488488        }
    489        
     489
    490490        function save() {
    491491                global $wpdb, $bp;
    492                
     492
    493493                $this->subject = apply_filters( 'messages_notice_subject_before_save', $this->subject, $this->id );
    494494                $this->message = apply_filters( 'messages_notice_message_before_save', $this->message, $this->id );
    495495
    496496                do_action( 'messages_notice_before_save', $this );
    497                
     497
    498498                if ( !$this->id ) {
    499                         $sql = $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_notices} (subject, message, date_sent, is_active) VALUES (%s, %s, FROM_UNIXTIME(%d), %d)", $this->subject, $this->message, $this->date_sent, $this->is_active ); 
     499                        $sql = $wpdb->prepare( "INSERT INTO {$bp->messages->table_name_notices} (subject, message, date_sent, is_active) VALUES (%s, %s, FROM_UNIXTIME(%d), %d)", $this->subject, $this->message, $this->date_sent, $this->is_active );
    500500                } else {
    501                         $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_notices} SET subject = %s, message = %s, is_active = %d WHERE id = %d", $this->subject, $this->message, $this->is_active, $this->id );                               
    502                 }
    503        
     501                        $sql = $wpdb->prepare( "UPDATE {$bp->messages->table_name_notices} SET subject = %s, message = %s, is_active = %d WHERE id = %d", $this->subject, $this->message, $this->is_active, $this->id );
     502                }
     503
    504504                if ( !$wpdb->query($sql) )
    505505                        return false;
    506                
     506
    507507                if ( !$id = $this->id )
    508508                        $id = $wpdb->insert_id;
    509                        
     509
    510510                // Now deactivate all notices apart from the new one.
    511511                $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_notices} SET is_active = 0 WHERE id != %d", $id ) );
    512                
    513                 update_usermeta( $bp->loggedin_user->id, 'last_activity', date( 'Y-m-d H:i:s' ) ); 
     512
     513                update_usermeta( $bp->loggedin_user->id, 'last_activity', date( 'Y-m-d H:i:s' ) );
    514514
    515515                do_action( 'messages_notice_after_save', $this );
    516                                
     516
    517517                return true;
    518518        }
    519        
     519
    520520        function activate() {
    521521                $this->is_active = 1;
    522522                if ( !$this->save() )
    523523                        return false;
    524                        
     524
    525525                return true;
    526526        }
    527        
     527
    528528        function deactivate() {
    529529                $this->is_active = 0;
    530530                if ( !$this->save() )
    531531                        return false;
    532                
     532
    533533                return true;
    534534        }
    535        
     535
    536536        function delete() {
    537537                global $wpdb, $bp;
    538                
     538
    539539                $sql = $wpdb->prepare( "DELETE FROM {$bp->messages->table_name_notices} WHERE id = %d", $this->id );
    540                
     540
    541541                if ( !$wpdb->query($sql) )
    542542                        return false;
    543                
    544                 return true;   
    545         }
    546        
     543
     544                return true;
     545        }
     546
    547547        // Static Functions
    548        
     548
    549549        function get_notices() {
    550550                global $wpdb, $bp;
    551                
     551
    552552                $notices = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->messages->table_name_notices} ORDER BY date_sent DESC" ) );
    553553                return $notices;
    554554        }
    555        
     555
    556556        function get_total_notice_count() {
    557557                global $wpdb, $bp;
    558                
     558
    559559                $notice_count = $wpdb->get_var( $wpdb->prepare( "SELECT count(id) FROM " . $bp->messages->table_name_notices ) );
    560560                return $notice_count;
    561561        }
    562        
     562
    563563        function get_active() {
    564564                global $wpdb, $bp;
    565                
     565
    566566                $notice_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->messages->table_name_notices} WHERE is_active = 1") );
    567567                return new BP_Messages_Notice($notice_id);
Note: See TracChangeset for help on using the changeset viewer.