Exemple #1
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $tables = config('center.tables');
     $joining_tables = [];
     //dd($tables);
     foreach ($tables as $table) {
         //create if doesn't exist, every table gets an id
         if (!Schema::hasTable($table->name)) {
             Schema::create($table->name, function ($t) {
                 $t->increments('id');
             });
         }
         foreach ($table->fields as $field) {
             if ($field->type == 'checkboxes') {
                 //create linking table
                 if (!Schema::hasTable($field->name)) {
                     Schema::create($field->name, function ($t) {
                         $t->increments('id');
                     });
                 }
                 $column = RowController::formatKeyColumn($table->name);
                 if (!Schema::hasColumn($field->name, $column)) {
                     Schema::table($field->name, function ($t) use($column) {
                         $t->integer($column);
                     });
                 }
                 $column = RowController::formatKeyColumn($field->source);
                 if (!Schema::hasColumn($field->name, $column)) {
                     Schema::table($field->name, function ($t) use($column) {
                         $t->integer($column);
                     });
                 }
             } else {
                 //create column
                 Schema::table($table->name, function ($t) use($table, $field) {
                     //set type
                     switch ($field->type) {
                         //boolean
                         case 'checkbox':
                             eval('$t->boolean($field->name)' . ($field->required ? '' : '->nullable()') . (!Schema::hasColumn($table->name, $field->name) ? '' : '->change()') . '; ');
                             break;
                             //strings
                         //strings
                         case 'address':
                         case 'color':
                         case 'country':
                         case 'email':
                         case 'password':
                         case 'phone':
                         case 'slug':
                         case 'stripe_charge':
                         case 'stripe_customer':
                         case 'string':
                         case 'url':
                         case 'us_state':
                         case 'zip':
                             if (!isset($field->maxlength)) {
                                 $field->maxlength = 255;
                             }
                             eval('$t->string($field->name, $field->maxlength)' . ($field->required ? '' : '->nullable()') . (!Schema::hasColumn($table->name, $field->name) ? '' : '->change()') . '; ');
                             break;
                             //date
                         //date
                         case 'date':
                             eval('$t->date($field->name)' . ($field->required ? '' : '->nullable()') . (!Schema::hasColumn($table->name, $field->name) ? '' : '->change()') . '; ');
                             break;
                             //datetime
                         //datetime
                         case 'datetime':
                             eval('$t->datetime($field->name)' . ($field->required ? '' : '->nullable()') . (!Schema::hasColumn($table->name, $field->name) ? '' : '->change()') . '; ');
                             break;
                             //time
                         //time
                         case 'time':
                             eval('$t->time($field->name)' . ($field->required ? '' : '->nullable()') . (!Schema::hasColumn($table->name, $field->name) ? '' : '->change()') . '; ');
                             break;
                             //text
                         //text
                         case 'html':
                         case 'text':
                             eval('$t->text($field->name)' . ($field->required ? '' : '->nullable()') . (!Schema::hasColumn($table->name, $field->name) ? '' : '->change()') . '; ');
                             break;
                             //foreign key
                         //foreign key
                         case 'image':
                         case 'integer':
                         case 'select':
                         case 'user':
                             eval('$t->integer($field->name)' . ($field->required ? '' : '->nullable()') . (!Schema::hasColumn($table->name, $field->name) ? '' : '->change()') . '; ');
                             break;
                             //decimal
                         //decimal
                         case 'money':
                             eval('$t->decimal($field->name, 5, 2)' . ($field->required ? '' : '->nullable()') . (!Schema::hasColumn($table->name, $field->name) ? '' : '->change()') . '; ');
                             break;
                             //latitude
                         //latitude
                         case 'latitude':
                             eval('$t->decimal($field->name, 11, 8)' . ($field->required ? '' : '->nullable()') . (!Schema::hasColumn($table->name, $field->name) ? '' : '->change()') . '; ');
                             break;
                             //latitude
                         //latitude
                         case 'longitude':
                             eval('$t->decimal($field->name, 11, 8)' . ($field->required ? '' : '->nullable()') . (!Schema::hasColumn($table->name, $field->name) ? '' : '->change()') . '; ');
                             break;
                             //special: permissions
                         //special: permissions
                         case 'permissions':
                             break;
                         default:
                             trigger_error($field->type . ' not supported yet!');
                     }
                     //remove unused columns?
                     if ($table->keep_clean) {
                         $columns = Schema::getColumnListing($table->name);
                         $fields = array_keys((array) $table->fields);
                         $columns = array_diff($columns, $fields, ['id']);
                         foreach ($columns as $column) {
                             $t->dropColumn($column);
                         }
                     }
                 });
             }
         }
     }
     //now can set permissions, had to wait for permissions table potentially to be created
     foreach ($tables as $table) {
         //set default permissions
         if (!$table->hidden) {
             LoginController::setDefaultTablePermissions($table->name);
         }
     }
     $this->comment(PHP_EOL . trans('center::site.refresh_success') . PHP_EOL);
 }
    private function models()
    {
        $relationships = $dates = [];
        $tables = config('center.tables');
        //loop through once to create relationships between tables
        foreach ($tables as $table) {
            $dates[$table->name] = [];
            if (!isset($relationships[$table->name])) {
                $relationships[$table->name] = [];
            }
            foreach ($table->fields as $field) {
                //define relationships
                if ($field->type == 'checkboxes') {
                    //out from this object
                    $order_by = [];
                    foreach ($tables[$field->source]->order_by as $column => $direction) {
                        $order_by[] = '->orderBy("' . $column . '", "' . $direction . '")';
                    }
                    $relationships[$table->name][] = '
					public function ' . $tables[$field->source]->name . '() {
						return $this->belongsToMany("LeftRight\\Center\\Models\\' . $tables[$field->source]->model . '", "' . $field->name . '", "' . RowController::formatKeyColumn($table->name) . '", "' . RowController::formatKeyColumn($tables[$field->source]->name) . '")' . implode($order_by) . ';
					}
					';
                    //back from the related object
                    $order_by = [];
                    foreach ($table->order_by as $column => $direction) {
                        $order_by[] = '->orderBy("' . $column . '", "' . $direction . '")';
                    }
                    $relationships[$tables[$field->source]->name][] = '
					public function ' . $table->name . '() {
						return $this->belongsToMany("LeftRight\\Center\\Models\\' . $table->model . '", "' . $field->name . '", "' . RowController::formatKeyColumn($tables[$field->source]->name) . '", "' . RowController::formatKeyColumn($table->name) . '")' . implode($order_by) . ';
					}
					';
                } elseif (in_array($field->type, ['date', 'datetime'])) {
                    $dates[$table->name][] = '\'' . $field->name . '\'';
                } elseif ($field->type == 'image') {
                    //cannot overwrite property
                    $relationships[$table->name][] = '
					public function ' . substr($field->name, 0, -3) . '() {
						return $this->hasOne("LeftRight\\Center\\Models\\File", "id", "' . $field->name . '");
					}
					';
                } elseif (in_array($field->type, ['select', 'user'])) {
                    //public function ' . $tables[$field->source]->name . '() {
                    //out from this object
                    $relationships[$table->name][] = '
					public function ' . substr($field->name, 0, -3) . '() {
						return $this->belongsTo("LeftRight\\Center\\Models\\' . $tables[$field->source]->model . '", "' . $field->name . '");
					}
					';
                    //back from the related object
                    $relationships[$tables[$field->source]->name][] = '
					public function ' . $table->name . '() {
						return $this->hasMany("LeftRight\\Center\\Models\\' . $table->model . '", "' . $field->name . '");
					}
					';
                }
            }
        }
        //debug
        //dd($relationships);
        //now we must loop through again; the first loop set relationships on other tables
        foreach ($tables as $table) {
            eval('namespace LeftRight\\Center\\Models;
			use Illuminate\\Database\\Eloquent\\SoftDeletes;
			use Auth;
			use DateTime;
			use DB;
			use Eloquent;

			class ' . $table->model . ' extends Eloquent {
			    ' . (isset($table->fields->deleted_at) ? 'use SoftDeletes;' : '') . '
				public $table      = \'' . $table->name . '\'; //public intentionally
				public $timestamps = false; //going to override if present
				protected $guarded = [];
				protected $dates   = [' . implode(',', $dates[$table->name]) . '];

				public static function boot() {
					parent::boot();
			        static::creating(function($object) {' . (isset($table->fields->precedence) ? '
						$object->precedence = DB::table(\'' . $table->name . '\')->max(\'precedence\') + 1;
						' : '') . (isset($table->fields->created_by) ? '
						$object->created_by = Auth::id();
						' : '') . ($table->timestamps && isset($table->fields->created_at) ? '
						$object->created_at = new DateTime();
						' : '') . (isset($table->fields->updated_by) ? '
						$object->updated_by = Auth::id();
						' : '') . ($table->timestamps && isset($table->fields->updated_at) ? '
						$object->updated_at = new DateTime();
						' : '') . '});
			        static::updating(function($object) {' . (isset($table->fields->updated_by) ? '
						$object->updated_by = Auth::id();
						' : '') . ($table->timestamps && isset($table->fields->updated_at) ? '
						$object->updated_at = new DateTime();
						' : '') . '});
				}

				public function creator() {
					return $this->belongsTo(\'User\', \'created_by\');
				}

				public function updater() {
					return $this->belongsTo(\'User\', \'updated_by\');
				}

				' . implode(' ', $relationships[$table->name]) . '
			}');
        }
    }