/** * Funtion to check news recipients * * @access public * @param \Syscover\Forms\Models\Record $record * @param \Syscover\Forms\Models\Form $form * @return void */ public static function checkRecipients($record, $form) { $forwards = $form->getForwards; $newRecipients = []; $oldRecipients = []; $recipients = Recipient::where('record_id_406', $record->id_403)->where('forward_406', true)->get(); // found new recipients foreach ($forwards as $forward) { $matchRecipient = false; foreach ($recipients as $recipient) { if ($forward->email_402 == $recipient->email_406) { $matchRecipient = true; break; } } if (!$matchRecipient) { $newRecipients[] = ['record_id_406' => $record->id_403, 'forward_406' => true, 'name_406' => $forward->name_402, 'email_406' => $forward->email_402, 'comments_406' => $forward->comments_402, 'states_406' => $forward->states_402]; } } // found recipients to delete foreach ($recipients as $recipient) { $matchForward = false; foreach ($forwards as $forward) { if ($forward->email_402 == $recipient->email_406) { $matchForward = true; break; } } if (!$matchForward) { $oldRecipients[] = $recipient->id_406; } } if (count($newRecipients) > 0) { Recipient::insert($newRecipients); } if (count($oldRecipients) > 0) { Recipient::whereIn('id_406', $oldRecipients)->delete(); } }
/** * Function to record a data form * * @access public * @return json | \Illuminate\Http\RedirectResponse */ public function recordForm() { $fields = json_decode($this->request->input('_fields')); $form = Form::find(Crypt::decrypt($this->request->input('_tokenForm'))); $forwards = $form->getForwards; $recipients = []; $names = []; $messages = []; $recordDate = date('U'); // test that, there are any form if ($form == null) { $response = ['success' => false, 'message' => "Form don't exist"]; return response()->json($response); } $data = []; foreach ($fields->data as $field) { $obj = ['type' => $field->type, 'name' => $field->name, 'value' => $this->request->input($field->name)]; if (isset($field->length)) { $obj['length'] = $field->length; } if (isset($field->label)) { $obj['label'] = $field->label; } $data[] = $obj; } $defaultState = Preference::getValue('formsDefaultState', 4); $dataRecord = ['form_id_403' => $form->id_401, 'date_403' => $recordDate, 'date_text_403' => date(config('pulsar.datePattern'), $recordDate), 'state_id_403' => $defaultState->value_018, 'subject_403' => $this->request->input($fields->subject, null), 'name_403' => $this->request->input($fields->name, null), 'surname_403' => $this->request->input($fields->surname, null), 'company_403' => $this->request->input($fields->company, null), 'email_403' => $this->request->input($fields->email, null), 'data_403' => json_encode($data)]; $record = Record::create($dataRecord); $state = $record->getState; // set data with array with decode information to make $dataRecord for message $dataRecord['data_403'] = $data; // set ID record $dataRecord['id_403'] = $record->id_403; // set records unopened $form->n_unopened_401 = Record::where('form_id_403', $form->id_401)->where('opened_403', false)->count(); $form->save(); foreach ($forwards as $forward) { // set recipients from forwards to sow in the email message $names[] = $forward->name_402; $recipients[] = ['record_id_406' => $record->id_403, 'forward_406' => true, 'name_406' => $forward->name_402, 'email_406' => $forward->email_402, 'comments_406' => $forward->comments_402, 'states_406' => $forward->states_402]; } if (count($recipients) > 0) { Recipient::insert($recipients); } // get recipient emails to compare with new user email $recipients = $record->getRecipients; foreach ($recipients as $recipient) { // get user and permissions $user = User::where('email_010', $recipient->email_406)->first(); if ($user != null) { $userAcl = AclLibrary::getProfileAcl($user->profile_id_010); } $messages[] = ['type_id_405' => 'record', 'record_id_405' => $record->id_403, 'date_405' => date('U'), 'recipient_id_405' => $recipient->id_406, 'forward_405' => true, 'subject_405' => 'forms::pulsar.subject_email_record', 'name_405' => $recipient->name_406, 'email_405' => $recipient->email_406, 'form_id_405' => $form->id_401, 'user_id_405' => $user == null ? null : $user->id_010, 'template_405' => 'forms::emails.record', 'text_template_405' => 'forms::emails.text_record', 'data_message_405' => json_encode(['name_form_405' => $form->name_401, 'name_state_405' => $state->name_400, 'color_state_405' => $state->color_400, 'names_405' => implode(", ", $names), 'permission_state_405' => $user == null ? false : $userAcl->allows('forms-record', 'edit', $user->profile_id_010), 'permission_comment_405' => $user == null ? false : $userAcl->allows('forms-comment', 'create', $user->profile_id_010), 'permission_forward_405' => $user == null ? false : $userAcl->allows('forms-form', 'edit', $user->profile_id_010), 'permission_record_405' => $user == null ? false : $userAcl->allows('forms-record', 'show', $user->profile_id_010)]), 'data_405' => json_encode($dataRecord)]; } if (count($messages) > 0) { Message::insert($messages); } if ($this->request->input('_redirectOk') == '') { $response = ['success' => true, 'form' => ['date_403' => $recordDate, 'date_text_403' => date(config('pulsar.datePattern'), $recordDate), 'subject_403' => $this->request->input($fields->subject, null), 'state_id_403' => $defaultState->value_018, 'name_403' => $this->request->input($fields->name, null), 'surname_403' => $this->request->input($fields->surname, null), 'company_403' => $this->request->input($fields->company, null), 'email_403' => $this->request->input($fields->email, null), 'data_403' => json_encode($data)]]; return response()->json($response); } else { return redirect($this->request->input('_redirectOk')); } }