diff --git a/LICENCE b/LICENCE index 102137d..de52a6b 100644 --- a/LICENCE +++ b/LICENCE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014 Samuel De Backer +Copyright (c) 2016 Samuel De Backer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/composer.json b/composer.json index 6c4a31c..2383e34 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,8 @@ } ], "require": { - "typicms/core": "~2.5.0" + "roumen/feed": "~2.10", + "typicms/core": "~2.7.0" }, "autoload": { "psr-4": { diff --git a/src/Http/Controllers/AdminController.php b/src/Http/Controllers/AdminController.php index b0439f1..51a5762 100644 --- a/src/Http/Controllers/AdminController.php +++ b/src/Http/Controllers/AdminController.php @@ -4,6 +4,7 @@ use TypiCMS\Modules\Core\Http\Controllers\BaseAdminController; use TypiCMS\Modules\News\Http\Requests\FormRequest; +use TypiCMS\Modules\News\Models\News; use TypiCMS\Modules\News\Repositories\NewsInterface; class AdminController extends BaseAdminController @@ -13,12 +14,38 @@ public function __construct(NewsInterface $news) parent::__construct($news); } + /** + * Create form for a new resource. + * + * @return \Illuminate\View\View + */ + public function create() + { + $model = $this->repository->getModel(); + + return view('core::admin.create') + ->with(compact('model')); + } + + /** + * Edit form for the specified resource. + * + * @param \TypiCMS\Modules\News\Models\News $news + * + * @return \Illuminate\View\View + */ + public function edit(News $news) + { + return view('core::admin.edit') + ->with(['model' => $news]); + } + /** * Store a newly created resource in storage. * - * @param FormRequest $request + * @param \TypiCMS\Modules\News\Http\Requests\FormRequest $request * - * @return Redirect + * @return \Illuminate\Http\RedirectResponse */ public function store(FormRequest $request) { @@ -30,15 +57,15 @@ public function store(FormRequest $request) /** * Update the specified resource in storage. * - * @param $model - * @param FormRequest $request + * @param \TypiCMS\Modules\News\Models\News $news + * @param \TypiCMS\Modules\News\Http\Requests\FormRequest $request * - * @return Redirect + * @return \Illuminate\Http\RedirectResponse */ - public function update($model, FormRequest $request) + public function update(News $news, FormRequest $request) { $this->repository->update($request->all()); - return $this->redirect($request, $model); + return $this->redirect($request, $news); } } diff --git a/src/Http/Controllers/ApiController.php b/src/Http/Controllers/ApiController.php index fce31a1..12bb183 100644 --- a/src/Http/Controllers/ApiController.php +++ b/src/Http/Controllers/ApiController.php @@ -2,8 +2,9 @@ namespace TypiCMS\Modules\News\Http\Controllers; -use Illuminate\Support\Facades\Input; +use Illuminate\Support\Facades\Request; use TypiCMS\Modules\Core\Http\Controllers\BaseApiController; +use TypiCMS\Modules\News\Models\News; use TypiCMS\Modules\News\Repositories\NewsInterface as Repository; class ApiController extends BaseApiController @@ -20,7 +21,7 @@ public function __construct(Repository $repository) */ public function store() { - $model = $this->repository->create(Input::all()); + $model = $this->repository->create(Request::all()); $error = $model ? false : true; return response()->json([ @@ -32,16 +33,30 @@ public function store() /** * Update the specified resource in storage. * - * @param $model + * @return \Illuminate\Http\JsonResponse + */ + public function update() + { + $updated = $this->repository->update(Request::all()); + + return response()->json([ + 'error' => !$updated, + ]); + } + + /** + * Remove the specified resource from storage. + * + * @param \TypiCMS\Modules\News\Models\News $news * * @return \Illuminate\Http\JsonResponse */ - public function update($model) + public function destroy(News $news) { - $error = $this->repository->update(Input::all()) ? false : true; + $deleted = $this->repository->delete($news); return response()->json([ - 'error' => $error, - ], 200); + 'error' => !$deleted, + ]); } } diff --git a/src/Http/Controllers/PublicController.php b/src/Http/Controllers/PublicController.php index 24b94e9..42b9681 100644 --- a/src/Http/Controllers/PublicController.php +++ b/src/Http/Controllers/PublicController.php @@ -3,25 +3,30 @@ namespace TypiCMS\Modules\News\Http\Controllers; use Illuminate\Pagination\LengthAwarePaginator as Paginator; -use Illuminate\Support\Facades\Input; +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); } /** * Display a listing of the resource. * - * @return \Illuminate\Support\Facades\Response + * @return \Illuminate\View\View */ public function index() { - $page = Input::get('page'); + $page = Request::input('page'); $perPage = config('typicms.news.per_page'); $data = $this->repository->byPage($page, $perPage, ['translations']); $models = new Paginator($data->items, $data->totalItems, $perPage, null, ['path' => Paginator::resolveCurrentPath()]); @@ -30,10 +35,46 @@ 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. * - * @return \Illuminate\Support\Facades\Response + * @return \Illuminate\View\View */ public function show($slug) { 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/Models/News.php b/src/Models/News.php index 5485816..890dfdc 100644 --- a/src/Models/News.php +++ b/src/Models/News.php @@ -17,8 +17,6 @@ class News extends Base protected $dates = ['date']; - // protected $dateFormat = 'Y-m-d'; - protected $fillable = [ 'date', 'image', 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 6c41440..650ffae 100644 --- a/src/Providers/RouteServiceProvider.php +++ b/src/Providers/RouteServiceProvider.php @@ -17,20 +17,6 @@ class RouteServiceProvider extends ServiceProvider */ protected $namespace = 'TypiCMS\Modules\News\Http\Controllers'; - /** - * Define your route model bindings, pattern filters, etc. - * - * @param \Illuminate\Routing\Router $router - * - * @return void - */ - public function boot(Router $router) - { - parent::boot($router); - - $router->model('news', 'TypiCMS\Modules\News\Models\News'); - } - /** * Define the routes for the application. * @@ -50,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']); } } @@ -58,12 +45,18 @@ public function map(Router $router) /* * Admin routes */ - $router->resource('admin/news', 'AdminController'); + $router->get('admin/news', ['as' => 'admin.news.index', 'uses' => 'AdminController@index']); + $router->get('admin/news/create', ['as' => 'admin.news.create', 'uses' => 'AdminController@create']); + $router->get('admin/news/{news}/edit', ['as' => 'admin.news.edit', 'uses' => 'AdminController@edit']); + $router->post('admin/news', ['as' => 'admin.news.store', 'uses' => 'AdminController@store']); + $router->put('admin/news/{news}', ['as' => 'admin.news.update', 'uses' => 'AdminController@update']); /* * API routes */ - $router->resource('api/news', 'ApiController'); + $router->get('api/news', ['as' => 'api.news.index', 'uses' => 'ApiController@index']); + $router->put('api/news/{news}', ['as' => 'api.news.update', 'uses' => 'ApiController@update']); + $router->delete('api/news/{news}', ['as' => 'api.news.destroy', 'uses' => 'ApiController@destroy']); }); } } 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 707bbaf..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') @@ -25,28 +25,15 @@
- {!! BootForm::date(trans('validation.attributes.date'), 'date')->value($model->present()->dateOrNow('date'))->addClass('datepicker') !!} + {!! BootForm::date(trans('validation.attributes.date'), 'date')->value(old('date') ? : $model->present()->dateOrNow('date'))->addClass('datepicker') !!}
- - @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 4ff092a..c37f424 100644 --- a/src/resources/views/admin/_index.blade.php +++ b/src/resources/views/admin/_index.blade.php @@ -1,11 +1,13 @@
+ New

- New @{{ 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/_list-item.blade.php b/src/resources/views/public/_list-item.blade.php index 173aab5..b2ff333 100644 --- a/src/resources/views/public/_list-item.blade.php +++ b/src/resources/views/public/_list-item.blade.php @@ -1,10 +1,13 @@ -
  • - +
  • +
  • diff --git a/src/resources/views/public/_list.blade.php b/src/resources/views/public/_list.blade.php index 07e6248..421c9a4 100644 --- a/src/resources/views/public/_list.blade.php +++ b/src/resources/views/public/_list.blade.php @@ -1,4 +1,4 @@ -
      +
        @foreach ($items as $news) @include('news::public._list-item') @endforeach diff --git a/src/resources/views/public/index.blade.php b/src/resources/views/public/index.blade.php index 70ef133..c37633b 100644 --- a/src/resources/views/public/index.blade.php +++ b/src/resources/views/public/index.blade.php @@ -12,6 +12,6 @@ @include('news::public._list', ['items' => $models]) @endif - {!! $models->appends(Input::except('page'))->render() !!} + {!! $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 ee75a44..a6cea2e 100644 --- a/src/resources/views/public/show.blade.php +++ b/src/resources/views/public/show.blade.php @@ -3,22 +3,24 @@ @section('title', $model->title . ' – ' . trans('news::global.name') . ' – ' . $websiteTitle) @section('ogTitle', $model->title) @section('description', $model->summary) -@section('image', $model->present()->thumbAbsoluteSrc()) +@section('image', $model->present()->thumbUrl()) @section('bodyClass', 'body-news body-news-' . $model->id . ' body-page body-page-' . $page->id) @section('main') @include('core::public._btn-prev-next', ['module' => 'News', 'model' => $model]) -
        -

        {{ $model->title }}

        + +
        +

        {{ $model->title }}

        {!! $model->present()->thumb(null, 200) !!} -
        @lang('news::global.Published on') - + +
        @lang('news::global.Published on') +
        -

        {{ nl2br($model->summary) }}

        -
        {!! $model->present()->body !!}
        +

        {{ nl2br($model->summary) }}

        +
        {!! $model->present()->body !!}
        @include('galleries::public._galleries') -@stop +@endsection