<?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; });
use Admin\Libraries\Fields\Field; use Admin\Libraries\ModelConfig; use Admin\Libraries\Menu; //View Composers //admin index view View::composer('administrator::index', function ($view) { //get a model instance that we'll use for constructing stuff $config = $view->config; $model = $config->model; $baseUrl = URL::to_route('admin_index'); $route = parse_url($baseUrl); //get the edit fields $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);
//accountingjs $assets->add('accountingjs', 'js/accounting.js'); //historyjs $assets->add('historyjs', 'js/history/native.history.js'); //and finally the admin js file $assets->add('admin', 'js/admin.js'); }); //validate_admin filter Route::filter('validate_admin', function () { //get the admin check closure that should be supplied in the config $authCheck = Config::get('administrator::administrator.auth_check'); if (!$authCheck()) { $loginUrl = URL::to(Config::get('administrator::administrator.login_path', 'user/login')); $redirectKey = Config::get('administrator::administrator.login_redirect_key', 'redirect'); $redirectUri = URL::to_route('admin_dashboard'); return Redirect::to($loginUrl)->with($redirectKey, $redirectUri); } }); //validate_model filter Route::filter('validate_model', function () { $modelName = URI::segment(2); $model = ModelHelper::getModelInstance($modelName); //if the model doesn't exist at all, redirect to 404 if (!$model) { return Response::error('404'); } //if the model does exist, check if this user has permission to access it if (!ModelHelper::checkPermission($modelName)) { Redirect::to_route('admin_dashboard'); } });
/** * Takes a model menu name and returns a ModelConfig instance if it can be found, or false otherwise * * @param string|int $modelName //the model config/uri name * * @return false|ModelConfig object */ public static function get($modelName) { //first we need to find the model's config (if it exists) if (!($config = static::find($modelName))) { return false; } //now that we have the config, we can begin to check if all of the required fields are provided //but first we have to check if the user has permission to access this model $permission = array_get($config, 'permission'); if (is_callable($permission) && !$permission()) { return false; } //if the title or single names are provided, throw an exception if (!is_string(array_get($config, 'title')) || !is_string(array_get($config, 'single'))) { throw new Exception("Administrator: " . __('administrator::administrator.valid_title')); } //get an instance of the model $modelName = array_get($config, 'model'); if (!is_string($modelName)) { throw new Exception("Administrator: " . __('administrator::administrator.valid_model')); } //grab an instance of the Eloquent model $config['model'] = ModelHelper::getModelInstance($modelName); //check if the required columns array was provided if (!is_array(array_get($config, 'columns'))) { throw new Exception("Administrator: " . __('administrator::administrator.valid_columns')); } //check if the edit fields array was provided if (!is_array(array_get($config, 'edit_fields'))) { throw new Exception("Administrator: " . __('administrator::administrator.valid_edit')); } //now we can instantiate the object return new static($config); }
/** * Gets a list of related items based on a string search param called 'term' * * @param string $modelName * @param string $field //the relationship field/method name * * @return array of objects [{id: string} ... {1: 'name'}, ...] */ public function action_search_relation($modelName, $field, $type) { //try to get the object $model = ModelHelper::getModel($modelName); //get the search term $term = Input::get('term', ''); $selectedItems = Input::get('selectedItems', false); //return the rows return Response::json(ModelHelper::getRelationshipSuggestions($model, $field, $type, $selectedItems, $term)); }
/** * POST method for handling custom actions on the settings page * * @param SettingsConfig $config * * @return JSON */ public function action_settings_custom_action($config) { $model = ModelHelper::getModel($config, $id, false, true); $actionName = Input::get('action_name', false); //get the action and perform the custom action $action = Action::getByName($config, $actionName); $result = $action->perform($model); //if the result is a string, return that as an error. if (is_string($result)) { return Response::json(array('success' => false, 'error' => $result)); } else { if (!$result) { return Response::json(array('success' => false, 'error' => $action->messages['error'])); } else { return Response::json(array('success' => true)); } } }
/** * Gets a list of related items given constraints * * @param ModelConfig $config * * @return array of objects [{id: string} ... {1: 'name'}, ...] */ public function action_update_options($config) { //get the constraints, the search term, and the currently-selected items $constraints = Input::get('constraints', array()); $term = Input::get('term', ''); $type = Input::get('type', false); $field = Input::get('field', false); $selectedItems = Input::get('selectedItems', false); //return the rows return Response::json(ModelHelper::updateRelationshipOptions($config->model, $field, $type, $constraints, $selectedItems, $term)); }