1<?php
2/**
3 * @package     Joomla.Administrator
4 * @subpackage  com_languages
5 *
6 * @copyright   Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
7 * @license     GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10defined('_JEXEC') or die;
11
12/**
13 * Languages Override Controller
14 *
15 * @since  2.5
16 */
17class LanguagesControllerOverride extends JControllerForm
18{
19	/**
20	 * Method to edit an existing override.
21	 *
22	 * @param   string  $key     The name of the primary key of the URL variable (not used here).
23	 * @param   string  $urlVar  The name of the URL variable if different from the primary key (not used here).
24	 *
25	 * @return  void
26	 *
27	 * @since   2.5
28	 */
29	public function edit($key = null, $urlVar = null)
30	{
31		// Do not cache the response to this, its a redirect
32		JFactory::getApplication()->allowCache(false);
33
34		$app     = JFactory::getApplication();
35		$cid     = $this->input->post->get('cid', array(), 'array');
36		$context = "$this->option.edit.$this->context";
37
38		// Get the constant name.
39		$recordId = (count($cid) ? $cid[0] : $this->input->get('id'));
40
41		// Access check.
42		if (!$this->allowEdit())
43		{
44			$this->setError(JText::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'));
45			$this->setMessage($this->getError(), 'error');
46			$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false));
47
48			return;
49		}
50
51		$app->setUserState($context . '.data', null);
52		$this->setRedirect('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, 'id'));
53	}
54
55	/**
56	 * Method to save an override.
57	 *
58	 * @param   string  $key     The name of the primary key of the URL variable (not used here).
59	 * @param   string  $urlVar  The name of the URL variable if different from the primary key (not used here).
60	 *
61	 * @return  void
62	 *
63	 * @since   2.5
64	 */
65	public function save($key = null, $urlVar = null)
66	{
67		// Check for request forgeries.
68		$this->checkToken();
69
70		$app     = JFactory::getApplication();
71		$model   = $this->getModel();
72		$data    = $this->input->post->get('jform', array(), 'array');
73		$context = "$this->option.edit.$this->context";
74		$task    = $this->getTask();
75
76		$recordId = $this->input->get('id');
77		$data['id'] = $recordId;
78
79		// Access check.
80		if (!$this->allowSave($data, 'id'))
81		{
82			$this->setError(JText::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'));
83			$this->setMessage($this->getError(), 'error');
84			$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false));
85
86			return;
87		}
88
89		// Validate the posted data.
90		$form = $model->getForm($data, false);
91
92		if (!$form)
93		{
94			$app->enqueueMessage($model->getError(), 'error');
95
96			return;
97		}
98
99		// Require helper for filter functions called by JForm.
100		JLoader::register('LanguagesHelper', JPATH_ADMINISTRATOR . '/components/com_languages/helpers/languages.php');
101
102		// Test whether the data is valid.
103		$validData = $model->validate($form, $data);
104
105		// Check for validation errors.
106		if ($validData === false)
107		{
108			// Get the validation messages.
109			$errors = $model->getErrors();
110
111			// Push up to three validation messages out to the user.
112			for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++)
113			{
114				if ($errors[$i] instanceof Exception)
115				{
116					$app->enqueueMessage($errors[$i]->getMessage(), 'warning');
117				}
118				else
119				{
120					$app->enqueueMessage($errors[$i], 'warning');
121				}
122			}
123
124			// Save the data in the session.
125			$app->setUserState($context . '.data', $data);
126
127			// Redirect back to the edit screen.
128			$this->setRedirect(
129				JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, 'id'), false)
130			);
131
132			return;
133		}
134
135		// Attempt to save the data.
136		if (!$model->save($validData))
137		{
138			// Save the data in the session.
139			$app->setUserState($context . '.data', $validData);
140
141			// Redirect back to the edit screen.
142			$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()));
143			$this->setMessage($this->getError(), 'error');
144			$this->setRedirect(
145				JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, 'id'), false)
146			);
147
148			return;
149		}
150
151		// Add message of success.
152		$this->setMessage(JText::_('COM_LANGUAGES_VIEW_OVERRIDE_SAVE_SUCCESS'));
153
154		// Redirect the user and adjust session state based on the chosen task.
155		switch ($task)
156		{
157			case 'apply':
158				// Set the record data in the session.
159				$app->setUserState($context . '.data', null);
160
161				// Redirect back to the edit screen
162				$this->setRedirect(
163					JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($validData['key'], 'id'), false)
164				);
165				break;
166
167			case 'save2new':
168				// Clear the record id and data from the session.
169				$app->setUserState($context . '.data', null);
170
171				// Redirect back to the edit screen
172				$this->setRedirect(
173					JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend(null, 'id'), false)
174				);
175				break;
176
177			default:
178				// Clear the record id and data from the session.
179				$app->setUserState($context . '.data', null);
180
181				// Redirect to the list screen.
182				$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false));
183				break;
184		}
185	}
186
187	/**
188	 * Method to cancel an edit.
189	 *
190	 * @param   string  $key  The name of the primary key of the URL variable (not used here).
191	 *
192	 * @return  void
193	 *
194	 * @since   2.5
195	 */
196	public function cancel($key = null)
197	{
198		$this->checkToken();
199
200		$app     = JFactory::getApplication();
201		$context = "$this->option.edit.$this->context";
202
203		$app->setUserState($context . '.data', null);
204		$this->setRedirect(JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false));
205	}
206}
207