static function addRoutes($app, $authenticateForRole) { $app->map("/element-visibility/get-key/", $authenticateForRole('public'), function () use($app) { FieldController::getVisibilityKey($app); })->via('GET', 'POST'); $app->map("/element-visibility/init/", $authenticateForRole('admin'), function () use($app) { FieldController::initVisibilityElement($app); })->via('GET', 'POST'); $app->group('/field', $authenticateForRole('admin'), function () use($app) { /* * id */ $app->map("/get/:fieldId/", function ($fieldId) use($app) { FieldController::getField($app, $fieldId); })->via('GET', 'POST'); /* * identifier, type, desc */ $app->post("/insert/", function () use($app) { FieldController::addField($app); }); /* * id, identifier, type, desc */ $app->post("/update/:fieldId/", function ($fieldId) use($app) { FieldController::saveField($app, $fieldId); }); /* * id */ $app->post("/initialize/:fieldId/", function ($fieldId) use($app) { FieldController::initializeField($app, $fieldId); }); /* * id */ $app->map("/delete/:fieldId/", function ($fieldId) use($app) { FieldController::deleteField($app, $fieldId); })->via('DELETE', 'POST'); /* * roleId, fieldId */ $app->post("/unassign-role/", function () use($app) { FieldController::unassignRole($app); }); /* * roleId, fieldId */ $app->post("/assign-role/", function () use($app) { FieldController::assignRole($app); }); }); }
Route::get('/schema/save', 'ObjectController@saveSchema'); Route::get('/schema/load', 'ObjectController@loadSchema'); Route::get('/image/test', 'FileController@test'); Route::get('/slug/test', function () { $phrases = ['', 'and', 'this is a normal test', 'this is a really really really long test because it\'s amazing and great and am i at 50 YET???']; foreach ($phrases as $phrase) { echo '<p>' . $phrase . ' becomes <em>' . Slug::make($phrase, ['', 'normal-test', 'normal-test-1']) . '</em></p>'; } }); Route::get('/slug/object/{object_id}', function ($object_id) { $object = DB::table(DB_OBJECTS)->find($object_id); Slug::setForObject($object); die('object was ' . $object->name); }); Route::get('cleanup', function () { FieldController::cleanup(); FileController::findOrphans(); FileController::cleanup(); }); # Complex instance routing, optionally with linked_id for related objects Route::get('/{object_name}/delete/{instance_id}', 'InstanceController@delete'); Route::get('/{object_name}', 'InstanceController@index'); Route::get('/{object_name}/export', 'InstanceController@export'); Route::get('/{object_name}/create/{linked_id?}', 'InstanceController@create'); Route::post('/{object_name}/reorder', 'InstanceController@reorder'); Route::post('/{object_name}/{linked_id?}', 'InstanceController@store'); Route::get('/{object_name}/{instance_id}/{linked_id?}', 'InstanceController@edit'); Route::put('/{object_name}/{instance_id}/{linked_id?}', 'InstanceController@update'); Route::delete('/{object_name}/{instance_id}', 'InstanceController@destroy'); }); }
<?php /** * Created by PhpStorm. * User: Haziq * Date: 12/6/2015 * Time: 11:09 PM */ require_once $_SERVER['DOCUMENT_ROOT'] . '/matrimonialweb/Controller/FieldController.php'; $Object = new FieldController(); $result = $Object->getWorkingList(); echo json_encode($result);
public function edit($object_name, $instance_id, $linked_id = false) { # Get object / field / whatever infoz $object = DB::table(DB_OBJECTS)->where('name', $object_name)->first(); $fields = DB::table(DB_FIELDS)->where('object_id', $object->id)->where('visibility', '<>', 'hidden')->orderBy('precedence')->get(); $instance = DB::table($object->name)->where('id', $instance_id)->first(); # Add return var to the queue if ($linked_id) { $return_to = action('InstanceController@edit', [self::getRelatedObjectName($object), $linked_id]); } elseif (URL::previous()) { $return_to = URL::previous(); } else { $return_to = action('InstanceController@index', $object->name); } //format instance values for form foreach ($fields as &$field) { if ($field->type == 'datetime') { if (!empty($instance->{$field->name})) { $instance->{$field->name} = date('m/d/Y h:i A', strtotime($instance->{$field->name})); } } elseif ($field->type == 'checkboxes' || $field->type == 'select') { //load options for checkboxes or selects $related_object = self::getRelatedObject($field->related_object_id); $field->options = DB::table($related_object->name)->orderBy($related_object->order_by, $related_object->direction)->lists($related_object->field->name, 'id'); //indent nested selects if ($field->type == 'select' && !empty($related_object->group_by_field)) { $grouped_field = DB::table(DB_FIELDS)->where('id', $related_object->group_by_field)->first(); if ($grouped_field->object_id == $grouped_field->related_object_id) { $field->options = $parents = array(); $options = DB::table($related_object->name)->orderBy($related_object->order_by, $related_object->direction)->get(); foreach ($options as $option) { if (!empty($option->{$grouped_field->name})) { //calculate indent if (in_array($option->{$grouped_field->name}, $parents)) { $parents = array_slice($parents, 0, array_search($option->{$grouped_field->name}, $parents) + 1); } else { $parents[] = $option->{$grouped_field->name}; } $option->{$related_object->field->name} = str_repeat(' ', count($parents)) . $option->{$related_object->field->name}; } elseif (count($parents)) { $parents = array(); } $field->options[$option->id] = $option->{$related_object->field->name}; } } } //select might be nullable if ($field->type == 'select' && !$field->required) { $field->options = ['' => ''] + $field->options; } //get checkbox values todo make a function for consistently getting these checkbox column names if ($field->type == 'checkboxes') { $table_key = Str::singular($object->name) . '_id'; $foreign_key = Str::singular($related_object->name) . '_id'; $instance->{$field->name} = DB::table($field->name)->where($table_key, $instance->id)->lists($foreign_key); } } elseif ($field->type == 'image') { $instance->{$field->name} = DB::table(DB_FILES)->where('id', $instance->{$field->name})->first(); if (!empty($instance->{$field->name}->width) && !empty($instance->{$field->name}->height)) { $field->width = $instance->{$field->name}->width; $field->height = $instance->{$field->name}->height; } list($field->screen_width, $field->screen_height) = FileController::getImageDimensions($field->width, $field->height); } elseif ($field->type == 'images') { $instance->{$field->name} = DB::table(DB_FILES)->where('field_id', $field->id)->where('instance_id', $instance->id)->orderBy('precedence', 'asc')->get(); foreach ($instance->{$field->name} as &$image) { if (!empty($image->width) && !empty($image->height)) { $image->screen_width = $image->width; $image->screen_width = $image->height; } } list($field->screen_width, $field->screen_height) = FileController::getImageDimensions($field->width, $field->height); } elseif ($field->type == 'slug') { if ($field->required && empty($instance->{$field->name}) && $field->related_field_id) { //slugify related field to populate this one foreach ($fields as $related_field) { if ($related_field->id == $field->related_field_id) { $instance->{$field->name} = Str::slug($instance->{$related_field->name}); } } } } elseif ($field->type == 'user') { $field->options = DB::table(DB_USERS)->orderBy('name')->lists('name', 'id'); if (!$field->required) { $field->options = ['' => ''] + $field->options; } } elseif ($field->type == 'us_state') { $field->options = FieldController::usStates(); if (!$field->required) { $field->options = ['' => ''] + $field->options; } } } # Get linked objects $links = DB::table(DB_OBJECT_LINKS)->where('object_id', $object->id)->join(DB_OBJECTS, DB_OBJECT_LINKS . '.linked_id', '=', DB_OBJECTS . '.id')->lists(DB_OBJECTS . '.name'); foreach ($links as &$link) { $link = self::index($link, $instance_id, $linked_id); } return View::make('avalon::instances.edit', compact('object', 'fields', 'instance', 'links', 'linked_id', 'return_to')); }
<?php /** * Created by PhpStorm. * User: Haziq * Date: 12/6/2015 * Time: 9:57 PM */ require_once $_SERVER['DOCUMENT_ROOT'] . '/matrimonialweb/Controller/FieldController.php'; $Object = new FieldController(); $result = $Object->getAllFields(); echo json_encode($result);
public static function loadSchema() { //run this first, because any orphaned fields will cause it to squawk FieldController::cleanup(); //load schema from file and prepare $schema = json_decode(file_get_contents(storage_path() . '/avalon.schema.json')); //load current database into $objects and $fields variables $objects = $fields = []; $db_fields = DB::table(DB_FIELDS)->join(DB_OBJECTS, DB_OBJECTS . '.id', '=', DB_FIELDS . '.object_id')->select(DB_FIELDS . '.object_id', DB_FIELDS . '.id AS field_id', DB_OBJECTS . '.name AS table', DB_FIELDS . '.name AS column')->get(); foreach ($db_fields as $field) { if (!array_key_exists($field->object_id, $objects)) { $objects[$field->object_id] = $field->table; } $fields[$field->field_id] = ['table' => $field->table, 'column' => $field->column]; } //loop through new object schema and update foreach ($schema->objects as $object) { $values = ['id' => $object->id, 'title' => $object->title, 'name' => $object->name, 'model' => $object->model, 'order_by' => $object->order_by, 'direction' => $object->direction, 'group_by_field' => $object->group_by_field, 'list_help' => $object->list_help, 'form_help' => $object->form_help, 'list_grouping' => $object->list_grouping, 'can_create' => $object->can_create, 'can_edit' => $object->can_edit, 'can_see' => $object->can_see, 'url' => $object->url, 'singleton' => $object->singleton]; if (array_key_exists($object->id, $objects)) { DB::table(DB_OBJECTS)->where('id', $object->id)->update($values); } else { DB::table(DB_OBJECTS)->insert($values); self::addTable($object->name); } if (isset($objects[$object->id])) { unset($objects[$object->id]); } } foreach ($objects as $id => $table) { DB::table(DB_OBJECTS)->where('id', $id)->delete(); DB::table(DB_FIELDS)->where('object_id', $id)->delete(); Schema::dropIfExists($table); } foreach ($schema->fields as $field) { $values = ['id' => $field->id, 'object_id' => $field->object_id, 'type' => $field->type, 'title' => $field->title, 'name' => $field->name, 'visibility' => $field->visibility, 'required' => $field->required, 'related_field_id' => $field->related_field_id, 'related_object_id' => $field->related_object_id, 'width' => $field->width, 'height' => $field->height, 'help' => $field->help, 'updated_at' => $field->updated_at, 'updated_by' => $field->updated_by, 'precedence' => $field->precedence]; if ($field->id == 62) { dd($field); } if (array_key_exists($field->id, $fields)) { DB::table(DB_FIELDS)->where('id', $field->id)->update($values); } else { DB::table(DB_FIELDS)->insert($values); if ($field->type == 'checkboxes') { FieldController::addJoiningTable($fields[$field->id]['table'], $field->related_object_id); } else { FieldController::addColumn($fields[$field->id]['table'], $field->name, $field->type, $field->required); } } if (isset($fields[$field->id])) { unset($fields[$field->id]); } } foreach ($fields as $id => $props) { extract($props); DB::table(DB_FIELDS)->where('id', $id)->delete(); Schema::dropIfExists($table, $column); } }