Permalink
Browse files

Documents and Categories

  • Loading branch information...
bpocallaghan committed Oct 21, 2017
1 parent 7796bb5 commit 2164ebe0d7b6696714ed560636e71a2a31cf8c1c
@@ -0,0 +1,87 @@
<?php
namespace App\Http\Controllers\Admin\Documents;
use Redirect;
use App\Http\Requests;
use App\Models\DocumentCategory;
use Illuminate\Http\Request;
use App\Http\Controllers\Admin\AdminController;
class CategoriesController extends AdminController
{
/**
* Display a listing of document_category.
*
* @return Response
*/
public function index()
{
save_resource_url();
return $this->view('documents.categories.index')->with('items', DocumentCategory::all());
}
/**
* Show the form for creating a new document_category.
*
* @return Response
*/
public function create()
{
return $this->view('documents.categories.create_edit');
}
/**
* Store a newly created document_category in storage.
*
* @return Response
*/
public function store()
{
$attributes = request()->validate(DocumentCategory::$rules, DocumentCategory::$messages);
$category = $this->createEntry(DocumentCategory::class, $attributes);
return redirect_to_resource();
}
/**
* Show the form for editing the specified document_category.
*
* @param DocumentCategory $category
* @return Response
*/
public function edit(DocumentCategory $category)
{
return $this->view('documents.categories.create_edit')->with('item', $category);
}
/**
* Update the specified document_category in storage.
*
* @param DocumentCategory $category
* @return Response
*/
public function update(DocumentCategory $category)
{
$attributes = request()->validate(DocumentCategory::$rules, DocumentCategory::$messages);
$category = $this->updateEntry($category, $attributes);
return redirect_to_resource();
}
/**
* Remove the specified document_category from storage.
*
* @param DocumentCategory $category
* @return Response
*/
public function destroy(DocumentCategory $category)
{
$this->deleteEntry($category, request());
return redirect_to_resource();
}
}
@@ -0,0 +1,138 @@
<?php
namespace App\Http\Controllers\Admin\Documents;
use App\Models\DocumentCategory;
use Illuminate\Http\UploadedFile;
use Redirect;
use App\Http\Requests;
use App\Models\Document;
use Illuminate\Http\Request;
use App\Http\Controllers\Admin\AdminController;
class DocumentsController extends AdminController
{
/**
* Display a listing of document.
*
* @return Response
*/
public function index()
{
save_resource_url();
$items = Document::with('documentable')->get();
return $this->view('documents.index')->with('items', $items);
}
/**
* Show the Documentable's photos
* Create / Edit / Delete the photos
* @param $documentable
* @param $documents
* @return mixed
*/
private function showDocumentable($documentable, $documents)
{
save_resource_url();
return $this->view('documents.create_edit')
->with('documentable', $documentable)
->with('documents', $documents);
}
/**
* Show the category's documents
* @param DocumentCategory $category
* @return mixed
*/
public function showCategory(DocumentCategory $category)
{
$documents = $category->documents;
return $this->showDocumentable($category, $documents);
}
/**
* Upload a new photo to the album
* @return \Illuminate\Http\JsonResponse
*/
public function upload()
{
// upload the photo here
$attributes = request()->validate(Document::$rules);
// get the documentable
$documentable = input('documentable_type')::find(input('documentable_id'));
if (!$documentable) {
return json_response_error('Whoops', 'We could not find the documentable.');
}
// move and create the photo
$photo = $this->moveAndCreateDocument($attributes['file'], $documentable);
if (!$photo) {
return json_response_error('Whoops', 'Something went wrong, please try again.');
}
return json_response(['id' => $photo->id]);
}
/**
* Update the photo's name
* @param Document $document
* @return \Illuminate\Http\JsonResponse
*/
public function updateName(Document $document)
{
$document->update(['name' => input('name')]);
return json_response();
}
/**
* Remove the specified photo from storage.
*
* @param Document $document
* @return Response
*/
public function destroy(Document $document)
{
$this->deleteEntry($document, request());
return redirect_to_resource();
}
/**
* Move document to /uploads/documents
* @param UploadedFile $file
* @param $documentable
* @return \Illuminate\Http\JsonResponse|static
*/
private function moveAndCreateDocument(UploadedFile $file, $documentable)
{
$name = token();
$extension = '.' . $file->extension();
$filename = $name . $extension;
$file->move(upload_path('documents'), $filename);
// file not moved
if (!\File::exists(upload_path('documents') . $filename)) {
return false;
}
$originalName = $file->getClientOriginalName();
$originalName = substr($originalName, 0, strpos($originalName, $extension));
$name = strlen($originalName) <= 2 ? $documentable->name : $originalName;
$photo = Document::create([
'filename' => $filename,
'documentable_id' => $documentable->id,
'documentable_type' => get_class($documentable),
'name' => strlen($name) < 2 ? 'Document Name' : $name,
]);
return $photo;
}
}
@@ -20,8 +20,32 @@ class Document extends Model
protected $guarded = ['id'];
/**
* Validation rules for this model
*/
static public $rules = [
'file' => 'required|file|max:5000|mimes:pdf'
];
public function getUrlAttribute()
{
//return "/uploads/documents/{$this->filename}";
// for summernote - 'full' url
return url("/uploads/documents/{$this->filename}");
}
public function documentable()
{
return $this->morphTo();
}
/**
* Get all the rows as an array (ready for dropdowns)
*
* @return array
*/
public static function getAllList()
{
return self::with('documentable')->orderBy('name')->get()->pluck('name', 'id')->toArray();
}
}
@@ -0,0 +1,38 @@
<?php
namespace App\Models;
use App\Models\Traits\Documentable;
use Titan\Models\TitanCMSModel;
use Bpocallaghan\Sluggable\HasSlug;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class DocumentCategory
* @mixin \Eloquent
*/
class DocumentCategory extends TitanCMSModel
{
use SoftDeletes, HasSlug, Documentable;
protected $table = 'document_categories';
protected $guarded = ['id'];
/**
* Validation rules for this model
*/
static public $rules = [
'name' => 'required|min:3:max:255',
];
/**
* Get all the rows as an array (ready for dropdowns)
*
* @return array
*/
public static function getAllList()
{
return self::orderBy('name')->get()->pluck('name', 'id')->toArray();
}
}
@@ -0,0 +1,36 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDocumentCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('document_categories', function (Blueprint $table) {
$table->increments('id')->unique()->index();
$table->string('name')->index();
$table->string('slug');
$table->timestamps();
$table->softDeletes();
$table->integer('created_by')->unsigned();
$table->integer('updated_by')->unsigned()->nullable();
$table->integer('deleted_by')->unsigned()->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('document_categories');
}
}
@@ -0,0 +1,40 @@
@extends('layouts.admin')
@section('content')
<div class="row">
<div class="col-xs-12">
<div class="box box-primary box-solid">
<div class="box-header with-border">
<h3 class="box-title">
<span><i class="fa fa-edit"></i></span>
<span>{{ isset($item)? 'Edit the ' . $item->name . ' entry': 'Create a new Document Category' }}</span>
</h3>
</div>
<div class="box-body no-padding">
@include('admin.partials.info')
<form method="POST" action="{{$selectedNavigation->url . (isset($item)? "/{$item->id}" : '')}}" accept-charset="UTF-8">
<input name="_token" type="hidden" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="{{isset($item)? 'PUT':'POST'}}">
<fieldset>
<div class="row">
<div class="col-md-12">
<div class="form-group {{ form_error_class('name', $errors) }}">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Please insert the Name" value="{{ ($errors && $errors->any()? old('name') : (isset($item)? $item->name : '')) }}">
{!! form_error_message('name', $errors) !!}
</div>
</div>
</div>
</fieldset>
@include('admin.partials.form_footer')
</form>
</div>
</div>
</div>
</div>
@endsection
Oops, something went wrong.

0 comments on commit 2164ebe

Please sign in to comment.