1 <?php
2 /**
3 * @package FrameworkOnFramework
4 * @subpackage model
5 * @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE.txt
7 */
8 // Protect from unauthorized access
9 defined('FOF_INCLUDED') or die;
10
11 /**
12 * FrameworkOnFramework model behavior class to filter front-end access to items
13 * based on the language.
14 *
15 * @package FrameworkOnFramework
16 * @since 2.1
17 */
18 class FOFModelBehaviorLanguage extends FOFModelBehavior
19 {
20 /**
21 * This event runs before we have built the query used to fetch a record
22 * list in a model. It is used to blacklist the language filter
23 *
24 * @param FOFModel &$model The model which calls this event
25 * @param FOFDatabaseQuery &$query The model which calls this event
26 *
27 * @return void
28 */
29 public function onBeforeBuildQuery(&$model, &$query)
30 {
31 if (FOFPlatform::getInstance()->isFrontend())
32 {
33 $model->blacklistFilters('language');
34 }
35 }
36
37 /**
38 * This event runs after we have built the query used to fetch a record
39 * list in a model. It is used to apply automatic query filters.
40 *
41 * @param FOFModel &$model The model which calls this event
42 * @param FOFDatabaseQuery &$query The model which calls this event
43 *
44 * @return void
45 */
46 public function onAfterBuildQuery(&$model, &$query)
47 {
48 // This behavior only applies to the front-end.
49 if (!FOFPlatform::getInstance()->isFrontend())
50 {
51 return;
52 }
53
54 // Get the name of the language field
55 $table = $model->getTable();
56 $languageField = $table->getColumnAlias('language');
57
58 // Make sure the access field actually exists
59 if (!in_array($languageField, $table->getKnownFields()))
60 {
61 return;
62 }
63
64 // Make sure it is a multilingual site and get a list of languages
65 $app = JFactory::getApplication();
66 $hasLanguageFilter = method_exists($app, 'getLanguageFilter');
67
68 if ($hasLanguageFilter)
69 {
70 $hasLanguageFilter = $app->getLanguageFilter();
71 }
72
73 if (!$hasLanguageFilter)
74 {
75 return;
76 }
77
78 $lang_filter_plugin = JPluginHelper::getPlugin('system', 'languagefilter');
79 $lang_filter_params = new JRegistry($lang_filter_plugin->params);
80
81 $languages = array('*');
82
83 if ($lang_filter_params->get('remove_default_prefix'))
84 {
85 // Get default site language
86 $lg = FOFPlatform::getInstance()->getLanguage();
87 $languages[] = $lg->getTag();
88 }
89 else
90 {
91 $languages[] = JFactory::getApplication()->input->getCmd('language', '*');
92 }
93
94 // Filter out double languages
95 $languages = array_unique($languages);
96
97 // And filter the query output by these languages
98 $db = FOFPlatform::getInstance()->getDbo();
99
100 // Alias
101 $alias = $model->getTableAlias();
102 $alias = $alias ? $db->qn($alias) . '.' : '';
103
104 $languages = array_map(array($db, 'quote'), $languages);
105 $query->where($alias . $db->qn($languageField) . ' IN (' . implode(',', $languages) . ')');
106 }
107
108 /**
109 * The event runs after FOFModel has called FOFTable and retrieved a single
110 * item from the database. It is used to apply automatic filters.
111 *
112 * @param FOFModel &$model The model which was called
113 * @param FOFTable &$record The record loaded from the databae
114 *
115 * @return void
116 */
117 public function onAfterGetItem(&$model, &$record)
118 {
119 if ($record instanceof FOFTable)
120 {
121 $fieldName = $record->getColumnAlias('language');
122
123 // Make sure the field actually exists
124 if (!in_array($fieldName, $record->getKnownFields()))
125 {
126 return;
127 }
128
129 // Make sure it is a multilingual site and get a list of languages
130 $app = JFactory::getApplication();
131 $hasLanguageFilter = method_exists($app, 'getLanguageFilter');
132
133 if ($hasLanguageFilter)
134 {
135 $hasLanguageFilter = $app->getLanguageFilter();
136 }
137
138 if (!$hasLanguageFilter)
139 {
140 return;
141 }
142
143 $lang_filter_plugin = JPluginHelper::getPlugin('system', 'languagefilter');
144 $lang_filter_params = new JRegistry($lang_filter_plugin->params);
145
146 $languages = array('*');
147
148 if ($lang_filter_params->get('remove_default_prefix'))
149 {
150 // Get default site language
151 $lg = FOFPlatform::getInstance()->getLanguage();
152 $languages[] = $lg->getTag();
153 }
154 else
155 {
156 $languages[] = JFactory::getApplication()->input->getCmd('language', '*');
157 }
158
159 // Filter out double languages
160 $languages = array_unique($languages);
161
162 if (!in_array($record->$fieldName, $languages))
163 {
164 $record = null;
165 }
166 }
167 }
168 }
169