Home › Forums › Front End PM PRO › Insert Message from another application
Tagged: OpenEMR
- This topic has 15 replies, 2 voices, and was last updated 7 years, 3 months ago by Craig Tucker.
-
AuthorPosts
-
August 31, 2017 at 11:31 am #6092Craig TuckerParticipant
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_byI 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?
August 31, 2017 at 12:08 pm #6097Shamim HasanKeymasterYou 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 usefep_enable_email_send
filter hook to restrict email send.September 1, 2017 at 11:04 am #6142Craig TuckerParticipantI do not see how I can send attachments with fep_send_message(). Is this possible?
September 1, 2017 at 3:30 pm #6147Shamim HasanKeymasterYou 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.
September 2, 2017 at 8:29 am #6151Craig TuckerParticipantI 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.
September 2, 2017 at 2:08 pm #6163Shamim HasanKeymasterPlease 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?September 2, 2017 at 5:37 pm #6169Craig TuckerParticipantYes, 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
September 2, 2017 at 7:36 pm #6189Shamim HasanKeymasterPlease use following (Untested).
let me knowfunction 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')); } }
September 4, 2017 at 7:19 am #6212Craig TuckerParticipantThanks, 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
September 4, 2017 at 10:47 am #6219Shamim HasanKeymasterPlease 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 SettingsYou 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.
September 6, 2017 at 5:41 am #6301Craig TuckerParticipantNo 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?
September 6, 2017 at 11:34 am #6307Shamim HasanKeymasterYour attachment will not be sent with email in your approach.
From my codeadd_action ('fep_action_message_after_send', 'fep_open_emr_upload_attachments', 10, 3 );
and bellow everything move to abovefunction action_putmessage( $args ) {
. That means attachment hook have to be added first.Let me know.
September 9, 2017 at 9:12 am #6375Craig TuckerParticipantSorry 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.
September 10, 2017 at 7:10 pm #6393Craig TuckerParticipantI 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.September 10, 2017 at 8:05 pm #6397Shamim HasanKeymaster$admins = fep_get_option('oa_admins', array());
will give you array of admins. -
AuthorPosts
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.