CodeCharge Studio
search Register Login  

Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> Tips & Solutions

 PHP Treemenu solution for CCS

Print topic Send  topic

Author Message
lvalverdeb

Posts: 299
Posted: 06/11/2005, 1:22 AM

Below is an implementation of phpguru's Treemenu (http://www.phpguru.org/static/treemenu.html) and Tree classes suitable for use with CCS. You must have the Treemenu classes in your project directory (folder named "treemenu" or modify the class to the correct path). You must also have the images files as well as treemenu.js

This wrapper class is designed to work with CCS's label control with its Content property set to HTML. Here are some basic intructions to get you up and running. The example uses the directory_categories table in the internetDB included with CCS samplepack.

1) Copy the class code below to an includable location e.g. Common.php
2) Create a label in a page/form and set its Content property to HTML. I've named the label menuLabel.
3) In the label's beforeshow event and the following code:
  
	  
	$tmp = new myTreeMenu(new clsDBinternetDB(),"directory_categories","category_id_parent","category_id","category_name","category_name");  
	$tmp->rootMenuLegend = "Directory Categories Menu"; // Root menu legend  
	$tmp->expandedbyDefault = false;  
	$tmp->setAllLinks = false; //false generates links at innermost level. Set to true to generate all links  
	$menuLabel->SetValue($tmp->generateTree());  
	unset($tmp);  
	
4) Publish the page.

The class parameters are:
$connection - connection object
$table - table or view to be queried
$parentIdField - parent field name
$categoryField - primary key for menu items.
$descriptionField - field name to use for item descriptions
$urlField - field name containing url addresses to use


By default, the menu class assumes that the innermost item in the hierarchy is the url you want to go to. This behaviour can be overriden by setting the setAllLinks to true. Please also note that the class uses several CCS Studio functions e.g. CCDLookUp and a connection object based on CCS clsDBxxxx(). It is, of course, possible to use the class for purposes other than user menus but that will be up to you to figure out.

Please contact me at lvalverdeb@yahoo.com if you've any suggestions/comments.

Luis Valverde
San José, Costa Rica

treemenuclass.php

  
include_once("treemenu/TreeMenu.php");  
include_once("treemenu/Tree.php"); //usually not required to be included here but just in case ;-)  
  
class myTreeMenu	{  
	var $connection, $table, $parentIdField, $categoryField, $descriptionField;  
	var $urlField,$keyValueType,$where,$order;  
	var $setAllLinks,$linkTarget,$imagesPath;  
	var $expandedByDefault,$defaultMenuClass;  
  
	function myTreeMenu($connection=null,$table="",$parentIdField="",$categoryField="",$descriptionField="",$urlField="") {  
		if ($connection !== null)  {  
			$this->connection = $connection;    
		}  
		if (strlen($table))    
			$this->table = $table;    
		if (strlen($parentIdField))    
			$this->parentIdField = $parentIdField;    
		if (strlen($categoryField))    
			$this->categoryField = $categoryField;    
		if (strlen($descriptionField))  
			$this->descriptionField = $descriptionField;  
		if (strlen($urlField))    
			$this->urlField = $urlField;    
		$this->where = "";  
		$this->rootMenuLegend 	 = "My Tree Menu";  
		$this->setMenuOptions();  
	}  
  
	function generateTree()  {  
		//call this method from your label's BeforeShow event to generate the tree menu.  
		$this->_setMenuOptions();  
		return $this->_generateTree();  
	}  
      
	function setMenuOptions() {  
		// override these properties in your implementation class to customise the menu to your needs  
		$this->treeMenuFilesPath = "treemenu";  
		$this->order 		 = $this->descriptionField;  
		$this->icon 		 = "folder.gif";  
		$this->expandedIcon 	 = "folder-expanded.gif";  
		$this->linkTarget 	 = "_self";  
		$this->setAllLinks       = false;  
		$this->expandedByDefault = false;  
		$this->defaultStyleClass = "treeMenuDefault";  
		$this->keyValueType 	 = ccsInteger;  
		$this->HTMLStyle = "  
			<style type='text/css'>  
			body {  
				font-family: Comic Sans Serif;  
				font-size: 11pt;  
				font-color: #FFFFFF;  
			}  
		  
			.treeMenuDefault {  
				font-style: Normal;  
				font-color: #FFFFFF;  
			}  
		  
			.treeMenuBold {  
				font-color: #FFFFFF;  
				font-style: italic;  
				font-weight: bold;  
			}  
			</style>";  
  
	}  
	// I do not think there is a need to modify anything beyond this point.  
  
