Skip to content

Commit

Permalink
Merge branch 'Refactoring-files'
Browse files Browse the repository at this point in the history
  • Loading branch information
sdebacker committed May 6, 2014
2 parents 7f47d53 + 2be937f commit 1c5246e
Show file tree
Hide file tree
Showing 34 changed files with 187 additions and 271 deletions.
14 changes: 8 additions & 6 deletions app/TypiCMS/Controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ abstract class BaseController extends Controller

public function __construct($repository = null, $form = null)
{
$this->beforeFilter(function () {
Event::fire('clockwork.controller.start');
});
// Uncomment this if you want to use clockwork

$this->afterFilter(function () {
Event::fire('clockwork.controller.end');
});
// $this->beforeFilter(function () {
// Event::fire('clockwork.controller.start');
// });

// $this->afterFilter(function () {
// Event::fire('clockwork.controller.end');
// });

$this->repository = $repository;
$this->form = $form;
Expand Down
24 changes: 18 additions & 6 deletions app/TypiCMS/Modules/Events/Models/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,32 @@ class Event extends Base
public $direction = 'asc';

/**
* Relations
* Transform start_date in Carbon object
*
* @param string $value date string
*/
public function files()
{
return $this->morphMany('TypiCMS\Modules\Files\Models\File', 'fileable');
}

public function setStartDateAttribute($value)
{
$this->attributes['start_date'] = Carbon::parse($value);
}

/**
* Transform end_date in Carbon object
*
* @param string $value date string
*/
public function setEndDateAttribute($value)
{
$this->attributes['end_date'] = Carbon::parse($value);
}

/**
* Define a many-to-many polymorphic relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function files()
{
return $this->morphToMany('TypiCMS\Modules\Files\Models\File', 'fileable')->withTimestamps();
}
}
1 change: 0 additions & 1 deletion app/TypiCMS/Modules/Events/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Route::bind('events', function ($value, $route) {
return TypiCMS\Modules\Events\Models\Event::where('id', $value)
->with('translations')
->files(true)
->firstOrFail();
});

Expand Down
56 changes: 26 additions & 30 deletions app/TypiCMS/Modules/Files/Controllers/Admin/FilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,38 @@ public function __construct(FileInterface $file, FileForm $fileform)
* List models
* GET /admin/model
*/
public function index($parent = null)
public function index()
{
$page = Input::get('page');
$type = Input::get('type');
$filepicker = Input::get('filepicker');

if ($parent) {
$itemsPerPage = 100;
$data = $this->repository->byPageFrom($page, $itemsPerPage, $parent, array('translations'), true, $type);
} else {
$itemsPerPage = 10;
$data = $this->repository->byPageFrom($page, $itemsPerPage, null, array('translations'), true, $type);
}
$itemsPerPage = 10;
$data = $this->repository->byPageFrom($page, $itemsPerPage, null, array('translations'), true, $type);

$models = Paginator::make($data->items, $data->totalItems, $itemsPerPage);

if ($parent) {
$this->layout->content = View::make('files.admin.index')
->withModels($models)
->withParent($parent);
} else if ($filepicker) {
return View::make('files.admin.filepicker')
if ($filepicker) {
$this->layout->content = View::make('files.admin.filepicker')
->withModels($models);
} else {
$this->layout->content = View::make('files.admin.all')
$this->layout->content = View::make('files.admin.index')
->withModels($models);
}

}

/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create($parent)
public function create()
{
$model = $this->repository->getModel();
$this->title['child'] = trans('files::global.New');
$this->layout->content = View::make('files.admin.create')
->withModel($model)
->withParent($parent);
->withModel($model);
}

/**
Expand All @@ -76,12 +68,11 @@ public function create($parent)
* @param int $id
* @return Response
*/
public function edit($parent, $model)
public function edit($model)
{
$this->title['child'] = trans('files::global.Edit');
$this->layout->content = View::make('files.admin.edit')
->withModel($model)
->withParent($parent);
->withModel($model);
}

/**
Expand All @@ -90,33 +81,38 @@ public function edit($parent, $model)
* @param int $id
* @return Response
*/
public function show($parent, $model)
public function show($model)
{
return Redirect::route('admin.' . $parent->route . '.files.edit', array($parent->id, $model->id));
return Redirect::route('admin.files.edit', array($model->id));
}

/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store($parent)
public function store()
{

if ($model = $this->form->save(Input::all())) {

if (Request::ajax()) {
echo json_encode(array('id' => $model->id));
exit();
}

return (Input::get('exit')) ? Redirect::route('admin.' . $parent->route . '.files.index', $parent->id) : Redirect::route('admin.' . $parent->route . '.files.edit', array($parent->id, $model->id)) ;
if (Input::get('exit')) {
return Redirect::route('admin.files.index');
}
return Redirect::route('admin.files.edit', array($model->id));

}

if (Request::ajax()) {
return Response::json('error', 400);
}

return Redirect::route('admin.' . $parent->route . '.files.create', $parent->id)
return Redirect::route('admin.files.create')
->withInput()
->withErrors($this->form->errors());

Expand All @@ -128,16 +124,16 @@ public function store($parent)
* @param int $id
* @return Response
*/
public function update($parent, $model)
public function update($model)
{

Request::ajax() and exit($this->repository->update(Input::all()));

if ($this->form->update(Input::all())) {
return (Input::get('exit')) ? Redirect::route('admin.' . $parent->route . '.files.index', $parent->id) : Redirect::route('admin.' . $parent->route . '.files.edit', array($parent->id, $model->id)) ;
return (Input::get('exit')) ? Redirect::route('admin.files.index') : Redirect::route('admin.files.edit', array($model->id)) ;
}

return Redirect::route('admin.' . $parent->route . '.files.edit', array($parent->id, $model->id))
return Redirect::route('admin.files.edit', array($model->id))
->withInput()
->withErrors($this->form->errors());
}
Expand All @@ -159,7 +155,7 @@ public function sort()
* @param int $id
* @return Response
*/
public function destroy($parent, $model)
public function destroy($model)
{
if ($this->repository->delete($model)) {
if (! Request::ajax()) {
Expand Down
10 changes: 0 additions & 10 deletions app/TypiCMS/Modules/Files/Models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class File extends Base
protected $presenter = 'TypiCMS\Modules\Files\Presenters\FilePresenter';

protected $fillable = array(
'fileable_id',
'fileable_type',
'folder_id',
'user_id',
'type',
Expand Down Expand Up @@ -61,14 +59,6 @@ class File extends Base
public $order = 'position';
public $direction = 'asc';

/**
* Polymorphic relation.
*/
public function fileable()
{
return $this->morphTo();
}

/**
* Observers
*/
Expand Down
31 changes: 0 additions & 31 deletions app/TypiCMS/Modules/Files/Repositories/CacheDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,4 @@ public function byPageFrom($page = 1, $limit = 10, $from = null, array $with = a
return $models;

}

/**
* Create a new model
*
* @param array Data to create a new object
* @return boolean or model
*/
public function create(array $data)
{
$model = $this->repo->create($data);
if ($model) {
$parent = str_plural(class_basename($model->fileable_type));
$this->cache->flush('Files', $parent, 'Dashboard');
}

return $model;
}

/**
* Delete a file
*
* @param File model to delete
* @return bool
*/
public function delete($model)
{
$parent = str_plural(class_basename($model->fileable_type));
$this->cache->flush('Files', $parent, 'Dashboard');

return $this->repo->delete($model);
}
}
8 changes: 2 additions & 6 deletions app/TypiCMS/Modules/Files/Repositories/EloquentFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ public function byPageFrom($page = 1, $limit = 10, $from = null, array $with = a

$query = $this->make($with);

if ($from) {
$query->where('fileable_id', $from->id)
->where('fileable_type', get_class($from));
}
if ($type) {
$query->where('type', $type);
}
Expand Down Expand Up @@ -88,7 +84,7 @@ public function delete($model)
public function create(array $data)
{
if (isset($data['file']) and $data['file']) {
$path = 'uploads/' . str_plural(strtolower(class_basename($data['fileable_type'])));
$path = 'uploads/';
$file = FileUpload::handle($data['file'], $path);
$data = array_merge($data, $file);
}
Expand All @@ -114,7 +110,7 @@ public function create(array $data)
public function update(array $data)
{
if (isset($data['file']) and $data['file']) {
$path = 'uploads/' . str_plural(strtolower(class_basename($data['fileable_type'])));
$path = 'uploads/';
$file = FileUpload::handle($data['file'], $path);
$data = array_merge($data, $file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class FileFormLaravelValidator extends AbstractLaravelValidator
* @var Array
*/
protected $rules = array(
'file' => 'mimes:jpeg,gif,png,pdf|max:2000',
'fileable_id' => 'required',
'fileable_type' => 'required',
'file' => 'mimes:jpeg,gif,png,pdf|max:2000',
);
}
4 changes: 1 addition & 3 deletions app/TypiCMS/Modules/Files/Views/files/admin/_form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
<div class="form-group col-sm-12">
<button class="btn-primary btn" type="submit">@lang('validation.attributes.save')</button>
<button class="btn-primary btn" value="true" id="exit" name="exit" type="submit">@lang('validation.attributes.save and exit')</button>
<a href="{{ route('admin.' . $parent->route . '.files.index', $parent->id) }}" class="btn btn-default">@lang('validation.attributes.exit')</a>
<a href="{{ route('admin.files.index') }}" class="btn btn-default">@lang('validation.attributes.exit')</a>
</div>

{{ Form::hidden('id'); }}
{{ Form::hidden('fileable_id', $parent->id); }}
{{ Form::hidden('fileable_type', get_class($parent)); }}

<div class="col-sm-6">

Expand Down
35 changes: 0 additions & 35 deletions app/TypiCMS/Modules/Files/Views/files/admin/all.blade.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@section('main')

{{ Form::open( array( 'files' => true, 'route' => array('admin.' . $parent->route . '.files.index', $parent->id), 'method' => 'post', 'role' => 'form' ) ) }}
{{ Form::open( array( 'files' => true, 'route' => array('admin.files.index'), 'method' => 'post', 'role' => 'form' ) ) }}
@include('files.admin._form')
{{ Form::close() }}

Expand Down
2 changes: 1 addition & 1 deletion app/TypiCMS/Modules/Files/Views/files/admin/edit.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@section('main')

{{ Form::model( $model, array( 'files' => true, 'route' => array('admin.' . $parent->route . '.files.update', $model->fileable_id, $model->id), 'method' => 'patch', 'role' => 'form' ) ) }}
{{ Form::model( $model, array( 'files' => true, 'route' => array('admin.files.update', $model->id), 'method' => 'patch', 'role' => 'form' ) ) }}
@include('files.admin._form')
{{ Form::close() }}

Expand Down
Loading

0 comments on commit 1c5246e

Please sign in to comment.