Fixes fetching multi tiered pivot relation values
Refs https://github.com/daftspunk/oc-crm-plugin/issues/3 Minor code clean on select.js
This commit is contained in:
parent
4aca2d40ff
commit
46c473e2f2
|
|
@ -1,5 +1,6 @@
|
|||
<?php namespace Backend\Classes;
|
||||
|
||||
use October\Rain\Database\Model;
|
||||
use October\Rain\Html\Helper as HtmlHelper;
|
||||
|
||||
/**
|
||||
|
|
@ -192,4 +193,53 @@ class ListColumn
|
|||
|
||||
return HtmlHelper::nameToId($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this columns value from a supplied data set, which can be
|
||||
* an array or a model or another generic collection.
|
||||
* @param mixed $data
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValueFromData($data, $default = null)
|
||||
{
|
||||
$columnName = $this->valueFrom ?: $this->columnName;
|
||||
return $this->getColumnNameFromData($columnName, $data, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -713,12 +713,25 @@ class Lists extends WidgetBase
|
|||
$label = studly_case($name);
|
||||
}
|
||||
|
||||
/*
|
||||
* Auto configure pivot relation
|
||||
*/
|
||||
if (starts_with($name, 'pivot[') && strpos($name, ']') !== false) {
|
||||
$_name = HtmlHelper::nameToArray($name);
|
||||
$config['relation'] = array_shift($_name);
|
||||
$config['valueFrom'] = array_shift($_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;
|
||||
}
|
||||
/*
|
||||
* Auto configure standard relation
|
||||
*/
|
||||
elseif (strpos($name, '[') !== false && strpos($name, ']') !== false) {
|
||||
$config['valueFrom'] = $name;
|
||||
$config['sortable'] = false;
|
||||
|
|
@ -792,7 +805,9 @@ class Lists extends WidgetBase
|
|||
$value = $record->{$columnName}->lists($column->valueFrom);
|
||||
}
|
||||
elseif ($this->isColumnRelated($column) || $this->isColumnPivot($column)) {
|
||||
$value = $record->{$columnName} ? $record->{$columnName}->{$column->valueFrom} : null;
|
||||
$value = $record->{$columnName}
|
||||
? $column->getValueFromData($record->{$columnName})
|
||||
: null;
|
||||
}
|
||||
else {
|
||||
$value = null;
|
||||
|
|
@ -802,11 +817,7 @@ class Lists extends WidgetBase
|
|||
* Handle taking value from model attribute.
|
||||
*/
|
||||
elseif ($column->valueFrom) {
|
||||
$keyParts = HtmlHelper::nameToArray($column->valueFrom);
|
||||
$value = $record;
|
||||
foreach ($keyParts as $key) {
|
||||
$value = $value->{$key};
|
||||
}
|
||||
$value = $column->getValueFromData($record);
|
||||
}
|
||||
/*
|
||||
* Otherwise, if the column is a relation, it will be a custom select,
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@
|
|||
|
||||
var placeholder = $element.data('placeholder')
|
||||
if (placeholder) {
|
||||
extraOptions.placeholder = placeholder;
|
||||
extraOptions.placeholder = placeholder
|
||||
}
|
||||
|
||||
$element.select2($.extend({}, selectOptions, extraOptions))
|
||||
|
|
|
|||
Loading…
Reference in New Issue