/** * Safe accessor for configuration values. * @param $name Config name, supports array names like "field[key]" * @param $default Default value if nothing is found * @return string */ public function getConfig($name = null, $default = null) { /* * Return all config */ if (is_null($name)) { return $this->config; } /* * Array field name, eg: field[key][key2][key3] */ $keyParts = HtmlHelper::nameToArray($name); /* * First part will be the field name, pop it off */ $fieldName = array_shift($keyParts); if (!isset($this->config->{$fieldName})) { return $default; } $result = $this->config->{$fieldName}; /* * Loop the remaining key parts and build a result */ foreach ($keyParts as $key) { if (!is_array($result) || !array_key_exists($key, $result)) { return $default; } $result = $result[$key]; } return $result; }
public function testNameToArray() { $result = HtmlHelper::nameToArray('field'); $this->assertInternalType('array', $result); $this->assertEquals(1, count($result)); $this->assertTrue(in_array('field', $result)); $result = HtmlHelper::nameToArray('field[key1]'); $this->assertInternalType('array', $result); $this->assertEquals(2, count($result)); $this->assertTrue(in_array('field', $result)); $this->assertTrue(in_array('key1', $result)); $result = HtmlHelper::nameToArray('field[][key1]'); $this->assertInternalType('array', $result); $this->assertEquals(2, count($result)); $this->assertTrue(in_array('field', $result)); $this->assertTrue(in_array('key1', $result)); $result = HtmlHelper::nameToArray('field[key1][key2][key3]'); $this->assertInternalType('array', $result); $this->assertEquals(4, count($result)); $this->assertTrue(in_array('field', $result)); $this->assertTrue(in_array('key1', $result)); $this->assertTrue(in_array('key2', $result)); $this->assertTrue(in_array('key3', $result)); }
/** * Returns a unique ID for this widget. Useful in creating HTML markup. * @param string $suffix An extra string to append to the ID. * @return string A unique identifier. */ public function getId($suffix = null) { $id = class_basename(get_called_class()); if ($this->alias != $this->defaultAlias) { $id .= '-' . $this->alias; } if ($suffix !== null) { $id .= '-' . $suffix; } return HtmlHelper::nameToId($id); }
/** * Returns a value suitable for the column id property. * @param string $suffix Specify a suffix string * @return string */ public function getId($suffix = null) { $id = 'column'; $id .= '-' . $this->columnName; if ($suffix) { $id .= '-' . $suffix; } return HtmlHelper::nameToId($id); }
/** * Since the locker does always contain the latest values, this method * will take the save data from the repeater and merge it in to the * locker based on which ever locale is selected using an item map * @return void */ protected function rewritePostValues() { /* * Get the selected locale at postback */ $data = post('RLTranslateRepeaterLocale'); $fieldName = implode('.', HtmlHelper::nameToArray($this->fieldName)); $locale = array_get($data, $fieldName); if (!$locale) { return; } /* * Splice the save data in to the locker data for selected locale */ $data = $this->getPrimarySaveDataAsArray(); $fieldName = 'RLTranslate.' . $locale . '.' . implode('.', HtmlHelper::nameToArray($this->fieldName)); array_set($_POST, $fieldName, json_encode($data)); }
/** * Returns the final model and attribute name of a nested attribute. * Eg: list($model, $attribute) = $this->resolveAttribute('person[phone]'); * @param string $attribute. * @return array */ public function resolveModelAttribute($model, $attribute = null) { if ($attribute === null) { $attribute = $this->valueFrom ?: $this->fieldName; } $parts = is_array($attribute) ? $attribute : HtmlHelper::nameToArray($attribute); $last = array_pop($parts); foreach ($parts as $part) { $model = $model->{$part}; } return [$model, $last]; }
/** * Returns a raw column value * @return string */ public function getColumnValueRaw($record, $column) { $columnName = $column->columnName; /* * Handle taking value from model relation. */ if ($column->valueFrom && $column->relation) { $columnName = $column->relation; if (!array_key_exists($columnName, $record->getRelations())) { $value = null; } elseif ($this->isColumnRelated($column, true)) { $value = $record->{$columnName}->lists($column->valueFrom); } elseif ($this->isColumnRelated($column) || $this->isColumnPivot($column)) { $value = $record->{$columnName} ? $record->{$columnName}->{$column->valueFrom} : null; } else { $value = null; } } elseif ($column->valueFrom) { $keyParts = HtmlHelper::nameToArray($column->valueFrom); $value = $record; foreach ($keyParts as $key) { $value = $value->{$key}; } } else { if ($record->hasRelation($columnName) && array_key_exists($columnName, $record->attributes)) { $value = $record->attributes[$columnName]; } else { $value = $record->{$columnName}; } } return $value; }
/** * Returns an array of translated values for this field * @return array */ public function getLocaleSaveData() { $values = []; $data = post('RLTranslate'); if (!is_array($data)) { return $values; } $fieldName = implode('.', HtmlHelper::nameToArray($this->fieldName)); foreach ($data as $locale => $_data) { $values[$locale] = array_get($_data, $fieldName); } return $values; }
/** * Creates a list column object from it's name and configuration. */ protected function makeListColumn($name, $config) { if (is_string($config)) { $label = $config; } elseif (isset($config['label'])) { $label = $config['label']; } else { $label = studly_case($name); } /* * Auto configure pivot relation */ if (starts_with($name, 'pivot[') && strpos($name, ']') !== false) { $_name = HtmlHelper::nameToArray($name); $relationName = array_shift($_name); $valueFrom = array_shift($_name); if (count($_name) > 0) { $valueFrom .= '[' . implode('][', $_name) . ']'; } $config['relation'] = $relationName; $config['valueFrom'] = $valueFrom; $config['searchable'] = false; } elseif (strpos($name, '[') !== false && strpos($name, ']') !== false) { $config['valueFrom'] = $name; $config['sortable'] = false; $config['searchable'] = false; } $columnType = isset($config['type']) ? $config['type'] : null; $column = new ListColumn($name, $label); $column->displayAs($columnType, $config); return $column; }
/** * Returns a unique ID for this widget. Useful in creating HTML markup. */ public function getId($suffix = null) { $id = parent::getId($suffix); $id .= '-' . $this->fieldName; return HtmlHelper::nameToId($id); }
/** * Sets an attribute from a model/array with nesting support. * @param mixed $data * @param string $attribute * @return mixed */ protected function setAttributeFromData(&$data, $attribute, $value) { $keyArray = HtmlHelper::nameToArray($attribute); array_set($data, implode('.', $keyArray), $value); return $value; }
/** * Looks up the column */ public function getFieldValue($field) { if (is_string($field)) { if (!isset($this->fields[$field])) { throw new ApplicationException(Lang::get('backend::lang.form.missing_definition', compact('field'))); } $field = $this->fields[$field]; } $fieldName = $field->fieldName; $defaultValue = strlen($field->defaults) ? $field->defaults : null; /* * Array field name, eg: field[key][key2][key3] */ $keyParts = Helper::nameToArray($fieldName); $lastField = end($keyParts); $result = $this->data; // Sub fields are always the last field in the array return $result[$lastField]; }
/** * Returns a value suitable for the scope id property. */ public function getId($suffix = null) { $id = 'scope'; $id .= '-' . $this->scopeName; if ($suffix) { $id .= '-' . $suffix; } if ($this->idPrefix) { $id = $this->idPrefix . '-' . $id; } return HtmlHelper::nameToId($id); }
/** * Internal method to extract the value of a column name from a data set. * @param string $columnName * @param mixed $data * @param mixed $default * @return mixed */ protected function getColumnNameFromData($columnName, $data, $default = null) { /* * Array column name, eg: column[key][key2][key3] */ $keyParts = HtmlHelper::nameToArray($columnName); $result = $data; /* * Loop the column key parts and build a value. * To support relations only the last column should return the * relation value, all others will look up the relation object as normal. */ foreach ($keyParts as $key) { if ($result instanceof Model && $result->hasRelation($key)) { $result = $result->{$key}; } else { if (!isset($result->{$key})) { return $default; } $result = $result->{$key}; } } return $result; }
/** * Returns an array of translated values for this field * @return array */ public function getLocaleSaveData() { $values = []; $data = post('RLTranslate'); if (!is_array($data)) { return $values; } $fieldName = implode('.', HtmlHelper::nameToArray($this->fieldName)); $isJson = $this->isLocaleFieldJsonable(); foreach ($data as $locale => $_data) { $value = array_get($_data, $fieldName); $values[$locale] = $isJson ? json_decode($value, true) : $value; } return $values; }
protected function makeTableWidget() { $config = $this->makeConfig((array) $this->config); $config->dataSource = 'client'; $config->alias = studly_case(HtmlHelper::nameToId($this->fieldName)) . 'datatable'; $config->fieldName = $this->fieldName; $table = new Table($this->controller, $config); $table->bindEvent('table.getDropdownOptions', [$this, 'getDataTableOptions']); return $table; }
/** * Returns postback data from a submitted form. */ public function getSaveData() { $data = $this->arrayName ? post($this->arrayName) : post(); if (!$data) { $data = []; } /* * Number fields should be converted to integers */ foreach ($this->allFields as $field) { if ($field->type != 'number') { continue; } /* * Handle HTML array, eg: item[key][another] */ $parts = HtmlHelper::nameToArray($field->fieldName); $dotted = implode('.', $parts); if (($value = array_get($data, $dotted)) !== null) { $value = !strlen(trim($value)) ? null : (double) $value; array_set($data, $dotted, $value); } } /* * Give widgets an opportunity to process the data. */ foreach ($this->formWidgets as $field => $widget) { $parts = HtmlHelper::nameToArray($field); $dotted = implode('.', $parts); $widgetValue = $widget->getSaveValue(array_get($data, $dotted)); array_set($data, $dotted, $widgetValue); } /* * Handle fields that differ by fieldName and valueFrom * @todo @deprecated / Not needed? Remove if year >= 2016 */ // $remappedFields = []; // foreach ($this->allFields as $field) { // if ($field->fieldName == $field->valueFrom) { // continue; // } // /* // * Get the value, remove it from the data collection // */ // $parts = HtmlHelper::nameToArray($field->fieldName); // $dotted = implode('.', $parts); // $value = array_get($data, $dotted); // array_forget($data, $dotted); // /* // * Set the new value to the data collection // */ // $parts = HtmlHelper::nameToArray($field->valueFrom); // $dotted = implode('.', $parts); // array_set($remappedFields, $dotted, $value); // } // if (count($remappedFields) > 0) { // $data = array_merge($remappedFields, $data); // // Could be useful one day for field name collisions // // $data['X_OCTOBER_REMAPPED_FIELDS'] = $remappedFields; // } return $data; }
protected function makeTableWidget() { $config = $this->makeConfig((array) $this->config); $config->dataSource = 'client'; // It's safe to use the field name as an alias // as field names do not repeat in forms. This // approach lets to access the table data by the // field name in POST requests directly (required // in some edge cases). $config->alias = studly_case(HtmlHelper::nameToId($this->fieldName)) . 'datatable'; $table = new Table($this->controller, $config); $table->bindEvent('table.getDropdownOptions', [$this, 'getDataTableOptions']); return $table; }
/** * @deprecated Moved to October\Rain\Html\Helper::nameToArray */ public static function evalHtmlArray($string) { traceLog('Str::evalHtmlArray has been deprecated, use October\\Rain\\Html\\Helper::nameToArray instead.'); return HtmlHelper::nameToArray($string); }
/** * Internal method to extract the value of a field name from a data set. * @param string $fieldName * @param mixed $data * @param mixed $default * @return mixed */ protected function getFieldNameFromData($fieldName, $data, $default = null) { /* * Array field name, eg: field[key][key2][key3] */ $keyParts = HtmlHelper::nameToArray($fieldName); $lastField = end($keyParts); $result = $data; /* * Loop the field key parts and build a value. * To support relations only the last field should return the * relation value, all others will look up the relation object as normal. */ foreach ($keyParts as $key) { if ($result instanceof Model && $result->hasRelation($key)) { if ($key == $lastField) { $result = $result->getRelationValue($key) ?: $default; } else { $result = $result->{$key}; } } elseif (is_array($result)) { if (!array_key_exists($key, $result)) { return $default; } $result = $result[$key]; } else { if (!isset($result->{$key})) { return $default; } $result = $result->{$key}; } } return $result; }
/** * Returns post data from a submitted form. * * @return array */ public function getSaveData() { $this->defineFormFields(); $result = []; /* * Source data */ $data = $this->arrayName ? post($this->arrayName) : post(); if (!$data) { $data = []; } /* * Spin over each field and extract the postback value */ foreach ($this->allFields as $field) { /* * Disabled and hidden should be omitted from data set */ if ($field->disabled || $field->hidden) { continue; } /* * Handle HTML array, eg: item[key][another] */ $parts = HtmlHelper::nameToArray($field->fieldName); if (($value = $this->dataArrayGet($data, $parts)) !== null) { /* * Number fields should be converted to integers */ if ($field->type === 'number') { $value = !strlen(trim($value)) ? null : (double) $value; } $this->dataArraySet($result, $parts, $value); } } /* * Give widgets an opportunity to process the data. */ foreach ($this->formWidgets as $field => $widget) { $parts = HtmlHelper::nameToArray($field); $widgetValue = $widget->getSaveValue($this->dataArrayGet($result, $parts)); $this->dataArraySet($result, $parts, $widgetValue); } return $result; }
/** * Looks up the column value */ public function getColumnValue($record, $column) { $columnName = $column->columnName; /* * Handle taking value from model relation. */ if ($column->valueFrom && $column->relation) { $columnName = $column->relation; if (!array_key_exists($columnName, $record->getRelations())) { $value = null; } elseif ($this->isColumnRelated($column, true)) { $value = implode(', ', $record->{$columnName}->lists($column->valueFrom)); } elseif ($this->isColumnRelated($column) || $this->isColumnPivot($column)) { $value = $record->{$columnName} ? $record->{$columnName}->{$column->valueFrom} : null; } else { $value = null; } } elseif ($column->valueFrom) { $keyParts = HtmlHelper::nameToArray($column->valueFrom); $value = $record; foreach ($keyParts as $key) { $value = $value->{$key}; } } else { if ($record->hasRelation($columnName) && array_key_exists($columnName, $record->attributes)) { $value = $record->attributes[$columnName]; } else { $value = $record->{$columnName}; } } if (method_exists($this, 'eval' . studly_case($column->type) . 'TypeValue')) { $value = $this->{'eval' . studly_case($column->type) . 'TypeValue'}($record, $column, $value); } /* * Apply default value. */ if ($value === '' || $value === null) { $value = $column->defaults; } /* * Extensibility */ if (($response = Event::fire('backend.list.overrideColumnValue', [$this, $record, $column, $value], true)) !== null) { $value = $response; } if (($response = $this->fireEvent('list.overrideColumnValue', [$record, $column, $value], true)) !== null) { $value = $response; } return $value; }
/** * Returns postback data from a submitted form. */ public function getSaveData() { $result = []; /* * Source data */ $data = $this->arrayName ? post($this->arrayName) : post(); if (!$data) { $data = []; } /* * Spin over each field and extract the postback value */ foreach ($this->allFields as $field) { /* * Handle HTML array, eg: item[key][another] */ $parts = HtmlHelper::nameToArray($field->fieldName); $dotted = implode('.', $parts); if (($value = array_get($data, $dotted)) !== null) { /* * Number fields should be converted to integers */ if ($field->type == 'number') { $value = !strlen(trim($value)) ? null : (double) $value; } array_set($result, $dotted, $value); } } /* * Give widgets an opportunity to process the data. */ foreach ($this->formWidgets as $field => $widget) { $parts = HtmlHelper::nameToArray($field); $dotted = implode('.', $parts); $widgetValue = $widget->getSaveValue(array_get($result, $dotted)); array_set($result, $dotted, $widgetValue); } return $result; }