diff --git a/src/components/com_tc/administrator/controllers/content.php b/src/components/com_tc/administrator/controllers/content.php index 2e529df..5354991 100755 --- a/src/components/com_tc/administrator/controllers/content.php +++ b/src/components/com_tc/administrator/controllers/content.php @@ -9,6 +9,10 @@ // No direct access defined('_JEXEC') or die; +use Joomla\CMS\Factory; +use Joomla\CMS\Language\Text; +use Joomla\CMS\Router\Route; +use Joomla\CMS\Uri; jimport('joomla.application.component.controllerform'); @@ -39,7 +43,7 @@ public function __construct() */ public function checkDuplicateAndLatestVersionTC() { - $app = JFactory::getApplication(); + $app = Factory::getApplication(); // Get value $tcVersion = $app->input->post->getFloat('tcVersion', 0.0); @@ -61,4 +65,155 @@ public function checkDuplicateAndLatestVersionTC() jexit(); } + + /** + * Function to save field data + * + * @param string $key key + * @param string $urlVar urlVar + * + * @return boolean|void + */ + public function save($key = null, $urlVar = 'tc_id') + { + // Initialise variables. + $app = Factory::getApplication(); + $input = $app->input; + $task = $input->get('task'); + $data = $input->post->get('jform', array(), 'array'); + $model = $this->getModel('content'); + $form = $model->getForm($data, false); + $table = $model->getTable(); + $checkin = property_exists($table, $table->getColumnAlias('checked_out')); + + // Determine the name of the primary key for the data. + if (empty($key)) + { + $key = $table->getKeyName(); + } + + // To avoid data collisions the urlVar may be different from the primary key. + if (empty($urlVar)) + { + $urlVar = $key; + } + + $recordId = $this->input->getInt($urlVar); + + // Populate the row id from the session. + $data[$key] = $recordId; + + if (!$form) + { + $app->enqueueMessage($model->getError(), 'error'); + + return false; + } + + // Validate the posted data. + $validData = $model->validate($form, $data); + + // Check for errors. + if ($validData === false) + { + // Get the validation messages. + $errors = $model->getErrors(); + + // Push up to three validation messages out to the user. + for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) + { + if ($errors[$i] instanceof Exception) + { + $app->enqueueMessage($errors[$i]->getMessage(), 'error'); + } + else + { + $app->enqueueMessage($errors[$i], 'error'); + } + } + + // Save the data in the session. + $app->setUserState('com_tc.edit.content.data', $data); + + // Redirect back to the edit screen + $this->setRedirect(Route::_('index.php?option=com_tc&view=content' . $this->getRedirectToItemAppend($recordId, $urlVar), false)); + + $this->redirect(); + } + + // Attempt to save the data. + if (!$model->save($validData)) + { + // Save the data in the session. + $app->setUserState('com_tc.data', $data); + + // Redirect back to the edit screen. + $this->setError(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError())); + $this->setMessage($this->getError(), 'error'); + + $this->setRedirect(Route::_('index.php?option=com_tc&view=content' . $this->getRedirectToItemAppend($recordId, $urlVar))); + + return false; + } + // Save succeeded, so check-in the record. + if ($checkin && $model->checkin($validData['tc_id']) === false) + { + // Save the data in the session. + $app->setUserState('com_tc.edit.content.data', $validData); + + // Check-in failed, so go back to the record and display a notice. + $this->setError(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError())); + $this->setMessage($this->getError(), 'error'); + + $this->setRedirect(Route::_('index.php?option=com_tc&view=content' . $this->getRedirectToItemAppend($recordId, $urlVar))); + + return false; + } + + $this->setMessage(Text::_('COM_TC_MSG_SUCCESS_SAVE_CONTENT')); + + // Redirect the user and adjust session state based on the chosen task. + switch ($task) + { + case 'apply': + // Set the record data in the session. + $recordId = $model->getState('com_tc.edit.content.id'); + $this->holdEditId('com_tc.edit.content', $recordId); + $app->setUserState('com_tc.edit.content.data', null); + + // Redirect back to the edit screen. + $this->setRedirect(Route::_('index.php?option=com_tc&view=content&' . $this->getRedirectToItemAppend($recordId, $urlVar), false)); + + break; + + case 'save2new': + // Clear the record id and data from the session. + $this->releaseEditId('com_tc.edit.content', $recordId); + $app->setUserState('com_tc.edit.content.data', null); + + // Redirect back to the edit screen. + $this->setRedirect(Route::_('index.php?option=com_tc&view=content' . $this->getRedirectToItemAppend(null, $urlVar), false)); + break; + + default: + // Clear the record id and data from the session. + $this->releaseEditId('com_tc.edit.content', $recordId); + $app->setUserState('com_tc.edit.content.data', null); + + $url = 'index.php?option=com_tc&view=contents' . $this->getRedirectToListAppend(); + + // Check if there is a return value + $return = $this->input->get('return', null, 'base64'); + + if (!is_null($return) && Uri::isInternal(base64_decode($return))) + { + $url = base64_decode($return); + } + + // Redirect to the list screen. + $this->setRedirect(Route::_($url, false)); + + break; + } + } } diff --git a/src/components/com_tc/administrator/models/content.php b/src/components/com_tc/administrator/models/content.php index 6e79851..7960010 100755 --- a/src/components/com_tc/administrator/models/content.php +++ b/src/components/com_tc/administrator/models/content.php @@ -9,6 +9,7 @@ // No direct access. defined('_JEXEC') or die; +use Joomla\CMS\Factory; jimport('joomla.application.component.modeladmin'); @@ -66,7 +67,7 @@ public function getTable($type = 'Content', $prefix = 'TcTable', $config = array public function getForm($data = array(), $loadData = true) { // Initialise variables. - $app = JFactory::getApplication(); + $app = Factory::getApplication(); // Get the form. $form = $this->loadForm( @@ -94,7 +95,7 @@ public function getForm($data = array(), $loadData = true) protected function loadFormData() { // Check the session for previously entered form data. - $data = JFactory::getApplication()->getUserState('com_tc.edit.content.data', array()); + $data = Factory::getApplication()->getUserState('com_tc.edit.content.data', array()); if (empty($data)) { @@ -158,7 +159,7 @@ protected function prepareTable($table) // Set ordering to the last item if not set if (@$table->ordering === '') { - $db = JFactory::getDbo(); + $db = Factory::getDbo(); $db->setQuery('SELECT MAX(ordering) FROM #__tc_content'); $max = $db->loadResult(); $table->ordering = $max + 1; @@ -171,18 +172,16 @@ protected function prepareTable($table) * * @param array $data TC form data * - * @return void + * @return boolean|void */ public function save($data) { require_once JPATH_ADMINISTRATOR . '/components/com_tc/models/urlpattern.php'; parent::save($data); - $db = JFactory::getDBO(); - $table = $this->getTable(); - $key = $table->getKeyName(); - $tcId = (!empty($data[$key])) ? $data[$key] : (int) $this->getState($this->getName() . '.id'); + $tcId = (int) $this->getState($this->getName() . '.id'); $client = $data['client']; $url_pattern = $data['url_pattern']; + $isNew = true; // Delete existing url patterns $urlPatternModel = JModelLegacy::getInstance('urlpattern', 'TcModel'); @@ -222,6 +221,9 @@ public function save($data) } } + $this->setState('com_tc.edit.content.id', $tcId); + $this->setState('com_tc.edit.content.new', $isNew); + return true; } @@ -239,7 +241,7 @@ public function checkDuplicateAndLatestVersionTC($tcVersion,$tcClient) { if ($tcVersion && $tcClient) { - $db = JFactory::getDbo(); + $db = Factory::getDbo(); $query = $db->getQuery(true); $query->select('version'); $query->from($db->quoteName('#__tc_content')); @@ -285,7 +287,7 @@ public function checkDuplicateAndLatestVersionTC($tcVersion,$tcClient) */ public function getMatchingTCs($option, $view) { - $db = JFactory::getDbo(); + $db = Factory::getDbo(); $query = $db->getQuery(true); $query->select('p.tc_id, c.version, c.client'); $query->from($db->quoteName('#__tc_patterns', 'p')); @@ -320,7 +322,7 @@ public function getMatchingTCs($option, $view) public function hasUserAcceptedTC($loggedInUserId, $tcId) { // Get user accepted TC ids - $db = JFactory::getDBO(); + $db = Factory::getDBO(); $query = $db->getQuery(true); $query->select('c.tc_id'); $query->from($db->quoteName('#__tc_content', 'c')); @@ -374,7 +376,7 @@ public function hasUserAcceptedTC($loggedInUserId, $tcId) */ public function getTCValidationStatus($tcId) { - $db = JFactory::getDbo(); + $db = Factory::getDbo(); $query = $db->getQuery(true); $query->select('global,groups,is_blacklist'); $query->from($db->quoteName('#__tc_content')); @@ -416,9 +418,9 @@ public function getTCValidationStatus($tcId) */ public function checkUserGroupAccess($tcUserGroup,$isBlacklist) { - $user = JFactory::getUser(); + $user = Factory::getUser(); $loggedInUserId = $user->get('id'); - $userGroups = JFactory::getUser($loggedInUserId); + $userGroups = Factory::getUser($loggedInUserId); if ($loggedInUserId) { @@ -470,7 +472,7 @@ public function checkUserGroupAccess($tcUserGroup,$isBlacklist) */ public function getGlobalTCIdList() { - $db = JFactory::getDbo(); + $db = Factory::getDbo(); $query = $db->getQuery(true); $query->select('p.tc_id, c.version, c.client'); $query->from($db->quoteName('#__tc_patterns', 'p')); @@ -502,7 +504,7 @@ public function getGlobalTCIdList() */ public function getGlobalTCValidationStatus($tcId) { - $db = JFactory::getDbo(); + $db = Factory::getDbo(); $query = $db->getQuery(true); $query->select('groups,is_blacklist'); $query->from($db->quoteName('#__tc_content')); @@ -535,7 +537,7 @@ public function getGlobalTCValidationStatus($tcId) */ public function getTCClient($tcId) { - $db = JFactory::getDbo(); + $db = Factory::getDbo(); $query = $db->getQuery(true); $query->select('client'); $query->from($db->quoteName('#__tc_content')); @@ -575,4 +577,36 @@ public function isUserAcceptedTC($loggedInUserId, $tcId) } } } + + /** + * Loads ContentHelper for filters before validating data. + * + * @param object $form The form to validate against. + * @param array $data The data to validate. + * @param string $group The name of the group(defaults to null). + * + * @return mixed Array of filtered data if valid, false otherwise. + * + * @since 1.1 + */ + public function validate($form, $data, $group=null) + { + $url_pattern = $data['url_pattern']; + $global = $data['global']; + + foreach ($url_pattern as $pattern) + { + $option = $pattern['option']; + $view = $pattern['view']; + + if ($global == 0 && (empty($option) || empty($view))) + { + $this->setError(JText::_('COM_TC_ENTER_URL_PATTERN')); + + return false; + } + } + + return parent::validate($form, $data, $group); + } } diff --git a/src/components/com_tc/administrator/models/forms/content.xml b/src/components/com_tc/administrator/models/forms/content.xml index ef41836..d313d05 100755 --- a/src/components/com_tc/administrator/models/forms/content.xml +++ b/src/components/com_tc/administrator/models/forms/content.xml @@ -18,7 +18,7 @@ - + @@ -32,4 +32,4 @@ - \ No newline at end of file + diff --git a/src/components/com_tc/administrator/views/content/tmpl/edit.php b/src/components/com_tc/administrator/views/content/tmpl/edit.php index 610b8ba..7ab34c2 100755 --- a/src/components/com_tc/administrator/views/content/tmpl/edit.php +++ b/src/components/com_tc/administrator/views/content/tmpl/edit.php @@ -40,6 +40,10 @@ jQuery(document).ready(function(){ + if(jQuery('label[for="jform_global_id0"]').hasClass("active")==true){ + jQuery('#url_pattern_id').removeAttr('required'); + } + // Check if T&C is in edit state then set version and client is readonly var tc_id = jQuery("#tc_id").val(); diff --git a/src/components/com_tc/languages/administrator/en-GB/en-GB.com_tc.ini b/src/components/com_tc/languages/administrator/en-GB/en-GB.com_tc.ini index 9ee9e61..be6a009 100755 --- a/src/components/com_tc/languages/administrator/en-GB/en-GB.com_tc.ini +++ b/src/components/com_tc/languages/administrator/en-GB/en-GB.com_tc.ini @@ -116,4 +116,7 @@ COM_TC_CUSTOM_DATE_FORMAT="Custom date format" COM_TC_CUSTOM_DATE_FORMAT_DESC="You can customize the date format here" COM_TC_DATE_FORMAT_CUSTOM="custom" COM_TC_CONTENTS_SEARCH_FILTER_DESC="Search by Title or Version" -COM_TC_USERS_SEARCH_FILTER_DESC="Search by User name, Terms and Conditions or T&C code" \ No newline at end of file +COM_TC_USERS_SEARCH_FILTER_DESC="Search by User name, Terms and Conditions or T&C code" +COM_TC_ENTER_URL_PATTERN="Please fill URL Pattern" +COM_TC_MSG_SUCCESS_SAVE_CONTENT="Terms and conditions saved successfully" +COM_TC_INVALID_DATA="Invalid form"