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








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;
}