Skip to content

Commit

Permalink
Model classes improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
joomdonation committed Mar 20, 2017
1 parent a925fcb commit 1dc4480
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 30 deletions.
36 changes: 36 additions & 0 deletions libraries/cms/component/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,42 @@ public static function renderComponent($option, $params = array())
return $contents;
}

/**
* Gets the name for the component from class name
*
* @param string $className The class name of controller, model, view
*
* @return string The name of component
*
* @throws \Exception
*/
public static function getComponentName($className)
{
// In a namespace model class, the component name will be the part before Site/Administrator
if (strpos($className, '\\'))
{
$parts = explode('\\', $className);

$index = array_search('Site', $parts) ?: array_search('Administrator', $parts);

if ($index !== false && isset($parts[$index - 1]))
{
return 'com_' . strtolower($parts[$index - 1]);
}
}

// In a none namespace class, the component will be the part before Controller/Model/View
$r = null;

if (preg_match('/(.*)(Controller|Model|View)/i', $className, $r))
{
return 'com_' . strtolower($r[1]);
}

// Could not detect component name from given class, throws Exception
throw new \Exception(\JText::_('JLIB_APPLICATION_ERROR_CONTROLLER_GET_NAME'), 500);
}

/**
* Execute the component.
*
Expand Down
3 changes: 2 additions & 1 deletion libraries/src/Cms/Model/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,7 @@ public function publish(&$pks, $value = 1)
$user = \JFactory::getUser();
$table = $this->getTable();
$pks = (array) $pks;
$checkedOutField = $table->getColumnAlias('checked_out');

// Include the plugins for the change of state event.
\JPluginHelper::importPlugin($this->events_map['change_state']);
Expand All @@ -998,7 +999,7 @@ public function publish(&$pks, $value = 1)
}

// If the table is checked out by another user, drop it and report to the user trying to change its state.
if (property_exists($table, 'checked_out') && $table->checked_out && ($table->checked_out != $user->id))
if (property_exists($table, $checkedOutField) && $table->{$checkedOutField} && ($table->{$checkedOutField} != $user->id))
{
\JLog::add(\JText::_('JLIB_APPLICATION_ERROR_CHECKIN_USER_MISMATCH'), \JLog::WARNING, 'jerror');

Expand Down
18 changes: 3 additions & 15 deletions libraries/src/Cms/Model/ListModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,26 +340,14 @@ public function getStart()
*/
public function getFilterForm($data = array(), $loadData = true)
{
$form = null;

// Try to locate the filter form automatically. Example: ContentModelArticles => "filter_articles"
if (empty($this->filterFormName))
{
$classNameParts = explode('Model', get_called_class());

if (count($classNameParts) == 2)
{
$this->filterFormName = 'filter_' . strtolower($classNameParts[1]);
}
}

if (!empty($this->filterFormName))
{
// Get the form.
$form = $this->loadForm($this->context . '.filter', $this->filterFormName, array('control' => '', 'load_data' => $loadData));
$this->filterFormName = 'filter_' . strtolower($this->getName());
}

return $form;
// Return the form.
return $this->loadForm($this->context . '.filter', $this->filterFormName, array('control' => '', 'load_data' => $loadData));
}

/**
Expand Down
78 changes: 64 additions & 14 deletions libraries/src/Cms/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ abstract class Model extends \JObject
*/
protected $event_clean_cache = null;

/**
* The base namespace of the component model belong to
*
* @var string
*/
protected $namespace = null;

/**
* Add a directory where \JModelLegacy should search for models. You may
* either pass a string or an array of directories.
Expand Down Expand Up @@ -223,16 +230,20 @@ public function __construct($config = array())
// Guess the option from the class name (Option)Model(View).
if (empty($this->option))
{
$r = null;

if (!preg_match('/(.*)Model/i', get_class($this), $r))
if (isset($config['option']))
{
throw new \Exception(\JText::_('JLIB_APPLICATION_ERROR_MODEL_GET_NAME'), 500);
$this->option = $config['option'];
}
else
{
// Guess the option from the class name
$this->option = \JComponentHelper::getComponentName(get_class($this));
}

$this->option = 'com_' . strtolower($r[1]);
}

// Set the component namespace
$this->namespace = \JComponentHelper::getComponent($this->option)->namespace;

// Set the view name
if (empty($this->name))
{
Expand Down Expand Up @@ -374,7 +385,7 @@ protected function _createTable($name, $prefix = 'Table', $config = array())
{
// Clean the model name
$name = preg_replace('/[^A-Z0-9_]/i', '', $name);
$prefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix);
$prefix = preg_replace('/[^A-Z0-9_\\\\]/i', '', $prefix);

// Make sure we are returning a DBO object
if (!array_key_exists('dbo', $config))
Expand Down Expand Up @@ -412,14 +423,21 @@ public function getName()
{
if (empty($this->name))
{
$r = null;

if (!preg_match('/Model(.*)/i', get_class($this), $r))
if ($this->namespace)
{
throw new \Exception(\JText::_('JLIB_APPLICATION_ERROR_MODEL_GET_NAME'), 500);
$this->name = strtolower((new \ReflectionClass($this))->getShortName());
}
else
{
$r = null;

$this->name = strtolower($r[1]);
if (!preg_match('/Model(.*)/i', get_class($this), $r))
{
throw new \Exception(\JText::_('JLIB_APPLICATION_ERROR_MODEL_GET_NAME'), 500);
}

$this->name = strtolower($r[1]);
}
}

return $this->name;
Expand Down Expand Up @@ -461,13 +479,25 @@ public function getState($property = null, $default = null)
* @since 3.0
* @throws \Exception
*/
public function getTable($name = '', $prefix = 'Table', $options = array())
public function getTable($name = '', $prefix = '', $options = array())
{
if (empty($name))
{
$name = $this->getName();
}

if (empty($prefix))
{
if ($this->namespace)
{
$prefix = $this->namespace . '\\Administrator\\Table\\';
}
else
{
$prefix = 'Table';
}
}

if ($table = $this->_createTable($name, $prefix, $options))
{
return $table;
Expand Down Expand Up @@ -527,6 +557,26 @@ public function loadHistory($version_id, Table &$table)
return $table->bind($rowArray);
}

/**
* Method to check if the given record is checked out by the current user
*
* @param \stdClass $item The record to check
*
* @return bool
*/
public function isCheckedOut($item)
{
$table = $this->getTable();
$checkedOutField = $table->getColumnAlias('checked_out');

if (property_exists($item, $checkedOutField) && $item->{$checkedOutField} != \JFactory::getUser()->id)
{
return true;
}

return false;
}

/**
* Method to auto-populate the model state.
*
Expand Down Expand Up @@ -587,7 +637,7 @@ protected function cleanCache($group = null, $client_id = 0)
$conf = \JFactory::getConfig();

$options = array(
'defaultgroup' => ($group) ? $group : (isset($this->option) ? $this->option : \JFactory::getApplication()->input->get('option')),
'defaultgroup' => ($group) ? $group : $this->option,
'cachebase' => ($client_id) ? JPATH_ADMINISTRATOR . '/cache' : $conf->get('cache_path', JPATH_SITE . '/cache'),
'result' => true,
);
Expand Down

0 comments on commit 1dc4480

Please sign in to comment.