Page 1 of 1

A nicer parent selector drop-down.

Posted: Fri Feb 10, 2006 8:00 pm
by redkevin11
My client needs an easier to follow hierarchy when selecting a content's parent.

Here's my solution:
Image

in lib/classes/class.content.inc.php, around line 2146 replace the current CreateHierarchyDropdown function with:

Code: Select all

	function CreateHierarchyDropdown($current = '', $parent = '', $name = 'parent_id')
	{
		$result = '';

		$allcontent = ContentManager::GetAllContent();

		if ($allcontent !== FALSE && count($allcontent) > 0)
		{
			$result .= '<select name="'.$name.'" style="font-family:courier;">';
			$result .= '<option value="-1">None</option>';

			$curhierarchy = '';

			foreach ($allcontent as $one)
			{
				if ($one->Id() == $current)
				{
#Grab hierarchy just in case we need to check children
#(which will always be after)
					$curhierarchy = $one->Hierarchy();

#Then jump out.  We don't want ourselves in the list.
					continue;
				}
#If it's a child of the current, we don't want to show it as it
#could cause a deadlock.
				if ($curhierarchy != '' && strstr($one->Hierarchy(), $curhierarchy) == $one->Hierarchy())
				{
					continue;
				}
#Don't include content types that do not want children either...
				if ($one->WantsChildren() == true)
				{
					$result .= '<option value="'.$one->Id().'"';

#Select current parent if it exists
					if ($one->Id() == $parent)
					{
						$result .= ' selected="true"';
					}

					$indent = get_preference($userid, 'indent', true);
					$length = strlen($one->Hierarchy());
					$hiercount = substr_count($one->Hierarchy(), '.');
					$offset = 6 - $length;
					$result .= '>'.$one->Hierarchy() ."  ";
    				for ($i=0;$i <= $offset;$i++) 
	   				    $result .= " ";
					if ($indent){	
    					for ($i=1;$i <= $hiercount;$i++) 
	   				        $result .= "-  ";
		            }

					$result .= $one->Name().'</option>'."\n";
				}
			}

			$result .= '</select>';
		}

		return $result;
	}

Some might not like that I have an inline style to make the font courier on the dropdown.  I just needed a monospaced font to make everything line up.  This assumes that you aren't going more than about 4 levels deep.  One would just need to change the line "$offset = 6 - $length;" to have it indent more/less.

I'm know there's an easy way to figure out the maximum depth of your site, but didn't mess with iterating through the array.

It DOES adhere to the user setting for "emphasize hierarchy".