180 lines
5.3 KiB
PHP
180 lines
5.3 KiB
PHP
<?php namespace RainLab\Blog\Controllers;
|
|
|
|
use BackendMenu;
|
|
use Flash;
|
|
use Lang;
|
|
use Backend\Classes\Controller;
|
|
use RainLab\Blog\Models\Post;
|
|
use October\Rain\Network\Http;
|
|
|
|
class Posts extends Controller
|
|
{
|
|
public $implement = [
|
|
'Backend.Behaviors.FormController',
|
|
'Backend.Behaviors.ListController',
|
|
'Backend.Behaviors.ImportExportController',
|
|
'Backend.Behaviors.RelationController'
|
|
];
|
|
|
|
public $formConfig = 'config_form.yaml';
|
|
public $listConfig = 'config_list.yaml';
|
|
public $importExportConfig = 'config_import_export.yaml';
|
|
public $relationConfig = 'config_relation.yaml';
|
|
|
|
public $requiredPermissions = ['rainlab.blog.access_other_posts', 'rainlab.blog.access_posts'];
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
|
|
BackendMenu::setContext('RainLab.Blog', 'blog', 'posts');
|
|
}
|
|
|
|
public function onNotify($id) {
|
|
$result = Http::post('https://fcm.googleapis.com/fcm/send', function($http) use ($id){
|
|
$http->header('Content-Type', 'application/json');
|
|
$http->header('Authorization', 'key=AAAAxAUM6JY:APA91bFhUTph1MyI8BiDdgICwvR4YVim7rgW5cohk7j8Y5AGj3NLkQlw9gGc9vj9TtCzL8asMYijmbN47cQA0sAwW2L7CuhDI_Is3iqUuN7YQk4QjSGxjGbcGqgF-Lzy97A8eIKAPhDG');
|
|
|
|
$post = Post::find($id);
|
|
$data = [
|
|
"data"=>[
|
|
"click_action" => "FLUTTER_NOTIFICATION_CLICK",
|
|
"type" => "news",
|
|
"post" => [
|
|
"id" => $post->id,
|
|
"title" => $post->title,
|
|
"content" => $post->excerpt
|
|
],
|
|
],
|
|
"priority" => "high",
|
|
"notification" => [
|
|
"title" => $post->title,
|
|
"body" => $post->excerpt
|
|
],
|
|
"to" => "/topics/notifications_".$post->locale
|
|
];
|
|
|
|
$http->setOption(CURLOPT_POSTFIELDS, json_encode($data));
|
|
|
|
});
|
|
|
|
return $result;
|
|
if($result->message_id){
|
|
return Flash::success('Notification ugradyldy.');
|
|
}elseif($result->error){
|
|
return Flash::error('Ýalňyşlyk Ýüze Çykdy!!!');
|
|
}
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$this->vars['postsTotal'] = Post::count();
|
|
$this->vars['postsPublished'] = Post::isPublished()->count();
|
|
$this->vars['postsDrafts'] = $this->vars['postsTotal'] - $this->vars['postsPublished'];
|
|
|
|
$this->asExtension('ListController')->index();
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
BackendMenu::setContextSideMenu('new_post');
|
|
|
|
$this->bodyClass = 'compact-container';
|
|
$this->addCss('/plugins/rainlab/blog/assets/css/rainlab.blog-preview.css');
|
|
$this->addJs('/plugins/rainlab/blog/assets/js/post-form.js');
|
|
|
|
return $this->asExtension('FormController')->create();
|
|
}
|
|
|
|
public function update($recordId = null)
|
|
{
|
|
$this->bodyClass = 'compact-container';
|
|
$this->addCss('/plugins/rainlab/blog/assets/css/rainlab.blog-preview.css');
|
|
$this->addJs('/plugins/rainlab/blog/assets/js/post-form.js');
|
|
|
|
return $this->asExtension('FormController')->update($recordId);
|
|
}
|
|
|
|
public function export()
|
|
{
|
|
$this->addCss('/plugins/rainlab/blog/assets/css/rainlab.blog-export.css');
|
|
|
|
return $this->asExtension('ImportExportController')->export();
|
|
}
|
|
|
|
public function listExtendQuery($query)
|
|
{
|
|
if (!$this->user->hasAnyAccess(['rainlab.blog.access_other_posts'])) {
|
|
$query->where('user_id', $this->user->id);
|
|
}
|
|
|
|
$query->leftJoin('vdomah_blogviews_views', 'rainlab_blog_posts.id', 'vdomah_blogviews_views.post_id');
|
|
|
|
$query->addSelect(
|
|
'vdomah_blogviews_views.views as sortable_views'
|
|
);
|
|
}
|
|
|
|
public function formExtendQuery($query)
|
|
{
|
|
if (!$this->user->hasAnyAccess(['rainlab.blog.access_other_posts'])) {
|
|
$query->where('user_id', $this->user->id);
|
|
}
|
|
}
|
|
|
|
public function formExtendFieldsBefore($widget)
|
|
{
|
|
if (!$model = $widget->model) {
|
|
return;
|
|
}
|
|
|
|
if ($model instanceof Post && $model->isClassExtendedWith('RainLab.Translate.Behaviors.TranslatableModel')) {
|
|
$widget->secondaryTabs['fields']['content']['type'] = 'RainLab\Blog\FormWidgets\MLBlogMarkdown';
|
|
}
|
|
}
|
|
|
|
public function index_onDelete()
|
|
{
|
|
if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) {
|
|
|
|
foreach ($checkedIds as $postId) {
|
|
if ((!$post = Post::find($postId)) || !$post->canEdit($this->user)) {
|
|
continue;
|
|
}
|
|
|
|
$post->delete();
|
|
}
|
|
|
|
Flash::success(Lang::get('rainlab.blog::lang.post.delete_success'));
|
|
}
|
|
|
|
return $this->listRefresh();
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public function listInjectRowClass($record, $definition = null)
|
|
{
|
|
if (!$record->published) {
|
|
return 'safe disabled';
|
|
}
|
|
}
|
|
|
|
public function formBeforeCreate($model)
|
|
{
|
|
$model->user_id = $this->user->id;
|
|
}
|
|
|
|
public function onRefreshPreview()
|
|
{
|
|
$data = post('Post');
|
|
|
|
$previewHtml = Post::formatHtml($data['content'], true);
|
|
|
|
return [
|
|
'preview' => $previewHtml
|
|
];
|
|
}
|
|
}
|