480b8473e24c80fc4cfdf6155e87775741fa543c
[living-lab-site.git] / application / core / Article_Controller.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2 /**
3  * Library Article_Controller can be extended by a controller to be used for 
4  * content pages that depend on the language.
5  *
6  * The page views are usually located in
7  * "application/views/article/$language/$method".
8  * Parameters:
9  * <ul>
10  *      <li><strong>Article Title:</strong> in language file 'article_lang.php':
11  * an entry named "article_$method".
12  * If not present "$method" is used as a name.</li>
13  *      <li><strong>Article Meta Description:</strong> in language file..:
14  * an entry "article_${method}_description"</li>
15  *      <li><strong>Helpers, Libraries:</strong> in config file 'article.php':
16  * an entry named "article_${method}_helpers" or "article_${method}_libraries"
17  * respectively with an array of helpers or libraries to be loaded for the
18  * article.</li>
19  * <li><strong>CSSs, JSs:</strong> in config file 'article.php':
20  * an entry named "article_${method}_css" or "article_${method}_js"
21  * respectively with an array of .css or .js to be loaded into members $css
22  * and $js. It's up to the programmer to define how this members are going
23  * to be used.</li>
24  * </ul> 
25  *
26  * @category    Base Controller Library
27  * @author              Călin-Andrei Burloiu
28  */
29 class Article_Controller extends CI_Controller {
30         
31         protected $title = NULL;
32         protected $metaDescription = NULL;
33         protected $helpers = array();
34         protected $libraries = array();
35         protected $css = array();
36         protected $js = array();
37         
38         function __construct()
39         {
40                 parent::__construct();
41                 
42                 // Language, title and description
43                 $this->lang->load('article');
44                 
45                 // Helpers and libraries.
46                 $this->config->load('article');
47         }
48         
49         /**
50          * Extend this with site specific information (header, menus...) and call
51          * $this->_load which is a generic method that loads the article.
52          * Both parameters must be passed to $this->_load.
53          */
54         public function _remap($method, $params = array())
55         {
56                 // Title
57                 $this->title = $this->lang->line("article_$method");
58                 if ($this->title === FALSE)
59                         $this->title = $method;
60
61                 // Meta Description
62                 $this->metaDescription = $this->lang->line("article_${method}_description");
63                 if ($this->metaDescription === FALSE)
64                         $this->metaDescription = '';
65                 
66                 // Helpers
67                 $this->helpers = $this->config->item("article_${method}_helpers");
68                 if ($this->helpers !== FALSE)
69                         $this->load->helper($this->helpers);
70                 
71                 // Libraries
72                 $this->libraries = $this->config->item("article_${method}_library");
73                 if ($this->libraries !== FALSE)
74                         $this->load->library($libraries);
75                 
76                 // CSSs
77                 $css =& $this->config->item("article_${method}_css");
78                 if ($css !== FALSE)
79                         $this->css = $css;
80                 
81                 // JavaScripts
82                 $js =& $this->config->item("article_${method}_js");
83                 if ($js !== FALSE)
84                         $this->js = $js;
85         }
86         
87         /**
88          * Returns the article based on the language from
89          * "application/views/article/$language/$method".
90          * 
91          * @param       string $method  defines article name
92          * @param       array $params   odd elements are keys and even elements are
93          * their values (eg.: [0] => key, [1] => value etc.). This are going to
94          * be converted to an associative array that is passed to the view if 
95          * $assoc parameter is FALSE. Otherwise this parameter is already an
96          * associative array.
97          * @param       bool $assoc     states whether or not $params is associative
98          */
99         public function _load($method, $params = array(), $assoc = FALSE)
100         {
101                 if (! $assoc)
102                 {
103                         $alt = 0;
104                         $params_assoc = array();
105                         $prev_val = NULL;
106                         foreach ($params as $i => $val)
107                         {
108                                 if ($alt == 0)
109                                         $prev_val = $val;
110                                 else if ($alt == 1)
111                                         $params_assoc[$prev_val] = $val;
112                                 
113                                 $alt = ($alt + 1) % 2;
114                         }
115                 }
116                 else
117                         $params_assoc = $params;
118                 
119                 return $this->load->view('article/'. $this->config->item('language')
120                         . '/' . $method, $params_assoc, TRUE);
121         }
122 }
123
124 /* End of file Article_Controller.php */
125 /* Location: ./application/core/Article_Controller.php */