add table viewing page

This commit is contained in:
2018-07-21 23:57:31 +02:00
parent 26488e5883
commit e01c63cfa2
11 changed files with 223 additions and 10 deletions
+24 -4
View File
@@ -12,6 +12,20 @@ use MightyPork\Exceptions\NotApplicableException;
class TableController extends Controller
{
public function view(User $user, string $table)
{
/** @var Table $tableModel */
$tableModel = $user->tables()->where('name', $table)->first();
$revision = $tableModel->activeRevision;
return view('table.view', [
'table' => $tableModel,
'revision' => $revision,
'columns' => Column::columnsFromJson(json_decode($revision->columns)),
'rows' => $revision->rows,
]);
}
/**
* SHow a form for creating a new table
*
@@ -20,8 +34,8 @@ class TableController extends Controller
public function create()
{
$exampleColumns =
"latin,string,Latin Name\n".
"common,string,Common Name\n".
"latin,string,Latin Name\n" .
"common,string,Common Name\n" .
"lifespan,int,Lifespan (years)";
$exampleData =
@@ -82,11 +96,12 @@ class TableController extends Controller
$rowTable = array_map('str_getcsv', explode("\n", $request->get('data')));
// Preparing data to insert into the Rows table
$rowsData = null;
try {
$rowsData = array_map(function ($row) use ($columns) {
if (count($row) != count($columns)) {
throw new NotApplicableException("All rows must have ".count($columns)." fields.");
throw new NotApplicableException("All rows must have " . count($columns) . " fields.");
}
$parsed = [];
foreach ($row as $i => $val) {
@@ -98,7 +113,7 @@ class TableController extends Controller
'refs' => 1,
];
}, $rowTable);
}catch (\Exception $e) {
} catch (\Exception $e) {
return $this->backWithErrors(['columns' => $e->getMessage()]);
}
@@ -118,6 +133,11 @@ class TableController extends Controller
'origin' => $request->get('origin'),
]);
// Attach the revision to the table, set as current
$table->revisions()->attach($revision);
$table->activeRevision()->associate($revision);
// Spawn the rows, linked to the revision
$revision->rows()->createMany($rowsData);
// Now we create rows, a revision pointing to them, and the table using it.
+1 -1
View File
@@ -79,7 +79,7 @@ class Table extends Model
/** Active revision */
public function activeRevision()
{
return $this->hasOne(Revision::class, 'revision_id');
return $this->belongsTo(Revision::class, 'revision_id');
}
/** Proposals submitted to this table */
+8
View File
@@ -2,6 +2,7 @@
namespace App\Providers;
use App\Models\User;
use App\View\WidgetFactory;
use Illuminate\Support\ServiceProvider;
@@ -17,6 +18,13 @@ class AppServiceProvider extends ServiceProvider
if($this->app->environment('production')) {
\URL::forceScheme('https');
}
\Route::bind('user', function ($value) {
$u = User::where('name', $value)->first();
// it may also be the _id directly
if (!$u) $u = User::find($value);
return $u;
});
}
/**
+7
View File
@@ -25,6 +25,13 @@ class Column implements JsonSerializable
private $title;
private $type;
public static function columnsFromJson($columns)
{
return array_map(function ($x) {
return new Column($x);
}, $columns);
}
public function __get($name)
{
if (property_exists($this, $name)) {
+15 -1
View File
@@ -53,18 +53,32 @@ class Widget
$this->$method = $arg;
}
else {
$this->attributesArray[$method] = $arg;
$this->attributesArray[kebab_case($method)] = $arg;
}
return $this;
}
/** Explicitly set a CSS rule */
public function css($prop, $val)
{
$this->styleArray[$prop] = $val;
return $this;
}
/** Explicitly set an attribute */
public function attr($attr, $val)
{
$this->attributesArray[$attr] = $val;
return $this;
}
/** Configure a text field for automatic alias generation */
public function autoAlias($targetName, $delimiter='_')
{
return $this->attr('data-autoalias', $targetName)->attr('data-aa-delimiter', $delimiter);
}
/** Apply a given layout (bootstrap grid, 12 cols total) */
public function layout($labelCols, $fieldCols)
{