public function getRecordArray(Request $request) { $id = $request->id; $rid = RecordPreset::where('id', '=', $id)->first()->rid; $record = Record::where('rid', '=', $rid)->first(); $form = Form::where('fid', '=', $record->fid)->first(); $field_collect = $form->fields()->get(); $field_array = array(); foreach ($field_collect as $field) { $data = array(); $data['flid'] = $field->flid; $data['type'] = $field->type; if ($field->type == 'Text') { $textfield = TextField::where('rid', '=', $record->rid)->first(); $data['text'] = $textfield->text; $flid_array[] = $field->flid; } elseif ($field->type == 'Rich Text') { $rtfield = RichTextField::where('rid', '=', $record->rid)->first(); $data['rawtext'] = $rtfield->rawtext; $flid_array[] = $field->flid; } elseif ($field->type == 'Number') { $numberfield = NumberField::where('rid', '=', $record->rid)->first(); $data['number'] = $numberfield->number; $flid_array[] = $field->flid; } elseif ($field->type == 'List') { $listfield = ListField::where('rid', '=', $record->rid)->first(); $data['option'] = $listfield->option; $flid_array[] = $field->flid; } elseif ($field->type == 'Multi-Select List') { $mslfield = MultiSelectListField::where('rid', '=', $record->rid)->first(); $data['options'] = explode('[!]', $mslfield->options); $flid_array[] = $field->flid; } elseif ($field->type == 'Generated List') { $gnlfield = GeneratedListField::where('rid', '=', $record->rid)->first(); $data['options'] = explode('[!]', $gnlfield->options); $flid_array[] = $field->flid; } elseif ($field->type == 'Date') { $datefield = DateField::where('rid', '=', $record->rid)->first(); $date_array['circa'] = $datefield->circa; $date_array['era'] = $datefield->era; $date_array['day'] = $datefield->day; $date_array['month'] = $datefield->month; $date_array['year'] = $datefield->year; $data['data'] = $date_array; $flid_array[] = $field->flid; } elseif ($field->type == 'Schedule') { $schedfield = ScheduleField::where('rid', '=', $record->rid)->first(); $data['events'] = explode('[!]', $schedfield->events); $flid_array[] = $field->flid; } $field_array[$field->flid] = $data; } $response['data'] = $field_array; $response['flids'] = $flid_array; return $response; }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($pid, $fid, $rid, Request $request) { if (!FormController::validProjForm($pid, $fid)) { return redirect('projects'); } foreach ($request->all() as $key => $value) { if (!is_numeric($key)) { continue; } $message = FieldValidation::validateField($key, $value, $request); if ($message != '') { flash()->error($message); return redirect()->back()->withInput(); } } $record = Record::where('rid', '=', $rid)->first(); $record->updated_at = Carbon::now(); $record->save(); $revision = RevisionController::storeRevision($record->rid, 'edit'); foreach ($request->all() as $key => $value) { if (!is_numeric($key)) { continue; } $field = FieldController::getField($key); if ($field->type == 'Text') { //we need to check if the field exist first if (TextField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first() != null) { $tf = TextField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first(); $tf->text = $value; $tf->save(); } else { $tf = new TextField(); $tf->flid = $field->flid; $tf->rid = $record->rid; $tf->text = $value; $tf->save(); } } else { if ($field->type == 'Rich Text') { //we need to check if the field exist first if (RichTextField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first() != null) { $rtf = RichTextField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first(); $rtf->rawtext = $value; $rtf->save(); } else { $rtf = new RichTextField(); $rtf->flid = $field->flid; $rtf->rid = $record->rid; $rtf->rawtext = $value; $rtf->save(); } } else { if ($field->type == 'Number') { //we need to check if the field exist first if (NumberField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first() != null) { $nf = NumberField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first(); $nf->number = $value; $nf->save(); } else { $nf = new NumberField(); $nf->flid = $field->flid; $nf->rid = $record->rid; $nf->number = $value; $nf->save(); } } else { if ($field->type == 'List') { //we need to check if the field exist first if (ListField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first() != null) { $lf = ListField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first(); $lf->option = $value; $lf->save(); } else { $lf = new ListField(); $lf->flid = $field->flid; $lf->rid = $record->rid; $lf->option = $value; $lf->save(); } } else { if ($field->type == 'Multi-Select List') { //we need to check if the field exist first if (MultiSelectListField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first() != null) { $mslf = MultiSelectListField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first(); $mslf->options = FieldController::msListArrayToString($value); $mslf->save(); } else { $mslf = new MultiSelectListField(); $mslf->flid = $field->flid; $mslf->rid = $record->rid; $mslf->options = FieldController::msListArrayToString($value); $mslf->save(); } } else { if ($field->type == 'Generated List') { //we need to check if the field exist first if (GeneratedListField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first() != null) { $glf = GeneratedListField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first(); $glf->options = FieldController::msListArrayToString($value); $glf->save(); } else { $glf = new GeneratedListField(); $glf->flid = $field->flid; $glf->rid = $record->rid; $glf->options = FieldController::msListArrayToString($value); $glf->save(); } } else { if ($field->type == 'Date') { //we need to check if the field exist first if (DateField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first() != null) { $df = DateField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first(); $df->circa = $request->input('circa_' . $field->flid, ''); $df->month = $request->input('month_' . $field->flid); $df->day = $request->input('day_' . $field->flid); $df->year = $request->input('year_' . $field->flid); $df->era = $request->input('era_' . $field->flid, 'CE'); $df->save(); } else { $df = new DateField(); $df->flid = $field->flid; $df->rid = $record->rid; $df->circa = $request->input('circa_' . $field->flid, ''); $df->month = $request->input('month_' . $field->flid); $df->day = $request->input('day_' . $field->flid); $df->year = $request->input('year_' . $field->flid); $df->era = $request->input('era_' . $field->flid, 'CE'); $df->save(); } } else { if ($field->type == 'Schedule') { //we need to check if the field exist first if (ScheduleField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first() != null) { $sf = ScheduleField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first(); $sf->events = FieldController::msListArrayToString($value); $sf->save(); } else { $sf = new ScheduleField(); $sf->flid = $field->flid; $sf->rid = $record->rid; $sf->events = FieldController::msListArrayToString($value); $sf->save(); } } else { if ($field->type == 'Geolocator') { //we need to check if the field exist first if (GeolocatorField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first() != null) { $gf = GeolocatorField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first(); $gf->locations = FieldController::msListArrayToString($value); $gf->save(); } else { $gf = new GeolocatorField(); $gf->flid = $field->flid; $gf->rid = $record->rid; $gf->locations = FieldController::msListArrayToString($value); $gf->save(); } } else { if ($field->type == 'Documents' && DocumentsField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first() != null | glob(env('BASE_PATH') . 'storage/app/tmpFiles/' . $value . '/*.*') != false) { //we need to check if the field exist first if (DocumentsField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first() != null) { $df = DocumentsField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first(); } else { $df = new DocumentsField(); $df->flid = $field->flid; $df->rid = $record->rid; $newPath = env('BASE_PATH') . 'storage/app/files/p' . $pid . '/f' . $fid . '/r' . $record->rid . '/fl' . $field->flid; mkdir($newPath, 0775, true); } //clear the old files before moving the update over foreach (new \DirectoryIterator(env('BASE_PATH') . 'storage/app/files/p' . $pid . '/f' . $fid . '/r' . $record->rid . '/fl' . $field->flid) as $file) { if ($file->isFile()) { unlink(env('BASE_PATH') . 'storage/app/files/p' . $pid . '/f' . $fid . '/r' . $record->rid . '/fl' . $field->flid . '/' . $file->getFilename()); } } //build new stuff $infoString = ''; if (file_exists(env('BASE_PATH') . 'storage/app/tmpFiles/' . $value)) { $types = FieldController::getMimeTypes(); foreach (new \DirectoryIterator(env('BASE_PATH') . 'storage/app/tmpFiles/' . $value) as $file) { if ($file->isFile()) { if (!array_key_exists($file->getExtension(), $types)) { $type = 'application/octet-stream'; } else { $type = $types[$file->getExtension()]; } $info = '[Name]' . $file->getFilename() . '[Name][Size]' . $file->getSize() . '[Size][Type]' . $type . '[Type]'; if ($infoString == '') { $infoString = $info; } else { $infoString .= '[!]' . $info; } rename(env('BASE_PATH') . 'storage/app/tmpFiles/' . $value . '/' . $file->getFilename(), env('BASE_PATH') . 'storage/app/files/p' . $pid . '/f' . $fid . '/r' . $record->rid . '/fl' . $field->flid . '/' . $file->getFilename()); } } } $df->documents = $infoString; $df->save(); } else { if ($field->type == 'Gallery' && GalleryField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first() != null | glob(env('BASE_PATH') . 'storage/app/tmpFiles/' . $value . '/*.*') != false) { //we need to check if the field exist first if (GalleryField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first() != null) { $gf = GalleryField::where('rid', '=', $rid)->where('flid', '=', $field->flid)->first(); } else { $gf = new GalleryField(); $gf->flid = $field->flid; $gf->rid = $record->rid; $newPath = env('BASE_PATH') . 'storage/app/files/p' . $pid . '/f' . $fid . '/r' . $record->rid . '/fl' . $field->flid; mkdir($newPath, 0775, true); mkdir($newPath . '/thumbnail', 0775, true); mkdir($newPath . '/medium', 0775, true); } //clear the old files before moving the update over foreach (new \DirectoryIterator(env('BASE_PATH') . 'storage/app/files/p' . $pid . '/f' . $fid . '/r' . $record->rid . '/fl' . $field->flid) as $file) { if ($file->isFile()) { unlink(env('BASE_PATH') . 'storage/app/files/p' . $pid . '/f' . $fid . '/r' . $record->rid . '/fl' . $field->flid . '/' . $file->getFilename()); unlink(env('BASE_PATH') . 'storage/app/files/p' . $pid . '/f' . $fid . '/r' . $record->rid . '/fl' . $field->flid . '/thumbnail/' . $file->getFilename()); unlink(env('BASE_PATH') . 'storage/app/files/p' . $pid . '/f' . $fid . '/r' . $record->rid . '/fl' . $field->flid . '/medium/' . $file->getFilename()); } } //build new stuff $infoString = ''; if (file_exists(env('BASE_PATH') . 'storage/app/tmpFiles/' . $value)) { $types = FieldController::getMimeTypes(); foreach (new \DirectoryIterator(env('BASE_PATH') . 'storage/app/tmpFiles/' . $value) as $file) { if ($file->isFile()) { if (!array_key_exists($file->getExtension(), $types)) { $type = 'application/octet-stream'; } else { $type = $types[$file->getExtension()]; } $info = '[Name]' . $file->getFilename() . '[Name][Size]' . $file->getSize() . '[Size][Type]' . $type . '[Type]'; if ($infoString == '') { $infoString = $info; } else { $infoString .= '[!]' . $info; } rename(env('BASE_PATH') . 'storage/app/tmpFiles/' . $value . '/' . $file->getFilename(), env('BASE_PATH') . 'storage/app/files/p' . $pid . '/f' . $fid . '/r' . $record->rid . '/fl' . $field->flid . '/' . $file->getFilename()); rename(env('BASE_PATH') . 'storage/app/tmpFiles/' . $value . '/thumbnail/' . $file->getFilename(), env('BASE_PATH') . 'storage/app/files/p' . $pid . '/f' . $fid . '/r' . $record->rid . '/fl' . $field->flid . '/thumbnail/' . $file->getFilename()); rename(env('BASE_PATH') . 'storage/app/tmpFiles/' . $value . '/medium/' . $file->getFilename(), env('BASE_PATH') . 'storage/app/files/p' . $pid . '/f' . $fid . '/r' . $record->rid . '/fl' . $field->flid . '/medium/' . $file->getFilename()); } } } $gf->images = $infoString; $gf->save(); } } } } } } } } } } } } $revision->oldData = RevisionController::buildDataArray($record); $revision->save(); flash()->overlay('Your record has been successfully updated!', 'Good Job!'); return redirect('projects/' . $pid . '/forms/' . $fid . '/records/' . $rid); }