I've been struggling for two days to understand the Media Plugin.
Intend to use it to deal with all media files, mostly images and pdfs,
for this project I'm developing.

I still have doubts on how I'm gonna do a lot of stuff with the plugin
but so far I'd like to share an example with you and hear your
thoughts about it.

Here is a minified context of the app:

I have a News model which will need at least two different images with
different sizes attached to it.
There's also a Download model which will need an image, using size
settings different from the one on news model, and a pdf file.

To be able to have different validations settings and image size
settings I first changed the attachments element. When calling it I
added a group variable to it. For example:

echo $this->element('attachments',
                    array('plugin' => 'Media',
                          'assocAlias' => 'Attachment.0',
                          'model' => 'Download',
                          'label' => 'Image',
                          'group' => 'img'));
echo $this->element('attachments',
                    array('plugin' => 'Media',
                          'assocAlias' => 'Attachment.1',
                          'model' => 'Download',
                          'label' => 'Pdf File',
                          'group' => 'pdf'));

This way I am able to have custom validation rules on model for each
type of file and then give differents responses for the user on each
input instance.

On attachments model I added this beforeValidate function where I
intend to write custom settings for each upload case on the
application.

function beforeValidate(){

    if($this->data['Attachment']['model'] === 'Download'){
        $m = array('convert' => 'image/jpeg', 'fitCrop' => array(150,
210));
        Configure::write('Media.filter', array('image' =>
compact('m')));
    }

    if($this->data['Attachment']['group'] === 'pdf'){
        $this->validate = array(
            'file' => array(
                'size'       => array(
                    'rule' => array('checkSize', '2M'),
                ),
                'extension'  => array(
                    'rule' => array('checkExtension', false,
array('pdf')),
                ),
                'mimeType'   => array(
                    'rule' => array('checkMimeType', false,
                                array('application/pdf')),
                )
            ),
        );
        $this->Behaviors->detach('Media.Generator');
    }else{
        $this->validate = array(
            'file' => array(
                'size'       => array(
                    'rule' => array('checkSize', '1M'),
                ),
                'extension'  => array(
                    'rule' => array('checkExtension', false,
array('jpg', 'jpeg', 'png','gif')),
                ),
                'pixels'     => array(
                    'rule' => array('checkPixels', '1600x1600'),
                ),
                'mimeType'   => array(
                    'rule' => array('checkMimeType', false,
                                array('image/jpeg', 'image/png',
'image/gif')),
                )
            ),
        );
        $this->Behaviors->attach('Media.Generator');
    }
    return true;
}



I'd really like to hear what you guys think of the approach used
above. I made these conditions on a beforeValidate function because I
couldn't figure out a way to change input file names and still make
the plugin auto magically upload the files and save them on a
database. I'm also wondering that if this Attachments model get too
huge of conditions I'd better create one model for each attachment
related model to let validations and other settings more clear.

Also found the architecture the plugin really fantastic, it'd take a
life for a begginer cakephp developer and object stuff student to
develop something like that.

Hope my english was good enough to make my point.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to