0

I want to compare a term_reference field in a node with a term_reference field in a user profile. If certain terms match, I want to message the users with those matching terms. Is this something that could be achieved with rules?

EDIT: This is my code so far:

dpm($node->field_project_resources);
dpm($node->field_project_resources['und']['0']['tid']);

$users = entity_load('user');
$arrayuids = array_keys($users);

$query = new EntityFieldQuery();    
$query->entityCondition('entity_type', 'profile2')
    ->propertyCondition('uid', $arrayuids, 'IN');

$profile = $query->execute();

$arrayofpids = array();

foreach($profile as $obj){

     foreach($obj as $objinner)
     {
     array_push($arrayofpids, $objinner->pid);
     }

}
$profile2s = profile2_load_multiple($arrayofpids, FALSE);
foreach($profile2s as $obj){dpm($obj);
    foreach($obj as $objinner){
            if(is_array($objinner)){
           foreach($objinner as $i)
           {dpm($i);
                   foreach($i as $n)
                         {

                        }
           }}
    }
}

I want to compare for each value in array field_project_resources result with the profile2 array field of field_resources for each user and then return an array of users that have matching fields. I am quite new to this and I would appreciate some help.

2 Answers 2

2

Yes, definitely something with rules together with http://drupal.org/project/mimemail (I would also recommend http://drupal.org/project/phpmailer together with mimemail), if you want to mail them, or use the https://drupal.org/project/messaging framework and notifications or similar.

You can use rules scheduler to do this on a schedule or use rules events such as "After saving a new term" to trigger the lookup when a term is saved. The tricky part is the conditions/actions of looking up.

You can use the data comparison condition together with a php evaluation to lookup the user profile/term and then generate an action of sending a mail.

Or you may need to use fetch entity by id/property depending on what you have available to the situation to load the node and the same for the user profile, and then compare those results in a php custom action.

The last option is to use views bulk operations to do the lookup for you and then load the list in the rule using the views bulk operations action.

5
  • hi, thanks for that useful response. How would I put custom php into rules? Commented Sep 5, 2012 at 20:16
  • 1
    You have to turn on php filter in the modules list. That will make php an option in rules and several other places.
    – 7wonders
    Commented Sep 6, 2012 at 6:45
  • 1
    You will also want to dload and enable the devel module. This will make your life a lot easier in figuring out the names of the variables to use when doing the comparisons (especially when using custom php). If you want to use the rules replacement patterns when using php, you have to escape them with ''. For example, $nid = '[node:nid]';
    – 7wonders
    Commented Sep 6, 2012 at 6:49
  • brilliant stuff! that makes things a lot more flexible. Cheers. Commented Sep 6, 2012 at 8:20
  • I ended up using the views bulk operations on my way with this problem. Bulk operations saved a lot of time from having to fetch profiles from users. Commented Sep 11, 2012 at 1:21
0

This is my code so far. I load a list of user profiles using view bulk operation, which made things a lot simpler.

<?php

//create an array for the project resources because there may be many

$arrayofprojectresources = array();
array_push($arrayofprojectresources, $node->field_project_resources['und']['0']['tid']);

$usersprofidstonotify = array();

//for each profile
foreach ($entity_list as $obj) {

    //drupal_set_message('pid');
    //dpm($obj->pid);
//make a new array with the profile field resources in
    $profile1 = $obj->field_resources['und'];

//for each inside the 'und' array
    foreach ($profile1 as $innerar) {

//go through each resource in project resource

        foreach ($arrayofprojectresources as $oneresource) {

//see if that resource is in the array of profile resources

            if (in_array($oneresource, $innerar)) {

                //drupal_set_message('the resource');
                //dpm($oneresource);
                //drupal_set_message('is in');
                //dpm($innerar);
                array_push($usersprofidstonotify, $obj->pid);
            } else {
                //drupal_set_message('the resource');
                //dpm($oneresource);
                //drupal_set_message('is not in');
                //dpm($innerar);
            }
        }
    }
}
$useridstonotify = array();
//for each of the profiles
foreach ($entity_list as $obj) {
//check for each of the usersprofilestonotify
    foreach ($usersprofidstonotify as $user) {
        if (($obj->pid) == ($user)) {
            //dpm($obj);
            //dpm($obj->pid);
            //drupal_set_message('is equal to');
            //dpm($user);
            //drupal_set_message('so send message to');
            //dpm($obj->uid);
            array_push($useridstonotify, ($obj->uid));
        }
    }
}

$arrayofprojectresourcenames = array();
array_push($arrayofprojectresourcenames, $node->field_project_resources['und']['0']['name']);

$usersprofidstonotify = array();

//dpm($node);

$name = dpm($node->name);
$resources = array();
$msg = $name;
$msg .= ' has posted a project called ';
$msg .= ($node->title);
$msg .= '</br>';
$msg .= '</br>';
$msg .= 'More details:';
$msg .= ($node->body['und']['0']['value']);
$msg .= '</br>';
$msg .= ' They need a ';
$msg .= implode(",", $arrayofprojectresourcenames);
$msg .= '</br>';
$msg .= 'Can you help them out? ';
$msg .= '</br>';
$msg .= ' If so, please sign up ';
$msg .= '<a href="' . '/node/' . ($node->vid) . '">' . 'here</a>';
//drupal_set_message($msg);
//drupal_set_message('users to notify:');
//dpm($useridstonotify);
foreach ($useridstonotify as $oneuser) {
    privatemsg_new_thread(array(user_load($oneuser)), 'Someone needs your resource', $msg);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.