I'm developing subscriptions_og with your latest 2.0-beta10, so, current function subscriptions_page_user_overview() at subscriptions.admin.inc makes a custom query for getting a count of subscriptions to a blog:

  // blog is a subtype of 'type' -- must do it separately
  $count['blog']['author_uid'] = db_result(db_query("SELECT count(*) FROM {subscriptions} WHERE module = 'node' AND field = 'type' AND value = 'blog' AND recipient_uid = %d", $uid));

In order to provide similar functionality(with a hook), I included a new hook within this patch just after previous code:

  // develCuy: Organic groups may have many subtypes of 'types' -- must do it separately
  $count = module_invoke_all('count_user_subscriptions', $count, $uid);

It opens the door for another modules to provide a count of user subscriptions. This is critical for subscriptions_og because there is not another elegant way I found.

Additionally, I've added a the new function subscriptions_get_full_subscription() at subscriptions.module, similar to subscriptions_get_subscription(), but provides an object with full subscription fields:

/**
 * Return all subscription fields for given parameters
 */
function subscriptions_get_full_subscription($uid, $module, $field, $value, $author_uid = -1) {
  static $subscriptions;

  if (!isset($subscriptions[$uid][$module][$field][$value][$author_uid])) {
    $sql = "SELECT * FROM {subscriptions} WHERE module = '%s' AND field = '%s' AND value = '%s' AND author_uid = %d AND recipient_uid = %d";
    $subscriptions[$uid][$module][$field][$value][$author_uid] = db_fetch_object(db_query($sql, $module, $field, $value, $author_uid, $uid));
  }

  return $subscriptions[$uid][$module][$field][$value][$author_uid];
}

This function is used by current subscriptions_og 5.x branch

Blessings!

CommentFileSizeAuthor
subscriptions-newhook.patch2.37 KBdevelcuy

Comments

salvis’s picture

Good idea with the hook — much better than may kludge.

And the function is ok, too, but I wonder whether the caching is a beneficial. How do you use this function? Are you (or another module) really going to ask twice for the same record? We could be using up some serious memory here...

develcuy’s picture

OK salvis, I agree on that, because it is used only for subscriptions control. Code should look like:

/**
 * Return all subscription fields for given parameters
 */
function subscriptions_get_full_subscription($uid, $module, $field, $value, $author_uid = -1) {
   $sql = "SELECT * FROM {subscriptions} WHERE module = '%s' AND field = '%s' AND value = '%s' AND author_uid = %d AND recipient_uid = %d";
   return db_fetch_object(db_query($sql, $module, $field, $value, $author_uid, $uid));
}

Blessings.

salvis’s picture

Version: 5.x-2.0-beta10 » 5.x-2.0-beta11
Status: Needs review » Fixed

@develCuy: your OP code was buggy: module_invoke_all() returns nothing if no module implements the hook, and this caused all the counts to go to 0.

It's working fine with subscriptions_blog_ui.module — please let me know whether it works with subscriptions_og.module and also with both modules.

develcuy’s picture

Thank you Salvis, I've updated subscriptions_og module to work with 2.0-beta11, it is working fine.

Blessings!

salvis’s picture

Good, thanks.

Please try enabling subscriptions_blog_ui.module at the same time and check whether the two get along.

develcuy’s picture

I've checked activating all subscriptions modules and subscriptions_og, blogs and og counts are fine activating only one both or no-one.

Blessings!

salvis’s picture

Perfect, thanks!

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

gustav’s picture

Title: New hook and function needed by subscriptions_og module » Get rid of hook_count_user_subscriptions
Status: Closed (fixed) » Active

I do not like this extra hook at all. Instead of introducing this hook the subscriptions_blog_ui module should be rewritten to use its own unique combination of ('module', 'field') instead of reusing ('node', 'type') that is already used by subscriptions_content. Then the existing way of counting subscriptions

  $counts = array();
  $result = db_query("SELECT module, field, count(1) as number FROM {subscriptions} WHERE recipient_uid = %d GROUP BY module, field", $uid);
  while ($subs = db_fetch_object($result)) {
    if (!empty($subs->module)) {
      $counts[$subs->module][$subs->field] = $subs->number;
    }
  }

will work for all modules and no extra hook is needed.

gustav’s picture

Priority: Critical » Normal

Sorry, I hadn't meant to mark this as critical.

salvis’s picture

What's wrong with the hook? No one needs to use it. Has subscriptions_og stopped using it?

The problem with subscriptions_blog_ui is the "_ui" part. It's not a full add-on module, it only provides a user interface for the 'blog' content type, but all the work is done by subscriptions_content (content type subscriptions). Before the hook, there was a very kludgy solution in subscriptions_page_user_overview() for getting the blog subscriptions count.

Is it the name of the hook that you don't like? I didn't like it either and I talked about it to chx at the time, but he said it was long and unique enough to not be a problem.

salvis’s picture

Status: Active » Closed (fixed)