2017 © Pedro Peláez
 

cakephp-plugin cake-attachments

File Attachments plugin for CakePHP

image

codekanzlei/cake-attachments

File Attachments plugin for CakePHP

  • Monday, July 16, 2018
  • by cleptric
  • Repository
  • 5 Watchers
  • 3 Stars
  • 9,670 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 5 Forks
  • 3 Open issues
  • 24 Versions
  • 5 % Grown

The README.md

CakePHP 3 cake-attachments

License, (*1)

Requirements

You can find the requirements in composer.json., (*2)

  • ImageMagick for resizing images
  • cake-frontend-bridge for easy access to the current controller and action derived from the URL
  • ghostscript for pdf previews. On Mac OS X, you can install ghostscript via homebrew:, (*3)

    `brew install ghostscript`
    

CakePHP 3 File Attachments Handling, (*4)

Note: This Plugin depends on the codekanzlei/cake-frontend-bridge Plugin., (*5)

Installation

1. require the plugin in your composer.json

    "require": {
        ...
        "codekanzlei/cake-attachments": "dev-master",
        ...
    }

2. Include the plugin using composer

Open a terminal in your project directory and run the following command:, (*6)

$ composer update

Setup & Configuration

1. Load the plugin in your config/bootstrap.php

Plugin::load('Attachments', ['bootstrap' => false, 'routes' => true]);

Also be sure to add the cake-frontend-bridge since it is required for this plugin to work properly., (*7)

Plugin::load('FrontendBridge', ['bootstrap' => false, 'routes' => true, 'autoload' => true]);

2. Create a table attachments in your project database

Run the following sql-query on your project database. You can find it in the Plugin's config/schema.sql file., (*8)

