public function addFilter($name, $field = null) { $col = $this->model->getField($name); if (!$col) { return; } if ($col['type'] == 'date') { $col['type'] = 'date_range'; } $control = Form::getControlByType($col['type']); if (!$control instanceof FormControlFilterable) { return; } $col['required'] = false; $col['name'] = $name; $col['field'] = $field ?? $name; if ($col['type'] == 'select') { $col['multiple'] = true; } if (isset($this->defaults[$name])) { $col['default'] = is_array($this->defaults[$name]) ? implode(',', $this->defaults[$name]) : $this->defaults[$name]; } $filter = Form::createControl($this->model, $col); if (!empty($field) && $field != $name) { $filter->setFilterColumnName($field); } $this->filters[] = $filter; return $this; }
public function getControls(CrudModel $model = null, $forView = true) { $controls = []; if (is_null($model)) { $model = CrudModel::createInstance($this->config['model']); } foreach ($this->config['fields'] as $field) { $control = Form::createControl($model, $model->getField($field)); if ($forView) { $id = $model->exists ? $model->getKey() : -1; $control->setField($this->name . '[' . $id . '][' . $control->config['name'] . ']'); $control->config['required'] = false; } $controls[] = $control; } return $controls; }
public function formatted($col, $args = []) { $rel = $this->crudRelations->resolveReference($col); if ($rel !== false) { try { $relObj = $this->{$rel['rel']}; if (!is_object($relObj)) { return ''; } $value = $relObj->{$rel['attr']}; } catch (\Exception $e) { return '(not found)' . $e->getMessage() . ':' . $e->getFile() . ':' . $e->getLine(); } } elseif ($this->__isset($col)) { $field = $this->getField($col); if (!empty($field['type'])) { $control = Form::createControl($this, $field); $value = $control->getOutputValue(); } else { $value = $this->{$col}; } } else { return; } if (!empty($args['formatter'])) { $formatter = 'crudFormatValue' . Str::camel($args['formatter']); if (method_exists($this, $formatter)) { $value = $this->{$formatter}($value, $args); } } return $value; }