commit 2840f58c61c906f5ac408e9681cf33f802625f74 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Nov 25 15:52:58 2016 +0000 Add changes commit 86b515a20c65e4956766242db424d84082c7e99e Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Nov 25 15:42:00 2016 +0000 gulp derived assets commit def587e2f3805a0ba669d804b12d83e9f3ec5ea7 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Nov 25 15:41:43 2016 +0000 Include new contact styles commit 8256dc30b0ad23096b3dcde264826fc6cfaa8788 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Fri Nov 25 15:41:19 2016 +0000 Better styled contacts commit f12ce1d6f68857d88ad6f39f8b835d036c793c8a Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 18:11:47 2016 +0000 Sort out views for contacts, better h-card commit 7be5fe82029b20f6cde3ce921f0cb625c27d21d6 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 18:09:00 2016 +0000 quick code tidy commit 71dad7e4918ff4e513715d4ef3a296fa39507ca1 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:58:32 2016 +0000 Get contact editing working commit 0b885190733979be4f5508d28523e8e0b45399a2 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:42:10 2016 +0000 Improve form layout, add facebook entry commit 0a6bd79e384dcd872cb9a89b1232afaf20e729b2 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:41:49 2016 +0000 Add facebook column commit 639d49045c9a213eafd970ceafe288a51cfc95c8 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:30:40 2016 +0000 Update changelog commit f4a018591d50bf9af7e1a64daa9e4a04daa6e1d4 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:27:42 2016 +0000 Reference right view, use a better variable name when parsing a URL commit 583f7d7f7cc577cf31b37bbc2bdcd8865c7b9980 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:26:58 2016 +0000 Move mini-hcard to templates folder, update content commit 2e1b13eff052b65cf2b86796a9509c81d9c86768 Author: Jonny Barnes <jonny@jonnybarnes.uk> Date: Wed Nov 23 16:26:10 2016 +0000 Add facebook column to contacts table
168 lines
5.2 KiB
PHP
168 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Contact;
|
|
use GuzzleHttp\Client;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Filesystem\Filesystem;
|
|
|
|
class ContactsAdminController extends Controller
|
|
{
|
|
/**
|
|
* Display the form to add a new contact.
|
|
*
|
|
* @return \Illuminate\View\Factory view
|
|
*/
|
|
public function newContact()
|
|
{
|
|
return view('admin.newcontact');
|
|
}
|
|
|
|
/**
|
|
* List the currect contacts that can be edited.
|
|
*
|
|
* @return \Illuminate\View\Factory view
|
|
*/
|
|
public function listContacts()
|
|
{
|
|
$contacts = Contact::all();
|
|
|
|
return view('admin.listcontacts', ['contacts' => $contacts]);
|
|
}
|
|
|
|
/**
|
|
* Show the form to edit an existing contact.
|
|
*
|
|
* @param string The contact id
|
|
* @return \Illuminate\View\Factory view
|
|
*/
|
|
public function editContact($contactId)
|
|
{
|
|
$contact = Contact::findOrFail($contactId);
|
|
|
|
return view('admin.editcontact', ['contact' => $contact]);
|
|
}
|
|
|
|
/**
|
|
* Show the form to confirm deleting a contact.
|
|
*
|
|
* @return \Illuminate\View\Factory view
|
|
*/
|
|
public function deleteContact($contactId)
|
|
{
|
|
return view('admin.deletecontact', ['id' => $contactId]);
|
|
}
|
|
|
|
/**
|
|
* Process the request to add a new contact.
|
|
*
|
|
* @param \Illuminate\Http|request $request
|
|
* @return \Illuminate\View\Factory view
|
|
*/
|
|
public function postNewContact(Request $request)
|
|
{
|
|
$contact = new Contact();
|
|
$contact->name = $request->input('name');
|
|
$contact->nick = $request->input('nick');
|
|
$contact->homepage = $request->input('homepage');
|
|
$contact->twitter = $request->input('twitter');
|
|
$contact->facebook = $request->input('facebook');
|
|
$contact->save();
|
|
|
|
return view('admin.newcontactsuccess', ['id' => $contact->id]);
|
|
}
|
|
|
|
/**
|
|
* Process the request to edit a contact.
|
|
*
|
|
* @todo Allow saving profile pictures for people without homepages
|
|
*
|
|
* @param string The contact id
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\View\Factory view
|
|
*/
|
|
public function postEditContact($contactId, Request $request)
|
|
{
|
|
$contact = Contact::findOrFail($contactId);
|
|
$contact->name = $request->input('name');
|
|
$contact->nick = $request->input('nick');
|
|
$contact->homepage = $request->input('homepage');
|
|
$contact->twitter = $request->input('twitter');
|
|
$contact->facebook =$request->input('facebook');
|
|
$contact->save();
|
|
|
|
if ($request->hasFile('avatar')) {
|
|
if ($request->input('homepage') != '') {
|
|
$dir = parse_url($request->input('homepage'))['host'];
|
|
$destination = public_path() . '/assets/profile-images/' . $dir;
|
|
$filesystem = new Filesystem();
|
|
if ($filesystem->isDirectory($destination) === false) {
|
|
$filesystem->makeDirectory($destination);
|
|
}
|
|
$request->file('avatar')->move($destination, 'image');
|
|
}
|
|
}
|
|
|
|
return view('admin.editcontactsuccess');
|
|
}
|
|
|
|
/**
|
|
* Process the request to delete a contact.
|
|
*
|
|
* @param string The contact id
|
|
* @return \Illuminate\View\Factory view
|
|
*/
|
|
public function postDeleteContact($contactId)
|
|
{
|
|
$contact = Contact::findOrFail($contactId);
|
|
$contact->delete();
|
|
|
|
return view('admin.deletecontactsuccess');
|
|
}
|
|
|
|
/**
|
|
* Download the avatar for a contact.
|
|
*
|
|
* This method attempts to find the microformat marked-up profile image
|
|
* from a given homepage and save it accordingly
|
|
*
|
|
* @param string The contact id
|
|
* @return \Illuminate\View\Factory view
|
|
*/
|
|
public function getAvatar($contactId)
|
|
{
|
|
$contact = Contact::findOrFail($contactId);
|
|
$homepage = $contact->homepage;
|
|
if (($homepage !== null) && ($homepage !== '')) {
|
|
$client = new Client();
|
|
try {
|
|
$response = $client->get($homepage);
|
|
$html = (string) $response->getBody();
|
|
$mf2 = \Mf2\parse($html, $homepage);
|
|
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
|
|
return "Bad Response from $homepage";
|
|
}
|
|
$avatarURL = null; // Initialising
|
|
foreach ($mf2['items'] as $microformat) {
|
|
if ($microformat['type'][0] == 'h-card') {
|
|
$avatarURL = $microformat['properties']['photo'][0];
|
|
break;
|
|
}
|
|
}
|
|
try {
|
|
$avatar = $client->get($avatarURL);
|
|
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
|
|
return "Unable to get $avatarURL";
|
|
}
|
|
$directory = public_path() . '/assets/profile-images/' . parse_url($homepage)['host'];
|
|
$filesystem = new Filesystem();
|
|
if ($filesystem->isDirectory($directory) === false) {
|
|
$filesystem->makeDirectory($directory);
|
|
}
|
|
$filesystem->put($directory . '/image', $avatar->getBody());
|
|
|
|
return view('admin.getavatarsuccess', ['homepage' => parse_url($homepage)['host']]);
|
|
}
|
|
}
|
|
}
|