Ejemplo n.º 1
0
 /**
  * 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');
 }