public static function setForObject($object) { $slug_source = Slug::source($object->id); $instances = DB::table($object->name)->get(); $slugs = []; foreach ($instances as $instance) { if ($slug_source === null) { $slug = Slug::make($instance->created_at->format('Y-m-d'), $slugs); } else { $slug = Slug::make($instance->{$slug_source}, $slugs); } DB::table($object->name)->where('id', $instance->id)->update(['slug' => $slug]); $slugs[] = $slug; } }
public function store($object_name, $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')->orderBy('precedence')->get(); //metadata $inserts = array('created_at' => new DateTime(), 'updated_at' => new DateTime(), 'created_by' => Auth::user()->id, 'updated_by' => Auth::user()->id, 'precedence' => DB::table($object->name)->max('precedence') + 1); //run various cleanup processes on the fields foreach ($fields as $field) { if (!in_array($field->type, array('checkboxes', 'images'))) { $inserts[$field->name] = self::sanitize($field); } } //determine where slug is coming from if ($slug_source = Slug::source($object->id)) { $slug_source = Input::get($slug_source); } else { $slug_source = date('Y-m-d'); } //get other values to check uniqueness $uniques = DB::table($object->name)->lists('slug'); //add unique, formatted slug to the insert batch $inserts['slug'] = Slug::make($slug_source, $uniques); //run insert $instance_id = DB::table($object->name)->insertGetId($inserts); //handle any checkboxes, had to wait for instance_id foreach ($fields as $field) { if ($field->type == 'checkboxes') { //figure out schema, loop through and save all the checkboxes $object_column = self::getKey($object->name); $remote_column = self::getKey($field->related_object_id); 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 == 'image') { DB::table(DB_FILES)->where('id', Input::get($field->name))->update(array('instance_id' => $instance_id)); } elseif ($field->type == 'images') { $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)); } } } //update objects table with latest counts DB::table(DB_OBJECTS)->where('id', $object->id)->update(array('count' => DB::table($object->name)->whereNull('deleted_at')->count(), 'updated_at' => new DateTime(), 'updated_by' => Auth::user()->id)); //clean up any abandoned files FileController::cleanup(); //return to target return Redirect::to(Input::get('return_to')); }