Files
UNIT3D-Community-Edition/app/Http/Controllers/Staff/ArticleController.php
HDVinnie 9893abfbb5 (Update) Refactor Controller
- closes #196
- removes use of redirect()->back() and Redirect::back()
2018-02-16 13:49:58 -05:00

145 lines
5.3 KiB
PHP
Executable File

<?php
/**
* NOTICE OF LICENSE
*
* UNIT3D is open-sourced software licensed under the GNU General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
* @author HDVinnie
*/
namespace App\Http\Controllers\Staff;
use App\Article;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
class ArticleController extends Controller
{
/**
* Show The Current Articles
*
* @access public
* @return Staff.article.index
*/
public function index()
{
$posts = Article::orderBy('created_at', 'DESC')->paginate(20);
return view('Staff.article.index', ['posts' => $posts]);
}
/**
* Add A Article
*
* @access public
* @return Staff.article.add
*/
public function add()
{
if (Request::isMethod('post')) {
$input = Request::all();
$post = new Article();
$post->title = $input['title'];
$post->slug = str_slug($post->title);
$post->content = $input['content'];
$post->user_id = Auth::user()->id;
// Verify that an image was upload
if (Request::hasFile('image') && Request::file('image')->getError() == 0) {
// The file is an image
if (in_array(Request::file('image')->getClientOriginalExtension(), ['jpg', 'jpeg', 'bmp', 'png', 'tiff'])) {
// Move and add the name to the object that will be saved
$post->image = 'article-' . uniqid() . '.' . Request::file('image')->getClientOriginalExtension();
Request::file('image')->move(getcwd() . '/files/img/', $post->image);
} else {
// Image null or wrong format
$post->image = null;
}
} else {
// Error on the image so null
$post->image = null;
}
$v = Validator::make($post->toArray(), $post->rules);
if ($v->fails()) {
// Delete the image because the validation failed
if (file_exists(Request::file('image')->move(getcwd() . '/files/img/' . $post->image))) {
unlink(Request::file('image')->move(getcwd() . '/files/img/' . $post->image));
}
return Redirect::route('staff_article_index')->with(Toastr::error('Your article has failed to published!', 'Error!', ['options']));
} else {
Auth::user()->articles()->save($post);
return Redirect::route('staff_article_index')->with(Toastr::success('Your article has successfully published!', 'Yay!', ['options']));
}
}
return view('Staff.article.add');
}
/**
* Edit Article
*
* @access public
* @param $slug Slug of article
* @param $id Id of article
* @return Staff.article.edit
*/
public function edit($slug, $id)
{
$post = Article::findOrFail($id);
if (Request::isMethod('post')) {
$input = Request::all();
$post->title = $input['title'];
$post->slug = str_slug($post->title);
$post->content = $input['content'];
$post->user_id = Auth::user()->id;
// Verify that an image was upload
if (Request::hasFile('image') && Request::file('image')->getError() == 0) {
// The file is an image
if (in_array(Request::file('image')->getClientOriginalExtension(), ['jpg', 'jpeg', 'bmp', 'png', 'tiff'])) {
// Move and add the name to the object that will be saved
$post->image = 'article-' . uniqid() . '.' . Request::file('image')->getClientOriginalExtension();
Request::file('image')->move(getcwd() . '/files/img/', $post->image);
} else {
// Image null or wrong format
$post->image = null;
}
} else {
// Error on the image so null
$post->image = null;
}
$v = Validator::make($post->toArray(), $post->rules);
if ($v->fails()) {
return Redirect::route('staff_article_index')->with(Toastr::error('Your article changes have failed to publish!', 'Error!', ['options']));
} else {
$post->save();
return Redirect::route('staff_article_index')->with(Toastr::success('Your article changes have successfully published!', 'Yay!', ['options']));
}
}
return view('Staff.article.edit', ['post' => $post]);
}
/**
* Delete the article
*
* @access public
* @param $slug Slug of article
* @param $id Id of article
* @return void
*/
public function delete($slug, $id)
{
$post = Article::findOrFail($id);
$post->delete();
return Redirect::route('staff_article_index')->with(Toastr::info('Article has successfully been deleted', 'Info!', ['options']));
}
}