Hi,

I'd like to send private messages to a selected group of users. For this purpose I use a view with VBO and a Rules component action with this custom PHP code:

$recipients= array();
foreach ($selected_users as $recipient) {
$recipients[] = user_load($recipient->uid);
}

privatemsg_new_thread($recipients, $msg_subject, $msg_body, $options = array());

I've checked with dpm(); and all the variables looks correct according to the API documentation: $recipients is an array of user objects, $msg_subject is a plain text and $msg_body is formatted text with 'full HTML' input format in my case.
But when I submit the action I got these error messages:

Notice: Array to string conversion _privatemsg_validate_message() (/var/www/drupal/sites/all/modules/privatemsg/privatemsg.module row 1860)

and

PDOException: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1: INSERT INTO {pm_message} (subject, author, body, format, timestamp, has_tokens) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2_0, :db_insert_placeholder_2_1, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5); Array ( [:db_insert_placeholder_0] => My subject [:db_insert_placeholder_1] => 1 [:db_insert_placeholder_3] => filtered_html [:db_insert_placeholder_4] => 1463218925 [:db_insert_placeholder_5] => 0 [:db_insert_placeholder_2_0] => <p>My <strong>body </strong>text</p> [:db_insert_placeholder_2_1] => full_html ) _privatemsg_send() (/var/www/drupal/sites/all/modules/privatemsg/privatemsg.module row 1920).

As I said, all three input variables seems correct so I don't understand any of these messages: I don't know how the array to string issue should rise here and the second message looks more confusing for me: It seems to contain an extra placeholder ([:db_insert_placeholder_3] => filtered_html).

Comments

StG created an issue. See original summary.

oadaeh’s picture

Category: Bug report » Support request

You have an extra text area format in your query. You have both "full_html" and "filtered_html", and "filtered_html" is being used for "[:db_insert_placeholder_3]", which is going into the "timestamp" field, and the values after that are then all out of place.

I would take a look at what you are getting from Views/VBO for $msg_body and how you have the fields defined, to see if they line up and what you are expecting.

StG’s picture

Actually, $msg_body and $msg_subject are not fields but parameters provided by this rule component:

{ "rules_msg_recipient" : {
    "LABEL" : "\u00dczenet c\u00edmzettjei",
    "PLUGIN" : "action set",
    "OWNER" : "rules",
    "REQUIRES" : [ "php", "rules" ],
    "USES VARIABLES" : {
      "selected_user" : { "label" : "Felhaszn\u00e1l\u00f3", "type" : "list\u003Cuser\u003E" },
      "msg_subject" : { "label" : "\u00dczenet t\u00e1rgya", "type" : "text" },
      "msg_body" : { "label" : "\u00dczenet sz\u00f6vege", "type" : "text_formatted" }
    },
    "ACTION SET" : [
      { "php_eval" : { "code" : "$recipients= array();\r\nforeach ($selected_users as $recipient) {\r\n$recipients[] = user_load($recipient-\u003Euid);\r\n}\r\n\/*dpm($msg_subject);\r\ndpm($msg_body);\r\ndpm($recipients);*\/\r\nprivatemsg_new_thread($recipients, $msg_subject, $msg_body, $options = array());" } }
    ]
  }
}
oadaeh’s picture

Okay. So maybe you need to modify what Rules is giving you. Take a look at what privatemsg_new_thread() expects (which may require reading through the function to see what is done with the data) and what Rules is providing and use your action to bridge the gap.

StG’s picture

I think I've figured it out: privatemsg_new_thread() needs a $message->format variable and if it's missing it uses the default text format of current user:

  if (!isset($message->format)) {
    $message->format = filter_default_format($message->author);
  }

Meanwhile the body parameter of the rule component also has a text format variable since it's a formated text. That's why I have an extra text area format value.

There's an other issue caused by the formatted text parameter: privatemsg_new_thread() expects a single value for $body but this parameter produces an array with $body['value'] and $body['format'] variables.

So I think two changes should be made in privatemsg_new_thread(), something like this (sorry, I can't create a proper patch):

  if (isset($body['value'])) {
    $message->body = $body['value'];
  } else {
    $message->body = $body;
  }

and

  if (!isset($message->format)) {
    if (isset($message->body['format'])) {
        $message->format = $message->body['format'];
    } else {
        $message->format = filter_default_format($message->author);
    }
  }
ivnish’s picture

Status: Active » Closed (outdated)