Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Random Quotes ?

edited December 2006 in Vanilla 1.0 Help
has anyone tackled this ? adding a random text quote in vanilla
«1

Comments

  • edited September 2006
    Hey. Funny, I was just trying to figure out how to adapt a Joomla! module as a Vanilla extension. Here's what I borrowed and messed up so far:

    $Context->SetDefinition('RandomQuoteError','Cannot find the quotes file.'); // Path to the randomquotes text file $quotesPath = 'extensions/RandomQuotes'; // Name of the randomquotes text file $quotesSource = 'quotes.txt'; $GLOBAL["quotesPath"] = $quotesPath; $lineSeparator = "\n"; $quoteFile = fopen( implode("/", array ( $quotesPath, $quotesSource)), "r") or DIE($this->Context->GetDefinition('RandomQuoteError')); while (!feof($quoteFile)) { $quotesString .= fread($quoteFile, 1024); } // Here we split it into lines $quotes = explode( $lineSeparator, $quotesString); // And then randomly choose a line $chosen = $quotes[ mt_rand(0, count($quotes) - 1 ) ]; // This just echoes the chosen line, we'll position it later preg_match ( "/^([^:]+).(.*)/", $chosen, $matches); list ($nada, $quote ) = $matches; echo <<<EOF <div id=RandomQuotes> <table align=right> <tr> <td>$quote</td> </tr> </table> </div> EOF; ?>

    It works, but I get the following error:
    Notice: Undefined variable: quotesString in ..\extensions\RandomQuotes\default.php on line 24

    I followed Mark's documentation and tried to understand how to give it a place under the forum title, but this far beyond my skills. For now it appears in the top right corner and displays random quote from a .txt file. I no idea how to make this a real extension (well, technically it is one, it appears in the setting panel, but... :B) I also can't see how to add the error message dictionary defintion.

    So, if somebody could explain what to change to make it Vanilla compliant, it would be great :)
  • You're getting the notice becuase $quotesString isn't declared as something before you start concantenating to it. To place it under the forum title you'd probably want to use one of the delegates in themes/menu.php.
  • edited September 2006
    Okay, I think I see the logic behind what you suggest, but I've no idea how to use a delegate. I'd like to, but I barely understand the very basis of PHP syntax and I don't know where to start. Looks like theme/menu.php has something to do with library/Framework/Framework.Control.Head.php, but I'm stuck with my lame copy-and-paste method which works fine for CSS but definitely not with PHP...

    The good new is I don't get the notice anymore since I've put:

    $quotesString = '';
    above
    while (!feof($quoteFile)) { $quotesString .= fread($quoteFile, 1024); }

    Dunno if it's elegant or even correct, but it seems to work.
  • I tried your code but it doesn't show up in the extensions settings is that all the code ? I notice you have a <?php missing up top but i'm still unable to get it in the list
  • No, it lacks the standard extension header. Anyway (sorry for people who don't like this word :P) it still won't be displayed properly until I find a way to integrate it under the forum's title. For now it just appears at the very top of the page. I guess you could fix this with CSS, but it would be kind of a ugly hack, nothing you could really call an extension.
  • how are you displaying the quotes in the text file.txt as a single line any code to go with it ?
  • Each line in the .txt stands for one quote.
  • BUMP

    Since Pol seems to be in an extension-writing frenzy phase at the moment, I was wondering he could have a look at this...
    I'd be pretty glad to have this finally working.
  • I have tested the following and it works. I removed your table output and simplified it. Add in the appropriate information in the heading area. <?php /* Extension Name: RandomQuotes Extension Url: http://www.xxx.com Description: This will produce a random quote in the side panel. Version: 1.0 Author: Curious George Author Url: http://www.xxx.com */ // add hook $Context->SetDefinition('RandomQuoteError','Cannot find the quotes file.'); // Path to the randomquotes text file $quotesPath = 'extensions/RandomQuotes'; // Name of the randomquotes text file $quotesSource = 'quotes.txt'; $GLOBAL["quotesPath"] = $quotesPath; $lineSeparator = "\n"; // add hook - thanks to Notify extension if (in_array($Context->SelfUrl, array("index.php", "account.php", "categories.php", "comments.php", "post.php", "search.php", "settings.php"))) { $Head->AddStyleSheet('extensions/RandomQuotes/style.css'); $quotesString = ''; $quoteFile = fopen( implode("/", array ( $quotesPath, $quotesSource)), "r") or DIE($this->Context->GetDefinition('RandomQuoteError')); while (!feof($quoteFile)) { $quotesString .= fread($quoteFile, 1024); } fclose($quoteFile); // Here we split it into lines $quotes = explode( $lineSeparator, $quotesString); // And then randomly choose a line $chosen = $quotes[ mt_rand(0, count($quotes) - 1 ) ]; // This just echoes the chosen line, we'll position it later preg_match ( "/^([^:]+).(.*)/", $chosen, $matches); list ($nada, $quote ) = $matches; $quotedisplay = '<br /><h2>Todays Quote</h2>'; // $quotedisplay .= '<div id=RandomQuotes><table align=right><tr><td>'.$quote.'</td></tr></table></div>'; $quotedisplay .= '<br /><p class="quote">' .$quote .'</p>'; $Panel->AddString($quotedisplay, 180); } ?> This is what I have in the style.css file: #Panel p.quote span { font-color: #000000; width:180px; }
  • This is great, thank you a lot jimw :D

    But would there be a way to display the quote somewhere in the forum header? Like under, above or instead of the forum title? Or maybe where notices and warnings are usually shown?
  • To put it in a notice, change the $Panel->AddString to $NoticeCollector->AddNotice
  • edited November 2006
    The syntax would be $NoticeCollector->AddNotice($quotedisplay); I just tried it and it works fine. Thanks, wallphone. Is there a way to close the notice? That would be cool.
  • Here is another version to put the quote above the header or below it. I couldn't figure out how to put it all into one extension. And if you are using the Announcement extension, you could put just the code in it (with some minor adjustments, of course). <?php /* Extension Name: RandomQuotes Extension Url: http://www.xxx.com Description: This will produce a random quote in the side panel. Version: 1.0 Author: Curious George Author Url: http://www.xxx.com */ // add hook $Context->SetDefinition('RandomQuoteError','Cannot find the quotes file.'); if (in_array($Context->SelfUrl, array("index.php", "account.php", "categories.php", "comments.php", "post.php", "search.php", "settings.php"))) { $Head->AddStyleSheet('extensions/RandomQuotes/style.css'); // for a header banner quote class QuoteBanner extends Control { function QuoteBanner(&$Context) { $this->Name = "QuoteBanner"; $this->Control($Context); } function Render() { $quotesPath = 'extensions/RandomQuotes'; // Path to the randomquotes text file $quotesSource = 'quotes.txt'; // Name of the randomquotes text file $GLOBAL["quotesPath"] = $quotesPath; $lineSeparator = "\n"; $quotesString = ''; $quoteFile = fopen( implode("/", array ( $quotesPath, $quotesSource)), "r") or DIE($this->Context->GetDefinition('RandomQuoteError')); while (!feof($quoteFile)) { $quotesString .= fread($quoteFile, 1024); } fclose($quoteFile); $quotes = explode( $lineSeparator, $quotesString); // Here we split it into lines $chosen = $quotes[ mt_rand(0, count($quotes) - 1 ) ]; // And then randomly choose a line // This just echoes the chosen line, we'll position it later preg_match ( "/^([^:]+).(.*)/", $chosen, $matches); list ($nada, $quote ) = $matches; $quotedisplay = '<br /><h2>Todays Quote: </h2>'; $quotedisplay .= '<p class="quote">' .$quote .'</p>'; echo '<div class="BannerContainer"> <div class="Banner"> <div class="bannerMenu">'; echo $quotedisplay; echo '</div> </div> </div>'; } } $QuoteBanner = new QuoteBanner($Context); // only use one of the following - comment out the one you dont want to use // the following adds the quote at the top of the screen // $Page->AddRenderControl($QuoteBanner,$Configuration["CONTROL_POSITION_HEAD"]+1); // the following adds the quote after the header and before any Announcement you have set $Page->AddRenderControl($QuoteBanner,$Configuration["CONTROL_POSITION_BODY_ITEM"]-2); } ?> I changed the stylesheet a little: .BannerContainer { background: url(images/bannerfadeblue3.gif) top left #ffffff; text-align: center; font-size:11px; font-family: Arial; } .Banner { margin-left: auto; margin-right: auto; width: 80%; text-align: left; } .Banner { padding-top: 5px; padding-bottom: 5px; } #Panel p.quote span { font-color: #000000; width:180px; }
  • Many thanks to both of you, it works great!
  • Are you going to make this an add-on and upload it or both of them?
  • Well yeah, I could upload it, but you did all the hard work here. Of course I can write a read me file and a description, however I don't see how I could fix anything if someone has a problem with the extension. I'll need to bug you if support is needed...
  • That's not a problem. The reason I jumped in to help was becuase I had some experience with quotes scripts. I even developed a more elaborate WP quotations plugin after I tried others.
  • Fine, I'll upload it today then. Thanks again for your help.
  • edited November 2006
    Okay, it's done. I included both your panel and header versions in the package and added a couple of things to the CSS file.
    I also added a dictionary definition for the extension's title (Today's Quote by default) but for some reason beyond my skills, it didn't work for the panel version. I guess it has something to do with $Panel->AddString($quotedisplay, 180);, but I'm not sure how to handle this.
  • I just installed your version and it works for me. What other add-ons are you using that put things in the sidepanel?
This discussion has been closed.