Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General stuff #298

Closed
66 changes: 66 additions & 0 deletions Sources/Load.php
Expand Up @@ -1242,6 +1242,72 @@ function loadMemberContext($user, $display_custom_fields = false)
return true;
}

/**
* Loads the user's custom profile fields
*
* @param mixed $users either an integer or an array of integers
* @param mixed $param either a string or an array of strings with profile fields names
* @return array
*/
function loadMemberCustomFields($users, $params)
{
global $smcFunc, $txt, $scripturl, $settings;

// Do not waste my time...
if (empty($users) || empty($params))
return false;

// Make sure it's an array.
$users = !is_array($users) ? array($users) : array_unique($users);
$params = !is_array($params) ? array($params) : array_unique($params);
$return = array();

$request = $smcFunc['db_query']('', '
SELECT c.id_field, c.col_name, c.field_name, c.field_desc, c.field_type, c.field_length, c.field_options, c.mask, show_reg,
c.show_display, c.show_profile, c.private, c.active, c.bbc, c.can_search, c.default_value, c.enclose, c.placement, t.variable, t.value, t.id_member
FROM {db_prefix}themes AS t
LEFT JOIN {db_prefix}custom_fields AS c ON (c.col_name = t.variable)
WHERE id_member' . (count($users) == 1 ? ' = {int:loaded_ids} ' : ' IN ({array_int:loaded_ids}) ') .
'AND variable' . (count($params) == 1 ? ' = {string:params}' : ' IN ({array_string:params})'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use just IN?
That would save you some comparisons:

    WHERE id_member IN ({array_int:loaded_ids})
      AND variable IN ({array_string:params})',

and here below:

      'loaded_ids' => $users,
      'params' => $params,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because thats how SMF do things... I just followed and copy/paste SMF coding style.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay...never checked before... 👼

/me feels is a bit odd
I'm not sure if there is any practical difference...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't if you aren't picky in terms of performance, it is if you want the code to be more organized but there is a bazillion other places where SMF does this, might as well go with the flow.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SMF avoids using 'IN' in the query, for the query to use the index instead. There's only an index on id_member iirc however.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my (quick) test with EXPLAIN, MySQL is smart enough to understand there is just one value and uses the index anyway. Of course if there is more than one then it doesn't.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks for checking it out!

array(
'loaded_ids' => (int) count($users) == 1 ? $users[0] : $users,
'params' => (string) count($params) == 1 ? $params[0] : $params,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what is the intention of these casts, but they won't work as (probably) expected

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.___. again, I just copy/paste SMF code... you should point that out to whoever wrote that ten years ago.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please point me to an example?

);

while ($row = $smcFunc['db_fetch_assoc']($request))
{
// BBC?
if (!empty($row['bbc']))
$row['value'] = parse_bbc($row['value']);

// ... or checkbox?
elseif (isset($row['type']) && $row['type'] == 'check')
$row['value'] = !empty($row['value']) ? $txt['yes'] : $txt['no'];

// Enclosing the user input within some other text?
if (!empty($row['enclose']))
$row['value'] = strtr($row['enclose'], array(
'{SCRIPTURL}' => $scripturl,
'{IMAGES_URL}' => $settings['images_url'],
'{DEFAULT_IMAGES_URL}' => $settings['default_images_url'],
'{INPUT}' => $row['value'],
));

// Send a simple array if there is just 1 param
if (count($params) == 1)
$return[$row['id_member']] = $row;

// More than 1? knock yourself out...
else
$return[$row['id_member']][$row['id_field']] = $row;
}

$smcFunc['db_free_result']($request);

return !empty($return) ? $return : false;
}

/**
* Loads information about what browser the user is viewing with and places it in $context
* - uses the class from Class-BrowerDetect.php
Expand Down
2 changes: 1 addition & 1 deletion Sources/ManageBans.php
Expand Up @@ -82,7 +82,7 @@ function Ban()
),
);

// Call the right function for this sub-acton.
// Call the right function for this sub-action.
$subActions[$_REQUEST['sa']]();
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/ManageLanguages.php
Expand Up @@ -55,7 +55,7 @@ function ManageLanguages()
'description' => $txt['language_description'],
);

// Call the right function for this sub-acton.
// Call the right function for this sub-action.
$subActions[$_REQUEST['sa']]();
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/ManageMail.php
Expand Up @@ -56,7 +56,7 @@ function ManageMail()
'description' => $txt['mailqueue_desc'],
);

// Call the right function for this sub-acton.
// Call the right function for this sub-action.
$subActions[$_REQUEST['sa']]();
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/ManagePaid.php
Expand Up @@ -65,7 +65,7 @@ function ManagePaidSubscriptions()
),
);

// Call the right function for this sub-acton.
// Call the right function for this sub-action.
$subActions[$_REQUEST['sa']][0]();
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/ManageSearch.php
Expand Up @@ -73,7 +73,7 @@ function ManageSearch()
),
);

// Call the right function for this sub-acton.
// Call the right function for this sub-action.
$subActions[$_REQUEST['sa']]();
}

Expand Down
12 changes: 6 additions & 6 deletions Sources/ManageSettings.php
Expand Up @@ -18,10 +18,10 @@
die('Hacking attempt...');

/**
* This just avoids some repetition.
* This function makes sure the requested subaction does exists, if it doesn't, it sets a default action or.
*
* @param array $subActions = array()
* @param string $defaultAction = ''
* @param array $subActions = array() An array containing all possible subactions.
* @param string $defaultAction = '' the default action to be called if no valid subaction was found.
*/
function loadGeneralSettingParameters($subActions = array(), $defaultAction = '')
{
Expand Down Expand Up @@ -94,7 +94,7 @@ function ModifyFeatureSettings()
),
);

// Call the right function for this sub-acton.
// Call the right function for this sub-action.
$subActions[$_REQUEST['sa']]();
}

Expand Down Expand Up @@ -137,7 +137,7 @@ function ModifySecuritySettings()
),
);

// Call the right function for this sub-acton.
// Call the right function for this sub-action.
$subActions[$_REQUEST['sa']]();
}

Expand Down Expand Up @@ -174,7 +174,7 @@ function ModifyModSettings()
),
);

// Call the right function for this sub-acton.
// Call the right function for this sub-action.
$subActions[$_REQUEST['sa']]();
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/ManageSmileys.php
Expand Up @@ -102,7 +102,7 @@ function ManageSmileys()
$context[$context['admin_menu_name']]['tab_data']['tabs']['setorder']['disabled'] = true;
}

// Call the right function for this sub-acton.
// Call the right function for this sub-action.
$subActions[$_REQUEST['sa']]();
}

Expand Down