(created at: April 02, 2015; last update: April 05, 2015)
We’ve created multilanguage pages, we’ve listed them, but what about editing them?
For this we will create a new method named edit(), which will receive the language slug and the page id.
We don’t have to worry about being lost in translations. Our method will first ask page_translation_model if there is a translation of the page in that particular language. If there is such a translation, the method will also make sure that there exists a page that the translation pretends it belongs to by asking the page_model.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$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'); } |
After we’ve made all the checks, we will pass all the needed data to the edit_view view:
1 2 3 4 5 6 7 8 9 10 11 12 |
$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; } } |
As you can see, we’ve also interrogated the slug_model that we’ve created in a previous tutorial. So ,let’s make sure it is loaded inside the controller’s constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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'); } ... |
Now, let’s get the rules and render the edit_view view if those rules are not respected:
1 2 3 4 5 6 |
$rules = $this->page_model->rules; $this->form_validation->set_rules($rules['update']); if($this->form_validation->run()===FALSE) { $this->render('admin/pages/edit_view'); } |
But we didn’t define the rules, so let’s return to our Page_model and update them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public $rules = array( 'insert' => array( 'parent_id' => array('field'=>'parent_id','label'=>'Parent ID','rules'=>'trim|is_natural|required'), 'title' => array('field'=>'title','label'=>'Title','rules'=>'trim|required'), 'menu_title' => array('field'=>'menu_title','label'=>'Menu title','rules'=>'trim'), 'slug' => array('field'=>'slug', 'label'=>'Slug', 'rules'=>'trim'), 'order' => array('field'=>'order','label'=>'Order','rules'=>'trim|is_natural|required'), 'teaser' => array('field'=>'teaser','label'=>'Teaser','rules'=>'trim'), 'content' => array('field'=>'content','label'=>'Content','rules'=>'trim'), 'page_title' => array('field'=>'page_title','label'=>'Page title','rules'=>'trim'), 'page_description' => array('field'=>'page_description','label'=>'Page description','rules'=>'trim'), 'page_keywords' => array('field'=>'page_keywords','label'=>'Page keywords','rules'=>'trim'), 'page_id' => array('field'=>'page_id', 'label'=>'Page ID', 'rules'=>'trim|is_natural|required'), 'language_slug' => array('field'=>'language_slug','label'=>'language_slug','rules'=>'trim|required') ), 'update' => array( 'parent_id' => array('field'=>'parent_id','label'=>'Parent ID','rules'=>'trim|is_natural|required'), 'title' => array('field'=>'title','label'=>'Title','rules'=>'trim|required'), 'menu_title' => array('field'=>'menu_title','label'=>'Menu title','rules'=>'trim'), 'slug' => array('field'=>'slug', 'label'=>'Slug', 'rules'=>'trim'), 'order' => array('field'=>'order','label'=>'Order','rules'=>'trim|is_natural|required'), 'teaser' => array('field'=>'teaser','label'=>'Teaser','rules'=>'trim'), 'content' => array('field'=>'content','label'=>'Content','rules'=>'trim'), 'page_title' => array('field'=>'page_title','label'=>'Page title','rules'=>'trim|required'), 'page_description' => array('field'=>'page_description','label'=>'Page description','rules'=>'trim'), 'page_keywords' => array('field'=>'page_keywords','label'=>'Page keywords','rules'=>'trim'), 'translation_id' => array('field'=>'translation_id', 'label'=>'Translation ID', 'rules'=>'trim|is_natural_no_zero|required'), 'page_id' => array('field'=>'page_id', 'label'=>'Page ID', 'rules'=>'trim|is_natural_no_zero|required'), 'language_slug' => array('field'=>'language_slug','label'=>'language_slug','rules'=>'trim|required') ) ); |
Now we create edit_view.php inside the views/admin/pages directory (which looks almost like the create_view.php, but with a few changes):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
<?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"> <h1>Edit Page in <?php echo strtolower($content_language);?></h1> <?php echo form_open('',array('class'=>'form-horizontal'));?> <div class="form-group"> <?php echo form_label('Parent page','parent_id'); echo form_dropdown('parent_id',$parent_pages,set_value('parent_id',$page->parent_id),'class="form-control"'); ?> </div> <div class="form-group"> <?php echo form_label('Title','title'); echo form_error('title'); echo form_input('title',set_value('title',$translation->title),'class="form-control"'); ?> </div> <div class="form-group"> <?php echo form_label('Menu title','menu_title'); echo form_error('menu_title'); echo form_input('menu_title',set_value('menu_title',$translation->menu_title),'class="form-control"'); ?> </div> <div class="form-group"> <?php echo form_label('Slug','slug'); echo form_error('slug'); echo form_input('slug',set_value('slug'),'class="form-control"'); ?> </div> <?php echo '<div class="panel panel-primary">'; echo '<div class="panel-heading">Currently active slugs</div>'; echo '<div class="panel-body">'; foreach($slugs as $slug) { echo $slug->url.'<br />'; } echo '</div>'; echo '</div>'; ?> <div class="form-group"> <?php echo form_label('Order','order'); echo form_error('order'); echo form_input('order',set_value('order', $page->order),'class="form-control"'); ?> </div> <div class="form-group"> <?php echo form_label('Teaser','teaser'); echo form_error('teaser'); echo form_textarea('teaser',set_value('teaser',$translation->teaser),'class="form-control"'); ?> </div> <div class="form-group"> <?php echo form_label('Content','content'); echo form_error('content'); echo form_textarea('content',set_value('content',$translation->content,false),'class="form-control"'); ?> </div> <div class="form-group"> <?php echo form_label('Page title','page_title'); echo form_error('page_title'); echo form_input('page_title',set_value('page_title',$translation->page_title),'class="form-control"'); ?> </div> <div class="form-group"> <?php echo form_label('Page description','page_description'); echo form_error('page_description'); echo form_input('page_description',set_value('page_description',$translation->page_description),'class="form-control"'); ?> </div> <div class="form-group"> <?php echo form_label('Keywords','page_keywords'); echo form_error('page_keywords'); echo form_input('page_keywords',set_value('page_keywords',$translation->page_keywords),'class="form-control"'); ?> </div> <?php echo form_error('page_id');?> <?php echo form_hidden('page_id',set_value('page_id',$translation->page_id));?> <?php echo form_error('language_slug');?> <?php echo form_hidden('language_slug',set_value('language_slug',$translation->language_slug));?> <?php echo form_error('translation_id');?> <?php echo form_hidden('translation_id',set_value('translation_id',$translation->id));?> <?php $submit_button = 'Edit translation'; echo form_submit('submit', $submit_button, 'class="btn btn-primary btn-lg btn-block"');?> <?php echo anchor('/admin/pages', 'Cancel','class="btn btn-default btn-lg btn-block"');?> <?php echo form_close();?> </div> </div> </div> |
Once we did that, we return to our edit() method and see what we can do after the validation was passed.
First of all we need to make sure we have a translation that needs updated. After that, we pass all POST data to variables and then do the necessary update:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
$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'); |
Let’s see the whole controller again:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
<?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()); 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'=>''); 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)); } 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) { $this->load->model('slug_model'); 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; } } |
And that’s it… Next we will do our delete() method….
anhnnp says:
The code from step 5 onwards are unreal. I see in your project “multilang site” in github is easier to understand because it’s true.
anhnnp says:
Why create a new slug without update slug already exists
avenirer says:
Because if your old slug was read by a search engine, once you update the slug, the search engine would send visitors to a nice big 404, because you simply removed the old slug.
avenirer says:
Wow… For real?
NJOKU OKECHUKWU VALENTINE says:
Hello Sir, thanks for great tutorials. I have been following your tutorials, but at this stage, when I execute the application, it gives me an error message that count() was not declared in Pages_model or MY_Model. Please help me to inform me via email when you have looked at this because I am integrating this into hmvc and would gladly submit the code to you when done, but i am hooked up with this isssue now.
Thanks in advance