Skip to content

Catalog (i18n Special Pages) Example

Lawrence Okoth-Odida edited this page Mar 27, 2016 · 5 revisions

Plugins Required

Outcome

If you have set up a catalog Special Page type with the i18n Special Pages plugin (in conjunction with i18n Search). To ensure that the item IDs don't conflict with existing pages, you may wish to prefix your catalog item entries (e.g. catalog-). At the same time, you might not want this prefixing to negatively impact the URL structure of your catalog. Furthermore, you may want to show all items from a specific category, or the results of a search:

catalog                                    -> show the available categories in the catalog
catalog/category/$category                 -> show items from $category
catalog/item/$item                         -> show item with id catalog-$item
catalog/search                             -> show search page for catalog
catalog/search?words=$query&order=$order   -> show search results for a given $query in $order
catalog/*                                  -> error page for catalog

You can do this with the following routes:

Main page

Route

catalog

Action

<?php

<?php
  
return array(
  'title'   => 'Catalog',
  'content' => function() {
    // Modified from morvy's example
    // http://get-simple.info/forums/showthread.php?tid=7132
    $settings = getXML(GSDATAOTHERPATH . 'i18n_special_catalog.xml');
    $categoryIndex = 0; // set this to the index of your 'category' field
    
    echo '<ul>';
    
    foreach ($settings->fields->item[$categoryIndex]->option as $category) {
      echo '<li><a href="/catalog/category/' . clean_url($category) . '">' . $category . '</a></li>';
    }
    
    echo '</ul>';
  }
);

Category

Route

catalog/category/([a-z0-9-]+)

Action

<?php

return array(
  // Page Title
  'title'   => 'Catalog (Category ' . $args[0] . ')',

  // Page Content
  'content' => function($category) {
    get_i18n_search_results(array(
      'tags' => array(
        '_special_catalog',
        $category,
      ),
    ));
  }
);

Item

Route

catalog/item/([a-z0-9-]+)

Action

<?php

$slug = 'catalog-' . $args[0];

return array(
  'title'   => returnPageField($slug, 'title'),
  'content' => function() use ($slug) {
    // Do whatever displaying you'd like for the item
    getPageContent($slug);
  }
);

Search Form

Route

catalog/search

Action

<?php

return array(
  // Page Title
  'title'   => 'Search Catalog',

  // Page Content
  'content' => function() {
    // Display search form
    // Provides a 'words' text field and an 'order' dropdown selector
    ?>
    <form method="get" action="/catalog/search">
      <input type="text" name="words" placeholder="Search here..."/>
      
      <select name="order">
        <option value="1">ASC</option>
        <option value="0">DESC</option>
      </select>

      <input type="submit" value="Go"/>
    </form>
    <?php
  }
);

Search Results

Route

catalog/search\?words=(.*)\&order=([01])

Action

<?php

// Decode the URL query
$words = urldecode($args[0]);
$order = ($args[1] == '1' ? '+' : '-') . 'pubDate';

return array(
  // Page Title
  'title'   => 'Catalog Search for "' . $words . '"',

  // Page Content
  'content' => function($query, $ord) use ($words, $order) { // import $words and $order into function
    // Show search from with values populated from search
    ?>
    <form method="get" action="/catalog/search">
      <input type="text" name="words" value="<?php echo $words; ?>" placeholder="Search here..."/>
      
      <select name="order">
        <option value="1" <?php echo ($ord)  ? 'selected' : null; ?>>ASC</option>
        <option value="0" <?php echo (!$ord) ? 'selected' : null; ?>>DESC</option>
      </select>

      <input type="submit" value="Go"/>
    </form>
    <?php
    // Show the search results
    get_i18n_search_results(array(
      'words' => $words,
      'order' => $order  // last updated
      'max'   => 999,    // all available entries
    ));
  }
);

Error

Route

catalog/(.*)

Action

<?php

return array(
  // Page Title
  'title'   => 'Catalog page not found',

  // Page Content
  'content' => function($url) {
    echo '<p>Sorry, but this page does not exist in the catalog!</p>';
    echo '<p><a href="/catalog">Back</a></p>';
  }
);