Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
passchn committed Jan 21, 2022
0 parents commit 04fb749
Show file tree
Hide file tree
Showing 18 changed files with 1,288 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/composer.lock
/composer.phar
/phpunit.xml
/.phpunit.result.cache
/phpunit.phar
/config/Migrations/schema-dump-default.lock
/vendor/
/.idea/
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Assets plugin for CakePHP

## Installation

You can install this plugin into your CakePHP application using [composer](https://getcomposer.org).

The recommended way to install composer packages is:

```
composer require passchn/cakephp-assets
```
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "passchn/cakephp-assets",
"description": "Asset management plugin for CakePHP",
"type": "cakephp-plugin",
"license": "MIT",
"require": {
"php": ">=8.0",
"cakephp/cakephp": "^4.3",
"josegonzalez/cakephp-upload": "^6.0",
"league/csv": "^9.8",
"nette/finder": "^2.5",
"nette/utils": "^3.2",
"intervention/image": "^2.7"
},
"require-dev": {
"phpunit/phpunit": "^8.5 || ^9.3"
},
"autoload": {
"psr-4": {
"Test\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Test\\Test\\": "tests/",
"Cake\\Test\\": "vendor/cakephp/cakephp/tests/"
}
}
}
83 changes: 83 additions & 0 deletions config/Migrations/20220121091709_AssetsAddAssetsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);

use Migrations\AbstractMigration;

class AssetsAddAssetsTable extends AbstractMigration
{
/**
* Up Method.
*
* More information on this method is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-up-method
* @return void
*/
public function up()
{
$this->table('assets_assets', ['id' => false, 'primary_key' => ['id']])
->addColumn('id', 'uuid', [
'default' => '',
'limit' => null,
'null' => false,
])
->addColumn('title', 'string', [
'default' => '',
'limit' => 255,
'null' => false,
])
->addColumn('description', 'text', [
'default' => null,
'limit' => null,
'null' => true,
])
->addColumn('category', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
])
->addColumn('filename', 'string', [
'default' => '',
'limit' => 255,
'null' => false,
])
->addColumn('directory', 'string', [
'default' => '',
'limit' => 255,
'null' => false,
])
->addColumn('mimetype', 'string', [
'default' => '',
'limit' => 255,
'null' => false,
])
->addColumn('filesize', 'string', [
'default' => '',
'limit' => 255,
'null' => false,
])
->addColumn('created', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->addColumn('modified', 'timestamp', [
'default' => null,
'limit' => null,
'null' => true,
])
->create();
}

/**
* Down Method.
*
* More information on this method is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-down-method
* @return void
*/
public function down()
{

$this->table('assets_assets')->drop()->save();
}
}
13 changes: 13 additions & 0 deletions config/app_assets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
/**
* Configuration for the Assets Plugin
*/
'AssetsPlugin' => [
'AssetsTable' => [
'DisplayField' => 'title',
'Behaviors' => [],
]
],
];
16 changes: 16 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;

return static function (RouteBuilder $routes) {
$routes->plugin(
'Assets',
['path' => '/assets'],
function (RouteBuilder $routes) {
$routes->setRouteClass(DashedRoute::class);

$routes->fallbacks(DashedRoute::class);
});
};

18 changes: 18 additions & 0 deletions src/Controller/AppController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);

namespace Assets\Controller;

use App\Controller\AppController as BaseController;
use Assets\Model\Table\AssetsAssetsTable;

class AppController extends BaseController
{
protected AssetsAssetsTable $Assets;

public function initialize(): void
{
parent::initialize();
$this->Assets = $this->fetchTable('Assets.AssetsAssets');
}
}
56 changes: 56 additions & 0 deletions src/Controller/AssetsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);

namespace Assets\Controller;

use Assets\Controller\AppController;
use function __;

/**
* Assets Controller
*
* @method \Cake\ORM\Entity[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
*/
class AssetsController extends AppController
{
/**
* View method
*
* @param string|null $id Asset id.
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$asset = $this->Assets->get($id, [
'contain' => [],
]);

$this->set(compact('asset'));
}

/**
* @return void
*
* Add ?download=1 to URL to download instead of view the file.
*/
public function download(string $id)
{
$asset = $this->Assets->get($id);

$disposition = $this->getRequest()->getQuery('download') ? 'attachment' : 'inline';

header("Content-type: " . $asset->mimetype);
header("Content-Disposition: $disposition; filename=\"{$asset->public_filename}\"");
header("Pragma: no-cache");
header("Expires: 0");

try {
echo $asset->read();
} catch (\Exception $e) {
echo __("Die Datei kann nicht geöffnet werden. ");
}

exit;
}
}
132 changes: 132 additions & 0 deletions src/Controller/admin/AssetsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
declare(strict_types=1);

namespace Assets\Controller\admin;

use Assets\Controller\AppController;
use function __;

/**
* Assets Controller
*
* @method \Cake\ORM\Entity[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
*/
class AssetsController extends AppController
{
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
$assets = $this->paginate($this->Assets);

$this->set(compact('assets'));
}

/**
* View method
*
* @param string|null $id Asset id.
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$asset = $this->Assets->get($id, [
'contain' => [],
]);

$this->set(compact('asset'));
}

/**
* Add method
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$asset = $this->Assets->newEmptyEntity();
if ($this->request->is('post')) {
$asset = $this->Assets->patchEntity($asset, $this->request->getData());
if ($this->Assets->save($asset)) {
$this->Flash->success(__('The asset has been saved.'));

return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The asset could not be saved. Please, try again.'));
}
$this->set(compact('asset'));
}

/**
* Edit method
*
* @param string|null $id Asset id.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function edit($id = null)
{
$asset = $this->Assets->get($id, [
'contain' => [],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$asset = $this->Assets->patchEntity($asset, $this->request->getData());
if ($this->Assets->save($asset)) {
$this->Flash->success(__('The asset has been saved.'));

return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The asset could not be saved. Please, try again.'));
}
$this->set(compact('asset'));
}

/**
* Delete method
*
* @param string|null $id Asset id.
* @return \Cake\Http\Response|null|void Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$asset = $this->Assets->get($id);
if ($this->Assets->delete($asset)) {
$this->Flash->success(__('The asset has been deleted.'));
} else {
$this->Flash->error(__('The asset could not be deleted. Please, try again.'));
}

return $this->redirect(['action' => 'index']);
}

/**
* @return void
*
* Add ?download=1 to URL to download instead of view the file.
*/
public function download(string $id)
{
$asset = $this->Assets->get($id);

$disposition = $this->getRequest()->getQuery('download') ? 'attachment' : 'inline';

header("Content-type: " . $asset->mimetype);
header("Content-Disposition: $disposition; filename=\"{$asset->public_filename}\"");
header("Pragma: no-cache");
header("Expires: 0");

try {
echo $asset->read();
} catch (\Exception $e) {
echo __("Die Datei kann nicht geöffnet werden. ");
}

exit;
}
}
14 changes: 14 additions & 0 deletions src/Enum/ImageSizes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Assets\Enum;

class ImageSizes
{
const THMB = 75;
const SM = 120;
const MD = 230;
const LG = 520;
const XL = 750;
const XXL = 1250;
const HD = 1920;
}
Loading

0 comments on commit 04fb749

Please sign in to comment.