Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] broken events before and after save on JModelAdmin. Related #4308 #5203

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions libraries/legacy/model/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ public function delete(&$pks)
{
if ($this->canDelete($table))
{
$context = $this->option . '.' . $this->name;
$context = $this->getContext();

// Trigger the before delete event.
$result = $dispatcher->trigger($this->event_before_delete, array($context, $table));
Expand Down Expand Up @@ -984,7 +984,7 @@ public function publish(&$pks, $value = 1)
return false;
}

$context = $this->option . '.' . $this->name;
$context = $this->getContext();

// Trigger the change state event.
$result = $dispatcher->trigger($this->event_change_state, array($context, $pks, $value));
Expand Down Expand Up @@ -1086,7 +1086,7 @@ public function save($data)
{
$dispatcher = JEventDispatcher::getInstance();
$table = $this->getTable();
$context = $this->option . '_' . $this->name;
$context = $this->option . '.' . $this->name;

if ((!empty($data['tags']) && $data['tags'][0] != ''))
{
Expand Down
33 changes: 33 additions & 0 deletions libraries/legacy/model/legacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
*/
abstract class JModelLegacy extends JObject
{
/**
* Context string for the model type. This is used to handle uniqueness
*
* @var string
* @since 3.4
*/
protected $context = null;

/**
* Indicates if the internal state has been set
*
Expand Down Expand Up @@ -284,6 +292,12 @@ public function __construct($config = array())
{
$this->event_clean_cache = 'onContentCleanCache';
}

// Initialize context. Only here for B/C
if (empty($this->context))
{
$this->context = $this->getContext();
}
}

/**
Expand Down Expand Up @@ -367,6 +381,25 @@ protected function _createTable($name, $prefix = 'Table', $config = array())
return JTable::getInstance($name, $prefix, $config);
}


/**
* Overridable method to the active context
*
* @return string
*
* @since 3.4
*/
public function getContext()
{
// Guess the context as Option.ModelName.
if (null === $this->context)
{
$this->context = strtolower($this->option . '.' . $this->getName());
}

return $this->context;
}

/**
* Method to get the database driver object
*
Expand Down
225 changes: 101 additions & 124 deletions libraries/legacy/model/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@ class JModelList extends JModelLegacy
*/
protected $cache = array();

/**
* Context string for the model type. This is used to handle uniqueness
* when dealing with the getStoreId() method and caching data structures.
*
* @var string
* @since 12.2
*/
protected $context = null;

/**
* Valid filter fields or ordering.
*
Expand Down Expand Up @@ -81,12 +72,6 @@ public function __construct($config = array())
{
$this->filter_fields = $config['filter_fields'];
}

// Guess the context as Option.ModelName.
if (empty($this->context))
{
$this->context = strtolower($this->option . '.' . $this->getName());
}
}

/**
Expand Down Expand Up @@ -245,7 +230,7 @@ protected function getStoreId($id = '')
$id .= ':' . $this->getState('list.ordering');
$id .= ':' . $this->getState('list.direction');

return md5($this->context . ':' . $id);
return md5($this->getContext() . ':' . $id);
}

/**
Expand Down Expand Up @@ -346,7 +331,7 @@ public function getFilterForm($data = array(), $loadData = true)
if (!empty($this->filterFormName))
{
// Get the form.
$form = $this->loadForm($this->context . '.filter', $this->filterFormName, array('control' => '', 'load_data' => $loadData));
$form = $this->loadForm($this->getContext() . '.filter', $this->filterFormName, array('control' => '', 'load_data' => $loadData));
}

return $form;
Expand Down Expand Up @@ -428,7 +413,7 @@ protected function loadForm($name, $source = null, $options = array(), $clear =
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState($this->context, new stdClass);
$data = JFactory::getApplication()->getUserState($this->getContext(), new stdClass);

// Pre-fill the list options
if (!property_exists($data, 'list'))
Expand Down Expand Up @@ -462,142 +447,133 @@ protected function loadFormData()
*/
protected function populateState($ordering = null, $direction = null)
{
// If the context is set, assume that stateful lists are used.
if ($this->context)
{
$app = JFactory::getApplication();
$app = JFactory::getApplication();

// Receive & set filters
if ($filters = $app->getUserStateFromRequest($this->context . '.filter', 'filter', array(), 'array'))
// Receive & set filters
if ($filters = $app->getUserStateFromRequest($this->getContext() . '.filter', 'filter', array(), 'array'))
{
foreach ($filters as $name => $value)
{
foreach ($filters as $name => $value)
{
$this->setState('filter.' . $name, $value);
}
$this->setState('filter.' . $name, $value);
}
}

$limit = 0;
$limit = 0;

// Receive & set list options
if ($list = $app->getUserStateFromRequest($this->context . '.list', 'list', array(), 'array'))
// Receive & set list options
if ($list = $app->getUserStateFromRequest($this->getContext() . '.list', 'list', array(), 'array'))
{
foreach ($list as $name => $value)
{
foreach ($list as $name => $value)
// Extra validations
switch ($name)
{
// Extra validations
switch ($name)
{
case 'fullordering':
$orderingParts = explode(' ', $value);

if (count($orderingParts) >= 2)
{
// Latest part will be considered the direction
$fullDirection = end($orderingParts);
case 'fullordering':
$orderingParts = explode(' ', $value);

if (in_array(strtoupper($fullDirection), array('ASC', 'DESC', '')))
{
$this->setState('list.direction', $fullDirection);
}
if (count($orderingParts) >= 2)
{
// Latest part will be considered the direction
$fullDirection = end($orderingParts);

unset($orderingParts[count($orderingParts) - 1]);

// The rest will be the ordering
$fullOrdering = implode(' ', $orderingParts);

if (in_array($fullOrdering, $this->filter_fields))
{
$this->setState('list.ordering', $fullOrdering);
}
}
else
if (in_array(strtoupper($fullDirection), array('ASC', 'DESC', '')))
{
$this->setState('list.ordering', $ordering);
$this->setState('list.direction', $direction);
$this->setState('list.direction', $fullDirection);
}
break;

case 'ordering':
if (!in_array($value, $this->filter_fields))
{
$value = $ordering;
}
break;
unset($orderingParts[count($orderingParts) - 1]);

// The rest will be the ordering
$fullOrdering = implode(' ', $orderingParts);

case 'direction':
if (!in_array(strtoupper($value), array('ASC', 'DESC', '')))
if (in_array($fullOrdering, $this->filter_fields))
{
$value = $direction;
$this->setState('list.ordering', $fullOrdering);
}
break;

case 'limit':
$limit = $value;
break;

// Just to keep the default case
default:
$value = $value;
break;
}

$this->setState('list.' . $name, $value);
}
}
else
// Keep B/C for components previous to jform forms for filters
{
// Pre-fill the limits
$limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->get('list_limit'), 'uint');
$this->setState('list.limit', $limit);

// Check if the ordering field is in the white list, otherwise use the incoming value.
$value = $app->getUserStateFromRequest($this->context . '.ordercol', 'filter_order', $ordering);

if (!in_array($value, $this->filter_fields))
{
$value = $ordering;
$app->setUserState($this->context . '.ordercol', $value);
}
else
{
$this->setState('list.ordering', $ordering);
$this->setState('list.direction', $direction);
}
break;

case 'ordering':
if (!in_array($value, $this->filter_fields))
{
$value = $ordering;
}
break;

case 'direction':
if (!in_array(strtoupper($value), array('ASC', 'DESC', '')))
{
$value = $direction;
}
break;

case 'limit':
$limit = $value;
break;

// Just to keep the default case
default:
$value = $value;
break;
}

$this->setState('list.ordering', $value);

// Check if the ordering direction is valid, otherwise use the incoming value.
$value = $app->getUserStateFromRequest($this->context . '.orderdirn', 'filter_order_Dir', $direction);

if (!in_array(strtoupper($value), array('ASC', 'DESC', '')))
{
$value = $direction;
$app->setUserState($this->context . '.orderdirn', $value);
}

$this->setState('list.direction', $value);
$this->setState('list.' . $name, $value);
}
}
else
// Keep B/C for components previous to jform forms for filters
{
// Pre-fill the limits
$limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->get('list_limit'), 'uint');
$this->setState('list.limit', $limit);

// Support old ordering field
$oldOrdering = $app->input->get('filter_order');
// Check if the ordering field is in the white list, otherwise use the incoming value.
$value = $app->getUserStateFromRequest($this->getContext() . '.ordercol', 'filter_order', $ordering);

if (!empty($oldOrdering) && in_array($oldOrdering, $this->filter_fields))
if (!in_array($value, $this->filter_fields))
{
$this->setState('list.ordering', $oldOrdering);
$value = $ordering;
$app->setUserState($this->getContext() . '.ordercol', $value);
}

// Support old direction field
$oldDirection = $app->input->get('filter_order_Dir');
$this->setState('list.ordering', $value);

// Check if the ordering direction is valid, otherwise use the incoming value.
$value = $app->getUserStateFromRequest($this->getContext() . '.orderdirn', 'filter_order_Dir', $direction);

if (!empty($oldDirection) && in_array(strtoupper($oldDirection), array('ASC', 'DESC', '')))
if (!in_array(strtoupper($value), array('ASC', 'DESC', '')))
{
$this->setState('list.direction', $oldDirection);
$value = $direction;
$app->setUserState($this->getContext() . '.orderdirn', $value);
}

$value = $app->getUserStateFromRequest($this->context . '.limitstart', 'limitstart', 0);
$limitstart = ($limit != 0 ? (floor($value / $limit) * $limit) : 0);
$this->setState('list.start', $limitstart);
$this->setState('list.direction', $value);
}
else

// Support old ordering field
$oldOrdering = $app->input->get('filter_order');

if (!empty($oldOrdering) && in_array($oldOrdering, $this->filter_fields))
{
$this->setState('list.start', 0);
$this->setState('list.limit', 0);
$this->setState('list.ordering', $oldOrdering);
}

// Support old direction field
$oldDirection = $app->input->get('filter_order_Dir');

if (!empty($oldDirection) && in_array(strtoupper($oldDirection), array('ASC', 'DESC', '')))
{
$this->setState('list.direction', $oldDirection);
}

$value = $app->getUserStateFromRequest($this->getContext() . '.limitstart', 'limitstart', 0);
$limitstart = ($limit != 0 ? (floor($value / $limit) * $limit) : 0);
$this->setState('list.start', $limitstart);
}

/**
Expand Down Expand Up @@ -697,6 +673,7 @@ protected function refineSearchStringToRegex($search, $regexDelimiter = '/')
unset($searchArr[$key]);
continue;
}

$searchArr[$key] = str_replace(' ', '.*', preg_quote(trim($searchString), $regexDelimiter));
}

Expand Down