hello

I would like to create a module to integrate with ModalFrame PrivateMsg http://drupal.org/project/privatemsg . I created an issue in PMS but without success.

So I started a piece of code and if you could help me create this module I would be very grateful ...

I am no expert in PHP

It starts with the file zmodal_privatemsg.module


/**** Private Message ***/
function zmodal_privatemsg_init() {

  if (!empty($_COOKIE['has_js']) && preg_match('`^messages(?:/list(?:/.*))?$`', $_GET['q'])) {
    modalframe_parent_js();
    drupal_add_js(drupal_get_path('module', 'zmodal_privatemsg') .'/zmodal_privatemsg.js');
  }
 }


function zmodal_privatemsg_list($form) {

if (!empty($_COOKIE['has_js']) && !empty($_GET['modalframe']) && in_array($form_id, array('edit-archive', 'edit-delete'))) {
    modalframe_child_js();

  $form['list'] = array('#value' => theme('table', $headers, $themed_rows, array('class' => 'privatemsg-list')), '#weight' => 5);
  return drupal_render($form);
}

}



function zmodal_privatemsg_form_submit_redirect($form, &$form_state){
 if ($form_state['values']['op'] == t('Save')) {
     modalframe_close_dialog();
  }
}

and zmodal_privatemsg.js

(function ($) {

Drupal.behaviors.ZmodalPrivatemsg = function(context) {

  var $links = $([])
    .add($('.tabs a[href*="messages/"]:not(.modalframe-path-processed)', context))
    .add($('table td a[href*="messages/"]:not(.modalframe-path-processed)', context));

  $links.addClass('modalframe-path-processed').each(function() {
    var $link = $(this), url = $link.attr('href');
    var regExp = new RegExp('^'+ Drupal.settings.basePath +'(?:[-a-z]+/)?messages/(?:/[0-9]+)?.*$');
    var urlParts = regExp.exec(url);
    if (urlParts) {
      var action = urlParts[1];

      // onSubmit callback for the modal frame.
      var onSubmitCallback = function(args) {
      if (args.redirect) {
        setTimeout(function() { window.location = args.redirect; }, 1);
      }
          else {
            window.location.reload();
          }
      };

      // Modal frame options.
      var modalFrameOptions = {
        url: url + (url.indexOf('?') == -1 ? '?' : '&') +'modalframe=1',
        width: 800,
        height: 800,
        onSubmit: onSubmitCallback
      };


      // Attach click event handler to the link.
      $link.click(function() {
        Drupal.modalFrame.open(modalFrameOptions);
        return false;
      });
    }
  });



    };
})(jQuery)

The method works, but it displays the complete body and not just the form or list.

Then when you click on "Write a message" from a block or another location it does not open the Modal Frame.

Try my code and show the improvements we can make.

Comments

markus_petrux’s picture

