86 lines
2.7 KiB
PHP
86 lines
2.7 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;
|
|
class postsController extends Controller
|
|
{
|
|
protected $Post;
|
|
|
|
protected $helpers;
|
|
|
|
public function __construct(Post $Post, Helpers $helpers)
|
|
{
|
|
parent::__construct();
|
|
$this->Post = $Post;
|
|
$this->helpers = $helpers;
|
|
}
|
|
|
|
//postes list
|
|
public function index(){
|
|
$path = Config::get('app.cdn').Config::get('cms.storage.media.path');
|
|
$data = $this->Post::with(['categories:id,name'])->listFrontEnd([
|
|
'page' => input('page'),
|
|
'sort' => input('sort')??'published_at desc',
|
|
'perPage' => input('count'),
|
|
'search' => trim(input('search')),
|
|
'category' => input('category'),
|
|
'date' => input('date'),
|
|
'typePost' => input('typePost'),
|
|
'postGroup' => input('group'),
|
|
'published' => true,
|
|
'featured' => input('featured') == true ? 1 : 0,
|
|
'select' => ['id','title','slug','published_at',DB::raw("IF(featured_image<>'',concat('$path',featured_image),featured_image) as main_image")]
|
|
]);
|
|
|
|
if($data){
|
|
$data->each(function ($item, $key) {
|
|
$item->url = $this->pageUrl('post',['id'=>$item->id,'slug'=>$item->slug]);
|
|
});
|
|
}
|
|
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'success', $data);
|
|
}
|
|
|
|
//posts item
|
|
public function show($locale,$id){
|
|
|
|
$post = $this->Post::find($id,['id','content_html','author','slug']);
|
|
|
|
if(!is_null($post)) {
|
|
|
|
$post->url = $this->pageUrl('post',['id'=>$post->id,'slug'=>$post->slug]);
|
|
|
|
$obj = Db::table('vdomah_blogviews_views')
|
|
->where('post_id', $post->getKey());
|
|
|
|
if ($obj->count() > 0) {
|
|
$row = $obj->first();
|
|
|
|
$views = $row->views + rand(1,10);;
|
|
|
|
$obj->update(['views' => $views]);
|
|
$post['views'] = $row->views;
|
|
}
|
|
else {
|
|
|
|
Db::table('vdomah_blogviews_views')->insert([
|
|
'post_id' => $post->getKey(),
|
|
'views' => rand(1,10)
|
|
]);
|
|
}
|
|
return $this->helpers->apiArrayResponseBuilder(200, 'success', [$post]);
|
|
}
|
|
else {
|
|
return $this->helpers->apiArrayResponseBuilder(404, 'not found', ['error' => 'Resource id=' . $id . ' could not be found']);
|
|
}
|
|
|
|
}
|
|
|
|
}
|