Skip to content

Commit

Permalink
Merge branch 'atom-feed' into 2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
sdebacker committed Feb 7, 2016
2 parents faca4fc + aa8a1bf commit c1a6ec2
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 102 deletions.
2 changes: 1 addition & 1 deletion LICENCE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014 Samuel De Backer <[email protected]>
Copyright (c) 2016 Samuel De Backer <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
}
],
"require": {
"typicms/core": "~2.5.0"
"roumen/feed": "~2.10",
"typicms/core": "~2.7.0"
},
"autoload": {
"psr-4": {
Expand Down
41 changes: 34 additions & 7 deletions src/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
{
Expand All @@ -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);
}
}
29 changes: 22 additions & 7 deletions src/Http/Controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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([
Expand All @@ -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,
]);
}
}
51 changes: 46 additions & 5 deletions src/Http/Controllers/PublicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]);
Expand All @@ -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)
{
Expand Down
19 changes: 5 additions & 14 deletions src/Http/Requests/FormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 0 additions & 2 deletions src/Models/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class News extends Base

protected $dates = ['date'];

// protected $dateFormat = 'Y-m-d';

protected $fillable = [
'date',
'image',
Expand Down
5 changes: 5 additions & 0 deletions src/Providers/ModuleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function register()
{
$app = $this->app;

/*
* Register sitemap package
*/
$app->register('Roumen\Feed\FeedServiceProvider');

/*
* Register route service provider
*/
Expand Down
25 changes: 9 additions & 16 deletions src/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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']);
}
}
Expand All @@ -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']);
});
}
}
11 changes: 1 addition & 10 deletions src/database/migrations/2013_11_07_185433_create_news_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

});
}

Expand Down
Loading

0 comments on commit c1a6ec2

Please sign in to comment.