Skip to content

Commit

Permalink
Make JFormFieldCheckboxes and JFormFieldRadio subclasses of JFormFiel…
Browse files Browse the repository at this point in the history
…dList so that there is only one implementation of getOptions().

New class JFormOption and assorted option-type classes to be used to generate lists of options based on the <option/> xml element.
Other related changes to JForm and JFormHelper.
  • Loading branch information
okonomiyaki3000 committed Oct 5, 2015
1 parent fd65c24 commit 0156819
Show file tree
Hide file tree
Showing 17 changed files with 932 additions and 3 deletions.
6 changes: 6 additions & 0 deletions libraries/joomla/form/fields/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

defined('JPATH_PLATFORM') or die;

jimport('joomla.form.option');

/**
* Form Field class for the Joomla Platform.
* Supports a generic list of options.
Expand Down Expand Up @@ -119,6 +121,7 @@ protected function getOptions()
}
}

<<<<<<< 9c37b27d5b2cc24361ea09777f6b92c2cf22e214
$value = (string) $option['value'];
$text = trim((string) $option) ? trim((string) $option) : $value;

Expand Down Expand Up @@ -147,6 +150,9 @@ protected function getOptions()

// Add the option object to the result set.
$options[] = (object) $tmp;
=======
$options = array_merge($options, JFormOption::getOptions($option, $this->fieldname));
>>>>>>> Make JFormFieldCheckboxes and JFormFieldRadio subclasses of JFormFieldList so that there is only one implementation of getOptions().
}

reset($options);
Expand Down
11 changes: 11 additions & 0 deletions libraries/joomla/form/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1921,6 +1921,17 @@ protected function syncPaths()
self::addRulePath($path);
}

// Get any addoptionpath attributes from the form definition.
$paths = $this->xml->xpath('//*[@addoptionpath]/@addoptionpath');
$paths = array_map('strval', $paths ? $paths : array());

// Add the option paths.
foreach ($paths as $path)
{
$path = JPATH_ROOT . '/' . ltrim($path, '/\\');
self::addOptionPath($path);
}

return true;
}

Expand Down
35 changes: 32 additions & 3 deletions libraries/joomla/form/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class JFormHelper
{
/**
* Array with paths where entities(field, rule, form) can be found.
* Array with paths where entities(option, field, rule, form) can be found.
*
* Array's structure:
* <code>
Expand Down Expand Up @@ -152,12 +152,27 @@ public static function loadRuleClass($type)
return self::loadClass('rule', $type);
}

/**
* Attempt to import the JFormOption class file if it isn't already imported.
* You can use this method outside of JForm for loading an option for inheritance or composition.
*
* @param string $type Type of option whose class should be loaded.
*
* @return mixed Class name on success or false otherwise.
*
* @since 11.1
*/
public static function loadOptionClass($type)
{
return self::loadClass('option', $type);
}

/**
* Load a class for one of the form's entities of a particular type.
* Currently, it makes sense to use this method for the "field" and "rule" entities
* Currently, it makes sense to use this method for the "option", "field" and "rule" entities
* (but you can support more entities in your subclass).
*
* @param string $entity One of the form entities (field or rule).
* @param string $entity One of the form entities (option, field or rule).
* @param string $type Type of an entity.
*
* @return mixed Class name on success or false otherwise.
Expand Down Expand Up @@ -267,6 +282,20 @@ public static function addRulePath($new = null)
return self::addPath('rule', $new);
}

/**
* Method to add a path to the list of rule include paths.
*
* @param mixed $new A path or array of paths to add.
*
* @return array The list of paths that have been added.
*
* @since 11.1
*/
public static function addOptionPath($new = null)
{
return self::addPath('option', $new);
}

/**
* Method to add a path to the list of include paths for one of the form's entities.
* Currently supported entities: field, rule and form. You are free to support your own in a subclass.
Expand Down
55 changes: 55 additions & 0 deletions libraries/joomla/form/option.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* @package Joomla.Platform
* @subpackage Form
*
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('JPATH_PLATFORM') or die;

/**
* Abstract Form Option class for the Joomla Platform.
*
* @package Joomla.Platform
* @subpackage Form
* @since 11.1
*/
abstract class JFormOption
{
/**
* Method to get a list of options.
*
* @param SimpleXMLElement $option <option/> element
* @param string $fieldname The name of the field containing this option.
*
* @return array A list of objects representing HTML option elements (such as created by JHtmlSelect::option).
*
* @since 11.1
*/
public static function getOptions(SimpleXMLElement $option, $fieldname = '')
{
// Filter requirements
if ($requires = explode(',', (string) $option['requires']))
{
// Requires multilanguage
if (in_array('multilanguage', $requires) && !JLanguageMultilang::isEnabled())
{
return array();
}

// Requires associations
if (in_array('associations', $requires) && !JLanguageAssociations::isEnabled())
{
return array();
}
}

$type = $option['type'] ? (string) $option['type'] : 'standard';

$class = JFormHelper::loadOptionClass($type);

return $class ? $class::getOptions($option, $fieldname) : array();
}
}
47 changes: 47 additions & 0 deletions libraries/joomla/form/options/cachehandlers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* @package Joomla.Platform
* @subpackage Form
*
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('JPATH_PLATFORM') or die;

