/** * * Mass assign a value to many records at once, similar to update, but loops through all of them * * @param $pid * @param $fid * @param Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ public function massAssignRecords($pid, $fid, Request $request) { if (!$this->checkPermissions($fid, 'modify')) { return redirect()->back(); } $flid = $request->input("field_selection"); if (!is_numeric($flid)) { flash()->overlay("That is not a valid field"); return redirect()->back(); } if ($request->has($flid)) { $form_field_value = $request->input($flid); //Note this only works when there is one form element being submitted, so if you have more, check Date } else { flash()->overlay("You didn't provide a value to assign to the records", "Whoops."); return redirect()->back(); } if ($request->has("overwrite")) { $overwrite = $request->input("overwrite"); //Overwrite field in all records, even if it has data } else { $overwrite = 0; } $field = Field::find($flid); foreach (Form::find($fid)->records()->get() as $record) { if ($field->type == "Text") { $matching_record_fields = $record->textfields()->where("flid", '=', $flid)->get(); $record->updated_at = Carbon::now(); $record->save(); if ($matching_record_fields->count() > 0) { $textfield = $matching_record_fields->first(); if ($overwrite == true || $textfield->text == "" || is_null($textfield->text)) { $revision = RevisionController::storeRevision($record->rid, 'edit'); $textfield->text = $form_field_value; $textfield->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } else { continue; } } else { $tf = new TextField(); $revision = RevisionController::storeRevision($record->rid, 'edit'); $tf->flid = $field->flid; $tf->rid = $record->rid; $tf->text = $form_field_value; $tf->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } } elseif ($field->type == "Rich Text") { $matching_record_fields = $record->richtextfields()->where("flid", '=', $flid)->get(); $record->updated_at = Carbon::now(); $record->save(); if ($matching_record_fields->count() > 0) { $richtextfield = $matching_record_fields->first(); if ($overwrite == true || $richtextfield->rawtext == "" || is_null($richtextfield->rawtext)) { $revision = RevisionController::storeRevision($record->rid, 'edit'); $richtextfield->rawtext = $form_field_value; $richtextfield->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } else { continue; } } else { $rtf = new RichTextField(); $revision = RevisionController::storeRevision($record->rid, 'edit'); $rtf->flid = $field->flid; $rtf->rid = $record->rid; $rtf->rawtext = $form_field_value; $rtf->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } } elseif ($field->type == "Number") { $matching_record_fields = $record->numberfields()->where("flid", '=', $flid)->get(); $record->updated_at = Carbon::now(); $record->save(); if ($matching_record_fields->count() > 0) { $numberfield = $matching_record_fields->first(); if ($overwrite == true || $numberfield->number == "" || is_null($numberfield->number)) { $revision = RevisionController::storeRevision($record->rid, 'edit'); $numberfield->number = $form_field_value; $numberfield->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } else { continue; } } else { $nf = new NumberField(); $revision = RevisionController::storeRevision($record->rid, 'edit'); $nf->flid = $field->flid; $nf->rid = $record->rid; $nf->number = $form_field_value; $nf->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } } elseif ($field->type == "List") { $matching_record_fields = $record->listfields()->where("flid", '=', $flid)->get(); $record->updated_at = Carbon::now(); $record->save(); if ($matching_record_fields->count() > 0) { $listfield = $matching_record_fields->first(); if ($overwrite == true || $listfield->option == "" || is_null($listfield->option)) { $revision = RevisionController::storeRevision($record->rid, 'edit'); $listfield->option = $form_field_value; $listfield->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } else { continue; } } else { $lf = new ListField(); $revision = RevisionController::storeRevision($record->rid, 'edit'); $lf->flid = $field->flid; $lf->rid = $record->rid; $lf->option = $form_field_value; $lf->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } } elseif ($field->type == "Multi-Select List") { $matching_record_fields = $record->multiselectlistfields()->where("flid", '=', $flid)->get(); $record->updated_at = Carbon::now(); $record->save(); if ($matching_record_fields->count() > 0) { $multiselectlistfield = $matching_record_fields->first(); if ($overwrite == true || $multiselectlistfield->options == "" || is_null($multiselectlistfield->options)) { $revision = RevisionController::storeRevision($record->rid, 'edit'); $multiselectlistfield->options = implode("[!]", $form_field_value); $multiselectlistfield->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } else { continue; } } else { $mslf = new MultiSelectListField(); $revision = RevisionController::storeRevision($record->rid, 'edit'); $mslf->flid = $field->flid; $mslf->rid = $record->rid; $mslf->options = implode("[!]", $form_field_value); $mslf->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } } elseif ($field->type == "Generated List") { $matching_record_fields = $record->generatedlistfields()->where("flid", '=', $flid)->get(); $record->updated_at = Carbon::now(); $record->save(); if ($matching_record_fields->count() > 0) { $generatedlistfield = $matching_record_fields->first(); if ($overwrite == true || $generatedlistfield->options == "" || is_null($generatedlistfield->options)) { $revision = RevisionController::storeRevision($record->rid, 'edit'); $generatedlistfield->options = implode("[!]", $form_field_value); $generatedlistfield->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } else { continue; } } else { $glf = new GeneratedListField(); $revision = RevisionController::storeRevision($record->rid, 'edit'); $glf->flid = $field->flid; $glf->rid = $record->rid; $glf->options = implode("[!]", $form_field_value); $glf->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } } elseif ($field->type == "Date") { $matching_record_fields = $record->datefields()->where("flid", '=', $flid)->get(); $record->updated_at = Carbon::now(); $record->save(); if ($matching_record_fields->count() > 0) { $datefield = $matching_record_fields->first(); if ($overwrite == true || $datefield->month == "" || is_null($datefield->month)) { $revision = RevisionController::storeRevision($record->rid, 'edit'); $datefield->circa = $request->input('circa_' . $flid, ''); $datefield->month = $request->input('month_' . $flid); $datefield->day = $request->input('day_' . $flid); $datefield->year = $request->input('year_' . $flid); $datefield->era = $request->input('era_' . $flid, 'CE'); $datefield->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } else { continue; } } else { $df = new DateField(); $revision = RevisionController::storeRevision($record->rid, 'edit'); $df->circa = $request->input('circa_' . $flid, ''); $df->month = $request->input('month_' . $flid); $df->day = $request->input('day_' . $flid); $df->year = $request->input('year_' . $flid); $df->era = $request->input('era_' . $flid, 'CE'); $df->rid = $record->rid; $df->flid = $flid; $df->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } } elseif ($field->type == "Schedule") { $matching_record_fields = $record->schedulefields()->where("flid", '=', $flid)->get(); $record->updated_at = Carbon::now(); $record->save(); if ($matching_record_fields->count() > 0) { $schedulefield = $matching_record_fields->first(); if ($overwrite == true || $schedulefield->events == "" || is_null($schedulefield->events)) { $revision = RevisionController::storeRevision($record->rid, 'edit'); $schedulefield->events = implode("[!]", $form_field_value); $schedulefield->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } else { continue; } } else { $sf = new ScheduleField(); $revision = RevisionController::storeRevision($record->rid, 'edit'); $sf->flid = $field->flid; $sf->rid = $record->rid; $sf->events = implode("[!]", $form_field_value); $sf->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } } elseif ($field->type == "Geolocator") { $matching_record_fields = $record->geolocatorfields()->where("flid", '=', $flid)->get(); $record->updated_at = Carbon::now(); $record->save(); if ($matching_record_fields->count() > 0) { $geolocatorfield = $matching_record_fields->first(); if ($overwrite == true || $geolocatorfield->locations == "" || is_null($geolocatorfield->locations)) { $revision = RevisionController::storeRevision($record->rid, 'edit'); $geolocatorfield->locations = implode("[!]", $form_field_value); $geolocatorfield->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } else { continue; } } else { $gf = new GeolocatorField(); $revision = RevisionController::storeRevision($record->rid, 'edit'); $gf->flid = $field->flid; $gf->rid = $record->rid; $gf->locations = implode("[!]", $form_field_value); $gf->save(); $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); } } } flash()->overlay("The records were updated", "Good Job!"); return redirect()->action('RecordController@index', compact('pid', 'fid')); }
public function restoreData(Request $request) { $this->json_file = null; $this->decoded_json = null; $users_exempt_from_lockout = new Collection(); $users_exempt_from_lockout->put(1, 1); //Add another one of these with (userid,userid) to exempt extra users $this->lockUsers($users_exempt_from_lockout); if ($request->session()->has("restore_file_path")) { $filepath = $request->session()->get("restore_file_path"); $request->session()->forget("restore_file_path"); } else { return $this->ajaxResponse(false, "You did not select a valid restore point or upload a valid backup file"); } try { $this->json_file = Storage::get($filepath); } catch (\Exception $e) { $this->ajaxResponse(false, "The backup file couldn't be opened. Make sure it still exists and the permissions are correct."); } try { $this->decoded_json = json_decode($this->json_file); $this->decoded_json->kora3; } catch (\Exception $e) { $this->ajaxResponse(false, "The backup file contains invalid JSON data, it may be corrupt or damaged. Check the file or try another one.\n The restore did not start, so data already in the database was not deleted."); } $backup_data = $this->decoded_json; //Delete all existing data try { foreach (User::all() as $User) { if ($User->id == 1) { //Do not delete the default admin user continue; } else { $User->delete(); } } foreach (Project::all() as $Project) { $Project->delete(); } foreach (Form::all() as $Form) { $Form->delete(); } foreach (Field::all() as $Field) { $Field->delete(); } foreach (Record::all() as $Record) { $Record->delete(); } foreach (Metadata::all() as $Metadata) { $Metadata->delete(); } foreach (Token::all() as $Token) { $Token->delete(); } foreach (Revision::all() as $Revision) { $Revision->delete(); } foreach (DateField::all() as $DateField) { $DateField->delete(); } foreach (FormGroup::all() as $FormGroup) { $FormGroup->delete(); } foreach (GeneratedListField::all() as $GeneratedListField) { $GeneratedListField->delete(); } foreach (ListField::all() as $ListField) { $ListField->delete(); } foreach (MultiSelectListField::all() as $MultiSelectListField) { $MultiSelectListField->delete(); } foreach (NumberField::all() as $NumberField) { $NumberField->delete(); } foreach (ProjectGroup::all() as $ProjectGroup) { $ProjectGroup->delete(); } foreach (RichTextField::all() as $RichTextField) { $RichTextField->delete(); } foreach (ScheduleField::all() as $ScheduleField) { $ScheduleField->delete(); } foreach (TextField::all() as $TextField) { $TextField->delete(); } } catch (\Exception $e) { $this->ajaxResponse(false, "There was a problem when attempting to remove existing information from the\n database, the database user may not have permission to do this or the database may be in use."); } try { //This try-catch is for non-QueryExceptions, like if a table is missing entirely from the JSON data // User foreach ($backup_data->users as $user) { try { $new_user = User::create(array("username" => $user->username, "name" => $user->name, "email" => $user->email, "password" => $user->password, "organization" => $user->organization, "language" => $user->language, "regtoken" => $user->regtoken)); $new_user->id = $user->id; $new_user->admin = $user->admin; $new_user->active = $user->active; $new_user->remember_token = $user->remember_token; $new_user->created_at = $user->created_at; $new_user->updated_at = $user->updated_at; $new_user->locked_out = true; $new_user->save(); } catch (\Exception $e) { $this->ajax_error_list->push($e->getMessage()); } } // Project foreach ($backup_data->projects as $project) { //$new_project = Project::create(array("name" => $project->name, "slug" => $project->slug, "description" => $project->description, "adminGID" => $project->adminGID, "active" => $project->active)); try { $new_project = Project::create(array()); $new_project->name = $project->name; $new_project->slug = $project->slug; $new_project->description = $project->description; $new_project->adminGID = $project->adminGID; $new_project->active = $project->active; $new_project->pid = $project->pid; $new_project->created_at = $project->created_at; $new_project->updated_at = $project->updated_at; $new_project->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // Form foreach ($backup_data->forms as $form) { try { $new_form = Form::create(array("pid" => $form->pid)); $new_form->fid = $form->fid; $new_form->name = $form->name; $new_form->slug = $form->slug; $new_form->description = $form->description; $new_form->adminGID = $form->adminGID; $new_form->layout = $form->layout; $new_form->public_metadata = $form->public_metadata; $new_form->layout = $form->layout; $new_form->adminGID = $form->adminGID; $new_form->created_at = $form->created_at; $new_form->updated_at = $form->updated_at; $new_form->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // Field foreach ($backup_data->fields as $field) { try { $new_field = Field::create(array("pid" => $field->pid, "fid" => $field->fid, "order" => $field->order, "type" => $field->type, "name" => $field->name, "slug" => $field->slug, "desc" => $field->desc, "required" => $field->required, "default" => $field->default, "options" => $field->options)); $new_field->flid = $field->flid; $new_field->default = $field->default; $new_field->options = $field->options; $new_field->created_at = $field->created_at; $new_field->updated_at = $field->updated_at; $new_field->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // FormGroup foreach ($backup_data->formgroups as $formgroup) { try { $new_formgroup = new FormGroup(); $new_formgroup->name = $formgroup->group_data->name; $new_formgroup->fid = $formgroup->group_data->fid; $new_formgroup->create = $formgroup->group_data->create; $new_formgroup->edit = $formgroup->group_data->edit; $new_formgroup->ingest = $formgroup->group_data->ingest; $new_formgroup->delete = $formgroup->group_data->delete; $new_formgroup->modify = $formgroup->group_data->modify; $new_formgroup->destroy = $formgroup->group_data->destroy; $new_formgroup->id = $formgroup->group_data->id; $new_formgroup->created_at = $formgroup->group_data->created_at; $new_formgroup->updated_at = $formgroup->group_data->updated_at; $new_formgroup->save(); foreach ($formgroup->user_data as $user_id) { $new_formgroup->users()->attach($user_id); } } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // ProjectGroup foreach ($backup_data->projectgroups as $projectgroup) { try { $new_projectgroup = new ProjectGroup(); $new_projectgroup->id = $projectgroup->group_data->id; $new_projectgroup->name = $projectgroup->group_data->name; $new_projectgroup->pid = $projectgroup->group_data->pid; $new_projectgroup->create = $projectgroup->group_data->create; $new_projectgroup->edit = $projectgroup->group_data->edit; $new_projectgroup->delete = $projectgroup->group_data->delete; $new_projectgroup->created_at = $projectgroup->group_data->created_at; $new_projectgroup->updated_at = $projectgroup->group_data->updated_at; $new_projectgroup->save(); foreach ($projectgroup->user_data as $user_id) { $new_projectgroup->users()->attach($user_id); } } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // Record foreach ($backup_data->records as $record) { try { $new_record = new Record(array("pid" => $record->pid, "fid" => $record->fid, "owner" => $record->owner, "kid" => $record->kid)); $new_record->rid = $record->rid; $new_record->created_at = $record->created_at; $new_record->updated_at = $record->updated_at; $new_record->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // TextField foreach ($backup_data->textfields as $textfield) { try { $new_textfield = new TextField(array("rid" => $textfield->rid, "flid" => $textfield->flid, "text" => $textfield->text)); $new_textfield->id = $textfield->id; $new_textfield->created_at = $textfield->created_at; $new_textfield->updated_at = $textfield->updated_at; $new_textfield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // RichTextField foreach ($backup_data->richtextfields as $richtextfield) { try { $new_richtextfield = new RichTextField(array("rid" => $richtextfield->rid, "flid" => $richtextfield->flid, "rawtext" => $richtextfield->rawtext)); $new_richtextfield->id = $richtextfield->id; $new_richtextfield->created_at = $richtextfield->created_at; $new_richtextfield->updated_at = $richtextfield->updated_at; $new_richtextfield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // NumberField foreach ($backup_data->numberfields as $numberfield) { try { $new_numberfield = new NumberField(array("rid" => $numberfield->rid, "flid" => $numberfield->flid, "number" => $numberfield->number)); $new_numberfield->id = $numberfield->id; $new_numberfield->created_at = $numberfield->created_at; $new_numberfield->updated_at = $numberfield->updated_at; $new_numberfield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // ListField foreach ($backup_data->listfields as $listfield) { try { $new_listfield = new ListField(array("rid" => $listfield->rid, "flid" => $listfield->flid, "option" => $listfield->option)); $new_listfield->id = $listfield->id; $new_listfield->created_at = $listfield->created_at; $new_listfield->updated_at = $listfield->updated_at; $new_listfield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // GeneratedListField foreach ($backup_data->generatedlistfields as $generatedlistfield) { try { $new_generatedlistfield = new GeneratedListField(array("rid" => $generatedlistfield->rid, "flid" => $generatedlistfield->flid, "options" => $generatedlistfield->options)); $new_generatedlistfield->id = $generatedlistfield->id; $new_generatedlistfield->created_at = $generatedlistfield->created_at; $new_generatedlistfield->updated_at = $generatedlistfield->updated_at; $new_generatedlistfield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // MultiSelectListField foreach ($backup_data->multiselectlistfields as $multiselectlistfield) { try { $new_multiselectlistfield = new MultiSelectListField(array("rid" => $multiselectlistfield->rid, "flid" => $multiselectlistfield->flid, "options" => $multiselectlistfield->options)); $new_multiselectlistfield->id = $multiselectlistfield->id; $new_multiselectlistfield->created_at = $multiselectlistfield->created_at; $new_multiselectlistfield->updated_at = $multiselectlistfield->updated_at; $new_multiselectlistfield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // DateField foreach ($backup_data->datefields as $datefield) { try { $new_datefield = new DateField(); $new_datefield->id = $datefield->id; $new_datefield->rid = $datefield->rid; $new_datefield->flid = $datefield->flid; $new_datefield->circa = $datefield->circa; $new_datefield->month = $datefield->month; $new_datefield->day = $datefield->day; $new_datefield->year = $datefield->year; $new_datefield->era = $datefield->era; $new_datefield->created_at = $datefield->created_at; $new_datefield->updated_at = $datefield->updated_at; $new_datefield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // ScheduleField foreach ($backup_data->schedulefields as $schedulefield) { try { $new_schedulefield = new ScheduleField(array("rid" => $schedulefield->rid, "flid" => $schedulefield->flid, "events" => $schedulefield->events)); $new_schedulefield->id = $schedulefield->id; $new_schedulefield->created_at = $schedulefield->created_at; $new_schedulefield->updated_at = $schedulefield->updated_at; $new_schedulefield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // GeolocatorField foreach ($backup_data->geolocatorfields as $geolocatorfield) { try { $new_geolocatorfield = new GeolocatorField(array("rid" => $geolocatorfield->rid, "flid" => $geolocatorfield->flid, "locations" => $geolocatorfield->locations)); $new_geolocatorfield->id = $geolocatorfield->id; $new_geolocatorfield->created_at = $geolocatorfield->created_at; $new_geolocatorfield->updated_at = $new_geolocatorfield->updated_at; $new_geolocatorfield->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // Token foreach ($backup_data->tokens as $token) { try { $new_token = new Token(array('token' => $token->token, 'type' => $token->type)); $new_token->id = $token->id; $new_token->created_at = $token->created_at; $new_token->updated_at = $token->updated_at; $new_token->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // Metadata foreach ($backup_data->metadatas as $metadata) { try { $new_metadata = new Metadata(array()); $new_metadata->flid = $metadata->flid; $new_metadata->pid = $metadata->pid; $new_metadata->fid = $metadata->fid; $new_metadata->name = $metadata->name; $new_metadata->created_at = $metadata->created_at; $new_metadata->updated_at = $metadata->updated_at; $new_metadata->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } // Revision foreach ($backup_data->revisions as $revision) { try { $new_revision = new Revision(array('id' => $revision->id, 'fid' => $revision->fid, 'rid' => $revision->rid, 'userId' => $revision->userId, 'type' => $revision->type, 'data' => $revision->data, 'oldData' => $revision->oldData, 'rollback' => $revision->rollback)); $new_revision->created_at = $revision->created_at; $new_revision->updated_at = $revision->updated_at; $new_revision->save(); } catch (QueryException $e) { $this->ajax_error_list->push($e->getMessage()); } } } catch (\Exception $e) { $this->ajax_error_list->push($e->getMessage()); $this->ajaxResponse(false, "An unknown error prevented the restore from completing.\n You can try restoring from a different backup file or restore point.\n Users will stay locked out until you run a successful restore or manually unlock them above.\n For this error, it's not recommended that you unlock users unless you have resolved the problem"); } if (count($this->ajax_error_list) != 0) { $this->ajaxResponse(false, "Not all of your data was restored, check the errors below for details.\n The errors are in order that they occurred, if you can resolve the first error, it will often correct\n one or more of the errors below it.\n Users will stay locked out until you run a successful restore or manually unlock them above."); } else { $this->unlockUsers(); $this->ajaxResponse(true, "The restore completed successfully."); } }