/** * Handle the posted data when we reorder items in the datatable * @param int $ctrl_class_id The ID of the Ctrl Class of the objects we're reordering * @return Response */ public function reorder_objects(Request $request, $ctrl_class_id) { $ctrl_class = CtrlClass::where('id', $ctrl_class_id)->firstOrFail(); $class = $ctrl_class->get_class(); if ($new_order = $request->input('new_order')) { foreach ($new_order as $order) { $object = $class::where('id', $order['id'])->firstOrFail(); $object->order = $order['order']; $object->save(); } $response = 'Items reordered'; $status = 200; } /* No, this might just mean we didn't actually change the order: if (empty($response)) { $response = 'An error has occurred'; $status = 400; } */ if (!empty($response)) { $json = ['response' => $response]; return \Response::json($json, $status); } }
/** * Import objects from a CSV file * @param string $action There are various things that this function can do; count rows, check headers, import data and so on. * @param collection $results The results of the CSV import, as returned by Maatwebsite\Excel * @param int $ctrlclass_id The ID of the CtrlClass we're importing * @param string $filter_string Any filters we've applied to the list before importing (not currently used) * @return various Cn be a boolean (for success/failure), an integer (for a row count), or a string (for a description of a result). Depends on context. */ protected function import_objects($action, $results, $ctrl_class_id, $filter_string = NULL, $csv_file) { // Argos has a good example of this function in use $ctrl_class = CtrlClass::where('id', $ctrl_class_id)->firstOrFail(); // Configure requred_headers, define the callback function switch ($ctrl_class->name) { case '[CLASS_NAME]': $required_headers = ['HEADER_1', 'HEADER_3', 'HEADER_3']; $pre_import_function = function ($ctrl_class_id, $filter_string, $csv_file) { // For example, we might want to truncate the table here, or even bypass the Excel:: import altogether (as we do for Argos) }; $callback_function = function ($results) { $count = 0; foreach ($results as $result) { $count++; } return $count; }; break; default: return false; // Can't import this class } if ($action == 'get-headers') { return $headers; } else { if ($action == 'get-callback-function') { return $callback_function; } else { if ($action == 'get-pre-import-function') { return !empty($pre_import_function) ? $pre_import_function : false; } else { dd("Unrecognised action {$action}"); } } } }
/** * Generate model files based on the ctrl_tables * * @return Response */ public function generate_model_files() { $model_folder = 'Ctrl/Models/'; if (!File::exists(app_path($model_folder))) { File::makeDirectory(app_path($model_folder), 0777, true); // See http://laravel-recipes.com/recipes/147/creating-a-directory } else { // Otherwise, empty the folder: File::cleanDirectory(app_path($model_folder)); } $ctrl_classes = \Sevenpointsix\Ctrl\Models\CtrlClass::get(); foreach ($ctrl_classes as $ctrl_class) { $view_data = ['model_name' => $ctrl_class->name, 'soft_deletes' => false, 'table_name' => $ctrl_class->table_name, 'fillable' => [], 'belongsTo' => [], 'hasMany' => [], 'belongsToMany' => []]; // NOTE: this may need to include properties that we set using a filter in the URL // ie, if we want to add a course to a client, but "client" isn't directly visible in the form; // instead, we get to the list of courses by clicking the filtered_list "courses" when listing clients. $fillable_properties = $ctrl_class->ctrl_properties()->where('fieldset', '!=', '')->where(function ($query) { $query->whereNull('relationship_type')->orWhere('relationship_type', 'belongsTo'); })->get(); // We can only fill relationships if they're belongsTo (ie, have a specific local key, such as one_id) // OR if they're belongsToMany, in which case we have a pivot table (I think?) foreach ($fillable_properties as $fillable_property) { $view_data['fillable'][] = $fillable_property->get_field_name(); // Does Laravel/Eloquent give us a quick way of extracting all ->name properties into an array? // I think it does. } // Which properties can be automatically filled via a filtered list? ie, clicking to add a related page to a pagesection, should set the pagesection variable. // This is a bit complex as we have to look at properties of other classes, linking to this class... $filtered_list_properties = \Sevenpointsix\Ctrl\Models\CtrlProperty::whereRaw('(find_in_set(?, flags))', ['filtered_list'])->where('related_to_id', $ctrl_class->id)->get(); if (!$filtered_list_properties->isEmpty()) { foreach ($filtered_list_properties as $filtered_list_property) { $default_properties = $ctrl_class->ctrl_properties()->where('relationship_type', 'belongsTo')->where('related_to_id', $filtered_list_property->ctrl_class_id)->get(); if (!$default_properties->isEmpty()) { foreach ($default_properties as $default_property) { $view_data['fillable'][] = $default_property->get_field_name(); } } } } $relationship_properties = $ctrl_class->ctrl_properties()->whereNotNull('related_to_id')->get(); foreach ($relationship_properties as $relationship_property) { $related_ctrl_class = \Sevenpointsix\Ctrl\Models\CtrlClass::find($relationship_property->related_to_id); $relationship_data = ['name' => $relationship_property->name, 'model' => $related_ctrl_class->name, 'foreign_key' => $relationship_property->foreign_key, 'local_key' => $relationship_property->local_key]; if ($relationship_property->relationship_type == 'belongsToMany') { $relationship_data['pivot_table'] = $relationship_property->pivot_table; } $view_data[$relationship_property->relationship_type][] = $relationship_data; } $model_code = View::make('ctrl::model_template', $view_data)->render(); $model_path = app_path($model_folder . $ctrl_class->name . '.php'); File::put($model_path, $model_code); } $this->info($ctrl_classes->count() . ' files generated'); }