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
    This snippet was inspired by the work of garryn and patricksamshire but has been re-written from scratch for Revolution and getResources.

    It creates an A to Z header and an alphabetical listing of selected resources.

    To use it, download and install getResources and create a getResources tag that displays all the resources you want to show.

    Next, create a snippet called SiteAtoZ with the code below.

    Then change "getResources" in the tag to "SiteAtoZ"

    Once that works, you can add in any of the optional properties listed below.

    <?php
    /**
     * SiteAtoZ snippet
     * @author Bob Ray <http://bobsguides.com>
     * @version 1.0.0
     * 01/25/2011
     *
     * This snippet was inspired by the work of
     * garryn, patricksamshire, and OpenGeek
    */
    /** This snippet makes use of getResources to list records alphabetically, with an A to Z header of links to anchors in the text.
     *
     * By far the best practice to get it working is to call getResources directly with
     * a snippet tag in a resources. Once you get it working, and it shows every resource
     * you want to index, just change "getResources" to "SiteAtoZ" in the snippet tag.
     * Once that's working, you can add any of the optional parameters listed below.
     *
     * All of the parameters for getResources will be passed through. They can be used
     * to select the parents, sort field, tpl for each entry, etc.
     *
     * The &resources parameter can only be used to exclude resources (&resources=`-12,19`),
     * using it to include docs not work because getResources will include those resources
     * regardless of any other criteria and they will be included in every alphabet section.
     * The &where parameter will be ignored becuase it interferes with the selection by initial letter.
     *
     * Simple use:
     * [[!SiteAtoZ? &parents=`6` &tpl=`MyTpl`]]
     *
     * Required parameters:
     * ---------------
     * @property parents - (string) Comma-separated list of ID's of container documents you want included (`0` for all docs)
     * @property tpl - (string) Tpl chunk used to format each entry; Default 'AzItemTpl'
     *
     * Optional parameters:
     * ---------------
     * @property useNumbers - (boolean) Put a number array in front of the alphabet' default '0'
     * @property useAlphabet - (boolean) Use the Alphabet; default: '1'
     * @property headingSeparator - (string) separator to use between letters in heading; Default '&nbsp| '
     * @property alphabetHeadingStart - (string) Letter to start with; Default: 'A'
     * @property alphabetHeadingEnd - (string) Letter to end with; Default 'Z'
     * @property title - (string) Field to used for search; Default: pagetitle
     * @property headingLinksTpl - (string) A tpl containing the entire A-Z heading (useful if you'd like to use images)
     *
     * All other parameters are those of getResources. They should all work as they do for getResources with two exceptions:
     * @property resources can be used to exclude documents, but not to include them (@property resources=`-2,24`).
     * @property where will be ignored (it conflicts with the selection by initial letter).
     */
    
    $sp =& $scriptProperties;
    $documentId = $modx->resource->get('id');
    
    
    $sp['tpl'] = empty($sp['tpl'])? 'AzItemTpl' : $sp['tpl'] ;
    
    $sp['parents'] = empty($sp['parents'])? '0' : $sp['parents'];
    $headingSeparator = empty($sp['headingSeparator'])? '<span class="az-separator"> | </span>'. "\n" : $sp['headingSeparator'] . "\n";
    $title = empty($sp['title'])? 'pagetitle' : $sp['title'];
    $alphabetHeadingStart = (empty($sp['alphabetHeadingStart']))? 'A' : $sp['alphabetHeadingStart'];
    $alphabetHeadingEnd = (empty($sp['alphabetHeadingEnd']))? 'Z' : $sp['alphabetHeadingEnd'];
    $useNumbers = $sp['useNumbers'] === '1'? true : false;
    $useAlphabet = $sp['useAlphabet'] === '0'? false: true;
    
    $n = range('0','9');
    $a = range($alphabetHeadingStart, $alphabetHeadingEnd);
    $alphabet = array();
    
    if ($useNumbers) {
        $alphabet=$n;
    }
    if ($useAlphabet) {
        $alphabet = array_merge($alphabet,$a);
    }
    unset($n,$a);
    
    $whereProperty = !empty($sp['where'])? $sp['where'] : false;
    foreach ($alphabet as $k=>$v) {
        $local_where = array(
            $title . ':LIKE' => $v . '%',
        );
        /* ToDo: Some day this may work */
        /*if ($whereProperty !== false) {
            $w = $modx->fromJSON($sp['where']);
            $local_where = array_merge($local_where,$w);
            unset($w);
        }*/
        $sp['where'] = $modx->toJSON($local_where);
    
        $ret = $modx->runSnippet('getResources',$sp);
        if (empty($ret)) {
            $header[] = $v;
        } else {
            $header[] = '<a class="az-headeritem" href="'.$modx->makeUrl($documentId).'#jump_to_' . $v . '">' . $v . '</a>';
            $output .= '<p class="az-anchor">' . "<a name=\"jump_to_$v\" id=\"jump_to_$v\"></a><strong>" . $v . "</strong></p>\n";
            $output .= $ret;
        }
    }
    
    $headingLinks = (empty($sp['headingLinksTpl']))? implode($headingSeparator,$header) : $modx->getChunk($sp['headingLinksTpl']);
    
    return $headingLinks . $output;
    
    
      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
      • 13284
      • 95 Posts
      BobRay,

      As requested I’m posting a few questions for your A to Z snippet.

      My call currently:
      [[!SiteAtoZ? &parents=`8` &includeContent=`1` &useNumbers=`1` &tpl=`AtoZ` &showHidden=`true`]]


      My template chunk:
      <a href="[[~[[+id]]]]" title="[[+pagetitle]]">[[+pagetitle]]</a><br />


      For anyone out there since it uses getresources you must have the &showHidden=`true` if you have items in the parent hidden from the menu for the AtoZ snippet to display.

      It is currently running on: http://www.tapanime.com/humor/drinking/

      Some things I’m not 100% how to do with the script any thoughts or help would be great:

      1.) the numbers being displayed display 0 to 9 each number at a time. Is it possible to display just 0-9 like the old atoz snippet?

      2.) I’d like to have the snippet display only the 0-9 and letters, then when a user clicks on said 0-9 or letter it will display the links starting with the letter under that parent (like the old AtoZ snippet, instead of having it display the full list and doing a jump down to that letter.
        • 3749
        • 24,544 Posts
        Quote from: Izumi at Jan 27, 2011, 03:14 PM

        BobRay,
        Some things I’m not 100% how to do with the script any thoughts or help would be great:

        1.) the numbers being displayed display 0 to 9 each number at a time. Is it possible to display just 0-9 like the old atoz snippet?

        2.) I’d like to have the snippet display only the 0-9 and letters, then when a user clicks on said 0-9 or letter it will display the links starting with the letter under that parent (like the old AtoZ snippet, instead of having it display the full list and doing a jump down to that letter.
        Hi Izumu,

        I would have to rewrite the snippet for that. The first one requires JS to show/hide the links and the second would require rewriting the search criterion for numbers to be an array. I’m not sure I have time for either right now. Sorry.
          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
          Ok Izumy,
          Here’s the version with the numbers in one section (I hope). I hadn’t thought of using REGEX for it. It won’t be very fast, but you should be able to cache the page anyway.

          Add these parameters:

          &useNumbers=`1`
          &combineNumbers=`1`


          Here’s the snippet code:
          <?php
          /**
           * SiteAtoZ snippet
           * @author Bob Ray <http://bobsguides.com>
           * @version 1.0.1
           * 01/25/2011
           *
           * This snippet was inspired by the work of
           * garryn, patricksamshire, and OpenGeek
          */
          /** This snippet makes use of getResources to list records alphabetically, with an A to Z header of links to anchors in the text.
           *
           * By far the best practice to get it working is to call getResources directly with
           * a snippet tag in a resources. Once you get it working, and it shows every resource
           * you want to index, just change "getResources" to "SiteAtoZ" in the snippet tag.
           * Once that's working, you can add any of the optional parameters listed below.
           *
           * All of the parameters for getResources will be passed through. They can be used
           * to select the parents, sort field, tpl for each entry, etc.
           *
           * The &resources parameter can only be used to exclude resources (&resources=`-12,19`),
           * using it to include docs not work because getResources will include those resources
           * regardless of any other criteria and they will be included in every alphabet section.
           * The &where parameter will be ignored becuase it interferes with the selection by initial letter.
           *
           * Simple use:
           * [[!SiteAtoZ? &parents=`6` &tpl=`MyTpl`]]
           *
           * Required parameters:
           * ---------------
           * @property parents - (string) Comma-separated list of ID's of container documents you want included (`0` for all docs)
           * @property tpl - (string) Tpl chunk used to format each entry; Default 'AzItemTpl'
           *
           * Optional parameters:
           * ---------------
           * @property useNumbers - (boolean) Put a number array in front of the alphabet' default '0'
           * @property combineNumbers (boolean) group 0-9 titles together; default '0'
           * @property useAlphabet - (boolean) Use the Alphabet; default: '1'
           * @property headingSeparator - (string) separator to use between letters in heading; Default '&nbsp| '
           * @property alphabetHeadingStart - (string) Letter to start with; Default: 'A'
           * @property alphabetHeadingEnd - (string) Letter to end with; Default 'Z'
           * @property title - (string) Field to used for search; Default: pagetitle
           * @property headingLinksTpl - (string) A tpl containing the entire A-Z heading (useful if you'd like to use images)
           * @property noData - (string) string to show if search comes up empty
           *
           * All other parameters are those of getResources. They should all work as they do for getResources with two exceptions:
           * @property resources can be used to exclude documents, but not to include them (@property resources=`-2,24`).
           * @property where will be ignored (it conflicts with the selection by initial letter).
           */
          
          /* http://support.internetconnection.net/CODE_LIBRARY/Javascript_Show_Hide.shtml */
          
          $sp =& $scriptProperties;
          $documentId = $modx->resource->get('id');
          
          
          $sp['tpl'] = empty($sp['tpl'])? 'AzItemTpl' : $sp['tpl'] ;
          
          $sp['parents'] = empty($sp['parents'])? '0' : $sp['parents'];
          $headingSeparator = empty($sp['headingSeparator'])? '<span class="az-separator"> | </span>'. "\n" : $sp['headingSeparator'] . "\n";
          $title = empty($sp['title'])? 'pagetitle' : $sp['title'];
          $alphabetHeadingStart = (empty($sp['alphabetHeadingStart']))? 'A' : $sp['alphabetHeadingStart'];
          $alphabetHeadingEnd = (empty($sp['alphabetHeadingEnd']))? 'Z' : $sp['alphabetHeadingEnd'];
          $useNumbers = $sp['useNumbers'] === '1'? true : false;
          $combineNumbers = $sp['combineNumbers'] === '1'? true : false;
          $useAlphabet = $sp['useAlphabet'] === '0'? false: true;
          
          if ($combineNumbers) {
              $n = array('[0-9]');
          } else {
              $n = range('0','9');
          }
          $a = range($alphabetHeadingStart, $alphabetHeadingEnd);
          $alphabet = array();
          
          if ($useNumbers) {
              $alphabet=$n;
          }
          if ($useAlphabet) {
              $alphabet = array_merge($alphabet,$a);
          }
          unset($n,$a);
          
          $whereProperty = !empty($sp['where'])? $sp['where'] : false;
          
          $noData = true;
          foreach ($alphabet as $k=>$v) {
              if ($combineNumbers && ($v == '[0-9]') ) {
                  $local_where = array(
                      $title . ':REGEXP' => '^[0-9]',
                  );
              } else {
                  $local_where = array(
                      $title . ':LIKE' => $v . '%',
                  );
              }
              /* ToDo: Some day this may work */
              /*if ($whereProperty !== false) {
                  $w = $modx->fromJSON($sp['where']);
                  $local_where = array_merge($local_where,$w);
                  unset($w);
              }*/
              $sp['where'] = $modx->toJSON($local_where);
          
              $ret = $modx->runSnippet('getResources',$sp);
              if (empty($ret)) {
                  $header[] = $v;
              } else {
                  $noData = false; /* found at least one */
                  $header[] = '<a class="az-headeritem" href="'.$modx->makeUrl($documentId).'#jump_to_' . $v . '">' . $v . '</a>';
                  $output .= '<p class="az-anchor">' . "<a name=\"jump_to_$v\" id=\"jump_to_$v\"></a><strong>" . $v . "</strong></p>\n";
                  $output .= $ret;
              }
          }
          if ($noData === true) {
              $modx->setPlaceholder('noData',$sp['noData']);
          }
          $headingLinks = (empty($sp['headingLinksTpl']))? implode($headingSeparator,$header) : $modx->getChunk($sp['headingLinksTpl']);
          
          return $headingLinks . "\n" . '<p class="az-noData">[[+noData]]</p>' . "\n" . $output;
          
          
            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
            • 13284
            • 95 Posts
            BobRay,

            Lots of thanks wasn’t expecting you to get around to doing this for awhile since you guys are busy with so many other things. Yes that worked just fine for the numbers 0-9, still same site if you wish to see it.

            http://www.tapanime.com/humor/drinking/
              • 3749
              • 24,544 Posts
              I did add a JS option and some CSS because I thought it would be more fun than what I was supposed to be doing (kind of wrong on that one).

              I hope to have it out as a package in a day or two.

              The code is here if you want to play with it: https://github.com/BobRay/SiteAtoZ
                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
                • 13284
                • 95 Posts
                BobRay,

                I’ve played with it a little bit today. Everything is working smoothly. I was able to figure out the css which I’m not good at. Works perfectly for how I have it set.

                Here is my call:

                [[!SiteAtoZ? &parents=`8` &useNumbers=`1` &combineNumbers=`1` &tpl=`AtoZ` &showHidden=`true` &useJS=`1` &cssFile=`/assets/templates/tapanime/style/siteatoz2.css`]]
                  • 3749
                  • 24,544 Posts
                  Hi Izumi,

                  It should be available through Package Manager now. I fixed some bugs and I think the CSS file has better comments in it to explain things. Your snippet tag looks good.
                    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
                    • 28948
                    • 42 Posts
                    Wanted to say a giant “Thanks!” to BobRay for this package. Exactly what I needed.
                      --
                      cal wilson
                      oksushi.com
                      • 3749
                      • 24,544 Posts
                      Quote from: sushi at Mar 02, 2011, 09:33 PM

                      Wanted to say a giant “Thanks!” to BobRay for this package. Exactly what I needed.

                      I’m glad that it works for you. smiley
                        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