/**
  * Create a new user instance after a valid registration.
  *
  * @param  array $data
  * @param $authenticated_user
  * @return CRUDResultData
  */
 protected function create(array $data, $authenticated_user)
 {
     $creation_data = new CRUDResultData();
     $validator = $this->validator($data);
     Log::info(print_r($data, true));
     if (!$validator->fails() && isset($authenticated_user) && isset($authenticated_user->id)) {
         $contact = Contact::create(['first_name' => $data['first_name'], 'last_name' => $data['last_name'], 'user_id' => $authenticated_user->id]);
         if ($contact->exists) {
             if (array_key_exists('added_email', $data)) {
                 foreach ($data['added_email'] as $key => $email) {
                     ContactEmail::create(['contact_id' => $contact->id, 'email' => $email, 'primary' => array_key_exists($key, $data['primary_email'])]);
                 }
             }
             ContactNumber::create(['contact_id' => $contact->id, 'phone_number' => $data['phone_number']]);
             if (array_key_exists('additional_number', $data)) {
                 foreach ($data['additional_number'] as $phone_number) {
                     ContactNumber::create(['contact_id' => $contact->id, 'phone_number' => $phone_number]);
                 }
             }
         } else {
             $creation_data->success = false;
             $creation_data->errors = $validator->errors()->all();
         }
         $creation_data->success = true;
     } else {
         $creation_data->success = false;
         $creation_data->errors = $validator->errors()->all();
     }
     return $creation_data;
 }
Beispiel #2
0
 protected function insertFromExcel($worksheet, $authenticated_user)
 {
     foreach ($worksheet as $row) {
         $data = array();
         $data['first_name'] = $row[0];
         $data['last_name'] = $row[1];
         $data['email'] = $row[2];
         $data['primary'] = $row[3] == 'true';
         $data['phone_number'] = $row[4];
         $contact = Contact::where(['first_name' => $data['first_name'], 'last_name' => $data['last_name'], 'user_id' => $authenticated_user->id]);
         if ($contact->exists()) {
             $contact = $contact->first();
         } else {
             $contact = Contact::create(['first_name' => $data['first_name'], 'last_name' => $data['last_name'], 'user_id' => $authenticated_user->id]);
         }
         if (isset($data['email']) && trim($data['email']) !== '') {
             ContactEmail::create(['contact_id' => $contact->id, 'email' => $data['email'], 'primary' => $data['primary']]);
         }
         if (isset($data['phone_number']) && trim($data['phone_number']) !== '') {
             ContactNumber::create(['contact_id' => $contact->id, 'phone_number' => $data['phone_number']]);
         }
     }
 }