Step 5.3 – Administer static pages for our multilanguage CMS – deleting the pages

OK…

Let’s delete all. Not really… but we need to create the delete() method. This method will receive the language slug and the page id. Further more, if the language slug will have “all” as value (I sure hope you won’t choose “all” as language slug in the future…), this will delete the whole page with it’s translations. And let’s not forget about the slugs. We delete them too.

I guess that if you got here then no more explanation is necessary about what I am going to do. But if you have questions, you can at any time approach me with a comment on my posts.

So, let me show you the method:

    public function delete($language_slug, $page_id)
    {
        if($page = $this->page_model->get($page_id))
        {
            if($language_slug=='all')
            {
                if($deleted_translations = $this->page_translation_model->where('page_id',$page_id)->delete())
                {
                    $deleted_slugs = $this->slug_model->where(array('content_type'=>'page','content_id'=>$page_id))->delete();
                    $deleted_pages = $this->page_model->delete($page_id);
                    $this->session->set_flashdata('message', $deleted_pages.' page deleted. There were also '.$deleted_translations.' translations and '.$deleted_slugs.' slugs deleted.');
                }
                else
                {
                    $deleted_pages = $this->page_model->delete($page_id);
                    $this->session->set_flashdata('message', $deleted_pages.' page was deleted');
                }
            }
            else
            {
                if($this->page_translation_model->where(array('page_id'=>$page_id,'language_slug'=>$language_slug))->delete())
                {
                    $deleted_slugs = $this->slug_model->where(array('content_type'=>'page','language_slug'=>$language_slug,'content_id'=>$page_id))->delete();
                    $this->session->set_flashdata('message', 'The translation and '.$deleted_slugs.' slugs were deleted.');
                }
            }
        }
        else
        {
            $this->session->set_flashdata('message', 'There is no translation to delete.');
        }
        redirect('admin/pages','refresh');

    }

Also, let’s make sure that the users will be asked again if they are sure they want to delete the pages/translations by appending to the delete links an onclick event:

<a href=".../delete" onclick="return confirm('Are you sure you want to delete?')">

More on this onclick event you can find in an older tutorial of mine: Confirm box in javascript: Are you sure you want to delete?

Let’s see our index_view view again:

<?php defined('BASEPATH') OR exit('No direct script access allowed');?>
<div class="container" style="margin-top:60px;">
    <div class="row">
        <div class="col-lg-12">
            <!-- Single button -->
            <div class="btn-group">
                <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Add page <span class="caret"></span>
                </button>
                <ul class="dropdown-menu" role="menu">
                    <?php
                    foreach($langs as $slug => $language)
                    {
                        echo '<li>'.anchor('admin/pages/create/'.$slug,$language['name']).'</li>';
                    }
                    ?>
                </ul>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-12" style="margin-top: 10px;">
            <?php
            echo '<table class="table table-hover table-bordered table-condensed">';
            echo '<tr>';
            echo '<td rowspan="2">ID</td>';
            echo '<td rowspan="2">Page title</td>';
            echo '<td colspan="'.sizeof($langs).'">Translations</td>';
            echo '<td rowspan="2">Created at</td>';
            echo '<td rowspan="2">Last update</td>';
            echo '<td rowspan="2">Operations</td>';
            echo '</tr>';
            echo '<tr>';
            foreach($langs as $slug => $language)
            {
                echo '<td>'.$slug.'</td>';
            }
            echo '</tr>';
            if(!empty($pages))
            {

                foreach($pages as $page_id => $page)
                {
                    echo '<tr>';
                    echo '<td>'.$page_id.'</td><td>'.$page['title'].'</td>';
                    foreach($langs as $slug=>$language)
                    {
                        echo '<td>';
                        if(array_key_exists($slug,$page['translations']))
                        {
                            echo anchor('admin/pages/edit/'.$slug.'/'.$page_id,'<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>');
                            echo ' '.anchor('admin/pages/delete/'.$slug.'/'.$page_id,'<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>','onclick="return confirm(\'Are you sure you want to delete?\')"');
                            echo '<br />'.$page['translations'][$slug]['created_at'];
                            echo '<br />'.$page['translations'][$slug]['last_update'];
                        }
                        else
                        {
                            echo anchor('admin/pages/create/'.$slug.'/'.$page_id,'<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>');
                        }
                        echo '</td>';
                    }
                    echo '<td>'.$page['created_at'].'</td>';
                    echo '<td>'.$page['last_update'].'</td>';
                    echo '<td>'.anchor('admin/pages/delete/all/'.$page_id,'<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>','onclick="return confirm(\'Are you sure you want to delete?\')"').'</td>';
                    echo '</tr>';
                }
            }
            echo '</table>';
            echo '<nav><ul class="pagination">';
            echo $next_previous_pages;
            echo '</ul></nav>';
            ?>
        </div>
    </div>
