/** * 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 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; }
/** * Ensures that a relationship field is valid * * @param array $options * @param bool $loadRelationships * * @return void */ public function setRelationshipType(array &$options, $loadRelationships) { //if this is a relationship if ($this->validator->arrayGet($options, 'type') === 'relationship') { //get the right key based on the relationship in the model $options['type'] = $this->getRelationshipKey($options['field_name']); //if we should load the relationships, set the option $options['load_relationships'] = $loadRelationships && !$this->validator->arrayGet($options, 'autocomplete', false); } }
/** * 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); } } }
/** * Sets up the default values for the $options array * * @param string $name //the key name for this action * @param array $options * * @return array */ public function parseDefaults($name, $options) { $model = $this->config->getDataModel(); //if the name is not a string or the options is not an array at this point, throw an error because we can't do anything with it if (!is_string($name) || !is_array($options)) { throw new \InvalidArgumentException("A custom action in your " . $this->config->getOption('action_name') . " configuration file is invalid"); } //set the action name $options['action_name'] = $name; //set the permission $permission = $this->validator->arrayGet($options, 'permission', false); $options['has_permission'] = is_callable($permission) ? $permission($model) : true; //check if the messages array exists $options['messages'] = $this->validator->arrayGet($options, 'messages', array()); $options['messages'] = is_array($options['messages']) ? $options['messages'] : array(); return $options; }
/** * 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; }
/** * Sets the filter options for this item * * @param array $filter * * @return void */ public function setFilter($filter) { $this->userOptions['value'] = $this->getFilterValue($this->validator->arrayGet($filter, 'value', $this->getOption('value'))); $this->userOptions['min_value'] = $this->getFilterValue($this->validator->arrayGet($filter, 'min_value', $this->getOption('min_value'))); $this->userOptions['max_value'] = $this->getFilterValue($this->validator->arrayGet($filter, 'max_value', $this->getOption('max_value'))); }