CREATE TABLE `attachments` (
  `id` char(36) NOT NULL,
  `filepath` varchar(255) NOT NULL,
  `filename` varchar(255) NOT NULL,
  `filetype` varchar(45) NOT NULL,
  `filesize` int(10) NOT NULL,
  `model` varchar(255) NOT NULL,
  `foreign_key` char(36) NOT NULL,
  `tags` text,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

3. Create additional folders in your project folder

Open a terminal in your project directory and run these commands:, (*9)

$ mkdir -p tmp/uploads
$ mkdir -p app_data/attachments

You might have to change the folder permissions for these folders depending on your environment. The application must have permissions to read and write data into them., (*10)

4. Adding JavaScript files to your project

In your webroot/js/app/app_controller.js, add the following key to the baseComponents Array:, (*11)

'Attachments'

This grants the project permission to use the plugin's .js files, (*12)

5. Adding Attachments to your project

In your config/app.php, add the following key:, (*13)

'Attachments' => [
    'tmpUploadsPath' => ROOT . '/tmp/uploads/',
    'path' => ROOT . '/app_data/attachments/',
    'acceptedFileTypes' => '/\.(jpe?g|png)$/i',
    'autorotate' => false
],

Further possible filetypes you want to allow can be specified in the 'acceptedFileTypes' filed, such as gif|jpe?g|png|pdf|docx|doc|xls|xlsx|tif|tiff|zip, (*14)

When setting autorotate to true, views and previews of picture attachments will be rotated depending on their EXIF data., (*15)

6. Adding AttachmentHelper to your project

In your /serc/Controller/AppController.php, add the following keys to the public $helpers Array:, (*16)

'Attachments.Attachments',

As the cake-frontend-bridge Plugin is required for the Attachments Plugin to work properly, some further configuarion is needed. Add the following key to the $helpers Array:, (*17)

'FrontendBridge' => ['className' => 'FrontendBridge.FrontendBridge'],

Use the FrontendBridge in your AppController extends Controller:, (*18)

use \FrontendBridge\Lib\FrontendBridgeTrait;

Lastly, add the FrontendBridge-key to public $components, (*19)

'FrontendBridge.FrontendBridge',

7. Include Attachments in your default layout

In your src/Template/Layout/default.ctp, you need to create a new div element that contains the UI-elements of the Attachments Plugin., (*20)

<div class="<?php echo $this->FrontendBridge->getMainContentClasses() ?>">

</div>

Note: Make sure that the line containing <?= $this->fetch('content') ?> is a child-element of this <div>-Element., (*21)

Usage

1. Setting up a Model

Go to the table you want use the Attachments plugin in. For example, if you want to be able to attach files to your Users, go to /Model/Table/UsersTable.php and add the following line to its initialize() callback method:, (*22)

$this->addBehavior('Attachments.Attachments');

2. Setting up an Entity

In your Entity (if we stick to the Users-example above this would be Model/Entity/User.php), make sure you add attachments and attachment_uploads to your $_accessible property like so:, (*23)

protected $_accessible = [
    'attachments' => true,
    'attachment_uploads' => true
];

attachment_uploads is the default form field name, which you can change via the Helper's and Behavior's options., (*24)

3. Setting up a Controller

Be sure to contain Attachments stored with this plugin in your Controllers., (*25)

If we stick to the Users-example above, your Controller/UsersController.php might look something like this:, (*26)

public function edit($id = null)
{
    $user = $this->Users->get($id, [
        'contain' => ['Attachments']
    ])

4. Setting up a view

In your Forms, use the AttachmentsHelper to create an attachments area:, (*27)

echo $this->Attachments->attachmentsArea($entity, [
    'label' => 'File Attachments',
    'formFieldName' => 'attachment_uploads'
]);

The Helper will automatically add CSS and JS dependencies to your script and css view blocks. If you don't want that, you can disable this behavior by setting includeDependencies to false in the Helper's config., (*28)

See AttachmentsHelper::addDependencies() for the JS/CSS dependencies you need to include., (*29)

Authorization

If you would like to restrict access to Attachments based on custom logic, you can pass a callback function to the Behavior config., (*30)

$this->addBehavior('Attachments.Attachments', [
    'downloadAuthorizeCallback' => function (Attachment $attachment, EntityInterface $relatedEntity, Request $request) {
        return false;
    }
]);

This callback prevents previewing, viewing, downloading, deleting and manipulating attachments., (*31)

The Versions

16/07 2018

dev-2.next

dev-2.next

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

16/07 2018

dev-replace-invalid-characters-in-filename

dev-replace-invalid-characters-in-filename

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

06/02 2018

dev-replace-invalid-caharacters-in-filename

dev-replace-invalid-caharacters-in-filename

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

02/02 2018

dev-missing-call

dev-missing-call

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

02/02 2018

dev-master

9999999-dev

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

02/02 2018

v1.1.2

1.1.2.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

01/02 2018

dev-feature/add-parent-call

dev-feature/add-parent-call

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

24/05 2017

v1.1.1

1.1.1.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

24/05 2017

v1.1.0

1.1.0.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

02/12 2016

v1.0.12

1.0.12.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

14/11 2016

v1.0.11

1.0.11.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

11/11 2016

dev-update-branch

dev-update-branch

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

15/09 2016

v1.0.10

1.0.10.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

25/08 2016

v1.0.9

1.0.9.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

19/07 2016

v1.0.8

1.0.8.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

19/07 2016

v1.0.7

1.0.7.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

20/06 2016

v1.0.6

1.0.6.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

07/06 2016

v1.0.5

1.0.5.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

 

The Development Requires

21/03 2016

v1.0.4

1.0.4.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

  • ext-imagick *
  • php >=5.4

 

The Development Requires

04/02 2016

v1.0.3

1.0.3.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

  • ext-imagick *
  • php >=5.4

 

The Development Requires

04/11 2015

v1.0.2

1.0.2.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

  • ext-imagick *
  • php >=5.4

 

The Development Requires

24/04 2015

v1.0.1

1.0.1.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

  • ext-imagick *
  • php >=5.4

 

The Development Requires

27/03 2015

v1.0.0

1.0.0.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

  • ext-imagick *
  • php >=5.4

 

The Development Requires

10/02 2015

v0.9.0

0.9.0.0

File Attachments plugin for CakePHP

  Sources   Download

The Requires

  • ext-imagick *
  • php >=5.4

 

The Development Requires