</div>

Now, let’s see the Pages controller again:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Pages extends Admin_Controller
{

	function __construct()
	{
		parent::__construct();
        if(!$this->ion_auth->in_group('admin'))
        {
            $this->session->set_flashdata('message','You are not allowed to visit the Pages page');
            redirect('admin','refresh');
        }
        $this->load->model('page_model');
        $this->load->model('page_translation_model');
        $this->load->model('slug_model');
        $this->load->model('language_model');
        $this->load->library('form_validation');
        $this->load->helper('text');
	}

	public function index()
	{
        $total_pages = $this->page_model->count();
        $list_pages = array();
        if($pages = $this->page_model->order_by('created_at, updated_at','desc')->with('translations')->paginate(30,$total_pages))
        {
            if(sizeof($pages)==1)
            {
                $list_pages[$pages->id] = array('created_at' => $pages->created_at, 'last_update' => $pages->updated_at, 'deleted' => $pages->deleted_at, 'translations' => array());
                if(isset($pages->translations)) {
                    foreach ($pages->translations as $translation)
                    {
                        $list_pages[$pages->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->id]['title'] = $translation->title;
                        } elseif (strlen($list_pages[$pages->id]['title']) == 0) {
                            $list_pages[$pages->id]['title'] = $translation->title;
                        }
                    }
                }
            }
            else
            {
                foreach ($pages as $page)
                {
                    $list_pages[$page->id] = array('created_at' => $page->created_at, 'last_update' => $page->updated_at, 'deleted' => $page->deleted_at, 'translations' => array(), 'title'=>'');
                    if(isset($page->translations))
                    {
                        foreach ($page->translations as $translation)
                        {
                            $list_pages[$page->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[$page->id]['title'] = $translation->title;
                            } elseif (strlen($list_pages[$page->id]['title']) == 0) {
                                $list_pages[$page->id]['title'] = $translation->title;
                            }
                        }
                    }
                }
            }
        }
        $this->data['pages'] = $list_pages;
        $this->data['next_previous_pages'] = $this->page_model->all_pages;
		$this->render('admin/pages/index_view');
	}

    public function create($language_slug = NULL, $page_id = 0)
    {
        $language_slug = (isset($language_slug) && array_key_exists($language_slug, $this->langs)) ? $language_slug : $this->current_lang;

        $this->data['content_language'] = $this->langs[$language_slug]['name'];
        $this->data['language_slug'] = $language_slug;
        $page = $this->page_model->get($page_id);
        if($page_id != 0 && $page==FALSE)
        {
            $page_id = 0;
        }
        if($this->page_translation_model->where(array('page_id'=>$page_id,'language_slug'=>$language_slug))->get())
        {
            $this->session->set_flashdata('message', 'A translation for that page already exists.');
            redirect('admin/pages', 'refresh');
        }
        $this->data['page'] = $page;
        $this->data['page_id'] = $page_id;
        $pages = $this->page_translation_model->where('language_slug',$language_slug)->order_by('menu_title')->fields('page_id,id,menu_title')->get_all();
        $this->data['parent_pages'] = array('0'=>'No parent page');
        if(!empty($pages))
        {
            foreach($pages as $page)
            {
                $this->data['parent_pages'][$page->page_id] = $page->menu_title;
            }
        }

        $rules = $this->page_model->rules;
        $this->form_validation->set_rules($rules['insert']);
        if($this->form_validation->run()===FALSE)
        {
            $this->render('admin/pages/create_view');
        }
        else
        {
            $parent_id = $this->input->post('parent_id');
            $title = $this->input->post('title');
            $menu_title = (strlen($this->input->post('menu_title')) > 0) ? $this->input->post('menu_title') : $title;
            $slug = (strlen($this->input->post('slug')) > 0) ? url_title($this->input->post('slug'),'-',TRUE) : url_title(convert_accented_characters($title),'-',TRUE);
            $order = $this->input->post('order');
            $content = $this->input->post('content');
            $teaser = (strlen($this->input->post('teaser')) > 0) ? $this->input->post('teaser') : substr($content, 0, strpos($content, '<!--more-->'));
            $page_title = (strlen($this->input->post('page_title')) > 0) ? $this->input->post('page_title') : $title;
            $page_description = (strlen($this->input->post('page_description')) > 0) ? $this->input->post('page_description') : ellipsize($teaser, 160);
            $page_keywords = $this->input->post('page_keywords');
            $page_id = $this->input->post('page_id');
            $language_slug = $this->input->post('language_slug');
            if ($page_id == 0)
            {
                $page_id = $this->page_model->insert(array('parent_id' => $parent_id, 'order' => $order, 'created_by'=>$this->user_id));
            }

            $insert_data = array('page_id'=>$page_id,'title' => $title, 'menu_title' => $menu_title, 'teaser' => $teaser,'content' => $content,'page_title' => $page_title, 'page_description' => $page_description,'page_keywords' => $page_keywords,'language_slug' => $language_slug,'created_by'=>$this->user_id);

            if($translation_id = $this->page_translation_model->insert($insert_data))
            {
                $this->page_model->update(array('parent_id'=>$parent_id, 'order'=>$order,'updated_by'=>$this->user_id),$page_id);
                $url = $this->_verify_slug($slug,$language_slug);
                $this->slug_model->insert(array(
                    'content_type'=>'page',
                    'content_id'=>$page_id,
                    'translation_id'=>$translation_id,
                    'language_slug'=>$language_slug,
                    'url'=>$url,
                    'created_by'=>$this->user_id));
                //$this->slug_model->where(array('content_type'=>'page','content_id'=>$page_id,'id !='=>$slug_id))->update(array('redirect'=>$slug_id));
            }

            redirect('admin/pages','refresh');

        }


    }

    public function edit($language_slug, $page_id)
    {
        $translation = $this->page_translation_model->where(array('page_id'=>$page_id, 'language_slug'=>$language_slug))->get();
        $this->data['content_language'] = $this->langs[$language_slug]['name'];
        if($translation == FALSE)
        {
            $this->session->set_flashdata('message', 'There is no translation for that page.');
            redirect('admin/pages', 'refresh');
        }
        $page = $this->page_model->get($page_id);
        if($page == FALSE)
        {
            $this->session->set_flashdata('message', 'There is no page to translate.');
            redirect('admin/pages', 'refresh');
        }
        $this->data['translation'] = $translation;
        $this->data['page'] = $page;
        $this->data['slugs'] = $this->slug_model->where(array('content_type'=>'page','translation_id'=>$translation->id))->get_all();
        $pages = $this->page_translation_model->where(array('language_slug'=>$language_slug,'page_id !='=>$page_id))->order_by('menu_title')->fields('page_id,id,menu_title')->get_all();
        $this->data['parent_pages'] = array('0'=>'No parent page');
        if(!empty($pages))
        {
            foreach($pages as $page)
            {
                $this->data['parent_pages'][$page->page_id] = $page->menu_title;
            }
        }

        $rules = $this->page_model->rules;
        $this->form_validation->set_rules($rules['update']);
        if($this->form_validation->run()===FALSE)
        {
            $this->render('admin/pages/edit_view');
        }
        else
        {
            $translation_id = $this->input->post('translation_id');
            if($translation = $this->page_translation_model->get($translation_id))
            {
                $parent_id = $this->input->post('parent_id');
                $title = $this->input->post('title');
                $menu_title = $this->input->post('menu_title');
                $slug = $this->input->post('slug');
                $order = $this->input->post('order');
                $content = $this->input->post('content');
                $teaser = (strlen($this->input->post('teaser')) > 0) ? $this->input->post('teaser') : substr($content, 0, strpos($content, '<!--more-->'));
                $page_title = (strlen($this->input->post('page_title')) > 0) ? $this->input->post('page_title') : $title;
                $page_description = (strlen($this->input->post('page_description')) > 0) ? $this->input->post('page_description') : ellipsize($teaser, 160);
                $page_keywords = $this->input->post('page_keywords');
                $page_id = $this->input->post('page_id');
                $language_slug = $this->input->post('language_slug');


                $update_data = array(
                    'title' => $title,
                    'menu_title' => $menu_title,
                    'teaser' => $teaser,
                    'content' => $content,
                    'page_title' => $page_title,
                    'page_description' => $page_description,
                    'page_keywords' => $page_keywords,
                    'updated_by' => $this->user_id);

                if ($this->page_translation_model->update($update_data, $translation_id))
                {
                    $this->page_model->update(array('parent_id' => $parent_id, 'order' => $order, 'updated_by' => $this->user_id), $page_id);
                    if(strlen($slug)>0)
                    {
                        $url = $this->_verify_slug($slug, $language_slug);
                        $new_slug = array(
                            'content_type' => 'page',
                            'content_id' => $page_id,
                            'translation_id' => $translation_id,
                            'language_slug' => $language_slug,
                            'url' => $url,
                            'created_by' => $this->user_id);
                        if($slug_id =  $this->slug_model->insert($new_slug))
                        {
                            $this->slug_model->where(array('content_type'=>'page', 'translation_id'=>$translation_id,'id != '=>$slug_id))->update(array('redirect'=>$slug_id,'updated_by'=>$this->user_id));
                        }
                    }
                    $this->session->set_flashdata('message', 'The translation was updated successfully.');
                }
            }
            else
            {
                $this->session->set_flashdata('message', 'There is no translation to update.');
            }
            redirect('admin/pages','refresh');
        }
    }
    private function _verify_slug($str,$language)
    {
        if($this->slug_model->where(array('url'=>$str,'language_slug'=>$language))->get() !== FALSE)
        {
            $parts = explode('-',$str);
            if(is_numeric($parts[sizeof($parts)-1]))
            {
                $parts[sizeof($parts)-1] = $parts[sizeof($parts)-1]++;
            }
            else
            {
                $parts[] = '1';
            }
            $str = implode('-',$parts);
            $this->_verify_slug($str,$language);
        }
        return $str;
    }
    public function delete($language_slug, $page_id)
    {
        if($page = $this->page_model->get($page_id))
        {
            if($language_slug=='all')
            {
                if($deleted_translations = $this->page_translation_model->where('page_id',$page_id)->delete())
                {
                    $deleted_slugs = $this->slug_model->where(array('content_type'=>'page','content_id'=>$page_id))->delete();
                    $deleted_pages = $this->page_model->delete($page_id);
                    $this->session->set_flashdata('message', $deleted_pages.' page deleted. There were also '.$deleted_translations.' translations and '.$deleted_slugs.' slugs deleted.');
                }
                else
                {
                    $deleted_pages = $this->page_model->delete($page_id);
                    $this->session->set_flashdata('message', $deleted_pages.' page was deleted');
                }
            }
            else
            {
                if($this->page_translation_model->where(array('page_id'=>$page_id,'language_slug'=>$language_slug))->delete())
                {
                    $deleted_slugs = $this->slug_model->where(array('content_type'=>'page','language_slug'=>$language_slug,'content_id'=>$page_id))->delete();
                    $this->session->set_flashdata('message', 'The translation and '.$deleted_slugs.' slugs were deleted.');
                }
            }
        }
        else
        {
            $this->session->set_flashdata('message', 'There is no translation to delete.');
        }
        redirect('admin/pages','refresh');

    }
}

Hope you liked it…

4 comments

  1. Hi, I’m a bit confused right now – everything works fine, I can create, edit and delete static pages with this CMS tool – but how can I access the static page as a normal user (as a user who did not login and who would just like to see the content of the page? What is the URL to the created static page?)

    1. Hello. Sorry for the time passed. I was in vacation. If you’ve followed the tutorial from start to end, you should simply access the page by writing the slug you’ve set up for the page at the end of your domain name. For example if you’ve saved a page with the slug “a-new-page”, you should see it by writing “http://localhost/a-new-page”

  2. Hi there! Anyone can help me out?
    I’ve got this error and didnt know how to fix it.
    Severity: Notice
    Message: Undefined property: Pages::$user_id
    Filename: admin/Pages.php
    Column ‘created_by’ cannot be null
    INSERT INTO `slugs` (`content_type`, `content_id`, `translation_id`, `language_slug`, `url`, `created_by`, `created_at`) VALUES (‘page’, 4, 5, ‘en’, ‘man-love-woman’, NULL, ‘2017-01-04 04:31:03’)
    Filename: F:/WEBROOT/cms_admin_ci3/application/core/MY_Model.php

Leave a Reply

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

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