⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Insert with different language



maicol.cantagallo
  • profile picture
  • Member

Posted 06 June 2013 - 08:07 AM

Hi, i have a question.

 

if i need to insert a post in a different language?

 

for example:

 

i have a table (post) with post_id, category_id, is_published, etc....and i have another table (post_i18n) with post_id, language, title, content.

 

so, how do I handle this situation during insertion managing multi-language?

 

 

thanks to all


maicol.cantagallo
  • profile picture
  • Member

Posted 06 June 2013 - 08:43 AM

This is my controller

<?php

class Post extends CI_Controller {  // modify Controller Name

    private static $data;

    function __construct() {
        parent::__construct();

        $this->load->library('table');
        $this->load->library('tags/tags_lib');
        $this->load->library('form_validation');
        $this->load->helper('url');

        $this->module = 'post';                           // modify here
        $this->tag_key = "post";                          // modify here

        $this->lang->load('post', $this->session->userdata('lang'));
        
        $this->load->model('post_model', 'db_model');   // modify here
        $this->load->library('post_lib');               // modify here
        
        $this->load->library('grocery_CRUD'); // istanza crud
        
        $this->msg = '';
        $this->active_langs = $this->session->userdata('active_langs');
    }

    function index() {
        $this->listpage();
    }
    
    /**
     * Author: Maicol Cantagallo
     * Responsability: invia alla view i dati per la stampa
     * @param type $output 
     */
    function _list_output($output = null) {
        $this->load->view($this->module . '/list', $output);
    }

    /**
     * Author: Maicol Cantagallo
     * Responsability: attraverso il CRUD vengono stabiliti i dati da 
     * visualizzare in fase di view, edit ed insert
     * @param type $output 
     */
    function listpage() {
        
        try{
            $crud = new grocery_CRUD();

            $crud->set_theme('flexigrid');    
            $crud->set_model('post_model');
            $crud->set_table('post');                                       
            $crud->set_subject('Post');   
            
            $crud->set_relation('category_id', 'post_categories', 'name');  
            
            $crud->set_field_upload('thumb','upload_area/');                
            $crud->set_field_upload('image','upload_area/');
            
            $crud->display_as('category_id','Categoria');                                   
            $crud->display_as('title','Titolo');
            $crud->display_as('date_from','Data creazione');
            $crud->display_as('date_to','Data scadenza');
            $crud->display_as('is_published','Pubblicato');
            $crud->display_as('hp','In homepage');
            $crud->display_as('creatore','Creato da');
            $crud->display_as('lang','Lingua');
            $crud->display_as('thumb','Thumb');
            $crud->display_as('image','Immagine');
            
            $crud->fields('category_id', 'thumb', 'image', 'date_from', 'date_to', 'is_published', 'hp');       
            $crud->columns('id', 'title', 'category_id', 'date_from', 'is_published', 'hp','lang', 'creatore');      

            $output = $crud->render();

            $this->_list_output($output);

        }catch(Exception $e){
                show_error($e->getMessage().' --- '.$e->getTraceAsString());
        }
    }
}

?>

And this my model

<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Post_model extends grocery_CRUD_model {


    var $table = 'post';  // Tabella madre
    var $table_i18n = 'post_i18n'; // Tabella decodifica lingue
    var $table_categories = 'post_categories'; // Tabella categoria
    var $post_photogalleries_table = 'post_photogalleries'; // Tabella photogallery
    var $photogalleries_table = 'photogalleries_photos'; // Tabella foto delle photogallery
    var $photos_table = 'photos'; // tabella foto
    var $photos_i18n_table = 'photos_i18n'; // tabella decodifica lingua foto
    var $post_files_table = 'post_files'; // tabella dei file di un post
    var $admins_table = 'admins'; // tabella degli admin
    
    
    /**
     * Author Maicol Cantagallo
     * Responsability model list of news
     * @return type 
     */
    public function get_list(){
        if($this->table === null)
            return false;

        $select = "{$this->table}.*";
        $select .= ", ".$this->table_i18n.".*";
        $select .= ", CONCAT(".$this->admins_table.".first_name, ' ',".$this->admins_table.".last_name) as creatore";

        if(!empty($this->relation))
            foreach($this->relation as $relation){
                list($field_name , $related_table , $related_field_title) = $relation;
                $unique_join_name = $this->_unique_join_name($field_name);
                $unique_field_name = $this->_unique_field_name($field_name);

                if(strstr($related_field_title,'{'))
                    $select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $unique_field_name";
                else	  
                    $select .= ", $unique_join_name.$related_field_title as $unique_field_name";

                if($this->field_exists($related_field_title))
                    $select .= ", {$this->table}.$related_field_title as '{$this->table}.$related_field_title'";
            }

            $this->db->select($select, false);
            
            $this->db->join($this->admins_table, $this->admins_table.".id = ".$this->table.".created_by");
            $this->db->join($this->table_i18n, $this->table_i18n.".post_id = ".$this->table.".id");

            $results = $this->db->get($this->table)->result();

            return $results;
    }
}

?>

thanks


davidoster
  • profile picture
  • Member

Posted 06 June 2013 - 13:05 PM

Hello.

Thanks for sharing your code.

I think you are making it very complicated.

It could be musch easier.

For example, on the table post I would have a field called i18n or lang.

There I would store this, $this->session->userdata('lang')

Then depending on this session variable I would do a where to show only those posts that match this language.


maicol.cantagallo
  • profile picture
  • Member

Posted 06 June 2013 - 14:06 PM

thank you so much.

 

yes,i also had thought of this solution but I wanted to have your opinion.

 

 

I think that I will follow your advice :)

 

thanks :)