Insert Message from another application


Home Forums Front End PM PRO Insert Message from another application

This topic is: Not Resolved

Tagged: 

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #6092
    Craig Tucker
    Participant

    I am writing an interface to work with OpenEMR. I have the queries working to import messages and attachments into OpenEMR. Now I am working on routines to submit files with messages from OpenEMR into Front End PM. I am wondering about all the variables I need to cover for the insert and what post meta values are necessary. I am thinking that the following would be sufficient for the post:

    $postarr = array(
    ‘post_status’ => ‘publish’,
    ‘post_type’ => ‘post’,
    ‘post_title’ => $args[‘title’],
    ‘post_content’ => $args[‘message’],
    ‘post_author’ => $sender,
    ‘post_type’ => ‘fep_message’,
    );

    And I need to insert these meta keys:
    _fep_parent_read_by_1
    _fep_participants
    _fep_participants
    _fep_last_reply_time
    _fep_last_reply_id
    _fep_last_reply_by

    I am also wondering if there are any functions I can use that are already built in to facilitate this or should I just keep writing from scratch?

    #6097
    Shamim Hasan
    Keymaster

    You can use fep_send_message() function for this. It will take care of everything. If you do not want to send emails for those imports you can use fep_enable_email_send filter hook to restrict email send.

    #6142
    Craig Tucker
    Participant

    I do not see how I can send attachments with fep_send_message(). Is this possible?

    #6147
    Shamim Hasan
    Keymaster

    You can see Fep_Attachment class. upload attachment is hooked to fep_action_message_after_send action. But if $_FILES[‘fep_upload’] not exists then no attachment will be uploaded. In that case you can see FEP_Email_Pipe class and see how it is done there.

    #6151
    Craig Tucker
    Participant

    I am sorry. I am having a hard time figuring out your functions. fep_send_message() works fine but I need to do the attachments. I am not sure of email piping would be a good work around. I am trying to stay completely on the server for this so no patient data goes out over the internet. I may be misunderstanding what you were suggesting however. So to get the job done I just did this because I can understand the wordpress stuff:

    
    // Logic to process the "putmessage" action.
    // Sends a message to the designated user with an optional attachment.
    // FrontendPM version
    function action_putmessage(&$args) {
      global $wpdb, $out, $admin_user_login;
      $sender = convertToID($admin_user_login);
      if (!$sender) {
        $out['errmsg'] = "No such sender '$admin_user_login'";
        return;
      }
      $recipient = convertToID($args['user']);
      if (!$recipient) {
        $out['errmsg'] = "No such recipient '{$args['user']}'";
        return;
      }
    
    	$year = date('Y');
    	$month = date('m');
    	$timestamp = idate("U"); 
    	$upload_dir =  ABSPATH . "wp-content/uploads/front-end-pm/" . $year . "/" . $month ;
    	if (!file_exists($upload_dir)) {
    		mkdir($upload_dir, 0755, true);
    	}	
    	$filename = $args['filename'];
    	$newfilename = wp_unique_filename( $upload_dir, $filename );
    	$pathtofile = $upload_dir . '/' . $newfilename;
    	file_put_contents($pathtofile, base64_decode($args['contents']));
    	
    	$postarr = array(
    	 'post_status' => 'publish',
    	 'post_type' => 'post',
    	 'post_title' => $args['title'],
    	 'post_content' => $args['message'],
    	 'post_author' => $sender,
    	 'post_type' => 'fep_message', 
    	 );
    	 
    	$parent_post_id = wp_insert_post($postarr);
    
    	add_post_meta( $parent_post_id, '_fep_participants', $sender);	
    	add_post_meta( $parent_post_id, '_fep_participants', $recipient);	
    	add_post_meta( $parent_post_id, '_fep_parent_read_by_' .$sender , $timestamp);	
    	add_post_meta( $parent_post_id, '_fep_last_reply_time', $timestamp);	
    	add_post_meta( $parent_post_id, '_fep_last_reply_id', parent_post_id);
    	add_post_meta( $parent_post_id, '_fep_last_reply_by', $sender);
    	
    	if(!$parent_post_id) {
    		$out['errmsg'] = "Message insert failed";
    		return;
    	}
    
    	$attachment = array(
    	 'post_author' => $sender,
    	 'post_mime_type' => $args['mimetype'],
    	 'post_title' => preg_replace('/\.[^.]+$/', '', basename($pathtofile)),
    	 'post_content' => '',
    	 'post_status' => 'inherit',
    	 'guid' => $pathtofile,
    	);
    	
    	$attach_id = wp_insert_attachment( $attachment, $pathtofile, $parent_post_id );
    	
    	require_once( ABSPATH . 'wp-admin/includes/image.php' );
    	$attach_data = wp_generate_attachment_metadata( $attach_id, $pathtofile );
    	wp_update_attachment_metadata( $attach_id, $attach_data );
    		
    	if ($attach_id === false) {
    	  $out['errmsg'] = "Attachment insert failed";
    	}
    }
    
    

    This works to input the post and file. I would like to trigger an email. If you have concerns about me submitting messages and attachments this way let me know. If you have a method I may be able to add to trigger your email functions let me know that. Otherwise I will write a phpmailer thing to get the job done. With this I am about 95% done with the OpenEMR interface. I will put it out on Github when it is all done.

    #6163
    Shamim Hasan
    Keymaster

    Please let me know following so that i can write some code for you.

    1. Sender is always $admin_user_login?
    2. what is $args[‘user’] ? ( user id or login ? )
    3. maximum 1 attachment allowed?
    4. Should i have to check mime type, size etc or it is already validated?

    #6169
    Craig Tucker
    Participant

    Yes, sender is always admin_user_login and it is the login user name for wordpress the recipient is always $args[‘user’] and it is the login user name for wordpress. The function they are sent to is,

    
    function convertToID($login) {
      global $wpdb;
      $result = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->users} WHERE user_login = %s", $login));
      if (!empty($result)) return $result;
      return 0;
    }
    
    

    This is built for one attachment only.
    The mimetype is already defined in $args['mimetype'] . Thanks for your patience with me on this.

    Craig

    #6189
    Shamim Hasan
    Keymaster

    Please use following (Untested).
    let me know

    
    function action_putmessage( $args ) {
    	global $out, $admin_user_login;
    	
    	if( ! function_exists( 'fep_send_message' ) )
    	return false;
    	
    	$sender = fep_get_userdata( $admin_user_login, 'ID', 'login' );
    	
    	if (!$sender) {
    		$out['errmsg'] = "No such sender '$admin_user_login'";
    		return false;
    	}
    	$recipient = fep_get_userdata( $args['user'], 'ID', 'login' );
    	if (!$recipient) {
    		$out['errmsg'] = "No such recipient '{$args['user']}'";
    		return false;
    	}
    	$message = array(
    		'message_title' => $args['title'],
    		'message_content' => $args['message'],
    		'message_to_id' => $recipient,
    		'open_emr_args' => $args, //pass to get in attachment function
    	);
    	$override = array(
    		'post_author' => $sender,
    	);
    	$message_id = fep_send_message( $message, $override );
    }
    
    add_action ('fep_action_message_after_send', 'fep_open_emr_upload_attachments', 10, 3 );
    
    function fep_open_emr_upload_attachments( $message_id, $message, $inserted_message ){
    	if ( ! fep_get_option( 'allow_attachment', 1 ) || ! $message_id || empty( $message['open_emr_args']) )
    	return false;
    	
    	$args = $message['open_emr_args'];
    	
    	$name = isset( $args['filename'] ) ? $args['filename'] : '';
    	
    	if( ! $name )
    	return false;
    	
    	$size_limit = (int) wp_convert_hr_to_bytes(fep_get_option('attachment_size','4MB'));
    	$fields = (int) fep_get_option('attachment_no', 4);
    	
    	if( class_exists( 'Fep_Attachment' ) ){
    		add_filter('upload_dir', array(Fep_Attachment::init(), 'upload_dir'));
    	}
    	
    	$mime = isset( $args['mimetype'] ) ? $args['mimetype'] : '';
    	$content = isset( $args['contents'] ) ? base64_decode($args['contents']) : '';
    	
    	if( !$mime || !in_array( $mime, get_allowed_mime_types() ) )
    	return false;
    	
    	$size = strlen( $content );
    	if( $size > $size_limit )
    	return false;
    	
    	$att = wp_upload_bits( $name, null, $content );
    	
    	if( ! isset( $att['file'] ) || ! isset( $att['url'] ) || ! isset( $att['type'] ) )
    	return false;
    	
    	$attachment = array(
    		'guid'           => $att['url'], 
    		'post_mime_type' => $att['type'],
    		'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $att['url'] ) ),
    		'post_content'   => '',
    		'post_author'	=> $inserted_message->post_author,
    		'post_status'    => 'inherit'
    	);
    	
    	// Insert the attachment.
    	wp_insert_attachment( $attachment, $att['file'], $message_id );
    	
    	if( class_exists( 'Fep_Attachment' ) ){
    		remove_filter('upload_dir', array(Fep_Attachment::init(), 'upload_dir'));
    	}
    }
    
    #6212
    Craig Tucker
    Participant

    Thanks, I tried it out. The email and post work fine but the attachment is not found and did not download to the server and did not show up on the post. I see what you are trying to do. I will try to fish out the problem. Thanks so much for taking the time to do this.

    Craig

    #6219
    Shamim Hasan
    Keymaster

    Please check following
    1. Allow attachment in Front End PM PRO Settings
    2. File mime type is present and it is in allowed type
    3. File size is less then from Front End PM PRO Settings

    You can also remove some check from this function and try to find out which check failed. eg remove

    
    if( !$mime || !in_array( $mime, get_allowed_mime_types() ) )
    	return false;
    

    then try. Also remove other check and try.

    #6301
    Craig Tucker
    Participant

    No that was not it. I could not get the fep_action_message_after_send hook to work in this location. It would not trigger after several different approaches. My work around has been this:

    `
    function action_putmessage( $args ) {
    global $out, $admin_user_login;

    if( ! function_exists( ‘fep_send_message’ ) )
    return false;

    $sender = fep_get_userdata( $admin_user_login, ‘ID’, ‘login’ );

    if (!$sender) {
    $out[‘errmsg’] = “No such sender ‘$admin_user_login'”;
    return false;
    }
    $recipient = fep_get_userdata( $args[‘user’], ‘ID’, ‘login’ );
    if (!$recipient) {
    $out[‘errmsg’] = “No such recipient ‘{$args[‘user’]}'”;
    return false;
    }
    $message = array(
    ‘message_title’ => $args[‘title’],
    ‘message_content’ => $args[‘message’],
    ‘message_to_id’ => $recipient,
    );
    $override = array(
    ‘post_author’ => $sender,
    );
    $message_id = fep_send_message( $message, $override );

    $upload_dir = wp_upload_dir();
    $upload_dir = $upload_dir[‘basedir’];
    $subdir = ”;
    if ( get_option( ‘uploads_use_yearmonth_folders’ ) ) {
    $time = current_time( ‘mysql’ );

    $y = substr( $time, 0, 4 );
    $m = substr( $time, 5, 2 );

    $subdir = “/$y/$m”;
    }
    $upsub = ‘/front-end-pm’ . $subdir;
    $filename = $args[‘filename’];
    $newfilename = wp_unique_filename( $upload_dir, $filename );
    $pathtofile = $upload_dir . $upsub . ‘/’ . $newfilename;
    $content = isset( $args[‘contents’] ) ? base64_decode($args[‘contents’]) : ”;

    $size_limit = (int) wp_convert_hr_to_bytes(fep_get_option(‘attachment_size’,’4MB’));
    $size = strlen( $content );
    if( $size > $size_limit )
    return false;

    $mime = isset( $args[‘mimetype’] ) ? $args[‘mimetype’] : ”;
    if( !$mime || !in_array( $mime, get_allowed_mime_types() ) )
    return false;

    file_put_contents($pathtofile, $content);

    $attachment = array(
    ‘guid’ => $pathtofile,
    ‘post_mime_type’ => $args[‘mimetype’],
    ‘post_title’ => preg_replace(‘/\.[^.]+$/’, ”, basename($pathtofile)),
    ‘post_content’ => ”,
    ‘post_author’ => $sender,
    ‘post_status’ => ‘inherit’,

    );

    $attach_id = wp_insert_attachment( $attachment, $pathtofile, $message_id );

    }

    I think it includes all the necessary checks. Can you see anything wrong with working this way? Everything works as it is. Do you have any concerns?

    #6307
    Shamim Hasan
    Keymaster

    Your attachment will not be sent with email in your approach.
    From my code add_action ('fep_action_message_after_send', 'fep_open_emr_upload_attachments', 10, 3 ); and bellow everything move to above function action_putmessage( $args ) {. That means attachment hook have to be added first.

    Let me know.

    #6375
    Craig Tucker
    Participant

    Sorry I was not clear about where I needed the attachment. I do not need it with the message so my method has worked fine. So, to integrate with OpenEMR and the Sunset Patient Portal I made the following modifications to the webserve.php file:

    https://github.com/CraigT543/Sunset-Patient-Portal-with-FrontendPM/blob/master/FrontendPMmods.php

    This works to replace cartpauj-pm with Frontend PM as the messaging application for OpenEMR.

    #6393
    Craig Tucker
    Participant

    I found another issue. I need to fix the method for verifying authorized admins. The variable $admin_user_login with Cartpauj PM was associated this wise:

    
    $adminOps = get_option('cartpaujPM_options');
    $admin_user_login = $adminOps['admin_user_login'];
    

    In Cartpauj PM there is only one admin so this is straightforward. But in Frontend PM you can have multiple authorized admins. So I need to develop a different procedure to verify authorized admins. I see you have a function “fep_get_option”. Can I use this to return an array of the ‘username’ entries for admins entered? If so how? With this array I can check if wp_authenticate($_REQUEST['login'] is in the array. That should work.

    #6397
    Shamim Hasan
    Keymaster

    $admins = fep_get_option('oa_admins', array()); will give you array of admins.

Viewing 15 posts - 1 through 15 (of 16 total)

You need to purchase ‘Front End PM PRO’ to create topic in this support forum.

If you already purchased ‘Front End PM PRO’ please LOGIN.