Skip to content

Commit

Permalink
Bug #39 fix: While adding T&C either Global or URL pattern need t o r…
Browse files Browse the repository at this point in the history
…equired otherwise user is able to T&C without selecting global and URL pattern (#44)

* Bug #157925 fix: While adding T&C either Global or URL pattern need to required otherwise user is able to T&C without selecting global and URL pattern

* Bug #157925 fix: While adding T&C either Global or URL pattern need to required otherwise user is able to T&C without selecting global and URL pattern

* Bug #157925 fix: While adding T&C either Global or URL pattern need to required otherwise user is able to T&C without selecting global and URL pattern

* Bug #157925 fix: While adding T&C either Global or URL pattern need to required otherwise user is able to T&C without selecting global and URL pattern

* Bug #157925 fix: While adding T&C either Global or URL pattern need to required otherwise user is able to T&C without selecting global and URL pattern
  • Loading branch information
paritshivani authored Feb 26, 2020
1 parent 351df84 commit 0f06690
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 21 deletions.
157 changes: 156 additions & 1 deletion src/components/com_tc/administrator/controllers/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
}
}
68 changes: 51 additions & 17 deletions src/components/com_tc/administrator/models/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

// No direct access.
defined('_JEXEC') or die;
use Joomla\CMS\Factory;

jimport('joomla.application.component.modeladmin');

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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;
Expand All @@ -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');
Expand Down Expand Up @@ -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;
}

Expand All @@ -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'));
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/components/com_tc/administrator/models/forms/content.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<option value="1">JYES</option>
<option value="0">JNO</option>
</field>
<field name= "url_pattern" id="url_pattern_id" description= "COM_TC_FORM_LBL_CONTENT_URL_PATTERN_DESC" type= "subform" label= "COM_TC_FORM_LBL_CONTENT_URL_PATTERN" min= "1" max= "1000" showon="global:0" formsource= "/administrator/components/com_tc/models/forms/url_pattern.xml" multiple= "true" buttons= "add,remove" layout="joomla.form.field.subform.repeatable-table" groupByFieldset="false" />
<field name= "url_pattern" required="true" id="url_pattern_id" description= "COM_TC_FORM_LBL_CONTENT_URL_PATTERN_DESC" type= "subform" label= "COM_TC_FORM_LBL_CONTENT_URL_PATTERN" min= "1" max= "1000" showon="global:0" formsource= "/administrator/components/com_tc/models/forms/url_pattern.xml" multiple= "true" buttons= "add,remove" layout="joomla.form.field.subform.repeatable-table" groupByFieldset="false" />
<field name="start_date" type="calendar" default="now" showtime="true" class="inputbox" format="%Y-%m-%d %H:%M:%S" required="true" label="COM_TC_FORM_LBL_CONTENT_START_DATE" description="COM_TC_FORM_DESC_CONTENT_START_DATE" hint="COM_TC_FORM_LBL_CONTENT_START_DATE" filter="user_utc" />
<field name="content" type="editor" filter="raw" label="COM_TC_FORM_LBL_CONTENT_CONTENT" buttons="true" description="COM_TC_FORM_DESC_CONTENT_CONTENT" hint="COM_TC_FORM_LBL_CONTENT_CONTENT" />
<field name="groups" type="usergrouplist" multiple="true" default="0" label="COM_TC_FORM_LBL_CONTENT_SELECT_USER_GROUP" description="COM_TC_FORM_LBL_CONTENT_SELECT_USER_GROUP_DESC" />
Expand All @@ -32,4 +32,4 @@
<field name="created_on" type="hidden" filter="unset" />
<field name="modified_on" type="hidden" filter="unset" />
</fieldset>
</form>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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"

0 comments on commit 0f06690

Please sign in to comment.