$editFields = Field::getEditFields($config); //add the view fields $view->primaryKey = $model::$key; $view->rows = ModelHelper::getRows($config, $config->sort); $view->editFields = $editFields; $view->actions = $config->actions; $view->filters = Field::getFilters($config); $view->baseUrl = $baseUrl; $view->assetUrl = URL::to('bundles/administrator/'); $view->route = $route['path'] . '/'; $view->model = isset($view->model) ? $view->model : false; }); //admin settings view View::composer('administrator::settings', function ($view) { $config = $view->config; $baseUrl = URL::to_route('admin_index'); $route = parse_url($baseUrl); //get the edit fields $editFields = Field::getEditFields($config); //add the view fields $view->editFields = $editFields; $view->actions = $config->actions; $view->baseUrl = $baseUrl; $view->assetUrl = URL::to('bundles/administrator/'); $view->route = $route['path'] . '/'; }); //header view View::composer(array('administrator::partials.header', 'administrator::dashboard'), function ($view) { $view->menu = Menu::getMenu(); $view->settingsPrefix = Admin\Libraries\SettingsConfig::$prefix; });
/** * After a model has been saved, this is called to save the relationships * * @param ModelConfig $config * @param Eloquent $model * * @return false|object */ public static function saveRelationships($config, &$model) { $editFields = Field::getEditFields($config); //run through the edit fields to see if we need to set relationships foreach ($editFields['objectFields'] as $field => $info) { if ($info->external) { $info->fillModel($model, \Input::get($field, NULL)); } } }
<?php use Admin\Libraries\ModelHelper; use Admin\Libraries\Fields\Field; use Admin\Libraries\Column; use Admin\Libraries\Sort; //View Composers //admin index view View::composer('administrator::index', function ($view) { //get a model instance that we'll use for constructing stuff $modelInstance = ModelHelper::getModel($view->modelName); $columns = Column::getColumns($modelInstance); $editFields = Field::getEditFields($modelInstance); $bundleConfig = Bundle::get('administrator'); //add the view fields $view->modelTitle = Config::get('administrator::administrator.models.' . $view->modelName . '.title', $view->modelName); $view->modelSingle = Config::get('administrator::administrator.models.' . $view->modelName . '.single', $view->modelTitle); $view->columns = $columns['columns']; $view->includedColumns = $columns['includedColumns']; $view->primaryKey = $modelInstance::$key; $view->sort = Sort::get($modelInstance)->toArray(); $view->rows = ModelHelper::getRows($modelInstance, $view->sort); $view->editFields = $editFields['arrayFields']; $view->dataModel = $editFields['dataModel']; $view->filters = ModelHelper::getFilters($modelInstance); $view->baseUrl = URL::to_route('admin_index'); $view->bundleHandles = $bundleConfig['handles']; $view->expandWidth = ModelHelper::getExpandWidth($modelInstance); $view->modelInstance = $modelInstance; $view->model = isset($view->model) ? $view->model : false; });
/** * Given a model, field, type (filter or edit), and search term, this returns an array of arrays with 'id' and 'name' * * @param Eloquent $model * @param string $field * @param string $type //either 'filter' or 'edit' * @param array|false $selectedItems //an array of ids of currently-selected items (necessary to maintain selections) * @param string $term //the search term * * @return array */ public static function getRelationshipSuggestions($model, $field, $type, $selectedItems, $term) { //first get the related model $related_model = $model->{$field}()->model; $info = false; //now we can sort out what the actual field info is if ($type === 'filter') { $fields = static::getFilters($model); } else { $editFields = Field::getEditFields($model); $fields = $editFields['arrayFields']; } //iterate over the fields to get the one for this $field value foreach ($fields as $key => $val) { if ($key === $field) { $info = $val; } } //if we can't find the field, return an empty array if (!$info) { return array(); } //set up the field object $info = Field::get($field, $info, $model, false); //now we can start to set up the query $query = new \Laravel\Database\Eloquent\Query($related_model); //set up the wheres foreach ($info->searchFields as $search) { $query->or_where(DB::raw($search), 'LIKE', '%' . $term . '%'); } //include the currently-selected items if ($selectedItems) { //if this isn't an array, set it up as one $selectedItems = is_array($selectedItems) ? $selectedItems : array($selectedItems); $query->or_where_in($related_model::$key, $selectedItems); } else { $selectedItems = array(); } //set up the limits $query->take($info->numOptions + count($selectedItems)); //return the array map based on the result set return array_map(function ($m) use($info, $field, $related_model) { return array($related_model::$key => $m->{$related_model::$key}, $info->nameField => $m->{$info->nameField}); }, $query->get()); }
/** * Gets a model's columns given the a model's config * * @param ModelConfig $config * * @return array( * 'columns' => array(detailed..), * 'includedColumns' => array(field => full_column_name, ...)), * 'computedColumns' => array(key, key, key) */ public static function getColumns($config) { $model = $config->model; $return = array('columns' => array(), 'columnArrays' => array(), 'columnObjects' => array(), 'includedColumns' => array(), 'computedColumns' => array(), 'relatedColumns' => array()); //check if there are columns to iterate over if (count($config->columns) > 0) { $columns = array(); foreach ($config->columns as $field => $column) { //get the column object if (!($columnObject = Column::get($field, $column, $config))) { continue; } //save the column object with a $field-based key, as a simple array (to use in knockout), and as a simple array of arrays $return['columnObjects'][$field] = $columnObject; $return['columns'][] = $columnObject; $return['columnArrays'][] = $columnObject->toArray(); //categorize the columns if ($columnObject->isRelated) { $return['relatedColumns'][$columnObject->field] = $columnObject->field; //if there are nested values, we'll want to grab the values slightly differently if (sizeof($columnObject->nested)) { $fk = $columnObject->nested['models'][0]->{$columnObject->nested['pieces'][0]}()->foreign; $return['includedColumns'][$fk] = $model->table() . '.' . $fk; } else { if ($fk = $columnObject->relationshipField->foreignKey) { $return['includedColumns'][$fk] = $model->table() . '.' . $fk; } } } else { if ($columnObject->isComputed) { $return['computedColumns'][$columnObject->field] = $columnObject->field; } else { $return['includedColumns'][$columnObject->field] = $model->table() . '.' . $columnObject->field; } } } } else { throw new Exception("Administrator: you must provide a valid 'columns' array in each model's config"); } //make sure the table key is included if (!array_get($return['includedColumns'], $model::$key)) { $return['includedColumns'][$model::$key] = $model->table() . '.' . $model::$key; } //make sure any belongs_to fields that aren't on the columns list are included $editFields = Field::getEditFields($config); foreach ($editFields['objectFields'] as $field => $info) { if (is_a($info, 'Admin\\Libraries\\Fields\\Relationships\\BelongsTo')) { $return['includedColumns'][$info->foreignKey] = $model->table() . '.' . $info->foreignKey; } } return $return; }