/**
* Cache Handlers Option class for the Joomla Platform.
*
* @package Joomla.Platform
* @subpackage Form
* @since 11.1
*/
abstract class JFormOptionCacheHandlers
{

protected $type = 'CacheHandlers';

/**
* Method to get a list of options.
*
* @param SimpleXMLElement $option <option/> element
* @param string $fieldname The name of the field containing this option.
*
* @return array A list of objects representing HTML option elements (such as created by JHtmlSelect::option).
*
* @since 11.1
*/
public static function getOptions(SimpleXMLElement $option, $fieldname = '')
{
$options = array();

// Convert to name => name array.
foreach (JCache::getStores() as $store)
{
$options[] = JHtml::_('select.option', $store, JText::_('JLIB_FORM_VALUE_CACHE_' . $store));
}

return $options;
}

}
69 changes: 69 additions & 0 deletions libraries/joomla/form/options/databaseconnections.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* @package Joomla.Platform
* @subpackage Form
*
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('JPATH_PLATFORM') or die;

/**
* Database Connections Option class for the Joomla Platform.
*
* @package Joomla.Platform
* @subpackage Form
* @since 11.1
*/
abstract class JFormOptionDatabaseConnections
{

protected $type = 'DatabaseConnections';

/**
* Method to get a list of options.
*
* @param SimpleXMLElement $option <option/> element
* @param string $fieldname The name of the field containing this option.
*
* @return array A list of objects representing HTML option elements (such as created by JHtmlSelect::option).
*
* @since 11.1
*/
public static function getOptions(SimpleXMLElement $option, $fieldname = '')
{
// List of options to return
$options = array();

// This gets the connectors available in the platform and supported by the server.
$connectors = JDatabaseDriver::getConnectors();

/**
* This gets the list of database types supported by the application.
* This should be entered in the form definition as a comma separated list.
* If no supported databases are listed, it is assumed all available databases
* are supported.
*/
$supported = array_filter(array_map('trim', explode(',', $option['supported'])));
if (!empty($supported))
{
$connectors = array_intersect($connectors, $supported);
}

foreach ($connectors as $connector)
{
$options[] = JHtml::_('select.option', $connector, JText::_(ucfirst($connector)));
}

// This will come into play if an application is installed that requires
// a database that is not available on the server.
if (empty($options))
{
$options[] = JHtml::_('select.option', '', JText::_('JNONE'));
}

return $options;
}

}
93 changes: 93 additions & 0 deletions libraries/joomla/form/options/files.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* @package Joomla.Platform
* @subpackage Form
*
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('JPATH_PLATFORM') or die;

/**
* Files Option class for the Joomla Platform.
*
* @package Joomla.Platform
* @subpackage Form
* @since 11.1
*/
abstract class JFormOptionFiles
{

protected $type = 'Files';

/**
* Method to get a list of options.
*
* @param SimpleXMLElement $option <option/> element
* @param string $fieldname The name of the field containing this option.
*
* @return array A list of objects representing HTML option elements (such as created by JHtmlSelect::option).
*
* @since 11.1
*/
public static function getOptions(SimpleXMLElement $option, $fieldname = '')
{
$options = array();

// Initialize some field attributes.
$filter = (string) $option['filter'];
$exclude = (string) $option['exclude'];
$stripExt = (string) $option['stripext'];
$hideNone = (string) $option['hide_none'];
$hideDefault = (string) $option['hide_default'];

// Get the path in which to search for file options.
$path = (string) $option['directory'];
if (!is_dir($path))
{
$path = JPATH_ROOT . '/' . $path;
}

// Prepend some default options based on field attributes.
if (!$hideNone)
{
$options[] = JHtml::_('select.option', '-1', JText::alt('JOPTION_DO_NOT_USE', preg_replace('/[^a-zA-Z0-9_\-]/', '_', $fieldname)));
}
if (!$hideDefault)
{
$options[] = JHtml::_('select.option', '', JText::alt('JOPTION_USE_DEFAULT', preg_replace('/[^a-zA-Z0-9_\-]/', '_', $fieldname)));
}

// Get a list of files in the search path with the given filter.
$files = JFolder::files($path, $filter);

// Build the options list from the list of files.
if (is_array($files))
{
foreach ($files as $file)
{

// Check to see if the file is in the exclude mask.
if ($exclude)
{
if (preg_match(chr(1) . $exclude . chr(1), $file))
{
continue;
}
}

// If the extension is to be stripped, do it.
if ($stripExt)
{
$file = JFile::stripExt($file);
}

$options[] = JHtml::_('select.option', $file, $file);
}
}

return $options;
}

}

0 comments on commit 0156819

Please sign in to comment.