diff --git a/composer.json b/composer.json index e15d07f..2383e34 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,8 @@ } ], "require": { - "typicms/core": "~2.6.0" + "roumen/feed": "~2.10", + "typicms/core": "~2.7.0" }, "autoload": { "psr-4": { diff --git a/src/Http/Controllers/PublicController.php b/src/Http/Controllers/PublicController.php index 0a34b1c..42b9681 100644 --- a/src/Http/Controllers/PublicController.php +++ b/src/Http/Controllers/PublicController.php @@ -4,13 +4,18 @@ use Illuminate\Pagination\LengthAwarePaginator as Paginator; use Illuminate\Support\Facades\Request; +use Roumen\Feed\Feed; +use TypiCMS\Modules\Core\Facades\TypiCMS; use TypiCMS\Modules\Core\Http\Controllers\BasePublicController; use TypiCMS\Modules\News\Repositories\NewsInterface; class PublicController extends BasePublicController { - public function __construct(NewsInterface $news) + private $feed; + + public function __construct(NewsInterface $news, Feed $feed) { + $this->feed = $feed; parent::__construct($news); } @@ -30,6 +35,42 @@ public function index() ->with(compact('models')); } + /** + * Generate Atom feed. + */ + public function feed() + { + $page = TypiCMS::getPageLinkedToModule('news'); + if (!$page) { + return; + } + $feed = $this->feed; + if (config('typicms.cache')) { + $feed->setCache(60, 'typicmsNewsFeed'); + } + if (!$feed->isCached()) { + $models = $this->repository->latest(10, ['translations']); + + $feed->title = $page->title.' – '.TypiCMS::title(); + $feed->description = $page->body; + if (config('typicms.image')) { + $feed->logo = url('uploads/settings/'.config('typicms.image')); + } + $feed->link = url()->route(config('app.locale').'.news.feed'); + $feed->setDateFormat('datetime'); // 'datetime', 'timestamp' or 'carbon' + $feed->pubdate = isset($models[0]) and $models[0]->created_at; + $feed->lang = config('app.locale'); + $feed->setShortening(true); // true or false + $feed->setTextLimit(100); // maximum length of description text + + foreach ($models as $model) { + $feed->add($model->title, null, url($model->uri()), $model->created_at, $model->summary, $model->body); + } + } + + return $feed->render('atom'); + } + /** * Show news. * diff --git a/src/Http/Requests/FormRequest.php b/src/Http/Requests/FormRequest.php index 25d9c41..71ed902 100644 --- a/src/Http/Requests/FormRequest.php +++ b/src/Http/Requests/FormRequest.php @@ -8,20 +8,11 @@ class FormRequest extends AbstractFormRequest { public function rules() { - $rules = [ - 'date' => 'required|date', - 'image' => 'image|max:2000', + return [ + 'date' => 'required|date', + 'image' => 'image|max:2000', + '*.title' => 'max:255', + '*.slug' => 'alpha_dash|max:255', ]; - foreach (config('translatable.locales') as $locale) { - $rules[$locale.'.slug'] = [ - 'required_with:'.$locale.'.title', - 'required_if:'.$locale.'.status,1', - 'alpha_dash', - 'max:255', - ]; - $rules[$locale.'.title'] = 'max:255'; - } - - return $rules; } } diff --git a/src/Providers/ModuleProvider.php b/src/Providers/ModuleProvider.php index 2b20556..1769ed1 100644 --- a/src/Providers/ModuleProvider.php +++ b/src/Providers/ModuleProvider.php @@ -49,6 +49,11 @@ public function register() { $app = $this->app; + /* + * Register sitemap package + */ + $app->register('Roumen\Feed\FeedServiceProvider'); + /* * Register route service provider */ diff --git a/src/Providers/RouteServiceProvider.php b/src/Providers/RouteServiceProvider.php index f76cd04..650ffae 100644 --- a/src/Providers/RouteServiceProvider.php +++ b/src/Providers/RouteServiceProvider.php @@ -36,6 +36,7 @@ public function map(Router $router) foreach (config('translatable.locales') as $lang) { if ($uri = $page->uri($lang)) { $router->get($uri, $options + ['as' => $lang.'.news', 'uses' => 'PublicController@index']); + $router->get($uri.'/feed', $options + ['as' => $lang.'.news.feed', 'uses' => 'PublicController@feed']); $router->get($uri.'/{slug}', $options + ['as' => $lang.'.news.slug', 'uses' => 'PublicController@show']); } } diff --git a/src/database/migrations/2013_11_07_185433_create_news_table.php b/src/database/migrations/2013_11_07_185433_create_news_table.php index cf20c5c..e7698fc 100644 --- a/src/database/migrations/2013_11_07_185433_create_news_table.php +++ b/src/database/migrations/2013_11_07_185433_create_news_table.php @@ -14,35 +14,26 @@ public function up() { Schema::create('news', function (Blueprint $table) { $table->engine = 'InnoDB'; - $table->increments('id'); $table->date('date'); $table->string('image')->nullable(); - $table->timestamps(); }); Schema::create('news_translations', function (Blueprint $table) { $table->engine = 'InnoDB'; - $table->increments('id'); $table->integer('news_id')->unsigned(); - $table->string('locale')->index(); - $table->boolean('status')->default(0); - $table->string('title'); $table->string('slug')->nullable(); - $table->text('summary'); $table->text('body'); - $table->timestamps(); - $table->unique(['news_id', 'locale']); + $table->unique(['locale', 'slug']); $table->foreign('news_id')->references('id')->on('news')->onDelete('cascade'); - }); } diff --git a/src/resources/views/admin/_form.blade.php b/src/resources/views/admin/_form.blade.php index bed3860..8021d37 100644 --- a/src/resources/views/admin/_form.blade.php +++ b/src/resources/views/admin/_form.blade.php @@ -1,7 +1,7 @@ @section('js') -@stop +@endsection @include('core::admin._buttons-form') @@ -29,24 +29,11 @@ - - @include('core::admin._tabs-lang-form', ['target' => 'content']) - -
- - @foreach ($locales as $lang) - -
- @include('core::form._title-and-slug') - - {!! BootForm::checkbox(trans('validation.attributes.online'), $lang.'[status]') !!} - {!! BootForm::textarea(trans('validation.attributes.summary'), $lang.'[summary]')->rows(4) !!} - {!! BootForm::textarea(trans('validation.attributes.body'), $lang.'[body]')->addClass('ckeditor') !!} -
- - @endforeach - -
+ @include('core::form._title-and-slug') + {!! TranslatableBootForm::hidden('status')->value(0) !!} + {!! TranslatableBootForm::checkbox(trans('validation.attributes.online'), 'status') !!} + {!! TranslatableBootForm::textarea(trans('validation.attributes.summary'), 'summary')->rows(4) !!} + {!! TranslatableBootForm::textarea(trans('validation.attributes.body'), 'body')->addClass('ckeditor') !!} diff --git a/src/resources/views/admin/_index.blade.php b/src/resources/views/admin/_index.blade.php index ebf2640..c37f424 100644 --- a/src/resources/views/admin/_index.blade.php +++ b/src/resources/views/admin/_index.blade.php @@ -5,7 +5,9 @@ @{{ models.length }} @choice('news::global.news', 2) - @include('core::admin._tabs-lang-list') +
+ @include('core::admin._lang-switcher') +
diff --git a/src/resources/views/public/index.blade.php b/src/resources/views/public/index.blade.php index 0e7a410..c37633b 100644 --- a/src/resources/views/public/index.blade.php +++ b/src/resources/views/public/index.blade.php @@ -14,4 +14,4 @@ {!! $models->appends(Request::except('page'))->render() !!} -@stop +@endsection diff --git a/src/resources/views/public/show.blade.php b/src/resources/views/public/show.blade.php index 0c92f38..a6cea2e 100644 --- a/src/resources/views/public/show.blade.php +++ b/src/resources/views/public/show.blade.php @@ -23,4 +23,4 @@ @include('galleries::public._galleries') -@stop +@endsection