Skip to content

Commit

Permalink
Adding all active positons in the option list to choose module posito…
Browse files Browse the repository at this point in the history
…n on front-end.
  • Loading branch information
Achal-Aggarwal committed Aug 8, 2015
1 parent 2dcb8e2 commit cda9f80
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 15 deletions.
126 changes: 117 additions & 9 deletions components/com_config/model/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,29 +131,137 @@ public function getPositions()

// Load templateDetails.xml file
$path = JPath::clean(JPATH_BASE . '/templates/' . $templateName . '/templateDetails.xml');
$currentPositions = array();
$currentTemplatePositions = array();

if (file_exists($path))
{
$xml = simplexml_load_file($path);

if (isset($xml->positions[0]))
{
// Load language files
$lang->load('tpl_' . $templateName . '.sys', JPATH_BASE, null, false, true)
|| $lang->load('tpl_' . $templateName . '.sys', JPATH_BASE . '/templates/' . $templateName, null, false, true);

foreach ($xml->positions[0] as $position)
{
// Load language files
$lang->load('tpl_' . $templateName . '.sys', JPATH_BASE, null, false, true)
|| $lang->load('tpl_' . $templateName . '.sys', JPATH_BASE . '/templates/' . $templateName, null, false, true);

$key = (string) $position;
$value = preg_replace('/[^a-zA-Z0-9_\-]/', '_', 'TPL_' . strtoupper($templateName) . '_POSITION_' . strtoupper($key));
$value = (string) $position;
$text = preg_replace('/[^a-zA-Z0-9_\-]/', '_', 'TPL_' . strtoupper($templateName) . '_POSITION_' . strtoupper($value));

// Construct list of positions
$currentPositions[$key] = JText::_($value) . ' [' . $key . ']';
$currentTemplatePositions[] = self::createOption($value, JText::_($text) . ' [' . $value . ']');
}
}
}

return $currentPositions;
$templateGroups = array();

// Add an empty value to be able to deselect a module position
$option = self::createOption();
$templateGroups[''] = self::createOptionGroup('', array($option));

$templateGroups[$templateName] = self::createOptionGroup($templateName, $currentTemplatePositions);

// Add custom position to options
$customGroupText = JText::_('COM_MODULES_CUSTOM_POSITION');

$editPositions = true;
$customPositions = self::getActivePositions(0, $editPositions);
$templateGroups[$customGroupText] = self::createOptionGroup($customGroupText, $customPositions);

return $templateGroups;
}

/**
* Get a list of modules positions
*
* @param integer $clientId Client ID
* @param boolean $editPositions Allow to edit the positions
*
* @return array A list of positions
*/
public static function getActivePositions($clientId, $editPositions = false)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('DISTINCT(position)')
->from('#__modules')
->where($db->quoteName('client_id') . ' = ' . (int) $clientId)
->order('position');

$db->setQuery($query);

try
{
$positions = $db->loadColumn();
$positions = is_array($positions) ? $positions : array();
}
catch (RuntimeException $e)
{
JError::raiseWarning(500, $e->getMessage());

return;
}

// Build the list
$options = array();

foreach ($positions as $position)
{
if (!$position && !$editPositions)
{
$options[] = JHtml::_('select.option', 'none', ':: ' . JText::_('JNONE') . ' ::');
}
else
{
$options[] = JHtml::_('select.option', $position, $position);
}
}

return $options;
}

/**
* Create and return a new Option
*
* @param string $value The option value [optional]
* @param string $text The option text [optional]
*
* @return object The option as an object (stdClass instance)
*
* @since 3.0
*/
private static function createOption($value = '', $text = '')
{
if (empty($text))
{
$text = $value;
}

$option = new stdClass;
$option->value = $value;
$option->text = $text;

return $option;
}

/**
* Create and return a new Option Group
*
* @param string $label Value and label for group [optional]
* @param array $options Array of options to insert into group [optional]
*
* @return array Return the new group as an array
*
* @since 3.0
*/
private static function createOptionGroup($label = '', $options = array())
{
$group = array();
$group['value'] = $label;
$group['text'] = $label;
$group['items'] = $options;

return $group;
}
}
18 changes: 12 additions & 6 deletions components/com_config/view/modules/tmpl/default_positions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
*/

defined('_JEXEC') or die;

$positions = $this->model->getPositions();

$currentPosition = $this->item['position'];
// Add custom position to options
$customGroupText = JText::_('COM_MODULES_CUSTOM_POSITION');

if (in_array($currentPosition, $positions) == false){
$positions[$currentPosition] = $currentPosition;
}
// Build field
$attr = array(
'id' => 'jform_position',
'list.select' => $this->item['position'],
'list.attr' => 'class="chzn-custom-value" '
. 'data-custom_group_text="' . $customGroupText . '" '
. 'data-no_results_text="' . JText::_('COM_MODULES_ADD_CUSTOM_POSITION') . '" '
. 'data-placeholder="' . JText::_('COM_MODULES_TYPE_OR_SELECT_POSITION') . '" '
);

echo JHtml::_('select.genericlist', $positions, 'jform[position]', '', '', '', $currentPosition);
echo JHtml::_('select.groupedlist', $positions, 'jform[position]', $attr);

0 comments on commit cda9f80

Please sign in to comment.