浏览代码

TreeHelper

euromark 13 年之前
父节点
当前提交
0205e106f6
共有 4 个文件被更改,包括 182 次插入42 次删除
  1. 0 1
      Lib/Utility/FileLib.php
  2. 131 0
      Test/Case/View/Helper/TreeHelperTest.php
  3. 20 16
      View/Helper/LoremHelper.php
  4. 31 25
      View/Helper/TreeHelper.php

+ 0 - 1
Lib/Utility/FileLib.php

@@ -1,5 +1,4 @@
 <?php
 <?php
-
 App::uses('File', 'Utility');
 App::uses('File', 'Utility');
 
 
 /**
 /**

+ 131 - 0
Test/Case/View/Helper/TreeHelperTest.php

@@ -0,0 +1,131 @@
+<?php
+
+App::uses('TreeHelper', 'Tools.View/Helper');
+App::uses('MyCakeTestCase', 'Tools.TestSuite');
+App::uses('View', 'View');
+App::uses('Hash', 'Utility');
+
+class TreeHelperTest extends MyCakeTestCase {
+
+	public $fixtures = array('core.after_tree');
+
+	public $Model;
+
+	public function setUp() {
+		parent::setUp();
+
+		$this->Tree = new TreeHelper(new View(null));
+		$this->Model = ClassRegistry::init('AfterTree');
+		$this->Model->Behaviors->attach('Tree');
+
+		$this->Model->truncate();
+
+		$data = array(
+			array('name' => 'One'),
+			array('name' => 'Two'),
+			array('name' => 'Three'),
+			array('name' => 'Four'),
+
+			array('name' => 'One-SubA', 'parent_id' => 1),
+			array('name' => 'Two-SubA', 'parent_id' => 2),
+			array('name' => 'Four-SubA', 'parent_id' => 4),
+
+			array('name' => 'Two-SubA-1', 'parent_id' => 6),
+
+			array('name' => 'Two-SubA-1-1', 'parent_id' => 8),
+		);
+		foreach ($data as $row) {
+			$this->Model->create();
+			$this->Model->save($row);
+		}
+	}
+
+	public function tearDown() {
+		parent::tearDown();
+	}
+
+	public function testObject() {
+		$this->assertTrue(is_a($this->Tree, 'TreeHelper'));
+	}
+
+	public function testGenerate() {
+		$tree = $this->Model->find('threaded');
+
+		$output = $this->Tree->generate($tree);
+
+		$expected = <<<TEXT
+
+<ul>
+	<li>One
+	<ul>
+		<li>One-SubA</li>
+	</ul>
+	</li>
+	<li>Two
+	<ul>
+		<li>Two-SubA
+		<ul>
+			<li>Two-SubA-1
+			<ul>
+				<li>Two-SubA-1-1</li>
+			</ul>
+			</li>
+		</ul>
+		</li>
+	</ul>
+	</li>
+	<li>Three</li>
+	<li>Four
+	<ul>
+		<li>Four-SubA</li>
+	</ul>
+	</li>
+</ul>
+
+TEXT;
+		$this->assertTextEquals($expected, $output);
+		$this->assertTrue(substr_count($output, '<ul>') === substr_count($output, '</ul>'));
+		$this->assertTrue(substr_count($output, '<li>') === substr_count($output, '</li>'));
+	}
+
+	//TODO: fixme: 8,9 is "Two-SubA-1-1" and so this entry should also be active
+	public function testGenerateWithAutoPath() {
+		$tree = $this->Model->find('threaded');
+		debug($tree);
+
+		$output = $this->Tree->generate($tree, array('autoPath' => array(8, 9)));
+		debug($output);
+		$expected = <<<TEXT
+
+<ul>
+	<li>One
+	<ul>
+		<li>One-SubA</li>
+	</ul>
+	</li>
+	<li>Two
+	<ul class="active">
+		<li class="active">Two-SubA
+		<ul class="active">
+			<li class="active">Two-SubA-1
+			<ul>
+				<li>Two-SubA-1-1</li>
+			</ul>
+			</li>
+		</ul>
+		</li>
+	</ul>
+	</li>
+	<li>Three</li>
+	<li>Four
+	<ul>
+		<li>Four-SubA</li>
+	</ul>
+	</li>
+</ul>
+
+TEXT;
+		$this->assertTextEquals($expected, $output);
+	}
+
+}

+ 20 - 16
View/Helper/LoremHelper.php

@@ -1,7 +1,10 @@
 <?php
 <?php
 App::uses('AppHelper', 'View/Helper');
 App::uses('AppHelper', 'View/Helper');
+
 class LoremHelper extends AppHelper {
 class LoremHelper extends AppHelper {
+
 	public $helpers = array('Html');
 	public $helpers = array('Html');
+
 	public $words = array();
 	public $words = array();
 
 
 	/**
 	/**
@@ -14,10 +17,11 @@ class LoremHelper extends AppHelper {
 	* @param array $attributes Additional HTML attributes of the list (ol/ul) tag, or paragraph (when applicable)
 	* @param array $attributes Additional HTML attributes of the list (ol/ul) tag, or paragraph (when applicable)
 	* @param array $itemAttributes Additional HTML attributes of the list item (LI) tag (when applicable)
 	* @param array $itemAttributes Additional HTML attributes of the list item (LI) tag (when applicable)
 	* @return string placeholder text
 	* @return string placeholder text
-	* @access public
 	*/
 	*/
 	public function ipsum($number = 1, $type = 'p', $attributes = array(), $itemAttributes = array()) {
 	public function ipsum($number = 1, $type = 'p', $attributes = array(), $itemAttributes = array()) {
-		$this->words = explode(' ', 'lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur excepteur sint occaecat cupidatat non proident sunt in culpa qui officia deserunt mollit anim id est laborum');
+		if (!$this->words) {
+			$this->words = explode(' ', 'lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur excepteur sint occaecat cupidatat non proident sunt in culpa qui officia deserunt mollit anim id est laborum');
+		}
 		switch ($type) {
 		switch ($type) {
 			// Words
 			// Words
 			case 'w':
 			case 'w':
@@ -30,25 +34,25 @@ class LoremHelper extends AppHelper {
 			case 'list':
 			case 'list':
 			// ordered list too!
 			// ordered list too!
 			case 'ol':
 			case 'ol':
-				for ($li=0;$li<$number;$li++) {
+				for ($li = 0; $li < $number; $li++) {
 					$list[] = $this->_sentence();
 					$list[] = $this->_sentence();
 				}
 				}
 				$string = $this->Html->nestedList($list, $attributes, $itemAttributes, ($type === 'ol') ? 'ol' : 'ul');
 				$string = $this->Html->nestedList($list, $attributes, $itemAttributes, ($type === 'ol') ? 'ol' : 'ul');
-			break;
+				break;
 			// everything else paragraphs
 			// everything else paragraphs
 			default:
 			default:
-				for ($p=0;$p<$number;$p++) {
+				for ($p = 0; $p < $number; $p++) {
 					$paraText = '';
 					$paraText = '';
-					$numberSentences = rand(16,20);
-					for ($s=0;$s<$numberSentences;$s++) {
+					$numberSentences = rand(16, 20);
+					for ($s = 0; $s < $numberSentences; $s++) {
 						$paraText .= $this->_sentence();
 						$paraText .= $this->_sentence();
 					}
 					}
 					$paras[] = $this->Html->para(null, $paraText, $attributes);
 					$paras[] = $this->Html->para(null, $paraText, $attributes);
 				}
 				}
 				$string = implode("\n", $paras);
 				$string = implode("\n", $paras);
-			break;
+				break;
 		}
 		}
-		return $string;
+		return trim($string);
 	}
 	}
 
 
 	/**
 	/**
@@ -58,22 +62,21 @@ class LoremHelper extends AppHelper {
 	* @param integer $minWords minimum number of words for this sentence
 	* @param integer $minWords minimum number of words for this sentence
 	* @param boolean $punctuation if false it will not append random commas and ending period
 	* @param boolean $punctuation if false it will not append random commas and ending period
 	* @return string greeked sentence
 	* @return string greeked sentence
-	* @access private
 	*/
 	*/
-	public function _sentence($maxWords = 10, $minWords = 4, $punctuation = true) {
+	protected function _sentence($maxWords = 10, $minWords = 4, $punctuation = true) {
 		$string = '';
 		$string = '';
 		$numWords = rand($minWords, $maxWords);
 		$numWords = rand($minWords, $maxWords);
-		for ($w=0;$w<$numWords;$w++) {
-			$word = $this->words[rand(0, (count($this->words)-1))];
+		for ($w = 0; $w < $numWords; $w++) {
+			$word = $this->words[rand(0, (count($this->words) - 1))];
 			// if first word capitalize letter...
 			// if first word capitalize letter...
-			if ($w == 0) {
+			if ($w === 0) {
 				$word = ucwords($word);
 				$word = ucwords($word);
 			}
 			}
 			$string .= $word;
 			$string .= $word;
 			// if not the last word,
 			// if not the last word,
-			if ($w != ($numWords-1)) {
+			if ($w !== ($numWords - 1)) {
 				// 5% chance of a comma...
 				// 5% chance of a comma...
-				if (rand(0,99) < 5) {
+				if (rand(0, 99) < 5) {
 					$string .= ', ';
 					$string .= ', ';
 				} else {
 				} else {
 					$string .= ' ';
 					$string .= ' ';
@@ -83,4 +86,5 @@ class LoremHelper extends AppHelper {
 		$string .= '. ';
 		$string .= '. ';
 		return $string;
 		return $string;
 	}
 	}
+
 }
 }

+ 31 - 25
View/Helper/TreeHelper.php

@@ -14,13 +14,7 @@
  *
  *
  * @filesource
  * @filesource
  * @copyright Copyright (c) 2008, Andy Dawson
  * @copyright Copyright (c) 2008, Andy Dawson
- * @link www.ad7six.com
- * @package cake-base
- * @subpackage cake-base.app.views.helpers
- * @since v 1.0
- * @version $Revision: 205 $
- * @modifiedBy $LastChangedBy: ad7six $
- * @lastModified $Date: 2008-08-13 16:13:32 +0200 (Wed, 13 Aug 2008) $
+ * @modifiedBy $LastChangedBy: 2013-02-05 ms
  * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  */
  */
 App::uses('AppHelper', 'View/Helper');
 App::uses('AppHelper', 'View/Helper');
@@ -38,28 +32,32 @@ class TreeHelper extends AppHelper {
 	 * @var array
 	 * @var array
 	 * @access private
 	 * @access private
 	 */
 	 */
-	public $_settings = array();
+	protected $_settings = array();
+
 	/**
 	/**
 	 * typeAttributes property
 	 * typeAttributes property
 	 *
 	 *
 	 * @var array
 	 * @var array
 	 * @access private
 	 * @access private
 	 */
 	 */
-	public $_typeAttributes = array();
+	protected $_typeAttributes = array();
+
 	/**
 	/**
 	 * typeAttributesNext property
 	 * typeAttributesNext property
 	 *
 	 *
 	 * @var array
 	 * @var array
 	 * @access private
 	 * @access private
 	 */
 	 */
-	public $_typeAttributesNext = array();
+	protected $_typeAttributesNext = array();
+
 	/**
 	/**
 	 * itemAttributes property
 	 * itemAttributes property
 	 *
 	 *
 	 * @var array
 	 * @var array
 	 * @access private
 	 * @access private
 	 */
 	 */
-	public $_itemAttributes = array();
+	protected $_itemAttributes = array();
+
 	/**
 	/**
 	 * helpers variable
 	 * helpers variable
 	 *
 	 *
@@ -67,6 +65,7 @@ class TreeHelper extends AppHelper {
 	 * @access public
 	 * @access public
 	 */
 	 */
 	public $helpers = array('Html');
 	public $helpers = array('Html');
+
 	/**
 	/**
 	 * Tree generation method.
 	 * Tree generation method.
 	 *
 	 *
@@ -126,12 +125,23 @@ class TreeHelper extends AppHelper {
 		if ($indent === null && Configure::read('debug')) {
 		if ($indent === null && Configure::read('debug')) {
 			$indent = true;
 			$indent = true;
 		}
 		}
+		if ($model === null && $this->_View->params['models']) {
+			foreach ($this->_View->params['models'] as $model => $value) {
+				break;
+			}
+		}
 		if ($model === null) {
 		if ($model === null) {
-			//$model = Inflector::classify($this->_View->params['models'][0]);
+			foreach ($data as $key => $value) {
+				foreach ($value as $model => $array) {
+					break;
+				}
+			}
 		}
 		}
 		if (!$model) {
 		if (!$model) {
 			$model = '_NULL_';
 			$model = '_NULL_';
 		}
 		}
+		$this->_settings['model'] = $model;
+
 		$stack = array();
 		$stack = array();
 		if ($depth == 0) {
 		if ($depth == 0) {
 			if ($class) {
 			if ($class) {
@@ -142,9 +152,6 @@ class TreeHelper extends AppHelper {
 			}
 			}
 		}
 		}
 		$return = '';
 		$return = '';
-		if ($indent) {
-			$return = "\r\n";
-		}
 		$_addType = true;
 		$_addType = true;
 		foreach ($data as $i => $result) {
 		foreach ($data as $i => $result) {
 			/* Allow 2d data arrays */
 			/* Allow 2d data arrays */
@@ -254,7 +261,7 @@ class TreeHelper extends AppHelper {
 					$return .= $this->_suffix();
 					$return .= $this->_suffix();
 					$return .= $this->generate($result['children'], $settings);
 					$return .= $this->generate($result['children'], $settings);
 					if ($itemType) {
 					if ($itemType) {
-						$return .= '</' . $itemType . '>';
+						$return .= $whiteSpace . "\t" . '</' . $itemType . '>';
 					}
 					}
 				} elseif ($numberOfTotalChildren) {
 				} elseif ($numberOfTotalChildren) {
 					$_addType = true;
 					$_addType = true;
@@ -267,6 +274,7 @@ class TreeHelper extends AppHelper {
 				$return .= $this->_suffix();
 				$return .= $this->_suffix();
 			}
 			}
 		}
 		}
+
 		/* Cleanup */
 		/* Cleanup */
 		while ($stack) {
 		while ($stack) {
 			array_pop($stack);
 			array_pop($stack);
@@ -281,15 +289,16 @@ class TreeHelper extends AppHelper {
 				$return .= '</' . $itemType . '>';
 				$return .= '</' . $itemType . '>';
 			}
 			}
 		}
 		}
-		if ($indent) {
+		if ($return && $indent) {
 			$return .= "\r\n";
 			$return .= "\r\n";
 		}
 		}
-		if ($type) {
-			$return .= '</' . $type . '>';
+		if ($return && $type) {
+			$return .= $whiteSpace . '</' . $type . '>';
 			if ($indent) {
 			if ($indent) {
 				$return .= "\r\n";
 				$return .= "\r\n";
 			}
 			}
 		}
 		}
+
 		return $return;
 		return $return;
 	}
 	}
 
 
@@ -358,7 +367,6 @@ class TreeHelper extends AppHelper {
 	 * supressChildren method
 	 * supressChildren method
 	 *
 	 *
 	 * @return void
 	 * @return void
-	 * @access public
 	 */
 	 */
 	public function supressChildren() {
 	public function supressChildren() {
 	}
 	}
@@ -368,10 +376,9 @@ class TreeHelper extends AppHelper {
 	 *
 	 *
 	 * Used to close and reopen a ul/ol to allow easier listings
 	 * Used to close and reopen a ul/ol to allow easier listings
 	 *
 	 *
-	 * @access private
 	 * @return void
 	 * @return void
 	 */
 	 */
-	public function _suffix() {
+	protected function _suffix() {
 		static $_splitCount = 0;
 		static $_splitCount = 0;
 		static $_splitCounter = 0;
 		static $_splitCounter = 0;
 		extract($this->_settings);
 		extract($this->_settings);
@@ -389,7 +396,7 @@ class TreeHelper extends AppHelper {
 			}
 			}
 			if ($depth == $splitDepth) {
 			if ($depth == $splitDepth) {
 				$_splitCounter++;
 				$_splitCounter++;
-				if ($type && ($_splitCounter % $_splitCount) == 0) {
+				if ($type && ($_splitCounter % $_splitCount) === 0) {
 					return '</' . $type . '><' . $type . '>';
 					return '</' . $type . '><' . $type . '>';
 				}
 				}
 			}
 			}
@@ -404,10 +411,9 @@ class TreeHelper extends AppHelper {
 	 *
 	 *
 	 * @param mixed $rType
 	 * @param mixed $rType
 	 * @param array $elementData
 	 * @param array $elementData
-	 * @access private
 	 * @return void
 	 * @return void
 	 */
 	 */
-	public function _attributes($rType, $elementData = array(), $clear = true) {
+	protected function _attributes($rType, $elementData = array(), $clear = true) {
 		extract($this->_settings);
 		extract($this->_settings);
 		if ($rType == $type) {
 		if ($rType == $type) {
 			$attributes = $this->_typeAttributes;
 			$attributes = $this->_typeAttributes;