We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    I’ve run into something odd with EZFaq in 0.9.7 that’s probably obvious to people more familiar with the snippets are processed.

    If I paste the code from ezfaq.inc.php into the snippet and save it, all is well.

    If I put it in a file and do a straight include (as follows), the snippet works but not correctly. I think none of the javascript is executed.
    include "ezfaq.inc.php";

    If, OTOH, I put the following in the snippet, it works as it should:
    return (include "ezfaq.inc.php");


    It bothers me that I don’t understand why this is. I’m also wondering if there’s anything I can modify that will make it work as a straight include.

    Here’s the snippet code (minus some comments and parameter setting) :

    <?php
    /*
    
    Snippet: EZfaq
    $Revision: 26 $
    $Date: 2008-03-14 21:27:09 -0400 (Fri, 14 Mar 2008) $
    Author:  Bob Ray (based on an idea from SorenG JavaScript code from DynamicDrive.com)
    Version: 1.0.4
    Compatibility: MODX 0.9.6, 0.9.7
    Thanks to: jaredc for suggestions on .css usage and cssPath parameter
     */
    
    /************************************************************
    Usage (see readme.txt for installation information and usage)
    -----
    
    Minimal EZfaq call:
    -------------------
    
    [[EZfaq? &docID=`12`]]
    
    &docID is required.
    
    ***************************************************/
    
    $faqPath = "assets/snippets/ezfaq/"; // set path to resources (language file, .css, .js)
    
    
    $error_message = ""; // initialize error message variable
    
    /* ********************************************
    Make sure we have what we need before proceding
    ***********************************************/
    
    $lang = isset($lang)?$lang:'en'; // set default language
    require_once $modx->config['base_path'].$faqPath."lang/".$lang.".inc.php"; // get the language file
    
    if (!isset($docID)) {  // user didn't send docID parameter
        return $faq_lang['ezfaq-docID-required'];
    }
    
    $doc = $modx->getDocument((string)$docID, '*', 1); // Search published first.
    if (empty($doc)) {
        $doc = $modx->getDocument((string)$docID, '*', 0); // Un-published next?
    }
    if (empty($doc)) { // user requested a non-existing document
       return $faq_lang['ezfaq-doc-not-found'];
    }
    
    /****************************************************************************************
    Now that we have the language file and the document with the FAQ
    content, we'll plug in the .css and .js and  initialize the optional parameters.
    *****************************************************************************************/
    
    /*  Inject .css file into document header - maybe.
        Use $cssPath=`` if you want to put the .css in your site's .css file
        rather than using a separate .css file for the FAQ
    */
    if (isset($cssPath) ) {    // user has set this parameter
        if ($cssPath == "") {
            // do nothing, user doesn't want a separate .css file
        } else { // user has specified the .css file to use
            $src = $cssPath;
            $modx->regClientCSS($src);
        }
    } else { // not set, use the default .css file
    
        $src = $faqPath."ezfaq.css";
        $modx->regClientCSS($src);
    }
    
    // inject the .js into the document header.
    $src = $faqPath."switchcontent.js";
    $modx->regClientStartupScript($src);
    
    
    // Work starts here
    
    //replace "EQUALS" in URLs with "="
    $statusOpenHTML = str_replace("EQUALS","=",$statusOpenHTML);
    $statusClosedHTML = str_replace("EQUALS","=",$statusClosedHTML);
    
    
    $output = "";
    
    
    $output .= '<div class="faqExpand">';
    $output .= '<p>'.$faq_lang['ezfaq-show-hide-msg'].'</p>';
    if ($showHideAllOption) {
    $output .= '<a href="javascript:faq.sweepToggle('."'expand')".'"> '.$faq_lang['ezfaq-expand-button-msg'].' </a>';
    $output .='    ';
    $output .= '<a href="javascript:faq.sweepToggle('."'contract')".'"> '.$faq_lang['ezfaq-contract-button-msg'].' </a>';
    }
    $output .= '</div>';
    
    
    $docString = $doc['content'];
    $docArray = explode("Q:",$docString);
    
    //$i = count($docArray);
    // echo "count: ".$i.'<br>';
    
    $itemCount=0; // used to make every id different
    
    
    $output .= '<div class="faqContainer">'."\n";
    foreach($docArray as $value) {
      $items = explode("A:",$value);
          if (stristr($items[0],"FAQ-END")) {
             break;
          }
    
      if ( ($items[0] != "") && ($items[1] != "") ) {
    
         $output .= '<p id="faq'.$itemCount.'-title" class="handcursor">';
         $output .= '<span class="faqQuestion">';
         $output .= $items[0];
         $output .= '</span>';
         $output .= '</p>';
         $output .= '<div id="faq'.$itemCount.'" class="switchgroup1">';
            //$output .= '<p class="faqAnswer">';
         $output .= $items[1];
            //$output .= '</p>';
         $output .= '</div>';
    
      }
      $itemCount++;
    }
    $output .= '</div>'."\n"; // end of faqContainer div
    
    $output .= '<script type="text/javascript">;'."\n";
    $output .= 'var faq=new switchcontent("switchgroup1", "div");'."\n";  //Limit scanning of switch contents to just "div" elements
    
    
    $output .= "faq.setStatus('".$statusOpenHTML." ','".$statusClosedHTML." ');"."\n"; // set open/closed indicators
    $output .= 'faq.setColor("'.$openColor.'", "'.$closedColor.'");'."\n"; // set open/closed text colors
    $output .= 'faq.setPersist('.$setPersist.');'."\n";
    $output .= 'faq.collapsePrevious('.$collapsePrevious.');'."\n"; //Only one content open at any given time
    $output .= 'faq.defaultExpanded("'.$defaultExpanded.'");'."\n"; // expand some on open?
    $output .= 'faq.init();'."\n";
    $output .= '</script>'."\n";
    
    //$output .='<hr>'.$doc['content'];
    
    return $output;
    ?>


    Any thoughts?

    Other suggestions on improving the code also welcome.

    Bob

      Did I help you? Buy me a beer
      Get my Book: MODX:The Official Guide
      MODX info for everyone: http://bobsguides.com/modx.html
      My MODX Extras
      Bob's Guides is now hosted at A2 MODX Hosting
      • 22303 MODX Staff
      • 10,725 Posts
      Your include returns content explictly, so in order to render it via a snippet, you must return include(...); it. If it echo’d the content you could simply include it.
        • 3749
        • 24,544 Posts
        Quote from: OpenGeek at May 04, 2008, 05:47 PM

        Your include returns content explictly, so in order to render it via a snippet, you must return include(...); it. If it echo’d the content you could simply include it.

        That makes perfect sense. Thanks! It also explains why SPForm works with a straight include since it echoes everything.

          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 3749
          • 24,544 Posts
          PS: Can I get set up for whatever our current process is to get EZFaq into the available 0.9.7 add-on pipeline?
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting