Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Joren Van Hocht authored and Joren Van Hocht committed May 23, 2015
1 parent 3d2e948 commit 1944414
Show file tree
Hide file tree
Showing 20 changed files with 120 additions and 91 deletions.
8 changes: 5 additions & 3 deletions src/Commands/BlogifyGeneratePublicPartCommand.php
Expand Up @@ -126,12 +126,14 @@ private function publishTemplates($namespace)
*/
private function publishViews()
{
$path = __DIR__."/../../../../../resources/views/blogify/templates/";
$basepath = __DIR__."/../../../../../resources/views/blogify/";
$path = $basepath."templates/";

if (!file_exists($path)) mkdir($path, 775, true);

foreach ($this->views as $key => $file)
{
$filename = __DIR__."/../../../../../resources/views/blogify/$key.blade.php";
$filename = $basepath . "$key.blade.php";
if (file_exists($filename))
{
if ($this->confirm("File $key allready exists, do you want to override it? Y/N"))
Expand All @@ -147,7 +149,7 @@ private function publishViews()

foreach ($this->layouts as $key => $file)
{
$filename = __DIR__."/../../../../../resources/views/blogify/templates/$key.blade.php";
$filename = $basepath . "templates/$key.blade.php";
if (file_exists($filename))
{
if ($this->confirm("File $key allready exists, do you want to override it? Y/N"))
Expand Down
3 changes: 2 additions & 1 deletion src/Commands/BlogifyMigrateCommand.php
Expand Up @@ -2,7 +2,8 @@

use Illuminate\Console\Command;

class BlogifyMigrateCommand extends Command {
class BlogifyMigrateCommand extends Command
{

/**
* The console command name.
Expand Down
6 changes: 3 additions & 3 deletions src/Controllers/Admin/ApiController.php
Expand Up @@ -49,12 +49,12 @@ public function __construct(Post $post)
*/
public function sort($table, $column, $order, $trashed = false, DatabaseManager $db)
{
$data = $db->table( $table );
$data = $db->table($table);

// Check for trashed data
$data = $trashed ? $data->whereNotNull('deleted_at') : $data->whereNull('deleted_at');

$data = $data->orderBy($column, $order)->paginate( $this->config->items_per_page );
$data = $data->orderBy($column, $order)->paginate($this->config->items_per_page);

return $data;
}
Expand All @@ -74,7 +74,7 @@ public function checkIfSlugIsUnique($slug)
while ($this->post->whereSlug($slug)->get()->count() > 0)
{
$i++;
$slug = $this->base_slug . '-' . $i;
$slug = "$this->base_slug-$i";
}

return $slug;
Expand Down
1 change: 1 addition & 0 deletions src/Controllers/Admin/BaseController.php
Expand Up @@ -31,4 +31,5 @@ public function __construct()
$this->config = objectify( config('blogify') );
$this->auth_user = $this->auth->check() ? $this->auth->user() : false;
}

}
4 changes: 2 additions & 2 deletions src/Controllers/Admin/CategoriesController.php
Expand Up @@ -123,7 +123,7 @@ public function update ($hash, CategoryRequest $request)
*/
public function destroy($hash)
{
$category = $this->category->byHash( $hash );
$category = $this->category->byHash($hash);
$category_name = $category->name;
$category->delete();

Expand Down Expand Up @@ -163,7 +163,7 @@ public function restore($hash)
*/
private function storeOrUpdateCategory($request)
{
$cat = $this->category->whereName( $request->name )->first();
$cat = $this->category->whereName($request->name)->first();

if (count($cat) > 0)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Controllers/Admin/CommentsController.php
Expand Up @@ -37,11 +37,11 @@ public function __construct(Comment $comment)
*/
public function index($revised = "pending")
{
$revised = $this->checkRevised( $revised );
$revised = $this->checkRevised($revised);
if ($revised === false) abort(404);

$data = [
'comments' => $this->comment->byRevised( $revised )->paginate( $this->config->items_per_page ),
'comments' => $this->comment->byRevised($revised)->paginate($this->config->items_per_page),
'revised' => $revised,
];

Expand All @@ -65,7 +65,7 @@ public function changeStatus($hash, $new_revised)
$revised = $this->checkRevised( $new_revised );
if ($revised === false) abort(404);

$comment = $this->comment->byHash( $hash );
$comment = $this->comment->byHash($hash);
$comment->revised = $revised;
$comment->save();

Expand All @@ -90,7 +90,7 @@ public function changeStatus($hash, $new_revised)
*/
private function checkRevised($revised)
{
$allowed = [1 => 'pending', Ò2 => 'approved', 3 => 'disapproved'];
$allowed = [1 => 'pending', 2 => 'approved', 3 => 'disapproved'];

return array_search($revised, $allowed);
}
Expand Down
60 changes: 33 additions & 27 deletions src/Controllers/Admin/PostsController.php
Expand Up @@ -144,7 +144,15 @@ public function index($trashed = false)
{
$scope = 'for'.$this->auth_user->role->name;
$data = [
'posts' => (! $trashed) ? $this->post->$scope()->orderBy('publish_date', 'DESC')->paginate($this->config->items_per_page) : $this->post->$scope()->onlyTrashed()->orderBy('publish_date', 'DESC')->paginate($this->config->items_per_page),
'posts' => (! $trashed) ?
$this->post->$scope()
->orderBy('publish_date', 'DESC')
->paginate($this->config->items_per_page)
:
$this->post->$scope()
->onlyTrashed()
->orderBy('publish_date', 'DESC')
->paginate($this->config->items_per_page),
'trashed' => $trashed,
];

Expand All @@ -171,10 +179,10 @@ public function create()
* @param $slug
* @return \Illuminate\View\View
*/
public function show ( $slug )
public function show ($slug)
{
$data = [
'post' => $this->post->bySlug( $slug ),
'post' => $this->post->bySlug($slug),
];

if ($data['post']->count() <= 0) abort(404);
Expand All @@ -190,13 +198,13 @@ public function show ( $slug )
*/
public function edit($hash)
{
$p = $this->post->byHash($hash);
$hash = $this->auth_user->hash;
$post = ($this->cache->has("autoSavedPost-$hash")) ? $this->buildPostObject() : $p;
$data = $this->getViewData( $post );
$originalPost = $this->post->byHash($hash);
$hash = $this->auth_user->hash;
$post = ($this->cache->has("autoSavedPost-$hash")) ? $this->buildPostObject() : $originalPost;
$data = $this->getViewData($post);

$p->being_edited_by = $this->auth_user->id;
$p->save();
$originalPost->being_edited_by = $this->auth_user->id;
$originalPost->save();

return view('blogify::admin.posts.form', $data);
}
Expand All @@ -220,14 +228,15 @@ public function store(PostRequest $request)

$post = $this->storeOrUpdatePost();

if ($this->status->byHash($this->data->status)->name == 'Pending review') $this->mailReviewer( $post );
if ($this->status->byHash($this->data->status)->name == 'Pending review') $this->mailReviewer($post);

$action = ($request->hash == '') ? 'created' : 'updated';

tracert()->log('posts', $post->id, $this->auth_user->id, $action);

$message = trans('blogify::notify.success', ['model' => 'Post', 'name' => $post->title, 'action' => $action]);
session()->flash('notify', ['success', $message]);

$this->cache->forget('autoSavedPost');

return redirect()->route('admin.posts.index');
Expand All @@ -242,13 +251,11 @@ public function store(PostRequest $request)
public function destroy($hash)
{
$post = $this->post->byHash($hash);
$name = $post->title;

$post->delete();

tracert()->log('posts', $post->id, $this->auth_user->id, 'delete');

$message = trans('blogify::notify.success', ['model' => 'Post', 'name' => $name, 'action' =>'deleted']);
$message = trans('blogify::notify.success', ['model' => 'Post', 'name' => $post->title, 'action' =>'deleted']);
session()->flash('notify', ['success', $message]);

return redirect()->route('admin.posts.index');
Expand All @@ -267,7 +274,7 @@ public function destroy($hash)
*/
public function uploadImage(ImageUploadRequest $request)
{
$image_name = $this->resizeAnsSaveImage( $request->file('upload') );
$image_name = $this->resizeAndSaveImage($request->file('upload'));
$path = config()->get('app.url').'/uploads/posts/' . $image_name;
$func = $request->get('CKEditorFuncNum');
$result = "<script>window.parent.CKEDITOR.tools.callFunction($func, '$path', 'Image has been uploaded')</script>";
Expand Down Expand Up @@ -306,10 +313,9 @@ public function cancel($hash = null)
public function restore($hash)
{
$post = $this->post->withTrashed()->byHash($hash);
$post_title = $post->title;
$post->restore();

$message = trans('blogify::notify.success', ['model' => 'Post', 'name' => $post_title, 'action' =>'restored']);
$message = trans('blogify::notify.success', ['model' => 'Post', 'name' => $post->title, 'action' =>'restored']);
session()->flash('notify', ['success', $message]);

return redirect()->route('admin.posts.index');
Expand Down Expand Up @@ -370,7 +376,7 @@ private function getViewData($post = null)
* @param $image
* @return string
*/
private function resizeAnsSaveImage($image)
private function resizeAndSaveImage($image)
{
$image_name = $this->createImageName();
$fullpath = $this->createFullImagePath($image_name, $image->getClientOriginalExtension());
Expand Down Expand Up @@ -501,11 +507,11 @@ private function buildPostObject()
$post['short_description'] = $cached_post['short_description'];
$post['content'] = $cached_post['content'];
$post['publish_date'] = $cached_post['publishdate'];
$post['status_id'] = $this->status->byHash( $cached_post['status'] )->id;
$post['visibility_id'] = $this->visibility->byHash( $cached_post['visibility'] )->id;
$post['reviewer_id'] = $this->user->ByHash( $cached_post['reviewer'] )->id;
$post['category_id'] = $this->category->byHash( $cached_post['category'] )->id;
$post['tag'] = $this->buildTagsArrayForPostObject( $cached_post['tags'] );
$post['status_id'] = $this->status->byHash($cached_post['status'])->id;
$post['visibility_id'] = $this->visibility->byHash($cached_post['visibility'])->id;
$post['reviewer_id'] = $this->user->byHash($cached_post['reviewer'])->id;
$post['category_id'] = $this->category->byHash($cached_post['category'])->id;
$post['tag'] = $this->buildTagsArrayForPostObject($cached_post['tags']);

return objectify($post);
}
Expand All @@ -521,14 +527,14 @@ private function buildTagsArrayForPostObject($tags)
{
if ($tags == "") return [];

$t = [];
$tags = explode(',', $tags);
$aTags = [];
$hashes = explode(',', $tags);

foreach ($tags as $tag)
foreach ($hashes as $tag)
{
array_push($t, $this->tag->byHash($tag));
array_push($aTags, $this->tag->byHash($tag));
}

return $t;
return $aTags;
}
}
11 changes: 4 additions & 7 deletions src/Controllers/Admin/ProfileController.php
Expand Up @@ -122,12 +122,12 @@ private function resizeAndSaveProfilePicture($image, $filename)
$extention = $image->getClientOriginalExtension();
$fullpath = $this->config->upload_paths->profiles->profilepictures . $filename . '.' . $extention;

Image::make( $image->getRealPath() )
->resize( $this->config->image_sizes->profilepictures[0], $this->config->image_sizes->profilepictures[1] , function ($constraint) {
Image::make($image->getRealPath())
->resize($this->config->image_sizes->profilepictures[0], $this->config->image_sizes->profilepictures[1] , function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})
->save( $fullpath );
->save($fullpath);

return $fullpath;
}
Expand All @@ -140,10 +140,7 @@ private function resizeAndSaveProfilePicture($image, $filename)
*/
private function removeOldPicture($oldPicture)
{
if (file_exists($oldPicture))
{
unlink($oldPicture);
}
if (file_exists($oldPicture)) unlink($oldPicture);
}

}
17 changes: 10 additions & 7 deletions src/Controllers/Admin/TagsController.php
@@ -1,6 +1,5 @@
<?php namespace jorenvanhocht\Blogify\Controllers\Admin;

use Illuminate\Support\Facades\Config;
use Input;
use jorenvanhocht\Blogify\Models\Tag;
use jorenvanhocht\Blogify\Requests\TagUpdateRequest;
Expand Down Expand Up @@ -55,7 +54,13 @@ public function __construct(Tag $tag)
public function index($trashed = null)
{
$data = [
'tags' => (! $trashed) ? $this->tag->orderBy('created_at', 'DESC')->paginate($this->config->items_per_page) : $this->tag->onlyTrashed()->orderBy('created_at', 'DESC')->paginate($this->config->items_per_page),
'tags' => (! $trashed) ?
$this->tag->orderBy('created_at', 'DESC')
->paginate($this->config->items_per_page)
:
$this->tag->onlyTrashed()
->orderBy('created_at', 'DESC')
->paginate($this->config->items_per_page),
'trashed' => $trashed,
];

Expand Down Expand Up @@ -137,7 +142,7 @@ public function storeOrUpdate()
*/
public function update($hash, TagUpdateRequest $request)
{
$tag = $this->tag->byHash( $hash );
$tag = $this->tag->byHash($hash);
$tag->name = $request->tags;
$tag->save();

Expand All @@ -158,12 +163,11 @@ public function update($hash, TagUpdateRequest $request)
public function destroy($hash)
{
$tag = $this->tag->byHash($hash);
$tag_name = $tag->name;
$tag->delete();

tracert()->log('tags', $tag->id, $this->auth_user->id, 'delete');

$message = trans('blogify::notify.success', ['model' => 'Tags', 'name' => $tag_name, 'action' =>'deleted']);
$message = trans('blogify::notify.success', ['model' => 'Tags', 'name' => $tag->name, 'action' =>'deleted']);
session()->flash('notify', ['success', $message]);

return redirect()->route('admin.tags.index');
Expand All @@ -176,10 +180,9 @@ public function destroy($hash)
public function restore($hash)
{
$tag = $this->tag->withTrashed()->byHash($hash);
$tag_name = $tag->name;
$tag->restore();

$message = trans('blogify::notify.success', ['model' => 'Post', 'name' => $tag_name, 'action' =>'restored']);
$message = trans('blogify::notify.success', ['model' => 'Tag', 'name' => $tag->name, 'action' =>'restored']);
session()->flash('notify', ['success', $message]);

return redirect()->route('admin.tags.index');
Expand Down

0 comments on commit 1944414

Please sign in to comment.