FOF30\Form\Header\Selectable::getOptions PHP Метод

getOptions() защищенный Метод

Create objects for the options
protected getOptions ( ) : array
Результат array The array of option objects
    protected function getOptions()
    {
        $options = array();
        // Get the field $options
        /** @var \SimpleXMLElement $option */
        foreach ($this->element->children() as $option) {
            // Only add <option /> elements.
            if ($option->getName() != 'option') {
                continue;
            }
            // Create a new option object based on the <option /> element.
            $options[] = JHtml::_('select.option', (string) $option['value'], JText::alt(trim((string) $option), preg_replace('/[^a-zA-Z0-9_\\-]/', '_', $this->fieldname)), 'value', 'text', (string) $option['disabled'] == 'true');
        }
        // Do we have a class and method source for our options?
        $source_file = empty($this->element['source_file']) ? '' : (string) $this->element['source_file'];
        $source_class = empty($this->element['source_class']) ? '' : (string) $this->element['source_class'];
        $source_method = empty($this->element['source_method']) ? '' : (string) $this->element['source_method'];
        $source_key = empty($this->element['source_key']) ? '*' : (string) $this->element['source_key'];
        $source_value = empty($this->element['source_value']) ? '*' : (string) $this->element['source_value'];
        $source_translate = empty($this->element['source_translate']) ? 'true' : (string) $this->element['source_translate'];
        $source_translate = in_array(strtolower($source_translate), array('true', 'yes', '1', 'on')) ? true : false;
        $source_format = empty($this->element['source_format']) ? '' : (string) $this->element['source_format'];
        if ($source_class && $source_method) {
            // Maybe we have to load a file?
            if (!empty($source_file)) {
                $source_file = $this->form->getContainer()->template->parsePath($source_file, true);
                if ($this->form->getContainer()->filesystem->fileExists($source_file)) {
                    include_once $source_file;
                }
            }
            // Make sure the class exists
            if (class_exists($source_class, true)) {
                // ...and so does the option
                if (in_array($source_method, get_class_methods($source_class))) {
                    // Get the data from the class
                    if ($source_format == 'optionsobject') {
                        $options = array_merge($options, $source_class::$source_method());
                    } else {
                        $source_data = $source_class::$source_method();
                        // Loop through the data and prime the $options array
                        foreach ($source_data as $k => $v) {
                            $key = empty($source_key) || $source_key == '*' ? $k : @$v[$source_key];
                            $value = empty($source_value) || $source_value == '*' ? $v : @$v[$source_value];
                            if ($source_translate) {
                                $value = JText::_($value);
                            }
                            $options[] = JHtml::_('select.option', $key, $value, 'value', 'text');
                        }
                    }
                }
            }
        }
        reset($options);
        return $options;
    }

Usage Example

Пример #1
0
Файл: Sql.php Проект: akeeba/fof
 /**
  * Create objects for the options
  *
  * @return  array  The array of option objects
  */
 protected function getOptions()
 {
     $options = array();
     // Initialize some field attributes.
     $key = $this->element['key_field'] ? (string) $this->element['key_field'] : 'value';
     $value = $this->element['value_field'] ? (string) $this->element['value_field'] : (string) $this->element['name'];
     $translate = $this->element['translate'] ? (string) $this->element['translate'] : false;
     $query = (string) $this->element['query'];
     // Get the database object.
     $db = $this->form->getContainer()->platform->getDbo();
     // Set the query and get the result list.
     $db->setQuery($query);
     $items = $db->loadObjectlist();
     // Build the field options.
     if (!empty($items)) {
         foreach ($items as $item) {
             if ($translate == true) {
                 $options[] = JHtml::_('select.option', $item->{$key}, JText::_($item->{$value}));
             } else {
                 $options[] = JHtml::_('select.option', $item->{$key}, $item->{$value});
             }
         }
     }
     // Merge any additional options in the XML definition.
     $options = array_merge(parent::getOptions(), $options);
     return $options;
 }
All Usage Examples Of FOF30\Form\Header\Selectable::getOptions
Selectable