Move admin controllers to own namespace, and give better method names

This commit is contained in:
Jonny Barnes 2017-02-18 21:36:22 +00:00
parent 6d4c566944
commit 1c8f696024
7 changed files with 144 additions and 137 deletions

View file

@ -0,0 +1,37 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
/*
|--------------------------------------------------------------------------
| Admin Controller
|--------------------------------------------------------------------------
|
| Here we have the logic for the admin cp
|
*/
/**
* Set variables.
*
* @var string
*/
public function __construct()
{
$this->username = config('admin.user');
}
/**
* Show the main admin CP page.
*
* @return \Illuminate\View\Factory view
*/
public function showWelcome()
{
return view('admin.welcome', ['name' => $this->username]);
}
}

View file

@ -0,0 +1,141 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Article;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ArticlesAdminController extends Controller
{
/**
* List the articles that can be edited.
*
* @return \Illuminate\View\Factory view
*/
public function index()
{
$posts = Article::select('id', 'title', 'published')->orderBy('id', 'desc')->get();
return view('admin.listarticles', ['posts' => $posts]);
}
/**
* Show the new article form.
*
* @return \Illuminate\View\Factory view
*/
public function create()
{
$message = session('message');
return view('admin.newarticle', ['message' => $message]);
}
/**
* Process an incoming request for a new article and save it.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\View\Factory view
*/
public function store(Request $request)
{
$published = $request->input('published');
if ($published == null) {
$published = '0';
}
//if a `.md` is attached use that for the main content.
$content = null; //set default value
if ($request->hasFile('article')) {
$file = $request->file('article')->openFile();
$content = $file->fread($file->getSize());
}
$main = $content ?? $request->input('main');
try {
$article = Article::create(
[
'url' => $request->input('url'),
'title' => $request->input('title'),
'main' => $main,
'published' => $published,
]
);
} catch (Exception $e) {
$msg = $e->getMessage();
$unique = strpos($msg, '1062');
if ($unique !== false) {
//We've checked for error 1062, i.e. duplicate titleurl
return redirect('admin/blog/new')->withInput()->with('message', 'Duplicate title, please change');
}
//this isn't the error you're looking for
throw $e;
}
return view('admin.newarticlesuccess', ['id' => $article->id, 'title' => $article->title]);
}
/**
* Show the edit form for an existing article.
*
* @param string The article id
* @return \Illuminate\View\Factory view
*/
public function edit($articleId)
{
$post = Article::select(
'title',
'main',
'url',
'published'
)->where('id', $articleId)->get();
return view('admin.editarticle', ['id' => $articleId, 'post' => $post]);
}
/**
* Process an incoming request to edit an article.
*
* @param \Illuminate\Http\Request $request
* @param string
* @return \Illuminate|View\Factory view
*/
public function update(Request $request, $articleId)
{
$published = $request->input('published');
if ($published == null) {
$published = '0';
}
$article = Article::find($articleId);
$article->title = $request->input('title');
$article->url = $request->input('url');
$article->main = $request->input('main');
$article->published = $published;
$article->save();
return view('admin.editarticlesuccess', ['id' => $articleId]);
}
/**
* Show the delete confirmation form for an article.
*
* @param string The article id
* @return \Illuminate\View\Factory view
*/
public function delete($articleId)
{
return view('admin.deletearticle', ['id' => $articleId]);
}
/**
* Process a request to delete an aricle.
*
* @param string The article id
* @return \Illuminate\View\Factory view
*/
public function destroy($articleId)
{
Article::where('id', $articleId)->delete();
return view('admin.deletearticlesuccess', ['id' => $articleId]);
}
}

View file

@ -0,0 +1,88 @@
<?php
namespace App\Http\Controllers\Admin;
use App\MicropubClient;
use App\Http\Controllers\Controller;
class ClientsAdminController extends Controller
{
/**
* Show a list of known clients.
*
* @return \Illuminate\View\Factory view
*/
public function index()
{
$clients = MicropubClient::all();
return view('admin.listclients', ['clients' => $clients]);
}
/**
* Show form to add a client name.
*
* @return \Illuminate\View\Factory view
*/
public function create()
{
return view('admin.newclient');
}
/**
* Show a form to edit a client name.
*
* @param string The client id
* @return \Illuminate\View\Factory view
*/
public function edit($clientId)
{
$client = MicropubClient::findOrFail($clientId);
return view('admin.editclient', [
'id' => $clientId,
'client_url' => $client->client_url,
'client_name' => $client->client_name,
]);
}
/**
* Process the request to adda new client name.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\View\Factory view
*/
public function store(Request $request)
{
MicropubClient::create([
'client_url' => $request->input('client_url'),
'client_name' => $request->input('client_name'),
]);
return view('admin.newclientsuccess');
}
/**
* Process the request to edit a client name.
*
* @param string The client id
* @param \Illuminate\Http\Request $request
* @return \Illuminate\View\Factory view
*/
public function update($clientId, Request $request)
{
$client = MicropubClient::findOrFail($clientId);
if ($request->input('edit')) {
$client->client_url = $request->input('client_url');
$client->client_name = $request->input('client_name');
$client->save();
return view('admin.editclientsuccess');
}
if ($request->input('delete')) {
$client->delete();
return view('admin.deleteclientsuccess');
}
}
}

