View source
<?php
include_once 'commons_follow_node.features.inc';
function commons_follow_node_commons_follow_get_nids($account, $options) {
$flag_ids = commons_follow_get_subscription_flags_ids('node');
if (empty($flag_ids)) {
return array();
}
$query = db_select('flagging', 'f');
if (!empty($options['range'])) {
$query
->range(0, $options['range']);
}
$result = $query
->condition('fid', $flag_ids, 'IN')
->condition('uid', $account->uid, '=')
->condition('entity_type', 'node', '=')
->fields('f', array(
'entity_id',
))
->execute()
->fetchAll();
$nids = array();
foreach ($result as $row) {
$nids[] = (int) $row->entity_id;
}
return $nids;
}
function commons_follow_node_commons_follow_get_message_ids(&$followed_mids, $followed_content = array()) {
$result = db_query("SELECT m.mid AS mid FROM {message} m INNER JOIN {field_data_field_target_nodes} tn ON m.mid=tn.entity_id WHERE tn.field_target_nodes_target_id IN (:nids)", array(
':nids' => array_values($followed_content['commons_follow_node']),
));
foreach ($result as $this_result) {
$followed_mids[] = $this_result->mid;
}
}
function commons_follow_node_node_insert($node) {
commons_follow_node_follow_node($node->nid, $node->type, $node->uid);
}
function commons_follow_node_comment_insert($comment) {
$content_type = str_replace('comment_node_', '', $comment->node_type);
commons_follow_node_follow_node($comment->nid, $content_type, $comment->uid);
}
function commons_follow_node_follow_node($nid, $content_type, $user_id = NULL) {
if ($flag = flag_get_flag('commons_follow_node')) {
if (in_array($content_type, $flag->types)) {
$account = isset($user_id) ? user_load($user_id) : NULL;
$flag
->flag('flag', $nid, $account);
}
}
}