/** * Gets the columns that are on the model's table (i.e. not related or computed) * * @param array $fields * * @return array */ public function getIncludedColumns(array $fields) { //make sure we only run this once and then return the cached version if (!sizeof($this->includedColumns)) { $model = $this->config->getDataModel(); foreach ($this->getColumns() as $column) { if ($column->getOption('is_related')) { $this->includedColumns = array_merge($this->includedColumns, $column->getIncludedColumn()); } else { if (!$column->getOption('is_computed')) { $this->includedColumns[$column->getOption('column_name')] = $model->getTable() . '.' . $column->getOption('column_name'); } } } //make sure the table key is included if (!$this->validator->arrayGet($this->includedColumns, $model->getKeyName())) { $this->includedColumns[$model->getKeyName()] = $model->getTable() . '.' . $model->getKeyName(); } //make sure any belongs_to fields that aren't on the columns list are included foreach ($fields as $field) { if (is_a($field, 'Frozennode\\Administrator\\Fields\\Relationships\\BelongsTo')) { $this->includedColumns[$field->getOption('foreign_key')] = $model->getTable() . '.' . $field->getOption('foreign_key'); } } } return $this->includedColumns; }
/** * Builds the necessary fields on the object * * @return void */ public function build() { $model = $this->config->getDataModel(); $options = $this->suppliedOptions; $this->tablePrefix = $this->db->getTablePrefix(); //set some options-based defaults $options['title'] = $this->validator->arrayGet($options, 'title', $options['column_name']); $options['sort_field'] = $this->validator->arrayGet($options, 'sort_field', $options['column_name']); //if the supplied item is an accessor, make this unsortable for the moment if (method_exists($model, camel_case('get_' . $options['column_name'] . '_attribute')) && $options['column_name'] === $options['sort_field']) { $options['sortable'] = false; } //however, if this is not a relation and the select option was supplied, str_replace the select option and make it sortable again if ($select = $this->validator->arrayGet($options, 'select')) { $options['select'] = str_replace('(:table)', $this->tablePrefix . $model->getTable(), $select); } //now we do some final organization to categorize these columns (useful later in the sorting) if (method_exists($model, camel_case('get_' . $options['column_name'] . '_attribute')) || $select) { $options['is_computed'] = true; } else { $options['is_included'] = true; } //run the visible property closure if supplied $visible = $this->validator->arrayGet($options, 'visible'); if (is_callable($visible)) { $options['visible'] = $visible($this->config->getDataModel()) ? true : false; } $this->suppliedOptions = $options; }
/** * Gets all action permissions * * @param bool $override * * @return array of Action objects */ public function getActionPermissions($override = false) { //make sure we only run this once and then return the cached version if (!sizeof($this->actionPermissions) || $override) { $this->actionPermissions = array(); $model = $this->config->getDataModel(); $options = $this->config->getOption('action_permissions'); $defaults = $this->actionPermissionsDefaults; //merge the user-supplied action permissions into the defaults $permissions = array_merge($defaults, $options); //loop over the actions to build the list foreach ($permissions as $action => $callback) { if (is_callable($callback)) { $this->actionPermissions[$action] = (bool) $callback($model); } else { $this->actionPermissions[$action] = (bool) $callback; } } } return $this->actionPermissions; }
/** * Sets up the sort options * * @param array $sort */ public function setSort($sort = null) { $sort = $sort && is_array($sort) ? $sort : $this->config->getOption('sort'); //set the sort values $this->sort = array('field' => isset($sort['field']) ? $sort['field'] : $this->config->getDataModel()->getKeyName(), 'direction' => isset($sort['direction']) ? $sort['direction'] : 'desc'); //if the sort direction isn't valid, set it to 'desc' if (!in_array($this->sort['direction'], array('asc', 'desc'))) { $this->sort['direction'] = 'desc'; } }
/** * Takes an eloquent result array and turns it into an options array that can be used in the UI * * @param \Illuminate\Database\Eloquent\Model $model * @param \Frozennode\Administrator\Fields\Field $field * @param array $results * * @return array */ public function formatSelectOptions($model, Field $field, array $results) { $model = $this->config->getDataModel(); return array_map(function($m) use ($field, $model) { return array( 'id' => $m->{$model->getKeyName()}, 'text' => strval($m->{$field->getOption('name_field')}), ); }, $results); }
/** * Filters a query object given * * @param \Illuminate\Database\Query\Builder $query * @param array $selects * * @return void */ public function filterQuery(QueryBuilder &$query, &$selects = null) { $model = $this->config->getDataModel(); //if this field has a min/max range, set it if ($this->getOption('min_max')) { if ($minValue = $this->getOption('min_value')) { $query->where($model->getTable() . '.' . $this->getOption('field_name'), '>=', $minValue); } if ($maxValue = $this->getOption('max_value')) { $query->where($model->getTable() . '.' . $this->getOption('field_name'), '<=', $maxValue); } } }
/** * Sets up the values of all the options that can be either strings or closures. * * @param array $options //the passed-by-reference array on which to do the transformation * @param array $keys //the keys to check */ public function buildStringOrCallable(array &$options, array $keys) { $model = $this->config->getDataModel(); //iterate over the keys foreach ($keys as $key) { //check if the key's value was supplied $suppliedValue = $this->validator->arrayGet($options, $key); //if it's a string, simply set it if (is_string($suppliedValue)) { $options[$key] = $suppliedValue; } elseif (is_callable($suppliedValue)) { $options[$key] = $suppliedValue($model); } } }
/** * Builds the necessary fields on the object * * @return void */ public function build() { $model = $this->config->getDataModel(); $options = $this->suppliedOptions; //check if a confirmation was supplied $confirmation = $this->validator->arrayGet($options, 'confirmation'); //if it's a string, simply set it if (is_string($confirmation)) { $options['confirmation'] = $confirmation; } //if it's callable pass it the current model and run it else if (is_callable($confirmation)) { $options['confirmation'] = $confirmation($model); } $this->suppliedOptions = $options; }
/** * Takes the supplied $selectedItems mixed value and formats it to a usable array * * @param mixed $constraints * @param \Illuminate\Database\Query\Builder $query * @param \Frozennode\Administrator\Fields\Field $fieldObject * * @return array */ public function applyConstraints($constraints, EloquentBuilder &$query, Field $fieldObject) { $configConstraints = $fieldObject->getOption('constraints'); if (sizeof($configConstraints)) { //iterate over the config constraints foreach ($configConstraints as $key => $relationshipName) { //now that we're looping through the constraints, check to see if this one was supplied if (isset($constraints[$key]) && $constraints[$key] && sizeof($constraints[$key])) { //first we get the other model and the relationship field on it $model = $this->config->getDataModel(); $relatedModel = $model->{$fieldObject->getOption('field_name')}()->getRelated(); $otherModel = $model->{$key}()->getRelated(); //set the data model for the config $this->config->setDataModel($otherModel); $otherField = $this->make($relationshipName, array('type' => 'relationship'), false); //constrain the query $otherField->constrainQuery($query, $relatedModel, $constraints[$key]); //set the data model back to the original $this->config->setDataModel($model); } } } }