jonnybarnes.uk/app/Http/Controllers/Admin/NotesController.php

132 lines
3.2 KiB
PHP
Raw Normal View History

2016-05-19 15:01:28 +01:00
<?php
namespace App\Http\Controllers\Admin;
2016-05-19 15:01:28 +01:00
use App\Note;
use Validator;
use Illuminate\Http\Request;
use App\Jobs\SendWebMentions;
2016-12-07 16:55:53 +00:00
use App\Services\NoteService;
use App\Http\Controllers\Controller;
2016-05-19 15:01:28 +01:00
2017-03-01 21:01:00 +00:00
class NotesController extends Controller
2016-05-19 15:01:28 +01:00
{
2016-06-23 14:27:00 +01:00
protected $noteService;
public function __construct(NoteService $noteService)
2016-06-23 14:27:00 +01:00
{
$this->noteService = $noteService;
2016-06-23 14:27:00 +01:00
}
2016-06-23 15:58:45 +01:00
2016-05-19 15:01:28 +01:00
/**
* List the notes that can be edited.
*
* @return \Illuminate\View\Factory view
*/
public function index()
2016-05-19 15:01:28 +01:00
{
$notes = Note::select('id', 'note')->orderBy('id', 'desc')->get();
foreach ($notes as $note) {
$note->originalNote = $note->getOriginal('note');
}
2017-06-11 17:10:17 +01:00
return view('admin.notes.index', compact('notes'));
2016-05-19 15:01:28 +01:00
}
/**
* Show the form to make a new note.
*
* @return \Illuminate\View\Factory view
*/
public function create()
{
return view('admin.notes.create');
}
2016-05-19 15:01:28 +01:00
/**
* Process a request to make a new note.
*
* @param Illuminate\Http\Request $request
* @todo Sort this mess out
*/
public function store(Request $request)
2016-05-19 15:01:28 +01:00
{
$validator = Validator::make(
$request->all(),
['photo' => 'photosize'],
['photosize' => 'At least one uploaded file exceeds size limit of 5MB']
);
if ($validator->fails()) {
return redirect('/admin/notes/create')
2016-05-19 15:01:28 +01:00
->withErrors($validator)
->withInput();
}
$data = [];
$data['content'] = $request->input('content');
$data['in-reply-to'] = $request->input('in-reply-to');
$data['location'] = $request->input('location');
$data['syndicate'] = [];
if ($request->input('twitter')) {
$data['syndicate'][] = 'twitter';
}
if ($request->input('facebook')) {
$data['syndicate'][] = 'facebook';
}
$note = $this->noteService->createNote($data);
2016-05-19 15:01:28 +01:00
return redirect('/admin/notes');
}
/**
* 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.notes.edit', compact('note'));
2016-05-19 15:01:28 +01:00
}
/**
* 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)
2016-05-19 15:01:28 +01:00
{
//update note data
$note = Note::findOrFail($noteId);
2016-05-19 15:01:28 +01:00
$note->note = $request->input('content');
$note->in_reply_to = $request->input('in-reply-to');
$note->save();
if ($request->input('webmentions')) {
dispatch(new SendWebMentions($note));
2016-05-19 15:01:28 +01:00
}
return redirect('/admin/notes');
2016-05-19 15:01:28 +01:00
}
/**
* Delete the note.
*
* @param int id
* @return view
*/
public function destroy($id)
{
$note = Note::findOrFail($id);
$note->delete();
return redirect('/admin/notes');
}
2016-05-19 15:01:28 +01:00
}