2021-04-08 12:38:39 +00:00
< ? php namespace AhmadFatoni\ApiGenerator\Controllers\API ;
use Cms\Classes\Controller ;
use BackendMenu ;
2021-05-11 12:39:09 +00:00
use DB ;
use Config ;
2021-04-08 12:38:39 +00:00
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 ;
}
2021-12-08 16:24:11 +00:00
//postes list
2021-04-08 12:38:39 +00:00
public function index (){
2021-05-12 09:29:47 +00:00
$path = Config :: get ( 'app.cdn' ) . Config :: get ( 'cms.storage.media.path' );
2023-01-09 12:49:08 +00:00
$filter = [
2021-04-13 09:07:38 +00:00
'page' => input ( 'page' ),
2023-05-02 06:53:47 +00:00
'sort' => input ( 'sort' ) ? ? 'published_at desc' ,
2021-04-13 09:07:38 +00:00
'perPage' => input ( 'count' ),
'search' => trim ( input ( 'search' )),
'category' => input ( 'category' ),
'date' => input ( 'date' ),
2022-12-14 15:55:13 +00:00
'typePost' => input ( 'typePost' ),
2023-01-09 12:49:08 +00:00
'postGroup' => input ( 'group' ),
2021-05-11 12:39:09 +00:00
'published' => true ,
2023-05-02 06:53:47 +00:00
'select' => [ 'id' , 'title' , 'slug' , 'featured' , 'published_at' , 'more_photo' , 'afisha_phone' , 'afisha_address' , 'afisha_phone_new' , DB :: raw ( " IF(featured_image<>'',concat(' $path ',featured_image),featured_image) as main_image " )]
2023-01-09 12:49:08 +00:00
];
2023-02-17 06:45:37 +00:00
2023-01-09 12:49:08 +00:00
if ( request () -> has ( 'featured' )){
2023-02-17 06:45:37 +00:00
$filter [ 'featured' ] = true ;
2023-01-09 12:49:08 +00:00
}
2023-02-17 06:45:37 +00:00
2023-01-09 12:49:08 +00:00
$data = $this -> Post :: with ([ 'categories:id,name' ]) -> listFrontEnd ( $filter );
2021-06-18 08:36:24 +00:00
if ( $data ){
$data -> each ( function ( $item , $key ) {
2023-02-27 04:39:19 +00:00
$item -> url = $this -> pageUrl ( 'new/newPost' ,[ 'id' => $item -> id , 'slug' => $item -> slug ]);
2021-06-18 08:36:24 +00:00
});
}
2021-04-08 12:38:39 +00:00
return $this -> helpers -> apiArrayResponseBuilder ( 200 , 'success' , $data );
}
2021-12-08 16:24:11 +00:00
//posts item
2021-05-11 09:47:33 +00:00
public function show ( $locale , $id ){
2023-03-23 09:47:46 +00:00
$path = Config :: get ( 'app.cdn' ) . Config :: get ( 'cms.storage.media.path' );
//$post = $this->Post::find($id,['id','content_html','author','slug', 'afisha_phone', 'afisha_address', 'afisha_phone_new']);
$post = $this -> Post :: where ( 'id' , $id ) -> select ( 'id' , 'content_html' , 'author' , 'title' , 'slug' , 'published_at' , 'more_photo' , 'afisha_phone' , 'afisha_address' , 'afisha_phone_new' , DB :: raw ( " IF(featured_image<>'',concat(' $path ',featured_image),featured_image) as main_image " )) -> with ([ 'categories:id,name' ]) -> first ();
2021-04-08 12:38:39 +00:00
2021-06-17 12:32:43 +00:00
2021-05-12 09:40:08 +00:00
if ( ! is_null ( $post )) {
2021-10-11 06:37:52 +00:00
2023-02-17 05:28:52 +00:00
$post -> url = $this -> pageUrl ( 'new/newPost' ,[ 'id' => $post -> id , 'slug' => $post -> slug ]);
2021-10-11 06:37:52 +00:00
2021-05-12 09:40:08 +00:00
$obj = Db :: table ( 'vdomah_blogviews_views' )
-> where ( 'post_id' , $post -> getKey ());
if ( $obj -> count () > 0 ) {
2021-05-12 10:05:27 +00:00
$row = $obj -> first ();
2021-05-19 06:41:34 +00:00
$views = $row -> views + rand ( 1 , 10 );;
2021-05-12 10:05:27 +00:00
$obj -> update ([ 'views' => $views ]);
$post [ 'views' ] = $row -> views ;
2021-05-12 09:40:08 +00:00
}
2021-05-19 06:41:34 +00:00
else {
Db :: table ( 'vdomah_blogviews_views' ) -> insert ([
'post_id' => $post -> getKey (),
'views' => rand ( 1 , 10 )
]);
}
2021-05-12 09:40:08 +00:00
return $this -> helpers -> apiArrayResponseBuilder ( 200 , 'success' , [ $post ]);
2021-10-11 06:37:52 +00:00
}
else {
2021-05-11 09:42:47 +00:00
return $this -> helpers -> apiArrayResponseBuilder ( 404 , 'not found' , [ 'error' => 'Resource id=' . $id . ' could not be found' ]);
2021-04-08 12:38:39 +00:00
}
}
2021-04-13 09:07:38 +00:00
}