![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/dceprojects.corals.io/Corals/modules/ProjectPlan/Import/ |
<?php namespace Corals\Modules\ProjectPlan\Import; use Corals\Modules\ProjectPlan\Http\Requests\ContactRequest; use Corals\Modules\ProjectPlan\Models\Contact; use Corals\Modules\ProjectPlan\Services\ContactService; use Corals\Modules\Utility\Models\Comment\Comment; class ContactImporter implements \Corals\Modules\ProjectPlan\Contracts\ModelImportContract { public function getValidationRules(): array { return [ 'name' => 'required|max:191', 'phone' => 'nullable|required_without:email', 'email' => 'nullable|email|required_without:phone', 'notes' => 'nullable', ]; } public function getData($record): array { $phone = data_get($record, 'phone',''); if ($phone){ $phone = getFormattedPhoneNumber($phone); } return [ 'name' => data_get($record, 'name'), 'phone' => $phone, 'email' => data_get($record, 'email'), 'notes' => explode('|', data_get($record, 'notes')), ]; } public function saveData($data): void { $phone = $data['phone']; $email = $data['email']; $contactModel = Contact::query() ->when($phone && !$email,function ($queryP) use ($phone){ $queryP->where('phone', $phone); }) ->when($email && !$phone ,function ($queryE) use ($email){ $queryE->where('email', $email); }) ->when($email && $phone ,function ($queryE) use ($email,$phone){ $queryE->where('email', $email)->orWhere('phone',$phone); }) ->first(); $userId = $data['userId']; unset($data['userId']); $contactRequest = new ContactRequest(); $contactRequest->replace($data); $contactService = new ContactService(); if ($contactModel) { $contact = $contactService->update($contactRequest, $contactModel); } else { $contact = $contactService->store($contactRequest, Contact::class); } $notes = $data['notes']; $comments = []; foreach ($notes as $note) { $comments[] = new Comment(['body' => $note, 'author_type' => 'User', 'author_id' => $userId]); } $contact->comments()->saveMany($comments); } }