Bridging Etomite and SMF

The snippet
You'll also need another PHP file to actually display the postings from SMF under the article inside Etomite.

First, create a new snippet in Etomite named Include. Copy and paste the following code in to the new snippet.

PHP:
  1. if ( !isset($phpfile) || $phpfile == "" ) return "No file specified."; //check if there's a file given.
  2.  
  3. ob_start();//Start the buffer
  4.  
  5. include $phpfile; //include
  6.  
  7. $ob_contents = ob_get_contents();//get contents from the buffer
  8. ob_end_clean();//and kill/delete the buffer
  9. return $ob_contents;//return it to etomite.

If you're interested, what this snippet does is turn on output buffering and captures the output from the include. Otherwise Etomite would throw an error.

The PHP File
You are also going to need to create a new php file. Personally I stored mine in the assets/scripts/ folder because that made the most sense to me, but you can store it where ever you want.

For the sake of this tutorial I'm going to assume the file is saved as assets/scripts/articlethreadcache.php.

PHP:
  1. <?php
  2. /*  paf_articlethreadcache.php
  3.     Returns the article thread cache, rebuilding it
  4.     if needed.  */
  5.  
  6. /*  Path to cache */
  7. $cachePath = '/full/path/to/a/cache/folder/';
  8. if (!isset($article_id))
  9.     {
  10.     die('Article id must be set by the template.');
  11.     }
  12.  
  13. if (!file_exists($cachePath.'forum_discussion'.$article_id.'.cache'))
  14.     {
  15.     rebuild_cache($article_id);
  16.     }
  17.  
  18. if (file_exists($cachePath.'forum_discussion'.$article_id.'.cache'))       
  19.     {
  20.     clearstatcache();
  21.     $filetime = filemtime($cachePath.'forum_discussion'.$article_id.'.cache');
  22.     if ((date('U') - $filetime)> 10*60)
  23.     //  Rebuild cache if more than 10 minutes old
  24.         {
  25.         rebuild_cache($article_id);
  26.         }
  27.         $fhandle = fopen($cachePath.'forum_discussion'.$article_id.'.cache', 'r');
  28.         echo fread($fhandle, filesize($cachePath.'forum_discussion'.$article_id.'.cache'));
  29.         fclose($fhandle);
  30.     }
  31.    
  32.  
  33.  
  34. function rebuild_cache($article_id)
  35.     {
  36.     global $cachePath;
  37.     //  Open the database record
  38.     $conn = mysql_connect('localhost', 'username', 'password');
  39.     $db = mysql_select_db('u_etomite');
  40.     $rs = mysql_query("SELECT * FROM article_forum WHERE article_id=".$article_id);
  41.     if (mysql_num_rows($rs)<1)
  42.         {
  43.         return false;
  44.         }
  45.    
  46.     $row = mysql_fetch_array($rs);
  47.    
  48.     $topic_id = $row['topic_id'];
  49.    
  50.     $output = '<div class="article_discussion">';
  51.     $output .= '<h2 class="article_discussion_header">Discuss this article</h2>';
  52.     $output .= '<p class="article_discussion_subhead">Visit our forums to <a href="http://forums.photoartsforum.com/index.php?topic='.$topic_id.'.0">discuss this article</a>.</p>';
  53.     $output .= '</div>';
  54.     $output .= "\n\n";
  55.  
  56.     $conn = mysql_connect('localhost', 'username', 'password');
  57.     $db = mysql_select_db('u_smf');
  58.     $rs = mysql_query("SELECT posterTime, ID_TOPIC, ID_MSG, subject, posterName, body FROM smf_messages WHERE ID_TOPIC=".$topic_id." ORDER BY posterTime DESC LIMIT 5");
  59.    
  60.     //  Get the first posting from this thread
  61.     $rs_first = mysql_query("SELECT ID_FIRST_MSG FROM smf_topics WHERE ID_TOPIC=".$topic_id);
  62.     $row_first = mysql_fetch_row($rs_first);
  63.     $first_id = $row_first[0];
  64.    
  65.     if (mysql_num_rows($rs)>1)
  66.         {
  67.         $output .= '<p class="recent_discussions_header">Recent Discussions</p>';
  68.         }
  69.    
  70.     while ($row = mysql_fetch_array($rs))
  71.         {
  72.         if ($row['ID_MSG']!=$first_id)
  73.             {
  74.             $body_text = shorten_string(strip_bbc($row['body']), 20);
  75.            
  76.             $output .= '<div class="article_discussion_single">';
  77.             $output .= '<a class="article_forum_link" href="http://www.example.com/index.php?topic='.$topic_id.'.msg'.$row['ID_MSG'].'#msg'.$row['ID_MSG'].'">'.$row['subject'].'</a><br>';
  78.             $output .= '<span class="article_forum_date">'.date('M jS, Y', $row['posterTime']).' @ '.date('g:i a').'</span>&nbsp;&nbsp;<span class="article_forum_author">'.$row['posterName'].'</span>';
  79.             $output .= '<div class="article_forum_blurb">'.$body_text.'</div>';
  80.             $output .= '</div>';
  81.             $output .= "\n\n";
  82.             }
  83.         }   
  84.        
  85.     //  Open file for output
  86.     $fhandle = fopen($cachePath.'forum_discussion'.$article_id.'.cache', 'w');
  87.     fwrite($fhandle, $output);
  88.     fclose($fhandle);
  89.    
  90.     }
  91.  
  92. function strip_bbc($string)
  93.     {
  94.     $retval = $string;  //   Just in case
  95.     $retval = preg_replace('/\[.*?\]/', '', $retval);
  96.     return $retval;
  97.     }
  98.  
  99. function shorten_string($string, $wordsreturned)
  100. /*  Returns the first $wordsreturned out of $string.  If string
  101.     contains more words than $wordsreturned, the entire string
  102.     is returned.
  103.     */
  104.     {
  105.     $retval = $string//    Just in case of a problem
  106.     $array = explode(" ", $string);
  107.     if (count($array)<=$wordsreturned)
  108.     /*  Already short enough, return the whole thing
  109.         */
  110.         {
  111.         $retval = $string;
  112.         }
  113.     else
  114.     /*  Need to chop of some words
  115.         */
  116.         {
  117.         array_splice($array, $wordsreturned);
  118.         $retval = implode(" ", $array)." ...";
  119.         }
  120.     return $retval;
  121.     }
  122.  
  123. ?>