Category: task » support
Status: Active » Closed (won't fix)

Re: "if you could help me create this module"

I'¡m afraid I do not have the time for this level of support. Too busy. Please, check out the documentation linked from the project page and/or study the Contribs Packake or other modules using the Modal Frame API.

Berliner-dupe’s picture

Status: Closed (won't fix) » Active

Hi charlie836,

have you found a working Code for integrate PrivateMsg in Modal Frame?

Regards Matthias

rv0’s picture

"The method works, but it displays the complete body and not just the form or list."

to avoid that, use hook_form_alter to call modalframe_child_js() on the privatemsg_new form..

Michsk’s picture

function modals_init() {
  $ref = $_SERVER['HTTP_REFERER'];
  drupal_add_js(drupal_get_path('module', 'modals') .'/assets/modals.js');

  if(isset($_GET['modal']) || (stristr($ref, 'modal') == TRUE && (arg(0) == 'legal_accept' || (arg(0) == 'statuses' && arg(2) == 'edit') || (arg(0) == 'statuses' && arg(2) == 'delete'))) ){
	modalframe_child_js();
  }
  else{
    modalframe_parent_js();
  }
}

/**
 * Implementation of hook_form_alter().
 */
function modals_form_alter(&$form, $form_state, $form_id) { 
  $ref = $_SERVER['HTTP_REFERER'];
  if(isset($_GET['modal']) || stristr($ref, 'modal') == TRUE){
    if ($form_id == 'privatemsg_new') {
	  $form['#submit'][] = 'modals_child_form_submit';
    }
  }
}

/**
 * Process the block settings or block delete forms.
 */
function modals_child_form_submit($form, &$form_state) {
  $modalframe_args = array(
    'submitted' => TRUE,
  );
  modalframe_close_dialog($modalframe_args);
}

This shows the form in the modal window. Tough i'm having problems with sending the form and closing the modalwindow. That should be handled by modalframe_close_dialog(), but it doesn't. After upgrading privatemsg it stoped working. Now after the messages has been send the complete body loads in the modal, i think this has got to do with drupal_goto() which is used in privatemsg:
http://drupal.org/node/1103210
http://drupal.org/node/1108720

Javascript

Drupal.behaviors.modalsParent = function(context) {

  $('#messaging', context).addClass('modalframe-message-processed').each(function() {
    $(this).parent().prepend('<div class="modalframe-modals-messages" style="display:none;"></div>');
  });
  $('.modalframe-page-wrapper .modal-close').click(function() {
    Drupal.modalFrameChild.triggerParentEvent('childClose', false); return false;
  });

  // Create an element to dynamically render status messages.
  var modalsConfigurationClick = function(action, url, refreshAfterSubmit) {
    // Hide status messages before opening the modal frame.
    $('.modalframe-modals-messages').hide();

    if (refreshAfterSubmit) {
      url += (url.indexOf('?') == -1 ? '?' : '&') +'refresh=1';
    }
	
    var onSubmitCallback = function(args, statusMessages) {
      if (args && args.submitted) {
        // Render new status messages, if any.
		if (statusMessages && statusMessages.length) {
          $('.modalframe-modals-messages').html(statusMessages).show('fast');
          window.scrollTo(0, 0);
        }
        if (refreshAfterSubmit) {
          window.scrollTo(0, 0);
          setTimeout(function() { window.location.reload(); }, 1500);
        }
      }
    };
    var modalFrameOptions = {
      url: url,
      width: 800,
      height: 200,
      draggable: false,
      //customDialogOptions: (modalFrameOptions.height = 100),
      onSubmit: onSubmitCallback
    };

    Drupal.modalFrame.open(modalFrameOptions);
    return false;
  };
  
  //modalize user login
  $('a[href$=user/login]', context).not('.modalframe-processed').attr('href', function() {
	return this.href + '?modal=1'
  }).addClass('use-modal modalframe-processed').click(function(){
	var $link = $(this), url = $link.attr('href');
	return modalsConfigurationClick('', url, true);  
  });
  
  //modalize user register
  $('a[href$=user/register]', context).not('.modalframe-processed').attr('href', function() {
	return this.href + '?modal=1'
  }).addClass('use-modal modalframe-processed').click(function(){
	var $link = $(this), url = $link.attr('href');
	return modalsConfigurationClick('', url, false);  
  });
  
  //modalize send new message
  $('a[href*=messages/new]', context).not('.modalframe-processed').attr('href', function() {
	return this.href + '?modal=1'
  }).addClass('use-modal modalframe-processed').click(function(){
	var $link = $(this), url = $link.attr('href');
	return modalsConfigurationClick('', url, false);  
  });
  
  //modalize relationship request
  $('a.user_relationships_popup_link', context).not('.modalframe-processed').attr('href', function() {
	return this.href + '&modal=1'
  }).addClass('use-modal modalframe-processed').click(function(){
	var $link = $(this), url = $link.attr('href');
	return modalsConfigurationClick('', url, true);  
  });
  
  //modalize flag abuse
  $('a.flag', context).not('.modalframe-processed').attr('href', function() {
	return this.href + '&modal=1'
  }).addClass('use-modal modalframe-processed').click(function(){
	var $link = $(this), url = $link.attr('href');
	return modalsConfigurationClick('', url, true);  
  });
  
  //modalize facebook status react
  $('.babbel_react a, a.send-babbel', context).not('.modalframe-processed').attr('href', function() {
	return this.href + '&modal=1'
  }).addClass('use-modal').click(function(){
	var $link = $(this), url = $link.attr('href');
	return modalsConfigurationClick('', url, false);  
  });
  
  //class modal options
  $('.modal', context).not('.modalframe-processed').attr('href', function() {
	return this.href + '?modal=1'
  }).addClass('use-modal modalframe-processed').click(function(){
	var $link = $(this), url = $link.attr('href');
	return modalsConfigurationClick('', url, false);  
  });
  
  //class modal-re options, refresh
  $('.modal-re', context).not('.modalframe-processed').attr('href', function() {
	return this.href + '?modal=1'
  }).addClass('use-modal modalframe-processed').click(function(){
	var $link = $(this), url = $link.attr('href');
	return modalsConfigurationClick('', url, true);  
  });
  
};
Michsk’s picture

Finally got it working with privatemsg_new form. Don't know why but for privatemsg to close after submit we need to use:

  	if($form_id == 'privatemsg_new' ){
	  $form[submit]['#submit'][] = 'modals_child_form_submit';
	}
rv0’s picture

@lasac
You forgot the '' around submit.

$form['submit']['#submit'][] = 'modals_child_form_submit';

But I have to thank you BIGTIME... I was searching for hours why my code wouldn't work with Private Messages and your snippet saved me :D

Michsk’s picture

you're welcome, happy i found it myself, this was a very annoying issue. Thanx for the pointer.

dsnopek’s picture

Issue summary: View changes
Status: Active » Fixed

Looks like @Lasac found the solution to the issue! Closing.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.