Skip to content

Commit

Permalink
Add CRUD operations for site pages
Browse files Browse the repository at this point in the history
  • Loading branch information
kutaloweb committed Aug 29, 2018
1 parent 05dd6a3 commit 4552492
Show file tree
Hide file tree
Showing 26 changed files with 1,205 additions and 8 deletions.
176 changes: 176 additions & 0 deletions app/Http/Controllers/PageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Validation\ValidationException;
use Illuminate\Http\Request;
use App\Http\Requests\PageRequest;
use App\Repositories\PageRepository;
use App\Repositories\ActivityLogRepository;

class PageController extends Controller
{
/**
* @var Request
*/
protected $request;

/**
* @var PageRepository
*/
protected $repo;

/**
* @var ActivityLogRepository
*/
protected $activity;

/**
* @var string
*/
protected $module = 'page';

/**
* Instantiate a new controller instance.
*
* @param Request $request
* @param PageRepository $repo
* @param ActivityLogRepository $activity
*/
public function __construct(Request $request, PageRepository $repo, ActivityLogRepository $activity)
{
$this->request = $request;
$this->repo = $repo;
$this->activity = $activity;
$this->middleware('permission:access-page')->except(['getPublicPages', 'getPublicPage']);
}

/**
* Display all public pages
*
* @return JsonResponse
*/
public function getPublicPages()
{
$pages = $this->repo->getPublished();

return $this->success(compact('pages'));
}

/**
* Display a public page
*
* @param string $slug
*
* @return JsonResponse
*/
public function getPublicPage($slug)
{
$page = $this->repo->getBySlugForGuests($slug);

return $this->success(compact('page'));
}

/**
* Store page
*
* @param PageRequest $request
*
* @return JsonResponse
* @throws ValidationException
*/
public function store(PageRequest $request)
{
$page = $this->repo->create($this->request->all());

$this->activity->record([
'module' => $this->module,
'module_id' => $page->id,
'activity' => 'published'
]);

return $this->success(['page' => trans('page.page_processed', ['action' => trans('page.published')])]);
}

/**
* Upload image in Summernote editor
*
* @param PageRequest $request
*
* @return JsonResponse
*/
public function uploadImage(PageRequest $request)
{
$upload_path = config('system.upload_path.images');
$extension = request()->file('file')->getClientOriginalExtension();
$filename = uniqid();
request()->file('file')->move($upload_path, $filename . "." . $extension);
$image_url = '/' . $upload_path . '/' . $filename . '.' . $extension;

return $this->success(compact('image_url'));
}

/**
* Fetch statistics
*
* @return JsonResponse
*/
public function statistics()
{
$published = $this->repo->getPublished()->count();

return $this->success(compact('published'));
}

/**
* Get all published pages
*
* @return JsonResponse
*/
public function getPublishedList()
{
$pages = $this->repo->getPublishedList($this->request->all());

return $this->success(compact('pages'));
}

/**
* Get page details
*
* @param string $slug
*
* @return JsonResponse
* @throws ValidationException
*/
public function show($slug)
{
$page = $this->repo->getBySlug($slug);

return $this->success(compact('page'));
}

/**
* Delete page
*
* @param string $slug
*
* @return JsonResponse
* @throws \Exception
*/
public function destroy($slug)
{
$page = $this->repo->getBySlug($slug);

$this->activity->record([
'module' => 'page',
'sub_module' => 'page',
'module_id' => $page->id,
'activity' => 'deleted'
]);

$page->delete();

return $this->success(['page' => trans('page.deleted', ['type' => trans('page.page')])]);
}
}
44 changes: 44 additions & 0 deletions app/Http/Requests/PageRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PageRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'sometimes|required',
'body' => 'sometimes|required',
];
}

/**
* Translate fields with user friendly name.
*
* @return array
*/
public function attributes()
{
return [
'title' => trans('page.title'),
'body' => trans('page.body'),
];
}
}
112 changes: 112 additions & 0 deletions app/Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace App;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;;

/**
* App\Profile
*
* @property int $id
* @property string|null $title
* @property string|null $slug
* @property string|null $body
* @property \Carbon\Carbon|null $created_at
* @property \Carbon\Carbon|null $updated_at
* @property-read string|null $stripped_body
*/
class Page extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'title',
'body',
'slug'
];

/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['stripped_body'];

/**
* Get created at in a human readable format.
*
* @return string
*/
public function getCreatedAtAttribute()
{
return Carbon::parse($this->attributes['created_at'])->diffForHumans();
}

/**
* Get updated at in a human readable format.
*
* @return string
*/
public function getUpdatedAtAttribute()
{
return Carbon::parse($this->attributes['updated_at'])->diffForHumans();
}

/**
* Get body with the stripped HTML tags.
*
* @return string
*/
public function getStrippedBodyAttribute()
{
return strip_tags($this->attributes['body']);
}

/**
* Sets the title and the readable slug.
*
* @param string $value
*/
public function setTitleAttribute($value)
{
$this->attributes['title'] = $value;
}

/**
* Scope a query to only include page with the given slug.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $slug
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeFilterBySlug($query, $slug = null)
{
if (!$slug) {
return $query;
}

return $query->where('slug', '=', $slug);
}

/**
* Scope a query to only include page with the given id.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int|null $id
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeFilterById($query, $id = null)
{
if (!$id) {
return $query;
}

return $query->where('id', '=', $id);
}
}
Loading

0 comments on commit 4552492

Please sign in to comment.