	function _generateTree() {  
		$category_id = 0;  
		$this->menu = new HTML_TreeMenu();  
		$this->menu->additem($this->_generateItems($category_id));  
		$treeMenu = &new HTML_TreeMenu_DHTML(  
							$this->menu,   
							array('images' => $this->imagesPath,  
							      'linkTarget'=> $this->linkTarget,    
								  'defaultClass' => $this->defaultStyleClass  
								)  
						);  
		    $html.= $this->HTMLStyle;  
			$html.= $this->menuJS;  
			$html.=$treeMenu->printMenu();  
			$html.='	';  
			return $html;  
	}  
  
  
	function &_generateItems($category_id)	{  
		$db = $this->connection;  
		$sql = "SELECT "  
			.$this->categoryField.","  
			.$this->descriptionField.","  
			.$this->urlField.","  
			.$this->parentIdField  
			." FROM ".$this->table;  
		$where = (strlen($this->where)) ? $this->where:"";  
		if ($category_id == 0) {  
			$sqlwhere = "(".$this->parentIdField."= 0 OR ".$this->parentIdField." IS NULL)";  
		} else {  
			$sqlwhere = $this->parentIdField."=".$db->ToSQL($category_id,$this->keyValueType);  
		}  
		$where = (strlen($where)) ? $where." AND ".$sqlwhere:$sqlwhere;  
		$order = $this->order;  
		$sql = CCBuildSQL($sql,$where,$order);  
		$db->query($sql);  
		$node = &$this->_addNode($category_id);  
		while ($db->next_record()) {  
			$parent_id = $db->f($this->categoryField);  
			$newnode = &$this->_generateItems($parent_id);  
			$node->additem($newnode);  
		}  
		unset($db);  
		return $node;  
	}  
  
	function &_addNode($category_id) {  
		if ($category_id == 0 || $category_id == null) {  
			$nodeText = $this->rootMenuLegend;  
			$nodeLink = "";  
		} else {  
			$db = $this->connection;  
			$nodeText = CCDLookUp(  
							$this->descriptionField,  
							$this->table,  
							$this->categoryField."="  
								.$db->ToSQL($category_id,$this->keyValueType),  
							$db  
						);  
			$nodeUrl = CCDLookUp(  
							$this->urlField,  
							$this->table,  
							$this->categoryField."="  
								.$db->ToSQL($category_id,$this->keyValueType),  
							$db  
						);  
			if ($this->setAllLinks) {  
				$nodeLink = $nodeUrl;  
			} else {  
				//generate a link only if there are not subcategories  
				$nodeLink = (!$this->_hasSubCats($category_id)) ? $nodeUrl:"";  
			}	  
			unset($db);  
		}  
		$node = &new HTML_TreeNode(  
					array(  
						'text' => $nodeText,  
						'link' => $nodeLink,   
						'icon' => $this->icon,   
						'expandedIcon' => $this->expandedIcon,   
						'expanded' => $this->expandedByDefault  
						)  
					);   
		return $node;  
	}  
  
	function _hasSubCats($category_id) {  
		$db = $this->connection;  
		$subcats = CCDLookup("count(*)",  
					$this->table,  
					$this->parentIdField."="  
						.$db->ToSQL($category_id,$this->keyValueType),  
					$db);  
		unset($db);  
		return $subcats;  
	}  
  
	function _setMenuOptions() {  
		//set/refresh runtime settings  
		$this->imagesPath=$this->treeMenuFilesPath."/images";  
		$this->menuJS = "<script src='"  
					    .$this->treeMenuFilesPath  
						."/TreeMenu.js' language='JavaScript'   
						type='text/javascript'></script>";  
	}  
  
}  
  

_________________
lvalverdeb
CR, GMT-6
XAMPP/Ubuntu/CCS3.2/4
View profile  Send private message
peterr


Posts: 5971
Posted: 06/13/2005, 11:46 PM

Thank you for your contribution Luis. It's nice to see the Tips forum expanding.
_________________
Peter R.
YesSoftware Forums Moderator
For product support please visit http://support.yessoftware.com
View profile  Send private message
Martin K.
Posted: 06/14/2005, 10:11 AM

Hello.
I have insert the Code into the example Pack 2 and the InternetDB
and I have download before the Treemenu.php and .js ....
Always the same error:

Database error: next_record called with no query pending.
MySQL Error: 0 ()
Session halted.

Sorry about my english and can you help me?

greets martin
lvalverdeb

Posts: 299
Posted: 06/15/2005, 5:07 AM

Martin,

Could you post the code you are using to instantiate the class? From the error, it appears that the query SQL is empty or the connection object is incorrect.

Luis

_________________
lvalverdeb
CR, GMT-6
XAMPP/Ubuntu/CCS3.2/4
View profile  Send private message
Martin K.
Posted: 06/17/2005, 10:32 PM

Hello Luis.
I have send you a mail becáuse in this forum we do not post the questions (Tips and Solutions)

greets martin

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

PHP Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.