/** * Actualiza los campos del formulario * @param $fieldsConfig */ public function updateFormFields($fieldsConfig) { $savedFieldsIds = []; foreach ($fieldsConfig as $fieldConfig) { if (array_get($fieldConfig, 'id', null)) { $field = FormField::find(array_get($fieldConfig, 'id', null)); } else { $field = new FormField(); } $field->form_id = $this->id; $field->name = array_get($fieldConfig, 'name', ''); $field->alias = array_get($fieldConfig, 'alias', ''); $field->type = array_get($fieldConfig, 'type.type', ''); $field->config = array_get($fieldConfig, 'config', null); $field->save(); $savedFieldsIds[] = $field->id; } $this->fields()->whereNotIn('id', $savedFieldsIds)->delete(); }
/** * @param Request $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function getDownloadResults(Request $request, $formId) { /** @var Form $form */ $form = Form::find($formId); $data['headers'] = FormField::where(['form_id' => $formId])->get(); $data['results'] = $form->resultsForUser(Auth::user()); $data['tzoffset'] = intval($request->get('tzoffset', 0)) * -1; Excel::create('resultados', function ($excel) use($data) { $excel->getDefaultStyle()->getAlignment()->setWrapText(true); $excel->sheet('Hoja 1', function (LaravelExcelWorksheet $sheet) use($data) { $contents = []; foreach ($data['results'] as $key => $result) { $row = []; foreach ($data['headers'] as $header) { if ($header->type == 'hidden' && object_get($header->config, 'dataType') == 'json') { $row[$header->name] = json_encode(object_get($result->results, $header->alias), JSON_UNESCAPED_UNICODE); } else { $row[$header->name] = $this->remove_emoji(object_get($result->results, $header->alias)); } if (gettype($row[$header->name]) === 'array') { $row[$header->name] = implode(',', $row[$header->name]); } if (substr($row[$header->name], 0, 1) === '=') { $row[$header->name] = ' ' . $row[$header->name]; } } $row['Fecha'] = $result->created_at->addMinutes($data['tzoffset']); $contents[] = $row; } $sheet->fromArray($contents); }); })->export('xls'); }