I'm using the cakephp media plugin in my project using the monolithic
style attachments table, i.e. all the attachments go in the one table
with foreign_key, model, group etc. saved with the file details. So my
model looks like:
class ProjectProfile extends AppModel {
var $name = 'ProjectProfile';
var $useDbConfig = 'default';
var $useTable = 'project_profiles';
var $actsAs = array('Media.Transfer', 'Media.Generator');
But I think you'd agree that's a bit cumbersome to have to click
browse for each individual file. The simplist method I could see to to
allow multiple file uploads was to use the HTML5 multiple file section
option - http://bakery.cakephp.org/articles/veganista/2012/01/31/html_5_multip... :
This allows you to shift click in the file browser to select multiple
files then puts the files into an array to save... however, this field
format isn't handled by the media plugin. Also, there'd be no way to
add the model, group etc. fields on the save as far as I could see.
So, does anybody know how I can handle multi file uploads with the
media plugin using the monolithic model? I'm open to all suggestions.
> I'm using the cakephp media plugin in my project using the monolithic > style attachments table, i.e. all the attachments go in the one table > with foreign_key, model, group etc. saved with the file details. So my > model looks like:
> class ProjectProfile extends AppModel {
> var $name = 'ProjectProfile'; > var $useDbConfig = 'default'; > var $useTable = 'project_profiles'; > var $actsAs = array('Media.Transfer', 'Media.Generator');
> Then a saveAll in the controller when saving my record saves the > attached file(s).
> This all works fine, however I'd really like to be able to upload > multiple files at once, which the plugin does support by doing this in > the form:
> But I think you'd agree that's a bit cumbersome to have to click > browse for each individual file. The simplist method I could see to to > allow multiple file uploads was to use the HTML5 multiple file section > option - > http://bakery.cakephp.org/articles/veganista/2012/01/31/html_5_multip... > :
> This allows you to shift click in the file browser to select multiple > files then puts the files into an array to save... however, this field > format isn't handled by the media plugin. Also, there'd be no way to > add the model, group etc. fields on the save as far as I could see.
> So, does anybody know how I can handle multi file uploads with the > media plugin using the monolithic model? I'm open to all suggestions.
> Thanks in advance.
> -- > Our newest site for the community: CakePHP Video Tutorials > http://tv.cakephp.org > Check out the new CakePHP Questions site http://ask.cakephp.org and help > others with their CakePHP related questions.
> To unsubscribe from this group, send email to > cake-php+unsubscribe@googlegroups.com For more options, visit this group > at http://groups.google.com/group/cake-php
-- Paulo de Almeida
Linux User #494076 Ubuntu User # 28289
"In a world without walls who needs windows and gates?"
You could try looping through the files array and saving them manually:
foreach $_FILES['files'] as $file { // create data array containing fields such as a foreignKey, model, etc. and save them as individual records
}
Or, instead of using the multiple option, you can try processing them each as a single request so you don't have to change anything in your controller but rather just how the view presents it. I've used drag and drop jQuery plugins and the like to help with this. It's by far my favorite solution, because it doesn't rely on browser specific features (such as HTML5 multiple upload).
On Sunday, April 15, 2012 5:42:25 PM UTC-7, double07 wrote:
> Hi All,
> I'm using the cakephp media plugin in my project using the monolithic > style attachments table, i.e. all the attachments go in the one table > with foreign_key, model, group etc. saved with the file details. So my > model looks like:
> class ProjectProfile extends AppModel {
> var $name = 'ProjectProfile'; > var $useDbConfig = 'default'; > var $useTable = 'project_profiles'; > var $actsAs = array('Media.Transfer', 'Media.Generator');
> Then a saveAll in the controller when saving my record saves the > attached file(s).
> This all works fine, however I'd really like to be able to upload > multiple files at once, which the plugin does support by doing this in > the form:
> But I think you'd agree that's a bit cumbersome to have to click > browse for each individual file. The simplist method I could see to to > allow multiple file uploads was to use the HTML5 multiple file section > option - > http://bakery.cakephp.org/articles/veganista/2012/01/31/html_5_multip... > :
> This allows you to shift click in the file browser to select multiple > files then puts the files into an array to save... however, this field > format isn't handled by the media plugin. Also, there'd be no way to > add the model, group etc. fields on the save as far as I could see.
> So, does anybody know how I can handle multi file uploads with the > media plugin using the monolithic model? I'm open to all suggestions.
> You could try looping through the files array and saving them manually:
> foreach $_FILES['files'] as $file {
> // create data array containing fields such as a foreignKey, model, etc.
> and save them as individual records
> }
> Or, instead of using the multiple option, you can try processing them each
> as a single request so you don't have to change anything in your controller
> but rather just how the view presents it. I've used drag and drop jQuery
> plugins and the like to help with this. It's by far my favorite solution,
> because it doesn't rely on browser specific features (such as HTML5
> multiple upload).
> On Sunday, April 15, 2012 5:42:25 PM UTC-7, double07 wrote:
> > Hi All,
> > I'm using the cakephp media plugin in my project using the monolithic
> > style attachments table, i.e. all the attachments go in the one table
> > with foreign_key, model, group etc. saved with the file details. So my
> > model looks like:
> > class ProjectProfile extends AppModel {
> > var $name = 'ProjectProfile';
> > var $useDbConfig = 'default';
> > var $useTable = 'project_profiles';
> > var $actsAs = array('Media.Transfer', 'Media.Generator');
> > Then a saveAll in the controller when saving my record saves the
> > attached file(s).
> > This all works fine, however I'd really like to be able to upload
> > multiple files at once, which the plugin does support by doing this in
> > the form:
> > But I think you'd agree that's a bit cumbersome to have to click
> > browse for each individual file. The simplist method I could see to to
> > allow multiple file uploads was to use the HTML5 multiple file section
> > option -
> >http://bakery.cakephp.org/articles/veganista/2012/01/31/html_5_multip...
> > :
> > This allows you to shift click in the file browser to select multiple
> > files then puts the files into an array to save... however, this field
> > format isn't handled by the media plugin. Also, there'd be no way to
> > add the model, group etc. fields on the save as far as I could see.
> > So, does anybody know how I can handle multi file uploads with the
> > media plugin using the monolithic model? I'm open to all suggestions.
> Or, instead of using the multiple option, you can try processing them each
> as a single request so you don't have to change anything in your controller
> but rather just how the view presents it. I've used drag and drop jQuery
> plugins and the like to help with this. It's by far my favorite solution,
> because it doesn't rely on browser specific features (such as HTML5
> multiple upload).
I don't have any templates, but I use file-uploader[1] on a currently active project. It doesn't require jQuery but works pretty nicely.
It sends each upload as a separate request, so write the controller code as if you're receiving one file (like the media plugin examples) and you should be good. This method is preferred, because when javascript is disabled the logic in the controller doesn't need to change will still work (i.e., old school single upload).
On Monday, April 30, 2012 8:09:33 PM UTC-7, double07 wrote: > Hi Jeremy,
> Not having much luck with the saving manually option, do you having > any working examples of drag and drop (jquery) I can look at?
> Thanks,
> -Brett
> On Apr 17, 7:44 am, jeremyharris <funeralm...@gmail.com> wrote:
> > Or, instead of using the multiple option, you can try processing them > each > > as a single request so you don't have to change anything in your > controller > > but rather just how the view presents it. I've used drag and drop jQuery > > plugins and the like to help with this. It's by far my favorite > solution, > > because it doesn't rely on browser specific features (such as HTML5 > > multiple upload).
Well this whole project has made me realise how much I suck at Ajax/JS
etc. I ended up getting this working using cakephp's HTML5 multi file
support and converting that data array to one that the media plugin
would understand (as per Jeremy's first suggestion!).
In case anybody else is looking to do this...
In the view add to your form:
echo $this->Form->input('files.', array('type' => 'file',
'multiple'));
So that'll allow you to select multiple files.
In the controller do something like:
$i = 0;
foreach ($this->request->data['Model']['files'] as $file) {
$this->set($this->request->data['Photo'][$i]['model'] = 'Model');
$this->set($this->request->data['Photo'][$i]['group'] = 'group');
$this->set($this->request->data['Photo'][$i]['file'] = $this->request-
>data['Model']['files'][$i]);
$i++;
}
Then make sure you have saveAll:
if ($this->Model->saveAll($this->request->data)) { ...
I'm going to spend some time sorting it out better and making it as
reusable as possible. This solution is good for me as it's for an
intranet site so I have control over what browser people are using.
And if the users's browser doesn't support html5 then they can still
upload, just one file at a time.
Thanks for your suggestions Jeremy.
On May 1, 11:27 pm, jeremyharris <funeralm...@gmail.com> wrote:
> I don't have any templates, but I use file-uploader[1] on a currently
> active project. It doesn't require jQuery but works pretty nicely.
> It sends each upload as a separate request, so write the controller code as
> if you're receiving one file (like the media plugin examples) and you
> should be good. This method is preferred, because when javascript is
> disabled the logic in the controller doesn't need to change will still work
> (i.e., old school single upload).
> On Monday, April 30, 2012 8:09:33 PM UTC-7, double07 wrote:
> > Hi Jeremy,
> > Not having much luck with the saving manually option, do you having
> > any working examples of drag and drop (jquery) I can look at?
> > Thanks,
> > -Brett
> > On Apr 17, 7:44 am, jeremyharris <funeralm...@gmail.com> wrote:
> > > Or, instead of using the multiple option, you can try processing them
> > each
> > > as a single request so you don't have to change anything in your
> > controller
> > > but rather just how the view presents it. I've used drag and drop jQuery
> > > plugins and the like to help with this. It's by far my favorite
> > solution,
> > > because it doesn't rely on browser specific features (such as HTML5
> > > multiple upload).
Just thought I'd post my slightly cleaner solution to this which makes
use of a modified version of the attachments element in the media
plugin. This allows you to use the attachment multiple times in a form
for when you have different groups of attachments i.e. photos, files,
videos etc. I'm no programmer, just a designer so there's probably a
better way to do it but I don't know it :)
To save me editing too much I'll just use my example of a
ProjectProfile model:
ProjectProfile.php >
<?php
App::uses('AppModel', 'Model');
class ProjectProfile extends AppModel {
var $name = 'ProjectProfile';
var $useDbConfig = 'default';
var $useTable = 'project_profiles';
var $actsAs = array('Media.Transfer',
'Media.Generator'
);
<?php
App::uses('AppController', 'Controller');
class ProjectProfilesController extends AppController {
....
public function admin_edit($id = null) {
$this->ProjectProfile->id = $id;
if (!$this->ProjectProfile->exists()) {
throw new NotFoundException(__('Invalid project profile'));
}
if ($this->request->is('post') || $this->request->is('put')) {
//convert cakephp html5 file upload data array to media plugin
required format
$model = Inflector::classify($this->params['controller']);
foreach ($this->request->data[$model]['assocAlias'] as
$assocAlias) {
$i = 0;
foreach ($this->request->data[$model][$assocAlias.'files'] as
$file) {
$this->set($this->request->data[$assocAlias][$i]['model'] =
$model);
$this->set($this->request->data[$assocAlias][$i]['group'] =
strtolower($assocAlias));
$this->set($this->request->data[$assocAlias][$i]['file'] =
$this->request->data[$model][$assocAlias.'files'][$i]);
$i++;
}
}
if ($this->ProjectProfile->saveAll($this->request->data)) {
$this->Session->setFlash(__('The project profile has been
saved'));
$this->redirect(array('action' => 'admin_edit', $id));
} else {
$this->Session->setFlash(__('The project profile could not be
saved. Please, try again.'));
}
} else {
$this->request->data = $this->ProjectProfile->read(null, $id);
}
$projects = $this->ProjectProfile->Project->find('list',
array('fields' => array('Project.pjID', 'Project.pntitle')));
$this->set(compact('projects'));
}
....
?>
Then in the view/form:
admin_edit.php >
<div class="projectProfiles form">
<?php echo $this->Form->create('ProjectProfile',
array('type'=>'file'));?>
<fieldset>
<legend><?php echo __('Admin Edit Project Profile'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('pjID', array('label' => 'Project',
'options' => $projects));
echo $this->Form->input('description');
echo $this->Form->input('longitude');
echo $this->Form->input('latitude');
echo $this->Form->input('major', array('label' => 'Major Project'));
//The num variable below is just used in the media
attachments element so the controller can loop through the array, so
just make sure you give each of the nums a sequential numerical order.
The assocAlias just sets the group of attachments.
echo $this->element('Media.attachments', array('model' =>
'ProjectProfile', 'assocAlias' => 'Photo', 'num' => 0));
echo $this->element('Media.attachments', array('model' =>
'ProjectProfile', 'assocAlias' => 'Attachment', 'num' => 1));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>
</div>
Then this is a slightly modified version of the media attachment
element, replace with this version...
/Plugin/Media/View/Elements/attachment.php
<?php
if (!isset($this->Media) || !is_a($this->Media, 'MediaHelper')) {
$message = 'Attachments Element - The media helper is not loaded but
required.';
trigger_error($message, E_USER_NOTICE);
return;
}
if (!isset($previewVersion)) {
$previewVersion = 's';
}
/* Set $assocAlias and $model if you're using this element multiple
times in one form */
if (!isset($assocAlias)) {
$assocAlias = 'Attachment';