Exemple #1
0
            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');
        });
    }
});
Route::filter('user', function () {
 public function update($object_name, $instance_id, $linked_id = false)
 {
     $object = DB::table(DB_OBJECTS)->where('name', $object_name)->first();
     $fields = DB::table(DB_FIELDS)->where('object_id', $object->id)->where('visibility', '<>', 'hidden')->get();
     //metadata
     $updates = array('updated_at' => new DateTime(), 'updated_by' => Auth::user()->id);
     //run loop through the fields
     foreach ($fields as $field) {
         if ($field->type == 'checkboxes') {
             # Figure out schema
             $object_column = self::getKey($object->name);
             $remote_column = self::getKey($field->related_object_id);
             # Clear old values
             DB::table($field->name)->where($object_column, $instance_id)->delete();
             # Loop through and save all the checkboxes
             if (Input::has($field->name)) {
                 foreach (Input::get($field->name) as $related_id) {
                     DB::table($field->name)->insert(array($object_column => $instance_id, $remote_column => $related_id));
                 }
             }
         } elseif ($field->type == 'images') {
             # Unset any old file associations (will get cleaned up after this loop)
             DB::table(DB_FILES)->where('field_id', $field->id)->where('instance_id', $instance_id)->update(array('instance_id' => null));
             # Create new associations
             $file_ids = explode(',', Input::get($field->name));
             $precedence = 0;
             foreach ($file_ids as $file_id) {
                 DB::table(DB_FILES)->where('id', $file_id)->update(array('instance_id' => $instance_id, 'precedence' => ++$precedence));
             }
         } else {
             if ($field->type == 'image') {
                 # Unset any old file associations (will get cleaned up after this loop)
                 DB::table(DB_FILES)->where('field_id', $field->id)->where('instance_id', $instance_id)->update(array('instance_id' => null));
                 # Capture the uploaded file by setting the reverse-lookup
                 DB::table(DB_FILES)->where('id', Input::get($field->name))->update(array('instance_id' => $instance_id));
             }
             $updates[$field->name] = self::sanitize($field);
         }
     }
     //slug
     if (!empty($object->url)) {
         $uniques = DB::table($object->name)->where('id', '<>', $instance_id)->lists('slug');
         $updates['slug'] = Slug::make(Input::get('slug'), $uniques);
     }
     /* //todo manage a redirect table if client demand warrants it
     		$old_slug = DB::table($object->name)->find($instance_id)->pluck('slug');
     		if ($updates['slug'] != $old_slug) {
     		}*/
     //run update
     DB::table($object->name)->where('id', $instance_id)->update($updates);
     //clean up abandoned files
     FileController::cleanup();
     //update object meta
     DB::table(DB_OBJECTS)->where('id', $object->id)->update(['count' => DB::table($object->name)->whereNull('deleted_at')->count(), 'updated_at' => new DateTime(), 'updated_by' => Auth::user()->id]);
     return Redirect::to(Input::get('return_to'));
 }