Craig Tucker

Forum Replies Created

Viewing 5 posts - 31 through 35 (of 35 total)
  • Author
    Posts
  • in reply to: Insert Message from another application #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?

    in reply to: Insert Message from another application #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

    in reply to: Insert Message from another application #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

    in reply to: Insert Message from another application #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.

    in reply to: Insert Message from another application #6142
    Craig Tucker
    Participant

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

Viewing 5 posts - 31 through 35 (of 35 total)