function recipe_unit_fuzzymatch in Recipe 6
Same name and namespace in other branches
- 7.2 recipe.admin.inc \recipe_unit_fuzzymatch()
- 7 recipe.module \recipe_unit_fuzzymatch()
Fetch a recipe_unit.
Parameters
$recipe_name_or_abbrev: A string representing a unit of measure abbreviation or a unit name.
Return value
A recipe_unit stdClass upon successful load or FALSE
3 calls to recipe_unit_fuzzymatch()
- recipe_import_element_end in plugins/
recipe_recipeML.module - Call-back function used by the XML parser.
- recipe_mastercook4_import_single in plugins/
recipe_mastercook4.module - recipe_plaintext_import in plugins/
recipe_plaintext.module - Parsing instance for plain text recipes
File
- ./
recipe.module, line 1593 - recipe.module - share recipes
Code
function recipe_unit_fuzzymatch($recipe_name_or_abbrev, $reset = FALSE) {
static $units;
// Empty strings should use the default non-printing 'Unit'.
if ($recipe_name_or_abbrev == '') {
$recipe_name_or_abbrev = 'Unit';
}
if (!isset($units) || $reset) {
// Get all units to prepare for fuzzy match.
$units = array();
$order_by = '';
// US measure preferred.
if (variable_get('recipe_preferred_system_of_measure', 0) == 0) {
$order_by = 'order by metric asc';
}
else {
$order_by = 'order by metric desc';
}
$result = db_query("SELECT id, name, abbreviation, aliases FROM {recipe_unit} {$order_by}");
while ($row = db_fetch_object($result)) {
$units[] = $row;
}
}
// First pass unit case must match exactly( T=Tbsp, t=tsp ).
foreach ($units as $u) {
$pats = array();
// Add name pattern.
$pats[] = '^' . $u->name . 's{0,1}$';
// Add abbreviation pattern.
$pats[] = '^' . $u->abbreviation . 's{0,1}\\.{0,1}$';
// Add comma separated alias patterns.
$aliases = explode(',', $u->aliases);
foreach ($aliases as $alias) {
$pats[] = '^' . trim($alias) . 's{0,1}\\.{0,1}$';
}
$search_pat = implode('|', $pats);
if (preg_match("/{$search_pat}/", $recipe_name_or_abbrev)) {
return $u;
}
}
// Second pass unit case doesn't matter.
foreach ($units as $u) {
$pats = array();
// Add name pattern.
$pats[] = '^' . $u->name . 's{0,1}$';
// Add abbreviation pattern.
$pats[] = '^' . $u->abbreviation . 's{0,1}\\.{0,1}$';
// Add comma separated alias patterns.
$aliases = explode(',', $u->aliases);
foreach ($aliases as $alias) {
$pats[] = '^' . trim($alias) . 's{0,1}\\.{0,1}$';
}
$search_pat = implode('|', $pats);
if (preg_match("/{$search_pat}/i", $recipe_name_or_abbrev)) {
return $u;
}
}
return FALSE;
}