getRelation();
$this->model = $relation->foreignModelClass;
$this->labelKey = $relation->selectFields[0];
if ($relation->type == 'belongsToMany')
$foreignKey = $relation->foreignKey;
else if ($relation->type == 'hasMany')
$foreignKey = $relation->localKey;
else // Fallback
$foreignKey = 'id';
// Parse values
$values = array();
if (!empty($this->value))
{
foreach($this->value as $row)
{
$val = $row;
if (is_object($row))
$val = (int)$row->$foreignKey;
$values[] = $val;
}
$this->value = $values;
}
// In case of Many to One, the options are limted to the available ones
if ($relation->type == 'hasMany')
{
$fkField = 'a.' . $relation->foreignKey;
$this->where = (empty($this->value)?'':'(' . $relation->foreignPkey . ' IN (' . implode(', ', $this->value). ')) OR')
. '(' . $fkField . ' IS NULL)'
. ' OR (' . $fkField . ' = 0)';
}
// This hidden input fix permits to send empty data
$htmlInputHiden = '';
return $htmlInputHiden . parent::getInput();
}
/**
* Method to get the items from a model.
*
* @access protected
*
* @return array The items list.
*/
protected function getItems()
{
if (!self::$_items)
{
$modelName = $this->getAttribute('model', $this->model);
$model = $this->getModel($modelName);
if (!$model)
return null;
if ($this->where)
$model->addWhere($this->where);
$model->setState('filter.formentry',1);
// var_dump($model);
// Prepare the list groups
$groups = $this->getListGroups();
if (!empty($groups))
{
$relations = array();
foreach($groups as $group)
$relations[] = $group->name;
// Include the joins in the SQL query BEFORE to get the items
$model->prepareQueryRelations(implode('.', $relations), true);
}
self::$_items = $model->getItems();
}
return self::$_items;
}
protected function getRelation()
{
// Uses the name as relation name if relation is not defined
$relation = $this->getAttribute('relation', $this->getAttribute('name'));
// Get this model (item)
$formInstance = $this->form->getName();
$parts = explode('.', $formInstance);
$modelName = $parts[1];
// Get the relation config
$model = CkJModel::getInstance($modelName, 'ShowmanagerModel');
$relation = $model->getRelation($relation);
return $relation;
}
/**
* Method to get the groups of the field.
*
* @access protected
*
* @return array The groups.
*/
protected function getListGroups()
{
$groups = array();
$defaultOn = '';
foreach ($this->element->children() as $option)
{
// Only get elements.
if ($option->getName() != 'group')
continue;
$group = new stdClass();
$group->name = (string) $option['name'];
$labelKey = (string) $option['labelKey'];
if ($labelKey || isset($option['on']))
{
$defaultOn .= '_' . $group->name;
// Label field. If not specified, find automaticaly the full underscore namespaced field name
$group->on = (isset($option['on'])?(string) $option['on']:$defaultOn . '_' . $labelKey);
}
else
$group->on = $group->name;
$groups[] = $group;
}
return $groups;
}
/**
* Method to a model from a namespace.
*
* @access protected
* @param string $model The namespaced model.
*
* @return array The model.
*/
protected function getModel($model = null)
{
if (!$model)
$model = $this->getAttribute('model');
$extension = self::$extension;
$parts = explode('.', $model);
if (count($parts) > 1)
{
if ($parts[0] != $extension)
{
$extension = $parts[0];
$this->includeThirdPaths($extension);
}
$model = $parts[1];
}
$model = CkJModel::getInstance($model, ucfirst($extension) . 'Model');
return $model;
}
/**
* Method to get the field options.
*
* @access protected
*
*
* @since 11.1
*
* @return array The field option objects.
*/
protected function getOptions()
{
$options = array();
// Get the value
$listKey = $this->getAttribute('listKey', $this->listKey);
if (!$listKey)
$listKey = 'id';
$labelKey = $this->getAttribute('labelKey', $this->labelKey);
$nullLabel = $this->getAttribute('nullLabel');
$options = array();
if ($nullLabel)
{
$options[''] = JText::_($nullLabel);
}
$items = $this->getItems();
if (!empty($items))
foreach ($items as $item) {
$option = new StdClass();
$option->text = $this->parseValue($labelKey, $item);
$option->value = $this->parseValue($listKey, $item);
$option->checked = false;
$options[$item->$listKey] = $option;
}
return $options;
}
/**
* Method to include a model path in the loader.
*
* @access protected
* @param string $extension The component alias.
*
* @return void
*/
protected function includeThirdPaths($extension)
{
$basePath = (JFactory::getApplication()->isSite()?JPATH_SITE:JPATH_ADMINISTRATOR);
CkJModel::addIncludePath($basePath .'/components/com_' . $extension . '/models');
}
/**
* Method to parse a field value.
*
* @access protected
* @param string $labelKey The field key. For concat : {field1} {field2}.
* @param Object $item The item data object.
*
* @return mixed Parsed value.
*/
protected function parseValue($labelKey, $item)
{
preg_match_all('/{([a-zA-Z0-9_]+)}/', $labelKey, $matches);
if (!count($matches[0])) {
if (isset($item->$labelKey)) {
return $item->$labelKey;
}
}
$replaceFrom = array();
$replaceTo = array();
foreach($matches[1] as $key)
{
$replaceFrom[] = '{' . $key . '}';
if (isset($item->$key)) {
$replaceTo[] = $item->$key;
}
}
$text = str_replace($replaceFrom, $replaceTo, $labelKey);
return $text;
}
}
// Load the fork
ShowmanagerHelper::loadFork(__FILE__);
// Fallback if no fork has been found
if (!class_exists('JFormFieldShowCheckBoxes')){ class JFormFieldShowCheckBoxes extends ShowmanagerCkFormFieldShowCheckBoxes{} }