View file

@ -0,0 +1,169 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Contact;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Filesystem\Filesystem;
class ContactsAdminController extends Controller
{
/**
* List the currect contacts that can be edited.
*
* @return \Illuminate\View\Factory view
*/
public function index()
{
$contacts = Contact::all();
return view('admin.listcontacts', ['contacts' => $contacts]);
}
/**
* Display the form to add a new contact.
*
* @return \Illuminate\View\Factory view
*/
public function create()
{
return view('admin.newcontact');
}
/**
* Show the form to edit an existing contact.
*
* @param string The contact id
* @return \Illuminate\View\Factory view
*/
public function edit($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 delete($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 store(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 update($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 destroy($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']]);
}
}
}

View file

@ -0,0 +1,133 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Note;
use Validator;
use Illuminate\Http\Request;
use App\Jobs\SendWebMentions;
use App\Services\NoteService;
use App\Http\Controllers\Controller;
class NotesAdminController extends Controller
{
protected $noteService;
public function __construct(NoteService $noteService = null)
{
$this->noteService = $noteService ?? new NoteService();
}
/**
* List the notes that can be edited.
*
* @return \Illuminate\View\Factory view
*/
public function index()
{
$notes = Note::select('id', 'note')->orderBy('id', 'desc')->get();
foreach ($notes as $note) {
$note->originalNote = $note->getOriginal('note');
}
return view('admin.listnotes', ['notes' => $notes]);
}
/**
* Show the form to make a new note.
*
* @return \Illuminate\View\Factory view
*/
public function create()
{
return view('admin.newnote');
}
/**
* Display the form to edit a specific note.
*
* @param string The note id
* @return \Illuminate\View\Factory view
*/
public function edit($noteId)
{
$note = Note::find($noteId);
$note->originalNote = $note->getOriginal('note');
return view('admin.editnote', ['id' => $noteId, 'note' => $note]);
}
/**
* The delete note page.
*
* @param int id
* @return view
*/
public function delete($noteId)
{
return view('admin.deletenote', ['id' => $id]);
}
/**
* Process a request to make a new note.
*
* @param Illuminate\Http\Request $request
* @todo Sort this mess out
*/
public function store(Request $request)
{
$validator = Validator::make(
$request->all(),
['photo' => 'photosize'],
['photosize' => 'At least one uploaded file exceeds size limit of 5MB']
);
if ($validator->fails()) {
return redirect('/admin/note/new')
->withErrors($validator)
->withInput();
}
$note = $this->noteService->createNote($request);
return view('admin.newnotesuccess', [
'id' => $note->id,
'shorturl' => $note->shorturl,
]);
}
/**
* Process a request to edit a note. Easy since this can only be done
* from the admin CP.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\View\Factory view
*/
public function update($noteId, Request $request)
{
//update note data
$note = Note::findOrFail($noteId);
$note->note = $request->input('content');
$note->in_reply_to = $request->input('in-reply-to');
$note->save();
if ($request->input('webmentions')) {
dispatch(new SendWebMentions($note));
}
return view('admin.editnotesuccess', ['id' => $noteId]);
}
/**
* Delete the note.
*
* @param int id
* @return view
*/
public function destroy($id)
{
$note = Note::findOrFail($id);
$note->delete();
return view('admin.deletenotesuccess');
}
}

View file

@ -0,0 +1,86 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Place;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Phaza\LaravelPostgis\Geometries\Point;
class PlacesAdminController extends Controller
{
/**
* List the places that can be edited.
*
* @return \Illuminate\View\Factory view
*/
public function index()
{
$places = Place::all();
return view('admin.listplaces', ['places' => $places]);
}
/**
* Show the form to make a new place.
*
* @return \Illuminate\View\Factory view
*/
public function create()
{
return view('admin.newplace');
}
/**
* Display the form to edit a specific place.
*
* @param string The place id
* @return \Illuminate\View\Factory view
*/
public function edit($placeId)
{
$place = Place::findOrFail($placeId);
$latitude = $place->getLatitude();
$longitude = $place->getLongitude();
return view('admin.editplace', [
'id' => $placeId,
'name' => $place->name,
'description' => $place->description,
'latitude' => $latitude,
'longitude' => $longitude,
]);
}
/**
* Process a request to make a new place.
*
* @param Illuminate\Http\Request $request
* @return Illuminate\View\Factory view
*/
public function store(Request $request)
{
$this->placeService->createPlace($request);
return view('admin.newplacesuccess');
}
/**
* Process a request to edit a place.
*
* @param string The place id
* @param Illuminate\Http\Request $request
* @return Illuminate\View\Factory view
*/
public function update($placeId, Request $request)
{
$place = Place::findOrFail($placeId);
$place->name = $request->name;
$place->description = $request->description;
$place->location = new Point((float) $request->latitude, (float) $request->longitude);
$place->save();
return view('admin.editplacesuccess');
}
}