147 Posts
MtnPavlas
6 years ago
Topic

Hi guys, how can I tap into the 'to' option/parameter of the Email field. I'm building a private messaging solution where the sender picks the recipient usernames, using Pulsar Informatique's multi-auto-complete field ('msg_recipients'), and then I have an after store field ('msg_get_email_address') where I'm pulling the emails based on the usernames, but I'm not sure how to feed it into the email field ('msg_email'). Something like this?

$userID = $fields['msg_get_recipients']->value;

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
  ->select($db->quoteName('email'))
  ->from($db->quoteName('#__users'))
  ->where($db->quoteName('id') . ' IN (' . $userID . ')');
$db->setQuery($query);
$db->execute();
$num_rows = $db->getNumRows();
$results = $db->loadColumn();

if ($num_rows > 1) {
  $emailList = $results [0];
  for ( $i = 1; $i <= $num_rows-1; $i++ ) {
    $emailList = $emailList . ',' . $results[$i];
    }
  } elseif ($num_rows = 1) {
  $emailList = $results [0];
} 

$fields['msg_email_address']->value = $emailList;
$config['storages']['#__cck_store_form_message']['email_address'] = $emailList;

Thanks a lot for any pointers!

Get a VIP membership
1283 Posts
Bucklash
6 years ago
5
Level 1

Hi

Just pointing out loadResult is for a single result 

Select From database

147 Posts
MtnPavlas
6 years ago
4
Level 2

thanks bucklash. I'm only experimenting with a single result right now. (Then I'll expand it for multiple.)

Any thoughts on how to inject the email address from the query into the 'to' option of the email field?

I realize I can save the email into another field, and then pull that field into 'to' but I'd rather avoid this.

1283 Posts
Bucklash
6 years ago
3
Level 3

Hi

I don't want to hijack your post at the risk of it being left alone by others but are you submitting the email when content typenis saved?

If so, wouldn't it be the case that you would get user emails using beforestore 

Then populated email stuff from there, and then save which then submits the email.  

What does $config or $fields offer you regarding storing in email field?

I made my own J!Dump fields using beforerender to easily check that

4229 Posts
Kadministrator
6 years ago
0
Level 4

All you need is some_field configured in email field as To(fields). The set this fields value in afterStore placed before email field in the fields list. 

147 Posts
MtnPavlas
6 years ago
1
Level 4

@Bucklash, are you saying I should temporarily "store" it into a field without actually saving it to the d/b? How would I do this? Just use a text field with storage "none" for the target field?

I am using a BeforeStore field where I'm grabbing the recipients' user ID's and querying for a list of email addresses delimited by a comma. I updated the code in my OP. 

For now, I'm just going to pass the results into another field (and store it in the d/b, too), though eventually I'd like to know if there's a way to inject it directly into the "to" option/parameter of the email field. Would be cool.

1283 Posts
Bucklash
6 years ago
0
Level 5

Hi 

Klas has shown us the 'good way'

Re: adding to the "to" option, it seems you would get:

$fields['my_email_field']->options2;

Do JSON stuff on it to get "to",

add your result from DB,

then put it all back in to $fields['my_email_field']->options2.


Don't need an extra field for that



147 Posts
MtnPavlas
6 years ago
1
Level 1

Thank you, again, for all the helpful tips.

I found out that injecting values from a BeforeStore field into, e.g., a text field with storage 'none' works like a charm. Then I pulled that field into the email field in the 'to' option (e.g. in my case it's #msg_email_address#). So, I still would like to learn the JSON solution @Bucklash mentions, but it's a bit beyond my knowledge.

Hopefully this may help someone in the future.

1283 Posts
Bucklash
6 years ago
0
Level 2

Hi

Yes, beforeStore and Afterstore and beforeRender are powerful tools.

Just remember that if you are using beforestore to manipulate values it is best to do

$config['storages']['#__some_table']['some_column'] ie

$config['storages']['#__content']['title']....

$fields['some_field_name']->value or form, or live_value, or defaultvalue etc, way too many to list

$cck, $fields, and $config are your friends, need to discover where they can and can not be accessed.

RE JSON...

I'm no guru, but try:

$stuff = $fields['my_email_field']->options2;
$emailStuff = json_decode($stuff);

Should get an object to extract data from ie

$to = $emailStuff->to;

...alternatively, try

$emailStuff = json_decode($stuff, true);

Should get an associative array to extract data from ie

$to = $emailStuff['to'];

Something like that, check out JSON

Get a Book for SEBLOD