Kenyu73's Email Hook
From Another Web Company
Create custom email format and send emails to all subscribed members by default
| Kenyu73's email hooks and subscriptions summary TAG Forum Addon/Mod | |
| Description | The following forum Extension/Mod will: |
| Forum Version | 2.5.5 |
| Add-on Version | 1.0 |
| Author | Eric Fortin |
| Extension Date | 3/15/2009 |
| Support | |
| Hooks used: |
awcsforum_add_newpost_thread_subscribe, awcsforum_add_newthread_forum_subscribe, awcsforum_subscribe_get_email_address, awcsforum_send_mail |
| How to install | The code below is an Extension for AWC's Forum. A Forum Extension works the same way as a Mediawiki Extension.
|
<?php # Eric Fortin # 3/15/2009 # localsettings.php: require_once( "$IP/extensions/awc_forum_hook.php" ); # Summary TAG: <awc_summary /> #Purpose: Create custom email format and send emails to all subscribed members by default /* clean up code UPDATE awc_f_mems set m_forum_subsrib=null,m_thread_subsrib=null; DELETE from awc_f_watchforums; DELETE from awc_f_watchthreads; reset stats: Special:AWCforum/admin/re_forum_stats */ $g_AWC_MAX_HISTORY_POSTS = 20; //max history $ge_awc_forum_hook_cls = new ge_awc_forum_hooks; $wgHooks['awcsforum_add_newpost_thread_subscribe'][] = array($ge_awc_forum_hook_cls, 'add_new_post'); $wgHooks['awcsforum_add_newthread_forum_subscribe'][] = array($ge_awc_forum_hook_cls, 'add_new_thread'); $wgHooks['awcsforum_subscribe_get_email_address'][] = array($ge_awc_forum_hook_cls, 'get_email_addresses' ); $wgHooks['awcsforum_send_mail'][] = array($ge_awc_forum_hook_cls, 'send_mail'); $wgExtensionFunctions[] = "wfAWC_Hook_Summary"; // function adds the wiki extension function wfAWC_Hook_Summary() { global $wgParser; $wgParser->setHook( "awc_summary", "build_subscription_summary" ); } function build_subscription_summary(){ $awcSummaryCls = new awcSubscriptionSummary; return $awcSummaryCls->buildSummary(); } class awcSubscriptionSummary { function buildSummary(){ $ret = self::render_awc_summary(); return $ret; } function awc_watched_threads($memID){ $linked_tread_url = "<a href='" . awc_url . "st/id[[id]]'>[[id]]</a>"; $rDB = wfGetDB( DB_SLAVE ); $awc_f_watchthreads = $rDB->tableName( 'awc_f_watchthreads' ); $sql = "SELECT wtcht_thread_id, wtcht_todo FROM $awc_f_watchthreads WHERE wtcht_mem_id=$memID;"; $res = $rDB->query($sql); while ( $r = $rDB->fetchObject($res) ) { $ret .= str_replace( '[[id]]', $r->wtcht_thread_id, $linked_tread_url ) . ", "; } $rDB->freeResult( $res ); return $ret; } function render_awc_summary(){ global $wgParser; $wgParser->disableCache(); $rDB = wfGetDB( DB_SLAVE ); $awc_f_mems = $rDB->tableName( 'awc_f_mems' ); $sql = "SELECT m_ID, m_IDname, m_forum_subsrib, m_thread_subsrib FROM $awc_f_mems;"; $html = ''; $res = $rDB->query($sql); while ($r = $rDB->fetchObject( $res )) { $user = User::newFromName($r->m_IDname); $temp .= "<b>" . $r->m_IDname . "</b> (" . $user->getRealName() .")<br>"; // ID: $r->m_ID<br>"; $forums = unserialize($r->m_forum_subsrib); $threads = unserialize($r->m_thread_subsrib); $temp .= " <i>Forums:</i> "; while (list($id,$str) = each($forums)){ $bFound = true; $temp .= self::get_forum_name($id) . ", "; } $temp .= "<br> <i>Linked Threads:</i> "; $temp .= self::awc_watched_threads($r->m_ID); if( $bFound ) $html .= $temp . "<hr>"; $bFound = false; $temp = ''; } $rDB->freeResult( $res ); return $html; } function get_forum_name($fID){ $rDB = wfGetDB( DB_SLAVE ); $awc_f_forums = $rDB->tableName( 'awc_f_forums' ); $sql = "SELECT f_name FROM $awc_f_forums WHERE f_id=$fID;"; $res = $rDB->query($sql); if ( $r = $rDB->fetchObject($res) ) { $ret = $r->f_name; } $rDB->freeResult( $res ); return $ret; } } class ge_awc_forum_hooks { var $awc_ext_thread = ''; var $awc_new_thread = false; var $members = ''; // capture NEW threads and set flag so we auto-subscribe all // forum members to new threads.... function add_new_thread(&$t_subscribe, &$thread){ $t_subscribe->email_lookup = false; if( $t_subscribe->tID > 0 ) { $this->awc_ext_thread = $t_subscribe; $this->awc_ext_thread->cur_m_posts = $thread->cur_m_posts; $this->awc_new_thread = true; } return true; } // capture standard posts; mainly getting the "post" data // so we can format as needed (header, footer, etc) function add_new_post(&$t_subscribe, &$thread){ $t_subscribe->email_lookup = false; if( $t_subscribe->tID > 0 ) { $this->awc_ext_thread = $t_subscribe; $this->awc_ext_thread->cur_m_posts = $thread->cur_m_posts; $this->awc_new_thread = false; } return true; } // this is "important"; we go through all forum members send an // email and also auto subscribe them... function get_email_addresses(&$address, &$class_info){ if($this->awc_ext_thread == '') return true; $class_info->thread_text_limit = '0'; $rDB = wfGetDB( DB_SLAVE ); $wiki_user = $rDB->tableName( 'user' ); $awc_f_watchforums = $rDB->tableName( 'awc_f_watchforums' ); $awc_f_watchthreads = $rDB->tableName( 'awc_f_watchthreads' ); $awc_f_mems = $rDB->tableName( 'awc_f_mems' ); if($this->awc_new_thread){ $sql = "SELECT wu.user_email, f.wtchf_mem_id as mem_ID, m.m_thread_subsrib FROM $awc_f_watchforums f JOIN $wiki_user wu ON f.wtchf_mem_id=wu.user_id JOIN $awc_f_mems m ON f.wtchf_mem_id=m.m_ID WHERE f.wtchf_forum_id=$class_info->fID"; } else { $sql = "SELECT wu.user_email, t.wtcht_mem_id as mem_ID, m.m_thread_subsrib FROM $awc_f_watchthreads t JOIN $wiki_user wu ON t.wtcht_mem_id=wu.user_id JOIN $awc_f_mems m ON t.wtcht_mem_id=m.m_ID WHERE t.wtcht_thread_id=$class_info->tID"; } $res = $rDB->query($sql); while ($r = $rDB->fetchObject( $res )) { $user = User::newFromID($r->mem_ID); if( $r->user_email != '' ) { $address[] = $r->user_email; $this->members .= $user->getRealName() . ", "; } $threads = unserialize($r->m_thread_subsrib); // we dont need to do a db call for the current user as the AWC code does this if($class_info->cur_memID != $r->mem_ID){ if($this->awc_new_thread == true) { self::subscribe_to_thread( $r->mem_ID, $this->awc_ext_thread->tID, $threads ); //auto subscribe other members } } } $rDB->freeResult( $res ); return true; } // the logic that subscribes the members of a forum function subscribe_to_thread($mem_ID, $tID, $threads){ $wDB = wfGetDB( DB_MASTER ); $wDB->insert( 'awc_f_watchthreads', array( 'wtcht_thread_id' => $tID, 'wtcht_mem_id' => $mem_ID, 'wtcht_todo' => 'email',) ); $threads[$tID] = 'email'; $wDB->update( 'awc_f_mems', array('m_thread_subsrib' => serialize($threads),), array('m_ID' => $mem_ID), '' ); } // this is where we capture the email before AWC sends the email... // we bascially throw out the default body and create our own header, body and footer function send_mail(&$to_address, &$from_address, &$send_title, &$awcsf_css_output, &$send_body, &$reply_address, &$contentType){ if($this->awc_ext_thread == '') return true; $send_title = htmlspecialchars_decode($send_title, ENT_QUOTES); $post = $this->buildHeader() . $this->post_phase( $this->awc_ext_thread->post) . $this->post_phase( $this->buildFooter() ) ; // awc - changed $post .= $this->get_history(); $send_body = $post; $from_address = new MailAddress( "NSC Forums <noreply@ge.com>" ); //$to_address = new MailAddress( "eric.fortin@ge.com" ); // send ALL mails here for testing... return true; } function get_history(){ global $g_AWC_MAX_HISTORY_POSTS; if($this->awc_new_thread) return ''; $ret = "<br><br><b>HISTORY</b>"; $rDB = wfGetDB( DB_SLAVE ); $awc_f_posts = $rDB->tableName( 'awc_f_posts' ); $sql = "SELECT thread_id, p_post, p_user, p_userID, p_date FROM $awc_f_posts WHERE thread_id = " . $this->awc_ext_thread->tID . " ORDER by p_id DESC"; $i=0; $skip = true; //we want to skip the 1st post $res = $rDB->query($sql); while ( ($r = $rDB->fetchObject($res)) && ($i <= $g_AWC_MAX_HISTORY_POSTS) ) { if( !$skip ){ //$posted_date = $r->p_date; $user = User::newFromID($r->p_userID); $ret .= "<hr><u><small><b>Posted by:</b> " . $r->p_user . " <i>(" . $user->getRealName() . ")</i></small></u>"; $ret .= "<br><br>" . $this->post_phase( $r->p_post ) . "<br><br>"; } $skip = false; $i++; } $rDB->freeResult( $res ); return $ret; } function buildHeader(){ $limit = 10; $total_posts = $this->get_thread_totals($this->awc_ext_thread->tID); $postfix = "#wpTextbox1"; if( $total_posts > $limit) { $postfix = "/limit:$total_posts,$limit/#wpTextbox1"; //#last } $html = "[<a href='" . awc_url . 'st/id' . $this->awc_ext_thread->tID . "$postfix'><b><big>reply</big></b></a>]<br><br>"; return $html; } function buildFooter(){ global $wgUser; // some of this is in "wiki syntax" formatting since we're using a wiki compatible parser... $url = awcsf_wiki_url; $userid = $wgUser->getName(); $username = $wgUser->getRealName(); if ($username != '') $username = "(<i>$username</i>)"; $forum_name = $this->get_forum_name($this->awc_ext_thread->fID); $html = "<br><br><hr>" . "<b>Posting by:</b> [[User:$userid | $userid]] " . $username . " - <b>Posts:</b> " . $this->awc_ext_thread->cur_m_posts . "<br><b>Forum:</b> [" . awc_url . "sf/id" . $this->awc_ext_thread->fID . " $forum_name]" . "<br><br><i><small>To unsubscribe to '''this''' thread/topic only, click [" . awc_url . 'post/sub/id' . $this->awc_ext_thread->tID . "/no here].</small></i><br>"; $html .= "<br><small><b>Subscribed Members:</b><i> " . $this->members . "</i></small>"; return $html; } function get_forum_name($fID){ $rDB = wfGetDB( DB_SLAVE ); $awc_f_forums = $rDB->tableName( 'awc_f_forums' ); $sql = "SELECT f_name FROM $awc_f_forums WHERE f_id=$fID"; $res = $rDB->query($sql); if ($r = $rDB->fetchObject( $res )) { $f_name = $r->f_name; } $rDB->freeResult( $res ); return $f_name; } function get_thread_totals($tID){ $rDB = wfGetDB( DB_SLAVE ); $awc_f_threads = $rDB->tableName( 'awc_f_threads' ); $sql = "SELECT t_topics FROM $awc_f_threads WHERE t_id = $tID"; $res = $rDB->query($sql); if ($r = $rDB->fetchObject( $res )) { $total_posts = $r->t_topics; } $rDB->freeResult( $res ); return $total_posts; } // parse wiki and AWC text into nice HTML function post_phase($msg){ require_once(awc_dir . 'includes/post_phase.php'); $this->fix_links($msg); $post_phase = new awcs_forum_post_phase(); $post_phase->convert_wTitle = 0; $post_phase->displaysmiles = 1; $msg = $post_phase->phase_post($msg, 0, false); return $this->fix_links($msg); } // fix wiki relative links --> // bad: href="/wiki/Image:Picture.gif" // good: href="http://server/wiki/Image:Picture.gif" function fix_links($msg){ global $wgServer; $url = $wgServer . "/"; $msg = str_replace( '="/', '="' . $url, $msg ); return $msg; } // debug helper function debug($data){ $file = dirname(__FILE__) . "/debug.txt"; $f = fopen($file, "a+"); fwrite($f, trim($data) . "\n"); fclose($f); } }
| Whos here now: Members 0 Guests 0 Bots & Crawlers 1 |