Extend News Module into multi-language

edited November 2014 in Modules
Hi,

I am trying to extend the news module into multi-language like the "Fuel Pages" style, the headline and content will be auto switch by the language field. But I got some problems and would like to get some hits. Below is my code.

Database:
CREATE TABLE IF NOT EXISTS `news_pages` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `description` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '', `slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '', `release_date` date NOT NULL, `published` enum('yes','no') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'yes', PRIMARY KEY (`id`), UNIQUE KEY `permalink` (`slug`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ; CREATE TABLE IF NOT EXISTS `news_pagevars` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `news_id` int(10) unsigned NOT NULL DEFAULT '0', `headline` varchar(255) COLLATE utf8_unicode_ci NOT NULL, `value` text COLLATE utf8_unicode_ci NOT NULL, `language` varchar(30) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'en', `active` enum('yes','no') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'yes', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;

news_pages_model.php:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require_once(FUEL_PATH.'models/base_module_model.php'); class News_pages_model extends Base_module_model { public $required = array('description'); public $unique_fields = array('slug'); public $record_class = 'News_page'; function __construct() { parent::__construct('news_pages'); } function form_fields($values = array()) { $fields = parent::form_fields(); $fields['description']['placeholder'] = 'Please enter the news description here in English only'; $fields['description']['size'] = 100; $fields['slug']['comment'] = 'If no slug is provided, one will be provided for you'; $fields['release_date']['comment'] = 'A release date will automatically be created for you of the current date if left blank'; $yes = lang('form_enum_option_yes'); $no = lang('form_enum_option_no'); $fields['language'] = array('type' => 'select', 'options' => $this->fuel->language->options(), 'order' => 4); return $fields; } function on_before_clean($values) { if (empty($values['slug'])) $values['slug'] = url_title($values['description'].datetime_now(), 'dash', TRUE); if (!intval($values['release_date'])) $values['release_date'] = datetime_now(); return $values; } public function on_after_delete($where) { $this->delete_related(array('news_pagevars_model'), 'news_id', $where); } function _common_query() { parent::_common_query(); // to do active and published $this->db->order_by('release_date desc'); } } class New_page_model extends Base_module_record { function get_variables($language = NULL) { $params =array(); if (!empty($language)) { $params['where'] = array('language' => $language); } return $this->lazy_load(array('news_id' => $this->id), array('news_pages_model'), TRUE, $params); } }

news_pagevars_model.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); require_once(FUEL_PATH.'models/base_module_model.php'); class News_pagevars_model extends Base_module_model { public $news_id; public $honor_page_status = FALSE; public $serialized_fields = array('value'); public $foreign_keys = array('news_id' => array('news_pages_model')); public function __construct() { parent::__construct('news_pagevars'); } public function list_items($limit = NULL, $offset = NULL, $col = 'location', $order = 'desc', $just_count = FALSE) { $this->db->select($this->_tables['news_pagevars'].'.*, '.$this->_tables['news_pages'].'.description, '.$this->_tables['news_pages'].'.slug, '.$this->_tables['news_pages'].'.published AS news_published'); $this->db->join($this->_tables['news_pages'], $this->_tables['news_pages'].'.id = '.$this->_tables['news_pagevars'].'.news_id', 'left'); $data = parent::list_items($limit, $offset, $col, $order, $just_count); return $data; } public function form_fields($values = array(), $related = array()) { $CI =& get_instance(); $fields = parent::form_fields($values, $related); if (isset($values['news_id'])) { $page = $CI->fuel->pages->find($values['news_id']); $news_vars = $this->find_all_by_news_id($values['news_id']); $values = array_merge($news_vars, $values); } return $fields; } public function _common_query($params = NULL) { $CI =& get_instance(); $lang_options = $CI->fuel->config('languages'); $this->db->select($this->_tables['news_pagevars'].'.*, '.$this->_tables['news_pages'].'.description, '.$this->_tables['news_pages'].'.slug, '.$this->_tables['news_pages'].'.published AS news_published'); $this->db->join($this->_tables['news_pages'], $this->_tables['news_pages'].'.id = '.$this->_tables['news_pagevars'].'.news_id', 'left'); $this->db->where(array($this->_tables['news_pagevars'].'.active' => 'yes')); if ($this->honor_page_status AND !defined('FUEL_ADMIN')) { $this->db->where(array($this->_tables['news_pages'].'.published' => 'yes')); } } } class news_pagevar_model extends Data_record { public function __toString() { return $this->value; } public function get_value() { return $this->_parent_model->cast($this->_fields['value']); } }

My_fuel_modules.php
$config['modules']['news_pages'] = array();

My problem is I cannot show news_pagevars "description", "slug", "value" input fields in my news_page module which I hope it will look like "Fuel Pages" model "Layout Variables". Is it I miss something?

Hope someone can give me a hits.

Thanks a lot.

Comments

  • edited 6:21PM
    you can modify my_fuel_layout.php to add fields
  • edited 6:21PM
    O~~Sorry for my poor English.

    My problem is I can only show up the news_pages_model input field in my CMS module (e.g. description, slug, release date etc). But another input field of news_pagevars_model cannot be show up (headline & value input field which will be auto switch by changing the language), I've capture my screen FYI.

    https://www.dropbox.com/s/io1mqz1ff5u8u6m/ScreenHunter_110 Nov. 25 16.56.jpg?dl=0

    Many thanks.
  • edited 6:21PM
    Hi, I just thinking my way is correct or not, please correct me if I'm wrong.

    1/ I've created 2 database and 2 model.
    2/ First, news_pages_model which will be store the news ID, description, slug, release date and publish status. These information are only in English for identify the news.
    3/ Second, news_pagevars_model which will be store the news content, news_id, language, headline. These information will have multi-language in array format.
    4/ In the news_pages_model, join news_pages database ID to news_pagevars database news_id together ($foreign_keys)
    5/ In the news_pages_model form_fields, arrange the input fields (news_pages description, slug, release date AND news_pagevars headline, content, language)
    6/ Hooking up news_pages_model.

    Am I correct?

    I don't know how to make a auto switch for the content, headline in the model by language. And don't know why the content and headline cannot show in my model.

    Please help and forgive my poor English.
  • edited 6:21PM
    It sounds like you are completely trying to replicate the Pages module instead of using a Layout for your News items correct? If so, that's quite a bit of work and not something FUEL is easily setup to do without you writing a lot of code. To point you in the right direction, you'll need a controller similar to the "pages" controller and a javascript file similar to the PageController.js included to get all the functionality to work.
  • edited 6:21PM
    A bit little like Pages module I think but not that complex.

    Now I was created a News module follow the user guide and it support multi-language. But I find that all news record are standalone, means if I click news A in english, it works good but if I change news A language to another it show 404 error, because that news is only in English version, other news language will have another slug so it cannot find another language in same slug.

    So, I want to create a News module like Pages module in real multi-language.

    I hope:
    1/ Every news only have 1 slug, description and release date, but included multi-language headline and content, these record is linkup by the ID just like User Guide - Authors Module and Articles Module
    2/ In the News Module CMS, when I create or edit the news, it can be auto switch the headline and content language by selecting the language field, just like the Pages Module.

    That's why I try to replicate the Pages module but seems it's not easy.

    Well, any hits for me that can make the auto switch field by language in my News Module? Do I still need a controller and a javascript?
  • edited 6:21PM
    Yes, modules are setup a little different and a language column must be added to the table with each record. What if you had the slug and language values be a unique key in your news table instead of just the slug value? Then on your front end, you query based on both the selected language and slug? Perhaps something like the following:
    $slug = uri_segment(3); $language = $CI->fuel->language->detect(); $data = fuel_model('news', array('where' => array('slug' => $slug, 'language' => $language'))); ...
  • edited November 2014
    Let me try~!!

    Thanks admin, you give me a lot of help.
Sign In or Register to comment.