public function google() { $google = new GoogleAPI(); $states = Location::states; $donePresident = false; foreach ($states as $state_abbrev => $state_name) { echo "state: " . $state_name . "\n"; $req = $google->address($state_name)->then(function ($data) use($donePresident) { foreach ($data->reps as $rep) { if ($rep->office == 'President' && !$donePresident) { unset($rep->state); $rep->save(); $donePresident = true; } else { if (Representative::exists($rep)) { $old = Representative::find($rep); foreach ($rep->getAttributes() as $k => $v) { if (!empty($v)) { $old->{$k} = $v; } } $old->save(); } else { if ($rep->office == 'Governor') { $rep->save(); } } } } }); $req->wait(); } }
/** * convert api data to contact my reps data format * @param object $data google response data * @return object contact my reps api response */ public function validate($data) { $response = (object) ['reps' => [], 'divisions' => []]; if (isset($data->normalizedInput)) { $l = $data->normalizedInput; $city = ucwords($l->city); $response->location = (object) array('city' => $city, 'state' => $l->state, 'zip' => $l->zip); if (!empty($city) && !empty($l->zip)) { $location = Location::where('zip', intval($l->zip))->first(); if (null !== $location) { $location->city = $city; $location->save(); } } } if (!isset($data->divisions)) { return $response; } $response->divisions = array_keys((array) $data->divisions); if (!isset($data->offices)) { return $response; } foreach ($data->offices as $office) { if (!Representative::isValidOffice($office->name)) { continue; } $divisionId = $office->divisionId; $l = divisions_split([$divisionId]); foreach ($office->officialIndices as $i) { $d = $data->officials[$i]; $d->office = $office->name; $d->division = $divisionId; if (isset($l['state'])) { $d->state = $l['state']; } $rep = Representative::find($d); // if (is_null($rep)) $rep = new Representative((array) $d); if (null === $rep) { continue; } //for now no new reps if (in_array('google', $rep->sources)) { $response->reps[] = $rep; continue; } if (empty($rep->name) && isset($d->name)) { $rep->name = $d->name; } if (empty($rep->party) && isset($d->party)) { $rep->party = $d->party; } if (empty($rep->photo) && isset($d->photoUrl)) { $rep->photo = $d->photoUrl; } if (empty($rep->address) && isset($d->address)) { $new = []; $a = $d->address; if (is_array($a)) { $a = $a[0]; } if (!empty($a->line1)) { $new[] = ucwords($a->line1); } if (!empty($a->line2)) { $new[] = ucwords($a->line2); } if (!empty($a->line3)) { $new[] = ucwords($a->line3); } if (!empty($a->city) && !empty($a->state) && !empty($a->zip)) { $new[] = $a->city . ', ' . $a->state . ' ' . $a->zip; } $rep->address = $new; } if (isset($d->phones) && count($d->phones) > 0) { $phones = $rep->phones ?? []; foreach ($d->phones as &$p) { $p = str_replace(['(', ') '], ['', '-'], $p); if (!in_array($p, $phones)) { array_push($phones, $p); } } $rep->phones = $phones; if (!isset($rep->phone)) { $rep->phone = $phones[0]; } } if (isset($d->urls) && count($d->urls) > 0) { $urls = $rep->urls ?? []; foreach ($d->urls as $u) { if (!in_array($u, $urls)) { array_push($urls, $u); } } $rep->urls = $urls; if (!isset($rep->website)) { $rep->website = $urls[0]; } } if (isset($d->emails) && count($d->emails) > 0) { $emails = $rep->emails ?? []; foreach ($d->emails as $e) { if (!in_array($e, $emails)) { array_push($emails, $e); } } $rep->emails = $emails; if (!isset($rep->email)) { $rep->email = $emails[0]; } } if (isset($d->channels)) { foreach ($d->channels as $c) { $key = strtolower($c->type) . '_id'; if (!isset($rep->{$key})) { $rep->{$key} = $c->id; } } } $rep->addSource('google'); $rep->save(); $response->reps[] = $rep; } } return $response; }
/** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $representante = Representative::find($id); return view('representative.edit', compact('representante')); }