On line 7 you're going to need to enter a path that is writeable by the web server, and it needs to be a complete path starting from the server root not from your web root.

On lines 38 & 56 you are going to need to enter the server, username, and password to connect to your MySQL databases.

I set my installation to rebuild the cache files if it's more than 10 minutes old. If you want to shorten or lengthen that time you can change it on line 22.

And most of the html that's output is wrapped in CSS classes that can be easily changed by using your external CSS files.

And the template file
Now you need to insert one line in to the template you're using for your articles. For my setup I have a separate template for articles that are being linked to the forums so that if I can use that template.

Here's the line:

HTML:
  1. [!Include?article_id=[*id]&phpfile=assets/scripts/articlethreadcache.php!]

Notice that we are using the variable [*id*] in the template so that it'll work for any page. You could also enter this string on individual posts if you don't want to have it on every template.

So, what's it look like?
I have a very simple article on DPI and web design that links back to the forum where you can see what it looks like article side. Notice that the most recent, in this case 2, follow up posts from the forum are displayed under the article with links back to the forum.

For the forum side, look at the posting that's tied to the article.

Help!!
I've tried to explain this as simply as possible, but it was a very convoluted process to get working so I imagine you may have some questions. Feel free to post away in the comments below and I'll see if I can help you along.

Pages: 1 2 3 4 5

Question, Comments...

Do you have more questions. Please either leave a comment below or join us in our new forum.

Leave a Reply