Great new release + new get children feature

8 replies [Last post]
aidan
Offline
Joined: 10/28/2009
Status: 
Answered

Hi again,

Great job on the new release and renaming the modules - much better name and it's great having the packages bundled the way you have them now.

I have a few small suggestions for improvements:

On some pages you have:

    $output .= theme('table', $headers, $rows, $attr);
    print theme('page', $output);

This can be replaced with:
 
    return theme('table', $headers, $rows, $attr);

Drupal will handle the rest, there's no need to manually call theme('page'), which is especially helpful if you're not displaying a page, e.g. embedding one of your pages into something like I'm doing.

Another small thing with the menu's, I realise you probably can't change them now without upsetting your existing userbase, but "admin/build/ms_affiliates" should really be "admin/user/ms_affiliates" or even "admin/user/pay_affiliates", I don't think "Structure" is the right parent for this item.

Also I wrote this function, which I think I think compliments your get_parents function. It would be handy to have included in your next release, if you think it's useful.

/**
 * Get all of the children of a user
 *
 * @param int $uid The UID whose children are to be found
 * @param int $max_depth The maximum depth
 * @return array Keyed array with UID as the key, and value as the depth
 */
function ms_affiliates_get_children($uid, $max_depth) {
  $stack    = array(); // FILO stack of UIDs
  $children = array(); // Keyed array with depth as value
  $depth    = 0;
 
  // Seed and iterate the stack
  $stack[] = $uid;
  while ($uid = array_pop($stack)) {
    $depth++;
   
    // Abort if we passed max_depth
    if ($depth > $max_depth) {
      continue;
    }
   
    // Find all affiliates for current uid
    $res = db_query('SELECT uid, aid FROM {ms_affiliates_genealogy} WHERE aid = %d', $uid);
    while ($row = db_fetch_array($res)) {
      $stack[]               = $row['uid'];
      $children[$row['uid']] = $depth;
    }
  }
 
  return $children;
}

You'll notice it avoids recursion and uses a FILO stack - I see a lot of fairly difficult to follow recursion in your code, you might find the stack based approach easier to work with. As well as being faster, it might avoid some code duplication problems with the way you've done small parts of the module currently.

Thanks again for this great set of modules,
Aidan

aidan
Offline
Joined: 10/28/2009
There was a bug in the

There was a bug in the version I posted, I didn't think about the complex structure of the genealogy. This version works better!

/**
 * Get all of the children of a user
 *
 * This approach uses a stack based query, thus avoiding recursion
 *
 * @param int $uid The UID whose children are to be found
 * @param int $max_depth The maximum depth
 */
function radar_affiliates_get_children($uid, $max_depth) {
  $stack     = array(); // FILO stack of UIDs
  $children  = array(); // Keyed array with depth as value
 
  // Seed and iterate the stack
  $stack[]      = $uid;
  while ($uid = array_pop($stack)) {

    // Find all affiliates for current uid
    $res = db_query('SELECT uid, aid FROM {ms_affiliates_genealogy} WHERE aid = %d', $uid);
   
    // Check the depth
    if ($children[$uid] > $max_depth - 1) {
      continue;
    }
   
    // Add all the children
    while ($row = db_fetch_array($res)) {
      $stack[]               = $row['uid'];
      $children[$row['uid']] = $children[$uid] + 1;
    }
  }
   
  return $children;
}

admin
Offline
Joined: 09/17/2008
Aidan, I agree that having a

Aidan,
I agree that having a get_children() function would be beneficial. I have added your function to the module, and I will implement it in a future version to generate the Referrals Tree more efficiently.

As for the theme('table'... functions, I still have to return the theme('page'... version because $output holds more than just the table, often times a subtitle or some text.

I agree with you about the menu, and I have moved it to admin/user/pay_affiliates

Sincerely,
Leighton Whiting

aidan
Offline
Joined: 10/28/2009
Oh, right you are about the

Oh, right you are about the other things like headings ... what I should have written was "return $output;". That should be exactly the same as "echo theme('page', $output);".

aidan
Offline
Joined: 10/28/2009
Did you give this a shot?

Did you give this a shot? Works as expected?

Leighton Whiting
Offline
Joined: 06/02/2009
I tried changing them, but it

I tried changing them, but it ended up giving me a weird display of the page inside of the content area. So in the end I just kept the original.

Sincerely,
Leighton Whiting

aidan
Offline
Joined: 10/28/2009
I think you might be doing

I think you might be doing return theme('page', ...) rather than return $output?

I need this fixed otherwise I can't embed your pages with ajax, or display your pages in other pages.

 

Cheers,

Leighton Whiting
Offline
Joined: 06/02/2009
Aidan, I got it working, I

Aidan,
I got it working, I will be putting this in the new release.

Sincerely,
Leighton Whiting

aidan
Offline
Joined: 10/28/2009
Great thanks!

Great thanks!

Twitter Feed