Attachment plugin for CakePHP.
- CakePHP 2.5+
- cakephp-upload
- jQuery
- Admin attachment CRUD
- Support CKEditor callback
- Dosen't support
cakephp-upload
callback methods likepathMethod
- Attachments are attached/detached only when relation model is saved
Put your app plugin directory as Attachment
.
In 2.0 you need to enable the plugin your app/Config/bootstrap.php file:
<?php
CakePlugin::load('Attachment', array('bootstrap' => false, 'routes' => true));
?>
Enable admin routing in app/Config/core.php file:
<?php
Configure::write('Routing.prefixes', array('admin'));
?>
Update schema:
CREATE TABLE `attacheds` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`model` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`foreign_key` int(11) NOT NULL,
`attachment_id` int(11) NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `INDEX_MODEL_FOREIGN_KEY` (`model`,`foreign_key`)
)
CREATE TABLE `attachments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`attachment` varchar(255) NOT NULL,
`dir` varchar(255) DEFAULT NULL,
`type` varchar(255) DEFAULT NULL,
`size` int(11) NOT NULL DEFAULT '0',
`active` tinyint(1) NOT NULL DEFAULT '1',
`settings` text NOT NULL,
`created` datetime NOT NULL,
`updated` datetime NOT NULL,
PRIMARY KEY (`id`)
)
In controller:
<?php
class AppController extends Controller {
public $helpers = array(
'Attachment.Attachment' => array(
'attachElement' => 'Attachment.Attachment/attach',
'detachElement' => 'Attachment.Attachment/detach'
)
);
}
In relation model example:
<?php
class Foo extends AppModel {
public $hasAndBelongsToMany = array(
'Video' => array(
'className' => 'Attachment.Attachment',
'joinTable' => 'attacheds',
'foreignKey' => 'foreign_key',
'associationForeignKey' => 'attachment_id',
'unique' => 'keepExisting',
'conditions' => array('Attached.model' => array('Video')),
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
),
'Pdf' => array(
'className' => 'Attachment.Attachment',
'joinTable' => 'attacheds',
'foreignKey' => 'foreign_key',
'associationForeignKey' => 'attachment_id',
'unique' => 'keepExisting',
'conditions' => array('Attached.model' => array('Pdf')),
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);
}
In relation model's view example:
echo $this->Attachment->js();
echo $this->Html->script('/Attachment/js/attachment');
Add window.attachmentCallback()
method is called when attachment is selected.
window.attachmentCallback = function(id, data, error) {
if (error != null) {
// Code error
} else {
id = id.replace(/\./g, '\\\\\.');
$('#' + id).html(data.html);
}
}
Add input tag with AttachmentHelper#input();.
<?php
for ($i = 0; $i < 3; $i++) {
$name = $i + 1;
echo $this->Attachment->input(array
'relation' => 'Video',
'model' => 'Video',
'name' => $name
);
}
for ($i = 0; $i < 3; $i++) {
$name = $i + 1;
echo $this->Attachment->input(array
'relation' => 'Pdf',
'model' => 'Pdf',
'name' => $name
);
}
?>
Set upload settings in your app/Config/bootstrap.php file:
<?php
Configure::write('Attachment.uploadSettings.1', array(
'thumbnailSizes' => array(
'xvga' => '1024x768',
'vga' => '640x480',
'thumb' => '80x80',
)
));
?>
Access AttachmentsController#admin_add() with query string containing upload setting key
/admin/attachment/attachments/add?uploadSetting=1
Set validate settings in your app/Config/bootstrap.php file:
<?php
Configure::write('Attachment.validaSettings.1', array(
'uploadError' => array(
'rule' => array('uploadError'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
));
?>
Access AttachmentsController#admin_add() with query string containing validate setting key
/admin/attachment/attachments/add?validateSetting=1
The MIT License (MIT)
Copyright (c) 2014 Toshimasa Oguni