-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
1,205 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')])]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.