public function download($model, $id) { if ($model == 'CrudFile') { $attachObj = CrudFile::findOrFail($id); } else { $attachObj = CrudModel::createInstance($model, null, $id); } if (file_exists($attachObj->attachGetPath())) { $fp = fopen($attachObj->attachGetPath(), 'rb'); if (strpos($attachObj->mime_type, 'image') !== false) { header('Content-Type: ' . $attachObj->mime_type); while (!feof($fp)) { echo fread($fp, 1024); } } else { if ($fp) { header('Cache-Control: no-cache, must-revalidate'); header('Pragma: no-cache'); //keeps ie happy header('Content-Disposition: attachment; filename= ' . $attachObj->file_name); header('Content-Type: ' . $attachObj->mime_type); header('Content-Length: ' . (string) filesize($attachObj->path)); header('Content-Transfer-Encoding: binary'); while (!feof($fp)) { echo fread($fp, 1024); } } } fclose($fp); } exit; //return \Response::download($attachObj->path); }
public function getOptions() { if (is_null($this->value)) { return []; } $class = CrudModel::resolveClass($this->config['model']); $obj = new $class(); $coll = $obj->find($this->getValueAsArray()); return $this->flatOptions($coll, $obj); }
public function pushToModel() { $ids = []; $class = CrudModel::resolveClass($this->config['model']); $dummyModel = new $class(); if (!empty($this->value)) { foreach ($this->value as $title) { $obj = $class::firstOrCreate([$dummyModel->confParam('title_field') => trim($title)]); $ids[] = $obj->getKey(); } } $this->model->setAttribute($this->name, $ids); }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * * @return mixed */ public function handle($request, Closure $next) { $modelName = $request->route()->parameter('model'); if (!empty($modelName)) { $modelInst = CrudModel::createInstance($modelName); if (!$modelInst->checkAcl()) { if ($request->ajax() || $request->wantsJson()) { return response('Unauthorized.', 401); } else { return redirect()->guest('login'); } } } return $next($request); }
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 crudPrefUI($type) { if ($this->app['request']->isMethod('post')) { $data = $this->app['request']->all(); $scope = $this->crudPrefGetScope($data); $pref = $this->crudPrefGet($data['pref_type'], $scope); $pref['user_id'] = $this->id; $pref['scope'] = $scope; $pref['type_id'] = $data['pref_type']; $pref['pref'] = json_encode($data['pref']); $this->crudPrefSave($pref); return ['success' => true]; } else { $obj = CrudModel::createInstance($this->app['request']->get('model'), $this->app['request']->get('scope')); // $model = 'App\Model\\' . studly_case(\Request :: get('model')); // $obj = new $model(); // $obj->config->setScope(\Request :: get('scope')); return $this->app['view']->make('crud::crud.choose_columns', ['crudObj' => $obj, 'pref_type' => $type]); } }
public function handle() { $models = Container::getInstance()['config']->get('geo.models'); foreach ($models as $model) { $class = CrudModel::resolveClass($model); $list = $class::geoUncoded()->take(100)->get(); $this->log("Geocode process started for " . $list->count() . " " . $model, "geocode"); foreach ($list as $obj) { try { $obj->geoFetchCoordinates(); if ($obj->geo_coded == 1) { $this->log($obj->full_text_address . " coded with [" . $obj->lng . ", " . $obj->lat . "]", "geocode"); } else { $this->log($obj->classViewName . "#" . $obj->id . " geocode failed", "geocode"); } } catch (\ErrorException $e) { $this->log($obj->classViewName . "#" . $obj->id . " geocode failed with error: " . $e->getMessage(), "geocode"); } } $this->log("Geocode process finished", "geocode"); } }
public function getOptions() { $class = CrudModel::resolveClass($this->config['model']); $modelObj = new $class(); if (!empty($this->config['find']) && !empty($this->config['model'])) { $method = $method = 'selectOptions' . studly_case($this->config['find']); $val = $this->getValue(); if (!is_array($val)) { if ($val instanceof Collection) { $val = $val->toArray(); } elseif (is_scalar($val)) { $val = [$val]; } } return $modelObj->{$method}($this->getName(), $val); } if (!empty($this->config['model'])) { return CrudModelCollectionBuilder::createTree($modelObj)->fetch(); } elseif (!empty($this->config['find']) && empty($this->config['model'])) { $method = $method = 'selectOptions' . studly_case($this->config['find']); return $this->model->{$method}($this->getName()); } }
public function configureModel(CrudModel $model, array $config) { $model->setDates($config['field']); return $config; }
private function getModelOptions() { $class = CrudModel::resolveClass($this->config['model']); $modelObj = CrudModel::createInstance($this->config['model'], null, is_numeric($this->value) ? $this->value : null); //$modelObj = new $class(); if (!empty($this->config['find'])) { $method = 'selectOptions' . studly_case($this->config['find']); return $this->formatOptionsArray($modelObj->{$method}($this->model)); } else { if ($modelObj->confParam('tree')) { $collection = CrudModelCollectionBuilder::create($modelObj)->fetch(); } else { $query = $class::query(); if (!empty($this->config['sort'])) { foreach ($this->config['sort'] as $c => $d) { $query->orderBy($c, $d); } } $collection = $query->get(); } } if ($this->isGrouped()) { $options = $this->groupedOptions($collection); } else { $options = $this->flatOptions($collection, $modelObj); } return $options; }
/** * Record Model files. */ protected function recordModels() { //record main model (ONLY ONCE) if (!file_exists($this->path . '/' . $this->config_data['name'] . '.php')) { @mkdir($this->path); $stub = file_get_contents(__DIR__ . '/../views/stubs/model.stub'); $stub = str_replace('[NAMESPACE]', $this->namespace, $stub); $stub = str_replace('[MODEL]', $this->config_data['name'], $stub); file_put_contents($this->path . '/' . $this->config_data['name'] . '.php', $stub); } else { if (count($this->useTraits)) { $insertTraits = []; $model = CrudModel::createInstance($this->config_data['name']); $reflection = new \ReflectionClass($model); $traits = $reflection->getTraitNames(); foreach ($this->useTraits as $trait) { $trait = ltrim($this->traits[$trait], '\\'); if (!in_array($trait, $traits)) { $insertTraits[] = ' use \\' . $trait . ';'; } } if (count($insertTraits)) { $fileCts = file_get_contents($this->path . '/' . $this->config_data['name'] . '.php'); if (preg_match('#.*(class.+' . $this->config_data['name'] . ".*\\{)#siUm", $fileCts, $matches) && !empty($matches[1])) { $fileCts = str_replace($matches[1], $matches[1] . "\n" . implode("\n", $insertTraits), $fileCts); file_put_contents($this->path . '/' . $this->config_data['name'] . '.php', $fileCts); } else { $this->errors[] = 'Unable to record traits.<br>Probably the model file is corrupt.<br>Please check the model file and add the following traits manually:<br><br>' . implode('<br>', $insertTraits); } } } } }
public function getWizardConfig($table, $include_common = true) { $modelConfig = $this->getModelConfig($table, false, false); $obj = CrudModel::createInstance($modelConfig['name']); $config = ['model_config' => ['table_columns' => array_combine($this->getTableColumns($table), $this->getTableColumns($table)), 'table_int_columns' => array_combine($this->getIntTableColumns($table), $this->getIntTableColumns($table)), 'find_methods' => $this->getAvailableSelectOptionsProviders($modelConfig['name']), 'attrs' => array_merge($this->getTableColumns($table), $this->getModelAccessors($modelConfig['name'])), 'formatters' => $obj->getAvailFormatters(), 'scopes' => $obj->getAvailScopes()], 'model' => $modelConfig]; if ($include_common) { $config['common_config'] = ['track_history_options' => $this->getTrackHistoryOptions(), 'acls' => $this->app['config']['acl.acls'], 'relation_options' => array_combine($this->getRelations(), $this->getRelations()), 'all_models' => array_combine($this->getAvailableModels('snake_case'), $this->getAvailableModels('snake_case')), 'on_delete_actions' => $this->getOndeleteActions(), 'pivot_tables' => $this->getPossiblePivotTables(), 'field_options' => $this->getAvailableFieldTypes(), 'fields_config' => $this->getFieldsConfigDefaults(), 'editor_types' => $this->getAvailableEditors(), 'date_formats' => $this->getAvailableDateFormats(), 'date_time_formats' => $this->getAvailableDateTimeFormats(), 'tree_lists' => $this->getAvailableTreeLists()]; } return json_encode($config, JSON_NUMERIC_CHECK); }
public function crudSearchOptions($model) { $obj = CrudModel::createInstance($model, CrudModel::DEFAULT_SCOPE, $this->request->get('id')); $fObj = $obj->getForm()->getFieldByName($this->request->get('field')); return $fObj->getSearchOptions($this->request->get('query')); }