Exemplo n.º 1
0
 public function run()
 {
     //i contatti su cui ciclaer
     $contacts = \App\Contacts::all();
     foreach ($contacts as $contact) {
         //qnt randomica di numeri di telefono da associare al contatto
         $number_qnt = mt_rand(1, 3);
         //ciclo sulla quantita
         for ($j = 0; $j <= $number_qnt; $j++) {
             //numero casuale
             $telefono = substr(number_format(time() * mt_rand(), 0, '', ''), 0, 10);
             \App\Numbers::create(array('number' => $telefono, 'contact_id' => $contact->id));
         }
     }
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update(Request $request, $id)
 {
     //validation rules
     $this->validate($request, array('name' => 'required', 'surname' => 'required', 'address' => 'required'));
     /* @var array $params */
     $params = Input::get();
     //phone numbers from request
     $formNumbers = $params['number'];
     /*
      * get contact model and associated phone numbers
      * @var Illuminate\Database\Eloquent\Model $contact
      * @var $contactNumbers
      */
     $contact = Contacts::findOrNew($id);
     $contactNumbers = Numbers::whereContactId($id)->get();
     //set values on contact
     $contact->name = $params['name'];
     $contact->surname = $params['surname'];
     $contact->address = $params['address'];
     if ($request->file('image')) {
         $thumb_dir = DESTINATION_PATH . THUMB_PATH;
         $img_dir = DESTINATION_PATH . IMG_PATH;
         //manage filename
         $extension = $request->file('image')->getClientOriginalExtension();
         $fileName = time() . '_' . rand(11111, 99999) . '.' . $extension;
         $image = $request->file('image');
         //resize images for thumbnails
         $thumb = \Intervention\Image\Facades\Image::make($image)->fit(120, 120)->save($thumb_dir . "/" . $fileName);
         //resize images for thumbnails
         $img = \Intervention\Image\Facades\Image::make($image)->fit(280, 320)->save($img_dir . "/" . $fileName);
         //store image on server
         $request->file('image')->move(DESTINATION_PATH, $fileName);
         $contact->photo = $fileName;
     }
     $contact->save();
     /*
      * managing associated phone numbers
      * 1. if numbers from request < of contact numbers, delete the other ones
      * 2. otherwise, foreach request number, check existence in contact, then save it
      */
     if (sizeof($formNumbers) < sizeof($contactNumbers)) {
         foreach ($contactNumbers as $contactNumber) {
             if (!in_array($contactNumber['number'], $formNumbers)) {
                 //delete
                 Numbers::whereId($contactNumber['id'])->delete();
             }
         }
     } else {
         foreach ($formNumbers as $number) {
             if (!in_array($number, $contactNumbers->toArray())) {
                 //new associated phone number
                 $brandNewNumber = Numbers::firstOrCreate(['number' => $number, 'contact_id' => $contact->id]);
                 $brandNewNumber->save();
             }
         }
     }
     return redirect('/')->with(array('contact' => $contact));
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update($id)
 {
     $params = Request::get('_form');
     $photo = "";
     $name = $params['name'];
     $surname = $params['surname'];
     $address = $params['address'];
     $photo = $params['image'];
     $contact = Contacts::find($id);
     $contact->name = $name;
     $contact->surname = $surname;
     $contact->address = $address;
     if ($photo != "") {
         $contact->photo = $photo;
     }
     //creo i numeri telefonici associati
     $numbers = $params['numbers'];
     $contact->numbers()->delete();
     foreach ($numbers as $number) {
         $new_number = Numbers::firstOrCreate(['number' => $number, 'contact_id' => $contact->id]);
         $contact->numbers()->save($new_number);
     }
     $contact->save();
     return response()->json(array('id' => $contact->id, 'name' => $contact->name, 'surname' => $contact->surname, 'photo' => $contact->photo));
 }