1.10. Browse All Draft Articles

We have a list of public articles; let's create a list of draft articles. This will be very similar to the index action and view.

1.10.1. The "Drafts" Action Method

Open the Blog.php page controller class file. (In this example we use the vim editor, but you can use anything you like.)

$ vim Blog.php

Add the following PHP code to the class.

<?php
    public function actionDrafts()
    {
        // draft blog articles in ascending order, all result pages
        $fetch = array(
            'where' => array('blogs.status = ?' => 'draft'),
            'order' => 'blogs.created ASC',
            'page'  => 'all',
        );
    
        // fetch all matching records
        $this->list = $this->_model->blogs->fetchAll($fetch);
    }

1.10.2. The "Drafts" View Script

Create a view called drafts.php for the actionDrafts() method.

$ vim Blog/View/drafts.php

Enter the following code in the new drafts.php view script.

<?php
    $title = $this->getTextRaw('TITLE_DRAFTS');
    $this->head()->setTitle($title);
?>

<h2><?php echo $this->getText('HEADING_DRAFTS'); ?></h2>

<?php if (! $this->list): ?>

    <p><?php echo $this->getText('ERR_NO_RECORDS'); ?></p>

<?php else: ?>

    <ul>
    
    <?php foreach ($this->list as $item): ?>
        <li><?php
            echo $this->escape($item->title);
            echo "&nbsp;&nbsp;";
            echo $this->action(
                "{$this->controller}/edit/{$item->id}",
                'ACTION_EDIT'
            );
        ?></li>
    
    <?php endforeach; ?>
    
    </ul>
    
<?php endif; ?>

<p><?php echo $this->action(
    "{$this->controller}/add",
    'ACTION_ADD'
); ?></p>

This view shows a list of draft articles stored in $this->list along with a link to edit each one. At the bottom of the page is a link to add a new article.

You should now be able to browse to http://localhost/index.php/blog/drafts to see this page.



Local