In Drupal 7, I would like to set up a rule that sends an email to all users in an ORGANIC GROUPS Role, based on an action. I know how to get the action, I know how to do the loop, I know how to send the email.
I cannot, for the life of me, figure out how to get a list of group members with group role "X".
PS - I've reviewed this link: http://www.sthlmconnection.se/en/blog/rules-based-notifications-organic-groups , and it's for D6.
GAAH ARGH! (And much hair pulling later), here's the answer:
Custom Module (myutil.module) - .module file empty, .info file with the same sparse info required for any other module.
Add file myutil.rules.inc with following code:
/**
* #file
* Rules code: actions, conditions and events.
*/
/**
* Implements hook_rules_action_info().
*/
function myutil_rules_action_info() {
$actions = array(
'myutil_action_send_email_to_group_editors' => array(
'label' => t('Get group editors from group audience'),
'group' => t('My Utilities'),
'configurable' => TRUE,
'parameter' => array(
'group_content' => array(
'type' => 'entity',
'label' => t('Group content'),
'description' => t('The group content determining the group audience.'),
),
),
'provides' => array(
'group_editors' => array('type' => 'list<user>', 'label' => t('List of group editors')),
),
'base' => 'myutil_rules_get_editors',
),
);
return $actions;
}
function myutil_rules_get_editors($group_content) {
if (!isset($group_content->og_membership)) {
// Not a group content.
return;
}
$members = array();
foreach ($group_content->og_membership->value() as $og_membership) {
// Get the group members the group content belongs to.
$current_members = db_select('og_membership', 'om');
$current_members->join('og_users_roles', 'ogur', 'om.etid = ogur.uid');
$current_members->fields('om', array('etid'));
$current_members->condition('om.gid', $og_membership->gid);
$current_members->condition('om.entity_type', 'user');
// FOR THIS LINE, YOU'LL NEED TO KNOW THE ROLE ID FROM THE `og_role` TABLE
$current_members->condition('ogur.rid', 14);
$result = $current_members->execute();
while ($res = $result->fetchAssoc()) {
$members[] = $res['etid'];
}
}
// Remove duplicate items.
$members = array_keys(array_flip($members));
return array('group_editors' => $members);
}
Enable the module as you would any other module. Clear cache. Go back to Rules and enjoy.
I've submitted a patch to a similar issue for OG https://drupal.org/node/1859698#comment-8719475 which should allow you to do this in Rules without the need for a custom module or needing to know the role id.
Once you apply the patch, you can use the "Get group members from group audience" action, and now filter by the "Membership States" and "Group Roles". Then add a loop to go through the list and use the "Send mail" action to send an email to each member.
Related
I am building my first Drupal 7 module and am having trouble with the screen to edit a fieldable entity. I am using field_attach_form and it is working great for all accept one field which is displaying the field default rather than the current content of that field for that entity.
I have a text field, a number field, a number of Boolean fields and the one list_text field which is failing.
Any ideas what I a doing incorrectly? Code below is what I think is needed but please do let me know if you need more.
Code to create the field in hook_enable:
if (!field_info_field('field_available')) {
$field = array (
'field_name' => 'field_available',
'type' => 'list_text',
'settings' => array(
'allowed_values' => array('No', 'Provisionally', 'Yes'),
),
);
field_create_field($field);
Code to create the instance, also in hook_enable:
if (!field_info_instance('appointments_status', 'field_available', 'appointments_status')) {
$instance = array(
'field_name' => 'field_available',
'entity_type' => 'appointments_status',
'bundle' => 'appointments_status',
'label' => t('Available?'),
'required' => TRUE,
'default_value' => array(array('value' => 'No')),
'description' => t('Set to No if appointments with this status make this slot unavailable, Provisionally means that it will only reserve a space temporarily'),
);
field_create_instance($instance);
This entity has only the one bundle with the same name as the entity.
The code to create the URL in hook_menu:
$items['admin/appointments/appointments_statii/%/edit'] = array(
'title' => 'Edit appointment status',
'description' => 'Edit the parameters of the selected status code',
'page callback' => 'drupal_get_form',
'page arguments' => array('appointments_status_edit_form',3),
'access arguments' => array('access administration pages'),
'type' => MENU_CALLBACK,
);
The form function is:
function appointments_status_edit_form($form, &$form_state) {
// Get the status id from the form_state args
$status_id = $form_state['build_info']['args'][0];
// Load the chosen status entity
$status = entity_load_single('appointments_status', $status_id);
// Set up the fields for the form
field_attach_form('appointments_status', $status, $form, $form_state);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save changes',
'#weight' => 99,
);
return $form;
}
I have used the Devel module's dpm to check that the data is loaded correctly by entity_load_single and it is.
Thanks
Rory
I have answered my own question!
I was also programmatically loading some entities and was not loading this field with the numbers that a list_text field stores, instead I was loading the visual text.
I used a metadata wrapper and the code looked like this:
$w_appointments_status->$appointments_availability= 'Yes';
I changed it to:
$w_appointments_status->$appointments_availability = 2;
In this example 'Yes' was the third allowed value - hence 2.
So the code in my question was in fact correct although I have since added 'widget' and 'formatter' parameters to the instance.
I am sorry if this got some of you scratching your heads thinking ' but that code is correct'!!
Regards
Rory
I am trying to get messages with their corresponding users, in order to display a chat list of profiles with their last message in chronological order.
array(
0 => array(
'Recepient' => array(
'id' => ...
'name' => ...
...
),
'Message' => array(
'content' => ...
'created' => ...
...
)
),
1 => ...
)
and in order to retrieve the results, I've written this find() method:
$msgs = $this->Message->find('all', array(
'group' => array('Recepient.id'),
'order'=>'Message.created DESC',
'conditions'=>
array(
'OR'=> array(
array('recepient_id'=>$pid),
array('sender_id' => $pid)
)
)
));
What I have:
message with corresponding "Recepient",
in chronological order
The problem:
the query DOES NOT retrieve the most recent message from $recepient_id/$sender_id combination.
So instead of list of users with the last message, I have a list of users with a message. What's wrong with my query? Thanks for help!
METHOD 2 results
I've created "chat_id" field in the database which is basically recepient_id+sender_id sorted alphabetically (because if user1 send user2 a message user1 is sender, later when user2 responds, he becomes the sender, so sorting will ensure two users will always have the same chat_id).
Than I added DISTINCT to the query:
$this->Message->recursive = 0;
$msgs = $this->Message->find('all', array(
'fields' => array('DISTINCT Message.chat_id','Message.*','Recepient.*'),
'order'=>'Message.created DESC',
'conditions'=>
array(
'OR'=> array(
array('recepient_id'=>$pid),
array('sender_id' => $pid)
)
)
));
and it does NOT work! I am now getting multiple messages for the same conversation.
If I remove Message fields and Recipient fields from the query, I get correct amount of "chats".
'fields' => array('DISTINCT Message.chat_id'),
but that's not the solution.
CakePHP version 2.7.0
MySQL DB
METHOD 3 results
$msgs = $this->Message->find('all', array(
'order'=>'Message.created DESC',
'fields' => 'recepient_id, content, max(Message.created) as max_created',
'group'=>'recepient_id',
// 'contain' => array('Recepient'),
'conditions'=>array( 'chat_id' => $chats )
));
I gave up on single-find method to resolve this, so now 1.I am getting list of chats, 2.I want to find the last message from each chat.
Acording to http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ my find query should work. What happens is
(int) 0 => array(
'Message' => array(
'recepient_id' => '55e6d764-1444-4aad-a909-9042f07f76da',
'content' => '1st msg',
'created' => '2015-09-20 18:24:17',
'created_nice' => '2 hours ago'
),
(int) 0 => array(
'max_created' => '2015-09-20 18:24:28'
)
),
the field max(Message.created) is indeed showing the latest message from conversation but the 0=>Message array is for different message! As you can see $results[0]['Message']['created'] time and $results[0][0]['max_created'] are different!
Finally! Working solution:
$db = $this->Message->getDataSource();
$chats = $db->fetchAll(
'select id from (select * from messages order by created desc) Message
where recepient_id = "'.$pid.'" or sender_id = "'.$pid.'"
group by `chat_id`'
);
Above code will retrieve all messages, following the requirements. You can
a) SINGLE QUERY add mysql JOIN to include associated model (we keep the code single-query neat)
b) TWO QUERIES BUT SMARTER easier method, to select just "ids" of the messages and create another query that will have cake's build in containable behaviour. This might be better also because Behaviours will be applied.
You will have array tree as a result, to dig out the actual ids for next query use following:
$chats = Set::extract("/Message/id",$chats);
c) translate the solution to CakePHP query builder... :) up for a challenge?
Here is the validation rule:
'name'=>array(
'Please enter customer\'s name'=>array(
'rule'=>'notEmpty',
'message'=>'Please enter customer\'s name'
),
'Unique' => array(
'rule' => array('nameIsUniqueForCompany'),
'message' => 'Customer with these initials already exists'
)
),
public function initialsAreUniqueForCompany($data){
$company_id = $this->data['Customer']['company_id'];
$initials = $this->data['Customer']['initials'];
if($this->find('first', array('conditions'=>array('initials'=>$initials, 'company_id'=>$company_id))))
{
return false;
}
return true;
}
The problem is that the rule is applied to a current object. Let's say I want to update a customer named 'ABC', but I just change his phone number. The application will then look if the name ABC already exists and it will ofcourse find it and will not update.
any ideas?
Your implementation is flawed:
By design your validation rule will always only work for add, not for edit.
On edit, you need to also take the current ID into consideration.
Check out my http://www.dereuromark.de/2011/10/07/maximum-power-for-your-validation-rules/ post. The part about uniqueness contains a working approach.
For you that means, using my enhanced unique validation method:
'name' => array(
'validate' => array(
'rule' => array('validateUnique', array('company_id')),
'message' => 'Customer with these initials already exists',
),
),
Basically, you always submit the id on edit (via hidden form field - automatically done by baked forms anyway), and remove it from the find result by using "id != current" in your conditions:
$this->alias . '.id !=' => $id
Reasoning: The currently edited record should not trigger the validation rule error, of course :)
You can pass something like this:
'email' => array(
'unique' => array(
'rule' => 'isUnique',
'message'=>'This e-mail has been used already',
'on'=>'create'
)
)
There is a node 'on'=>'create' in the array, which apply the rule only when the record has been created. Ofcourse there is also 'on'=>'update' and this will apply when the record is updated.
For update validation you should consider custom rule where you are checking row id to be different.
In your controller
public function edit($id = null) {
if ($this->request->isPut() || $this->request->isPost()) {
if ($this->YourModel->save()){
// do what ever you want
}
} else {
$this->request->data = $this->YourModel->read(null, $id)
}
}
and in your edit.ctp file
echo $this->Form->create('YourModel');
echo $this->Form->hidden('id');
please let me know if you found any problem, glad to help you.
you can try
'subscription_name' => array
(
'required'=>true,
'rule' => 'notEmpty',
'message' => 'Please enter Package name.',
'Unique'=>array(
'rule' => 'isUnique',
'message'=>'package name is already exit..'
)
),
Bounty:
+500 rep bounty to a GOOD solution. I've seriously banged my head against this wall for 2 weeks now, and am ready for help.
Tables/Models (simplified to show associations)
nodes
id
name
node_type_id
node_associations
id
node_id
other_node_id
node_types
id
name
General Idea:
A user can create node types (example "TV Stations", "TV Shows", and "Actors"...anything). If I knew ahead of time what the node types were and the associations between each, I'd just make models for them - but I want this to be very open-ended so the user can create any node-types they want. Then, each node (of a specific node-type) can relate to any other node of any other node-type.
Description and what I've tried:
Every node should be able to be related to any/every other node.
My assumption is that to do that, I must have an association table - so I made one called "node_associations" which has node_id and other_node_id.
Then I set up my association (using hasMany through I believe):
(below is my best recollection of my set-up... it might be slightly off)
//Node model
public $hasMany = array(
'Node' => array(
'className' => 'NodeAssociation',
'foreignKey' => 'node_id'
),
'OtherNode' => array(
'className' => 'NodeAssociation',
'foreignKey' => 'other_node_id'
)
);
//NodeAssociation model
public $belongsTo = array(
'Node' => array(
'className' => 'Node',
'foreignKey' => 'node_id'
),
'OtherNode' => array(
'className' => 'Node',
'foreignKey' => 'other_node_id'
)
);
At first, I thought I had it - that this made sense. But then I started trying to retrieve the data, and have been banging my head against the wall for the past two weeks.
Example Problem(s):
Lets say I have a the following nodes:
NBC
ER
George Clooney
Anthony Edwards
Tonight Show: Leno
Jay Leno
Fox
Family Guy
How can I set up my data structure to be able to pull the all TV Stations, and contain their TV Shows, which contain their Actors (as example)? This would be SIMPLE with normal model setup:
$this->TvStation->find('all', array(
'contain' => array(
'TvShow' => array(
'Actor'
)
)
));
And then, maybe I want to retrieve all male Actors and contain the TV Show which contain the TV Station. Or TV Shows that start at 9pm, and contain it's actor(s) and it's station...etc etc.
But - with HABTM or HasMany Through self (and more importantly, and unknown data set), I wouldn't know which field (node_id or other_node_id) the model is, and overall just can't wrap my head around how I'd get the content.
The Idea
Let's try to solve this with convention, node_id will be the model who's alias comes alphabetically first and other_node_id will be the one that comes second.
For each contained model, we create a HABTM association on-the-fly to Node class, creating an alias for each association (see bindNodes and bindNode method).
Each table we query we add an extra condition on node_type_id to only return results for that type of node. The id of NodeType is selected via getNodeTypeId() and should be cached.
For filtering results using condition in deeply related associations, you would need to manually add extra join, creating a join for each jointable with a unique alias and then joining each node type itself with an alias to be able to apply the conditions (ex. selecting all TvChannels that have Actor x). Create a helper method for this in Node class.
Notes
I used foreignKey for node_id and associationForeignKey for other_node_id for my demo.
Node (incomplete)
<?php
/**
* #property Model NodeType
*/
class Node extends AppModel {
public $useTable = 'nodes';
public $belongsTo = [
'NodeType',
];
public function findNodes($type = 'first', $query = []) {
$node = ClassRegistry::init(['class' => 'Node', 'alias' => $query['node']]);
return $node->find($type, $query);
}
// TODO: cache this
public function nodeTypeId($name = null) {
if ($name === null) {
$name = $this->alias;
}
return $this->NodeType->field('id', ['name' => $name]);
}
public function find($type = 'first', $query = []) {
$query = array_merge_recursive($query, ['conditions' => ["{$this->alias}.node_type_id" => $this->nodeTypeId()]]);
if (!empty($query['contain'])) {
$query['contain'] = $this->bindNodes($query['contain']);
}
return parent::find($type, $query);
}
// could be done better
public function bindNodes($contain) {
$parsed = [];
foreach($contain as $assoc => $deeperAssoc) {
if (is_numeric($assoc)) {
$assoc = $deeperAssoc;
$deeperAssoc = [];
}
if (in_array($assoc, ['conditions', 'order', 'offset', 'limit', 'fields'])) {
continue;
}
$parsed[$assoc] = array_merge_recursive($deeperAssoc, [
'conditions' => [
"{$assoc}.node_type_id" => $this->nodeTypeId($assoc),
],
]);
$this->bindNode($assoc);
if (!empty($deeperAssoc)) {
$parsed[$assoc] = array_merge($parsed[$assoc], $this->{$assoc}->bindNodes($deeperAssoc));
foreach($parsed[$assoc] as $k => $v) {
if (is_numeric($k)) {
unset($parsed[$assoc][$k]);
}
}
}
}
return $parsed;
}
public function bindNode($alias) {
$models = [$this->alias, $alias];
sort($models);
$this->bindModel(array(
'hasAndBelongsToMany' => array(
$alias => array(
'className' => 'Node',
'foreignKey' => ($models[0] === $this->alias) ? 'foreignKey' : 'associationForeignKey',
'associationForeignKey' => ($models[0] === $alias) ? 'foreignKey' : 'associationForeignKey',
'joinTable' => 'node_associations',
)
)
), false);
}
}
Example
$results = $this->Node->findNodes('all', [
'node' => 'TvStation', // the top-level node to fetch
'contain' => [ // all child associated nodes to fetch
'TvShow' => [
'Actor',
]
],
]);
I think you have incorrect relations between your models. I guess it will be enough with:
// Node Model
public $hasAdBelongsToMany = array(
'AssociatedNode' => array(
'className' => 'Node',
'foreignKey' => 'node_id'
'associationForeignKey' => 'associated_node_id',
'joinTable' => 'nodes_nodes'
)
);
// Tables
nodes
id
name
node_type_id
nodes_nodes
id
node_id
associated_node_id
node_types
id
name
Then you can try using ContainableBehavior to fetch your data. For Example, to find all TVShows belonging to a TVStation:
$options = array(
'contain' => array(
'AssociatedNode' => array(
'conditions' => array(
'AssociatedNode.node_type_id' => $id_of_tvshows_type
)
)
),
conditions => array(
'node_type_id' => $id_of_tvstations_type
)
);
$nodes = $this->Node->find('all', $options);
EDIT :
You can even have second level conditions (see last example on this section, look at the 'Tag' model conditions). Try this:
$options = array(
'contain' => array(
'AssociatedNode' => array(
'conditions' => array(
'AssociatedNode.node_type_id' => $id_of_tvshows_type
),
'AssociatedNode' => array(
'conditions' => array( 'AssociatedNode.type_id' => $id_of_actors_type)
)
)
),
conditions => array(
'node_type_id' => $id_of_tvstations_type
)
);
$nodes = $this->Node->find('all', $options);
I think unfortunately part of the problem is that you want your solution to contain user data in the code. Since all your nodes types are user data, you want to avoid trying to use those as the classes methods in your application, as there could be infinite of them. Instead I would try and create methods that model the data operations you want to have.
One omission I see in the provided data model is a way to record the relationships between types. In your example you mention a relationship between TvStation -> TvShows -> Actor etc. But where are these data relationships defined/stored? With all of your node types being user defined data, I think you'll want to/need to record store those relationships somewhere. It seems like node_types needs some additional meta data about what the valid or desired child types for a given type are. Having this recorded somewhere might make your situation a bit simpler when creating queries. It might help to think of all the questions or queries you're going to ask the database. If you cannot answer all those questions with data that is in the database, then you are probably missing some tables. Model associations are just a proxy for data relations that already exist in your tables. If there are gaps there are probably gaps in your data model.
I don't think this is the answer you're looking for but hopefully it helps you find the right one.
Why don't you create a method in the node model?
Something like:
<?php
// first argument is a nested array filled with integers
(corresponding to node_type_id)
//second one id of a node
//third one corresponds to the data you want(empty at beginning in most case)
public function custom_find($conditions,$id,&$array){
//there may several type of nodes wanted: for instances actors and director of a serie, so we loop
foreach($conditions as $key_condition=>$condition){
//test to know if we have reached the 'bottom' of the nested array: if yes it will be an integer '2', if no it will be an array like '2'=>array(...)
if(is_array($condition))){
//this is the case where there is deeper levels remaining
//a find request: we ask for the node defined by its id,
//and the child nodes constrained by their type: ex: all actors of "Breaking Bad"
$this->id=$id;
$result=$this->find('all',array(
'contain' => array(
'OtherNode' => array(
'conditions'=>array('node_type_id'=>$key_condition)
)
)
)
);
//we add to $array the nodes found. Ex: we add all the actors of the serie, with type_id as key
$array[$key_condition]=$result['OtherNode'];
//Then on each node we just defined we call the function recursively. Note it's $condition not $conditions
foreach($array[$key_condition] as &$value){
$this->custom_find($condition,$value['Node']['id'],$value);
}
}else{
//if we simply add data
$this->id=$id;
$result=$this->find('all',array(
'contain' => array(
'OtherNode' => array(
'conditions'=>array('node_type_id'=>$value)
)
)
)
);
$array[$condition]=$result['OtherNode'];
}
}
}
That code is almost certainly wrong, it's just to give you an idea of what I mean.
Edit:
What it does:
it's a recursive function that takes a nested array of conditions and the id of a node and gives back nested array of nodes.
For instance: $conditions=array('2','4'=>array('5','6'=>array('4')))
How it works:
For a single node it gives back all the child nodes corresponding to the condition in the array: then it does the same for the children with the conditions one level deeper, until there is no more levels left.
I have 3 models: Student, Course and StudentCourse. Course 'hasAndBelongsToMany' Student, Student 'hasMany' Course, and StudentCourse 'belongsTo' Student and Course. Before a student can signup for a course, I need to check a few things (ie: is the course full, has that student taken that course in the past, etc). I can handle the logic inside of the function, but which model should I place that function under? And, how should it be called? One way I thought of was:
// Student Model
public function canSignupForCourse($studentId, $courseId) {
// is the course full?
// have they signed up before, etc
// return either true or false
}
// Could it then be called anywhere as:
if($this->Student->canSignupForCourse($studentId, $courseId)) {
// etc
}
Or, is there a better/easier way to do it (and, do I need to send both the studentid and courseid each time)?
I think the best thing to do is to try to implement these restrictions as validation rule in the model.
According to your description, applying a student for a course is done by creating a new StudentCourse, so that's where you should try to fit the validation rules, for example:
// StudentCourse.php
$validate = array(
'course_id' => array(
'rule' => array('maxStudents', 30),
'required' => true,
'on' => 'create'
)
)
function maxStudents($check, $max) {
$count = $this->find('count', array(
'conditions' => array('course_id' => $check['course_id']),
'contain' => false
));
return $count < $max;
}
I'd first check out the example in the manual here: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany-through-the-join-model
This should convince you that you should probably make Student 'hasAndBelongsToMany' course as well (since Course has student but student doesnt belongto course in your model relationships)
You can then define that relationship model as something like CourseMembership (as in the example link above)
I would then put canSignupForCourse function in that model. However I'd probably split this function up into a few separate ones, like courseNotFull and courseNotTakenBefore
I would then put these functions into the model's validate object like so:
public $validate = array(
'course_id' => array(
'courseNotFull' => array(
'rule' => array('courseNotFull'),
'message' => "Course is full",
),
'courseNotTakenBefore' => array(
'rule' => array('courseNotTakenBefore'),
'message' => "Student has taken course before",
)
)
);
And define the model functions like this:
function courseNotFull() {
$this->Course->id = $this->data[$this->alias]['course_id'];
$course = $this->Course->read();
return $course['Course']['isFull'];
}
function courseTakenBefore() {
$this->Student->id = $this->data[$this->alias]['student_id'];
$this->Course->id = $this->data[$this->alias]['course_id'];
$course = $this->Student->Course->findById($this->Course->id);
return $course;
}
Now whenever you try to save or validate() CourseMembership, the validate will return a descriptive error message if it is unsuccessful.