Step 5.1 – Administer static pages for our multilanguage CMS – listing the created pages

Created at: March 31, 2015; Last update: December 8, 2015

(created at: March 31, 2015; last update: December 08, 2015)
Now that we’ve created a page, we want to see all pages in a list. So let’s create an index() method in our Pages controller.

What we want is the pages with translations, we also should limit the results so that the list would not be too long. So why not limit the results to 30 “pages”. How can we do that. Fortunately, MY_Model (https://github.com/avenirer/CodeIgniter-MY_Model) has helper functions that will make our work easier. Also, it will help us by giving us the translations for the pages.

So how do we do this? We first retrieve the pages:

Now, we can verify that we’ve retrieved the pages:

Well… it’s only one page… But where is the translation? To retrieve the translations of the pages we first need to establish a relationship between our models.

Let’s see… A page has many translations, and a translation has one page. Right?

How do we set these relationships? We define them in the models that extended the MY_Model. According to the documentation, we have to define the $has_many property.

We first start with the Page_model model:

See line 9? That’s it. We defined a “has many” relationship. For what we need, that is all. But let’s also define the one has one relationship inside our Page_translation_model model. For this we need to set the $has_one property:

…and that’s it. As you can see, in both cases we’ve passed to the property an array which has as elements: the model name, the foreign key and the local relationship key.

Now returning to our controller we only have to add a new method in the method chaining. So, instead of writing: $pages = $this->page_model->paginate(30,$total_pages); , we write: $pages = $this->page_model->with(‘translations’)->paginate(30,$total_pages);

Now, how do we order the list? How about using the created_at and modified_at fields to order the list descending?

After this we only need to pass the pages to our view. But before we do that, let’s clean it a bit and make sure it is a list and not only one row (or else we would get a big error…):

Now we send the list of pages to the view: $this->data[‘pages’] = $list_pages;

Also, we need the links to the next and previous list of 30 pages. We use our MY_Model helper for this: $this->data[‘next_previous_pages’] = $this->page_model->all_pages;

And we render the view: $this->render(‘admin/pages/index_view’);

Now, we create the index_view.php inside views/admin/pages directory:

Cool. Let’s see the Pages controller again:

Next we will take care of the edit part of the pages.

6 thoughts on “Step 5.1 – Administer static pages for our multilanguage CMS – listing the created pages

  1. Gursharan Singh Dhanjal

    Thank you for such a great tutorial. Although is its is intended towards beginners, but it has helped me a web site in two languages. Initially i was creating different models controllers for each language, but your approach for routing is really great… Thanks a lot..!! Keep Sharing for guys like US..!! 🙂

    I would also like to mention an error in index() function of Pages controller. After the
    “if(sizeof($pages)==1) {}” condition, $pages->anyobject is not accessed directly as $pages is an array. So you’ve to change $pages to $pages[0] inside this single page condition.

    Reply
    1. avenirer Post author

      Thank you for your input. I sure hope you were paid well for creating a multilanguage site using my ideas. Did you know you can buy me a coffee? Regarding the error, the sizeof() only verifies if there was only one page returned. I will take a look again. Thank you.

      Reply
  2. aji

    hello,

    firstly, thank your for your tutorial.
    it’s such a huge help for me.
    i’ve followed your tutorial until the previous part and i have no problem so far.
    but, right now i’m having some problems in this part.
    i’ve followed this part from top to bottom and when i try to access the page.
    the url is : localhost/blog/admin/pages.
    i’ve got several errors, those are

    “Severity: Notice
    Message: Undefined index: name
    Filename: pages/index_view.php
    Line Number: 13”
    on “Add Page” button

    line number 13 is : echo ”.anchor(‘admin/pages/create/’.$slug,$language[‘name’]).”;

    and then i tried to directly access the “create” page with the url “/admin/pages/create/en/1”
    it also gave me an error.
    here it is
    “Message: Undefined index: name
    Filename: admin/Pages.php
    Line Number: 78”

    Line number 78 is : $this->data[‘content_language’] = $this->langs[$language_slug][‘name’];

    thanks beforehand.

    Reply
    1. avenirer Post author

      You are right. While creating this series I was working and changing the files. But it seems I forgot to change the episodes. You should change “name” with “language_directory”. I will also update this episode. Thank you for noticing. If you find any more errors, please, do tell me.

      Reply
  3. sherif

    i love you work, your are a great man 🙂
    there is something i don’t know if it’s not working for me only but shouldn’t it be
    if (sizeof($pages) == 1) {
    $list_pages[$pages[0]->id] = array(‘created_at’ => $pages[0]->created_at, ‘last_update’ => $pages[0]->updated_at, ‘deleted’ => $pages[0]->deleted_at, ‘translations’ => array());
    if (isset($pages[0]->translations)) {
    foreach ($pages[0]->translations as $translation) {
    $list_pages[$pages[0]->id][‘translations’][$translation->language_slug] = array(‘translation_id’ => $translation->id, ‘title’ => $translation->title, ‘created_at’ => $translation->created_at, ‘last_update’ => $translation->updated_at, ‘deleted’ => $translation->deleted_at);
    if ($translation->language_slug == $this->default_lang) {
    $list_pages[$pages[0]->id][‘title’] = $translation->title;
    } elseif (strlen($list_pages[$pages[0]->id][‘title’]) == 0) {
    $list_pages[$pages[0]->id][‘title’] = $translation->title;
    }
    }
    }
    }
    please update it 🙂

    Reply
    1. avenirer Post author

      You may be right. But that depends on the MY_Model. At the time when I wrote the tutorial my MY_Model looked differently. Thank you for repairing.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

No spam? * Time limit is exhausted. Please reload CAPTCHA.