DrupalBin
Submit Code
About
Recent Posts
Fix for Code
Code
add_to_cart_by_sku_contents()
Fix for page.tpl.php
Page.tpl
foreach is beeing disregarded dispite variable is array. Please help. Outside Drupal it works as standalone.php file
foreach is beeing disregarded dispite variable is array inside drupal. Please help. Outside Drupal it works as standalone.php
Fix for filefield implementation in an admin page. "An HTTP error 0 occurred. /drupal/?q=filefield/ahah///"
PHP Code custom.module
Code
more
User login
Log in using OpenID:
What is OpenID?
Username:
*
Password:
*
Log in using OpenID
Cancel OpenID login
Create new account
Request new password
Tags
CCK
drupal
fapi
jquery
menu
module
php
simpletest
taxonomy
test
theme
views
more tags
Home
Fix for Code
View
Fix
November 30, 2008 - 8:21pm —
Wim Leers
/**
* Helper function to reconstruct the lineages given a set of selected items
* and the fact that the "save lineage" setting is enabled.
*
* Note that it's impossible to predict how many lineages if we know the
* number of selected items, exactly because the "save lineage" setting is
* enabled.
*
* Worst case time complexity is O(n^3), optimizations are still possible.
*
* @param $module
* The module that should be used for HS hooks.
* @param $selection
* The selection based on which a dropbox should be generated.
* @param $params
* Optional. An array of parameters, which may be necessary for some
* implementations.
* @return
* An array of dropbox lineages.
*/
function
_hierarchical_select_dropbox_reconstruct_lineages_save_lineage_enabled
(
$module
,
$selection
,
$params
)
{
// We have to reconstruct all lineages from the given set of selected items.
// That means: we have to reconstruct every possible combination!
$lineages
=
array
(
)
;
$root_level
=
module_invoke
(
$module
,
'hierarchical_select_root_level'
,
$params
)
;
foreach
(
$selection
as
$key
=
>
$item
)
{
// Create new lineage if the item can be found in the root level.
if
(
in_array
(
$item
,
array_keys
(
$root_level
)
)
)
{
$lineages
[
]
[
0
]
=
array
(
'value'
=
>
$item
,
'label'
=
>
$root_level
[
$item
]
)
;
unset
(
$selection
[
$key
]
)
;
}
}
// Keep on trying as long as at least one lineage has been extended.
$at_least_one
=
TRUE
;
for
(
$i
=
0
;
$at_least_one
;
$i
++
)
{
$at_least_one
=
FALSE
;
$num
=
count
(
$lineages
)
;
// Try to improve every lineage. Make sure we don't iterate over
// possibly new lineages.
for
(
$id
=
0
;
$id
<
$num
;
$id
++
)
{
$children
=
module_invoke
(
$module
,
'hierarchical_select_children'
,
$lineages
[
$id
]
[
$i
]
[
'value'
]
,
$params
)
;
$child_added_to_lineage
=
FALSE
;
foreach
(
array_keys
(
$children
)
as
$child
)
{
if
(
in_array
(
$child
,
$selection
)
)
{
if
(
!
$child_added_to_lineage
)
{
// Add the child to the lineage.
$lineages
[
$id
]
[
$i
+ 1
]
=
array
(
'value'
=
>
$child
,
'label'
=
>
$children
[
$child
]
)
;
$child_added_to_lineage
=
TRUE
;
$at_least_one
=
TRUE
;
}
else
{
// Create new lineage based on current one and add the child.
$lineage
=
$lineages
[
$id
]
;
$lineage
[
$i
+ 1
]
=
array
(
'value'
=
>
$child
,
'label'
=
>
$children
[
$child
]
)
;;
// Add the new lineage to the set of lineages
$lineages
[
]
=
$lineage
;
}
}
}
}
}
return
$lineages
;
}
hierarchical-select
Submit Fix
Summary:
Tags:
Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Show summary in full view
/** * Helper function to reconstruct the lineages given a set of selected items * and the fact that the "save lineage" setting is enabled. * * Note that it's impossible to predict how many lineages if we know the * number of selected items, exactly because the "save lineage" setting is * enabled. * * Worst case time complexity is O(n^3), optimizations are still possible. * * @param $module * The module that should be used for HS hooks. * @param $selection * The selection based on which a dropbox should be generated. * @param $params * Optional. An array of parameters, which may be necessary for some * implementations. * @return * An array of dropbox lineages. */ function _hierarchical_select_dropbox_reconstruct_lineages_save_lineage_enabled($module, $selection, $params) { // We have to reconstruct all lineages from the given set of selected items. // That means: we have to reconstruct every possible combination! $lineages = array(); $root_level = module_invoke($module, 'hierarchical_select_root_level', $params); foreach ($selection as $key => $item) { // Create new lineage if the item can be found in the root level. if (in_array($item, array_keys($root_level))) { $lineages[][0] = array('value' => $item, 'label' => $root_level[$item]); unset($selection[$key]); } } // Keep on trying as long as at least one lineage has been extended. $at_least_one = TRUE; for ($i = 0; $at_least_one; $i++) { $at_least_one = FALSE; $num = count($lineages); // Try to improve every lineage. Make sure we don't iterate over // possibly new lineages. for ($id = 0; $id < $num; $id++) { $children = module_invoke($module, 'hierarchical_select_children', $lineages[$id][$i]['value'], $params); $child_added_to_lineage = FALSE; foreach (array_keys($children) as $child) { if (in_array($child, $selection)) { if (!$child_added_to_lineage) { // Add the child to the lineage. $lineages[$id][$i + 1] = array('value' => $child, 'label' => $children[$child]); $child_added_to_lineage = TRUE; $at_least_one = TRUE; } else { // Create new lineage based on current one and add the child. $lineage = $lineages[$id]; $lineage[$i + 1] = array('value' => $child, 'label' => $children[$child]);; // Add the new lineage to the set of lineages $lineages[] = $lineage; } } } } } return $lineages; }
Syntax highlighting mode:
ActionScript
ColdFusion
Diff
Drupal 5
Drupal 6
HTML
INI
Javascript
MySQL
PHP
Python
robots.txt
SQL
Text
Select the syntax highlighting mode to use.
See Also:
Order
Title:
URL:
-1
0
1
Title:
URL:
-1
0
1
Any links you'd like to have associated with the post (Drupal.org issue, Wikipedia article, etc).
File attachments
Changes made to the attachments are not permanent until you save this post. The first "listed" file will be included in RSS feeds.
Attach new file:
The maximum upload size is
1 MB
. Only files with the following extensions may be uploaded:
jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp phps
.