Skip to content

Commit

Permalink
Add a JS hack to allow usage of the Uploadifyer in the dmGallery form
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanmonod committed Feb 8, 2011
1 parent 50739f9 commit f73a1cd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
1 change: 1 addition & 0 deletions config/config.php
Expand Up @@ -3,6 +3,7 @@


$this->dispatcher->connect('dm.media_library.control_menu', array('dmMediaUploadifyerConfig', 'listenToMediaLibraryControlMenuEvent'));
$this->dispatcher->connect('controller.change_action', array('dmMediaUploadifyerConfig', 'listenToActionChangeEvent'));

/**
* Let's talk a bit about the next piece of code
Expand Down
1 change: 1 addition & 0 deletions config/dm/assets.yml
Expand Up @@ -3,6 +3,7 @@ js:
dmMediaUploadifyerPlugin:

adminCtrl: dmAdminMediaUploadifyCtrl
galleryHack: dmAdminMediaUploadifyForGallery
swfobject: swfobject
sfWidget: sfWidgetFormDmUploadify.min
uploadify: jquery.uploadify.v2.1.0.min
Expand Down
13 changes: 13 additions & 0 deletions lib/dmMediaUploadifyerConfig.class.php
Expand Up @@ -16,5 +16,18 @@ public static function listenToMediaLibraryControlMenuEvent(sfEvent $event)
dmContext::getInstance()->getResponse()->addJavascript('dmMediaUploadifyerPlugin.adminCtrl');
}

/**
* Used to insert a JS hack to replace the dmMedia/gallery add button by the uplodifier dialog
* @param sfEvent $event
*/
public static function listenToActionChangeEvent(sfEvent $event) {
if ( dmContext::hasInstance() && $event['module'] == 'dmMedia' && $event['action'] == 'gallery' ){
$context = dmContext::getInstance();
$context->getResponse()->addStylesheet('lib.ui-dialog');
$context->getResponse()->addJavascript('lib.ui-dialog');
$context->getResponse()->addJavascript('dmMediaUploadifyerPlugin.galleryHack', 'last');
}
}

}

31 changes: 19 additions & 12 deletions modules/dmMediaUploadifyerAdmin/actions/actions.class.php
Expand Up @@ -10,12 +10,20 @@ class dmMediaUploadifyerAdminActions extends dmAdminBaseActions
*/
public function executeNewMultipleFile(dmWebRequest $request)
{
// create new media

$media = null;

$this->forward404Unless($folder = dmDb::table('DmMediaFolder')->find($request->getParameter('folder_id')));

// Retrived the folder by folder_id or by object in case of usage in dmMedia/gallery module
if ( $request->hasParameter('folder_id') )
{
$folderId = $request->getParameter('folder_id');
}
else
{
$objectModel = $request->getParameter('model');
$objectPk = $request->getParameter('pk');
$object = dmDb::table($objectModel)->find($objectPk);
$folderId = $object->getDmMediaFolder()->getId();
}
$this->forward404Unless($folder = dmDb::table('DmMediaFolder')->find($folderId));
if (!$folder->isWritable())
{
$this->getUser()->logAlert($this->getI18n()->__('Folder %1% is not writable', array('%1%' => $folder->fullPath)));
Expand All @@ -27,18 +35,17 @@ public function executeNewMultipleFile(dmWebRequest $request)
if ($request->isMethod('post') && $form->bindAndValid($request))
{
$media = $form->save();

if (isset($object)){
$object->addMedia($media); // In dmMedia/gallery usage, we also need to associate with the object
}
return $this->renderText('success');
}

$action = '+/dmMediaUploadifyerAdmin/newMultipleFile?folder_id='.$folder->id;

$uploadify_widget = new sfWidgetFormDmUploadify();
$action = '+/dmMediaUploadifyerAdmin/newMultipleFile?'.( isset($object) ? "&model=$objectModel&pk=$objectPk" : 'folder_id='.$folder->id);

return $this->renderAsync(array(
'html' => $form->render('.dm_form.list.little action="'.$action.'"'),
'css' => $uploadify_widget->getStylesheets(),
'js' => $uploadify_widget->getJavascripts()
'css' => $form->getStylesheets(),
'js' => $form->getJavascripts()
));
}

Expand Down
27 changes: 27 additions & 0 deletions web/js/dmAdminMediaUploadifyForGallery.js
@@ -0,0 +1,27 @@
// Update the add button of the gallery widget to allow multiple upload
(function($) {

var $gallery = $("div.dm_gallery_big");
var $metadata = $gallery.metadata();

// Replace the current click handler by a new one
$gallery.find('a.open_form').unbind('click')
.click(function(){

// Construct the url based on the standard form url, but changing the module/action
var url = $gallery.find('form.dm_add_media').attr('action').match(/(.*)\+\/dmMedia.*/);
url = url[1]+'+/dmMediaUploadifyerAdmin/newMultipleFile?model='+$metadata.model+'&pk='+$metadata.pk;

// Open the dialog
var $dialog = $.dm.ctrl.ajaxDialog({
modal: true,
title: $(this).html(),
url: url,
width: 500
}).bind('dmAjaxResponse', function(){
$dialog.prepare();
});

});

})(jQuery);

0 comments on commit f73a1cd

Please sign in to comment.