Book Image

concrete5 Cookbook

Book Image

concrete5 Cookbook

Overview of this book

concrete5 is an increasingly popular open source content management system because of its incredible and easy-to-use interface. But, it also comes complete with a robust PHP framework, allowing web developers to create custom websites and applications with ease. "concrete5 Cookbook" is a practical collection of solutions to tasks that both novice and experienced concrete5 developers face on a regular basis. Readers will learn multiple subjects, including full blueprints for developing an event calendar add-on and an image gallery block. Developers new to concrete5 will quickly learn how to customize concrete5 to their needs, and seasoned pros will find it an excellent quick reference for performing specific tasks. "concrete5 Cookbook" will transform ordinary PHP developers into concrete5 experts capable of bending concrete5 to their will and unleashing the true power of this up-and-coming content management system. Throughout the course of over 140 recipes and 3 bonus project blueprint chapters, PHP developers will learn how to create custom blocks and dashboard interfaces as well as programmatically work with pages, files, users, permissions, and more. Discover the built-in Active Record support that makes working with databases simple and maintainable. Readers will also learn how to take advantage of the numerous helper classes included in concrete5, and will dive deep into the concrete5 MVC framework to create powerful custom websites and applications. Tie together all of the concepts learned in the recipes with 3 bonus chapters featuring complete blueprints to create a calendar add-on, an image gallery block type, and tips on how to sell your themes and add-ons for money! "concrete5 Cookbook" is a complete collection of recipes to solve the most common (and some not-so-common) tasks that concrete5 developers will face on a regular basis.
Table of Contents (19 chapters)
concrete5 Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Getting a list of pages


Many applications will have a need to get a list of pages and perform various filter and sorting functions on the list. For this, there is the PageList class, which exposes many convenient helper functions to assist in working with large lists of pages.

Getting ready

In this example, we are going to find all of the pages under the /blog page. This is a common use of the page list model. The PageList class is not automatically loaded by concrete5, so we will need to use the Loader class to have it available to us.

If your site does not have a page located at /blog, it would be helpful to create that page, as well as a few child pages below it. Otherwise, feel free to adapt the code in this recipe to suit your needs.

How to do it...

In this recipe, we will get a list of 20 pages that exist under /blog in the sitemap. The steps are as follows:

  1. First, we must make the PageList class available for us to use.

    Loader::model('page_list');
  2. Create a new instance of the PageList class. We will assign it to a variable called $list.

    $list = new PageList();
  3. Filter the list to only include pages underneath the /blog page.

    $list->filterByPath('/blog');
  4. Limit the list of pages to only include the first 20 results.

    $list->setItemsPerPage(20);
  5. Run the database query and get the first "page" of results, which will return an array of 20 Page objects.

    $pages = $list->getPage();
  6. Dump the contents of the pages variable to see the array.

    foreach ($pages as $page) {
      echo $page->getCollectionTitle().'<br />';
    }
    exit;

How it works...

The PageList class extends the core DatabaseItemList class, which has a number of useful functions itself. The get() function accepts two parameters, the number of records to retrieve, and the offset.

There's more...

The page list class implements "magic methods," which allow allow us to filter the list by any page attribute. We simply need to create a function name based off of the camel cased version of the attribute handle, preceded by "filterBy". So to filter all pages that have the exclude_nav attribute, we would write the following function:

$list->filterByExcludeNav(true);

Alternatively, if you want to just get an array of the first 99 Page objects and ignore pagination, use the get() function.

$pages = $list->get(99);

Since version 5.5, concrete5 uses Twitter's open source Bootstrap CSS framework for many of their internal styles. You can get the pagination controls in nicely formatted HTML (with relevant Bootstrap CSS classes) by printing/echoing the results of the following function (preferably you'd use this function in a view or template file of a block or single page):

echo $list->displayPagingV2();

The DatabaseItemList class and PageList class have several functions that make working with a collection of pages easier. You can filter by keywords, by collection type, parent page, and by any attribute using magic methods. We will cover the DatabaseItemList class further in Chapter 5, Working with Databases and Models.