csDoctrineActAsAttachablePlugin
1.0.1stable
for sf 1.2 and Doctrine
MIT
This plugin allows you to associate various uploads with multiple modules. Include the attachable module to allow for inline uploading in your application. You can customize by type, add custom validation, etc. Attach anything from uploads to links to other modules.
Sponsored By Centre{source} interactive agency
Developers
| Name |
Status |
Email |
Brent Shaffer |
lead |
moc.ecruosertnec <<ta>> reffahsb
|
License
Copyright (c) 2009 Brent Shaffer
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
csActAsAttachablePlugin
Introduction
This behavior permits to attach files to Doctrine objects.
Features
- add/remove attached files on an object / multiple object classes
- validate uploaded files
- validate according to file type
- supports the module csAttachable that allows for ajax includes to upload and add files to a given class
Supported filetypes are the following:
- image
- audio
- video
- document
- application
- custom
- other
The type "other" represents the base filetype, and is not validated by mimetype.
Others can be set accordingly.
Implementation
add the following code to your module's schema file:
MyModel:
actAs: [Attachable]
csAttachable module
To use csAttachable module, add csAttachable to your settings.yml under "enabled_modules" and
add the following code to your template:
<?php include_component('csAttachable', 'attachments', array('form' => $form)) ?>
where $this->form represents the Doctrine Form for whatever class is acting as attachable. It is important to note
the plugin does not work if the passed $this->form does not carry an object already saved to the database.
For this reason, the component should not be included unless the form object exists, for example:
<?php if ($form->getObject()->getId()): ?>
<?php include_component('csAttachable', 'attachments', array('form' => $form)) ?>
<?php endif; ?>
Also supported is the csAttachable attachments_list component, which receives an object variable and outputs
a UL of attachments and their links. This is handy for frontend development / archive reasons, and also
serves as an example for how attachments should be accessed / displayed. Use the following code to access
this component:
<?php include_component('csAttachable', 'attachments_list', array('object' => $object)) ?>
Where $object is an item acting as Attachable
Integrating with the backend
After completing the steps above, you will want to override the _form.php partial in your backend module.
Copy all of the code from the same generated module in your project cache. Then, add the form code above after the tag.
At the end, your _form.php partial should look something like this:
<?php include_stylesheets_for_form($form) ?>
<?php include_javascripts_for_form($form) ?>
<div class="sf_admin_form">
<?php echo form_tag_for($form, '@mymodel') ?>
<?php echo $form->renderHiddenFields() ?>
<?php if ($form->hasGlobalErrors()): ?>
<?php echo $form->renderGlobalErrors() ?>
<?php endif; ?>
<?php foreach ($configuration->getFormFields($form, $form->isNew() ? 'new' : 'edit') as $fieldset => $fields): ?>
<?php include_partial('mymodel/form_fieldset', array('mymodel' => $mymodel, 'form' => $form, 'fields' => $fields, 'fieldset' => $fieldset)) ?>
<?php endforeach; ?>
<?php include_partial('mymodel/form_actions', array('mymodel' => $mymodel, 'form' => $form, 'configuration' => $configuration, 'helper' => $helper)) ?>
</form>
<?php if ($form->getObject()->getId()): ?>
<?php include_component('csAttachable', 'attachments', array('form' => $form)) ?>
<?php endif; ?>
</div>
Make sure your upload directory has proper permissions set. If you need to debug, unhide the iframe to see the errors being thrown.
Also, be sure to publish your assets ./symfony plugin:publish-assets to pull in the default attachable styles
Other available methods include
- $model->getAttachments(); Returns a doctrine collection object of related attachemnts
- $model->addAttachment($attachment) where $attachment is an Attachment object
- $model->addAttachments($attachments) where $attachments is an array or collection of Attachment objects.
- $attachment->getObject(); where $attachment is the attachment object returning its related model
Other Notes
Using the included partial, Attachment objects are hashed and placed in uploads/(model)/(file) where (model) is
the model acting as Attachable. The Attachment object is then assigned the previous filename to it's [name] property.
Array accessors are not yet available for ActAsAttachable. For example, $model['Attachments'] will return an error.
To Do
- Add automated mimetype validation for specified types
- Allow relationships specified in schema.yml to validate for specific types
- Allow multiple attachable types in schema.yml (for example, UserProfile has Video and Image attachments for Video and Photo galleries)
- Have automatic mimetype/extension parsing to determine attachment's type