245 lines
8.1 KiB
PHP
245 lines
8.1 KiB
PHP
|
|
<?php namespace AhmadFatoni\ApiGenerator\Controllers\API;
|
||
|
|
|
||
|
|
use Cms\Classes\Controller;
|
||
|
|
use BackendMenu;
|
||
|
|
use DB;
|
||
|
|
use Config;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use AhmadFatoni\ApiGenerator\Helpers\Helpers;
|
||
|
|
use Illuminate\Support\Facades\Validator;
|
||
|
|
use RainLab\Blog\Models\Post;
|
||
|
|
|
||
|
|
use RainLab\Blog\Classes\PostResource;
|
||
|
|
use RainLab\Blog\Classes\PostDetailResource;
|
||
|
|
|
||
|
|
|
||
|
|
class PostsController extends Controller
|
||
|
|
{
|
||
|
|
protected $Post;
|
||
|
|
|
||
|
|
protected $helpers;
|
||
|
|
|
||
|
|
public function __construct(Post $Post, Helpers $helpers)
|
||
|
|
{
|
||
|
|
parent::__construct();
|
||
|
|
$this->Post = $Post;
|
||
|
|
$this->helpers = $helpers;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function index(Request $request){
|
||
|
|
|
||
|
|
$data = $request->all();
|
||
|
|
|
||
|
|
$validator = Validator::make($data, [
|
||
|
|
'locale' => 'required|in:ru,en,tm',
|
||
|
|
]);
|
||
|
|
|
||
|
|
if($validator->fails()) {
|
||
|
|
return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
|
||
|
|
}
|
||
|
|
|
||
|
|
$filter = [
|
||
|
|
'page' => input('page'),
|
||
|
|
'sort' => input('sort')??'published_at desc',
|
||
|
|
'perPage' => input('count'),
|
||
|
|
'search' => trim(input('search')),
|
||
|
|
'category' => input('category'),
|
||
|
|
'date' => input('date'),
|
||
|
|
'type' => input('type'),
|
||
|
|
];
|
||
|
|
|
||
|
|
if(request()->has('featured')){
|
||
|
|
$filter['featured'] = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
//$data = $this->Post::select('id','title','slug', 'excerpt', 'published_at')->with(['categories:id,name'])->listFrontEnd($filter);
|
||
|
|
$data = response()->json(
|
||
|
|
PostResource::collection($this->Post::with(['categories:id,name'])->listFrontEnd($filter)))->header('Access-Control-Allow-Origin', '*');
|
||
|
|
return $data;
|
||
|
|
|
||
|
|
//return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public function indexPagination(Request $request){
|
||
|
|
$path = Config::get('app.url').Config::get('cms.storage.media.path');
|
||
|
|
|
||
|
|
$data = $request->all();
|
||
|
|
|
||
|
|
|
||
|
|
$validator = Validator::make($data, [
|
||
|
|
'locale' => 'required|in:ru,en,tm',
|
||
|
|
]);
|
||
|
|
|
||
|
|
if($validator->fails()) {
|
||
|
|
return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
|
||
|
|
}
|
||
|
|
|
||
|
|
$filter = [
|
||
|
|
'page' => input('page'),
|
||
|
|
'sort' => input('sort')??'published_at desc',
|
||
|
|
'perPage' => input('count'),
|
||
|
|
'search' => trim(input('search')),
|
||
|
|
'category' => input('category'),
|
||
|
|
'date' => input('date'),
|
||
|
|
'type' => input('type'),
|
||
|
|
];
|
||
|
|
|
||
|
|
if(request()->has('featured')){
|
||
|
|
$filter['featured'] = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
$data = $this->Post::select('id','title','slug', 'excerpt', 'published_at', 'video')
|
||
|
|
|
||
|
|
//->with(['featured_images', 'translations:locale,model_id,attribute_data', 'categories:id,name'])
|
||
|
|
->with(['featured_images', 'categories:id,name'])
|
||
|
|
->listFrontEnd($filter);
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
//$data = response()->json(PostResource::collection(
|
||
|
|
// $this->Post::with(['categories:id,name'])
|
||
|
|
// ->listFrontEnd($filter)
|
||
|
|
|
||
|
|
// )->response()->getData(), 200);
|
||
|
|
|
||
|
|
//return $data;
|
||
|
|
|
||
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public function indexPaginationLast(Request $request){
|
||
|
|
$path = Config::get('app.url').Config::get('cms.storage.media.path');
|
||
|
|
|
||
|
|
$data = $request->all();
|
||
|
|
|
||
|
|
|
||
|
|
$validator = Validator::make($data, [
|
||
|
|
'locale' => 'required|in:ru,en,tm',
|
||
|
|
]);
|
||
|
|
|
||
|
|
if($validator->fails()) {
|
||
|
|
return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
|
||
|
|
}
|
||
|
|
|
||
|
|
$filter = [
|
||
|
|
'page' => input('page'),
|
||
|
|
'sort' => input('sort')??'published_at desc',
|
||
|
|
'perPage' => input('count'),
|
||
|
|
'search' => trim(input('search')),
|
||
|
|
'category' => input('category'),
|
||
|
|
'date' => input('date'),
|
||
|
|
'type' => input('type'),
|
||
|
|
];
|
||
|
|
|
||
|
|
if(request()->has('featured')){
|
||
|
|
$filter['featured'] = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
$data = response()->json(PostResource::collection(
|
||
|
|
$this->Post::with(['categories'])
|
||
|
|
->listFrontEnd($filter)
|
||
|
|
)->response()->getData(), 200)->header('Access-Control-Allow-Origin', '*');
|
||
|
|
|
||
|
|
return $data;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public function show($id, Request $request)
|
||
|
|
{
|
||
|
|
$data = $request->all();
|
||
|
|
$validator = Validator::make($data, [
|
||
|
|
'locale' => 'required|in:ru,en,tm',
|
||
|
|
]);
|
||
|
|
if($validator->fails()) {
|
||
|
|
return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
|
||
|
|
}
|
||
|
|
|
||
|
|
if(!$post = $this->Post::find($id)) {
|
||
|
|
return $this->helpers->apiArrayResponseBuilder(404, 'not found', ['error' => "Resource id = {$id} could not be found"]);
|
||
|
|
} else {
|
||
|
|
$obj = Db::table('vdomah_blogviews_views')
|
||
|
|
->where('post_id', $post->getKey());
|
||
|
|
|
||
|
|
if ($obj->count() > 0) {
|
||
|
|
$row = $obj->first();
|
||
|
|
|
||
|
|
// $views = $row->views + rand(1,10);;
|
||
|
|
$views = $row->views + 1;;
|
||
|
|
|
||
|
|
$obj->update(['views' => $views]);
|
||
|
|
$post['views'] = $row->views;
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
|
||
|
|
Db::table('vdomah_blogviews_views')->insert([
|
||
|
|
'post_id' => $post->getKey(),
|
||
|
|
'views' => 1
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
return new PostDetailResource($post);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public function popular(Request $request){
|
||
|
|
$data = $request->all();
|
||
|
|
|
||
|
|
$validator = Validator::make($data, [
|
||
|
|
'locale' => 'required|in:ru,en,tm',
|
||
|
|
]);
|
||
|
|
|
||
|
|
if($validator->fails()) {
|
||
|
|
return $this->helpers->apiArrayResponseBuilder(400, 'fail', $validator->errors() );
|
||
|
|
}
|
||
|
|
|
||
|
|
$filter = [
|
||
|
|
'page' => input('page'),
|
||
|
|
'perPage' => input('count'),
|
||
|
|
'search' => trim(input('search')),
|
||
|
|
'category' => input('category'),
|
||
|
|
'date' => input('date'),
|
||
|
|
'type' => input('type'),
|
||
|
|
];
|
||
|
|
|
||
|
|
if(request()->has('featured')){
|
||
|
|
$filter['featured'] = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
//$posts = response()->json(
|
||
|
|
// PostResource::collection($this->Post::isPublished()->select("id", "excerpt")
|
||
|
|
// ->leftJoin('vdomah_blogviews_views as pv', 'pv.post_id', '=', 'rainlab_blog_posts.id')->paginate(8)));
|
||
|
|
|
||
|
|
// $posts = response()->json(
|
||
|
|
// Post::isPublished()->select("id", "title", "slug", "excerpt", "published_at", "type", "views", "video", "powerseo_title", "powerseo_description", "powerseo_keywords")
|
||
|
|
// ->with("featured_images")
|
||
|
|
// ->with("categories")
|
||
|
|
// ->leftJoin('vdomah_blogviews_views as pv', 'pv.post_id', '=', 'rainlab_blog_posts.id')->listFrontEnd($filter));
|
||
|
|
|
||
|
|
$posts = response()->json(PostResource::collection(
|
||
|
|
$this->Post::with(['categories'])
|
||
|
|
->leftJoin('vdomah_blogviews_views as pv', 'pv.post_id', '=', 'rainlab_blog_posts.id')
|
||
|
|
->orderByRaw('pv.views DESC')
|
||
|
|
->listFrontEnd($filter)
|
||
|
|
|
||
|
|
)->response()->getData(), 200)->header('Access-Control-Allow-Origin', '*');
|
||
|
|
|
||
|
|
return $posts;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public static function getAfterFilters() {return [];}
|
||
|
|
public static function getBeforeFilters() {return [];}
|
||
|
|
public static function getMiddleware() {return [];}
|
||
|
|
public function callAction($method, $parameters=false) {
|
||
|
|
return call_user_func_array(array($this, $method), $parameters);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|