Changeset 19614
- Timestamp:
- 06/27/09 19:00:03 (4 years ago)
- Files:
-
- plugins/csNavigationPlugin/trunk/config/doctrine/cs_navigation.yml (modified) (2 diffs)
- plugins/csNavigationPlugin/trunk/lib/base/BasecsNavigationItem.class.php (deleted)
- plugins/csNavigationPlugin/trunk/lib/base/BasecsTreeNavigation.class.php (deleted)
- plugins/csNavigationPlugin/trunk/lib/csBreadcrumbNavigation.php (modified) (4 diffs)
- plugins/csNavigationPlugin/trunk/lib/csNavigationCollectionManager.class.php (added)
- plugins/csNavigationPlugin/trunk/lib/csNavigationHelper.class.php (modified) (3 diffs)
- plugins/csNavigationPlugin/trunk/lib/csNavigationItem.class.php (deleted)
- plugins/csNavigationPlugin/trunk/lib/csNavigationRoute.class.php (modified) (2 diffs)
- plugins/csNavigationPlugin/trunk/lib/csTreeNavigation.class.php (deleted)
- plugins/csNavigationPlugin/trunk/lib/filter/csNavgationFilter.class.php (modified) (1 diff)
- plugins/csNavigationPlugin/trunk/lib/filter/doctrine/PlugincsNavigationFormFilter.class.php (deleted)
- plugins/csNavigationPlugin/trunk/lib/filter/doctrine/PlugincsNavigationItemFormFilter.class.php (added)
- plugins/csNavigationPlugin/trunk/lib/filter/doctrine/PlugincsNavigationMenuFormFilter.class.php (added)
- plugins/csNavigationPlugin/trunk/lib/form/doctrine/PlugincsNavigationForm.class.php (deleted)
- plugins/csNavigationPlugin/trunk/lib/form/doctrine/PlugincsNavigationItemForm.class.php (added)
- plugins/csNavigationPlugin/trunk/lib/form/doctrine/PlugincsNavigationMenuForm.class.php (added)
- plugins/csNavigationPlugin/trunk/lib/model/doctrine/PlugincsNavigation.class.php (deleted)
- plugins/csNavigationPlugin/trunk/lib/model/doctrine/PlugincsNavigationItem.class.php (added)
- plugins/csNavigationPlugin/trunk/lib/model/doctrine/PlugincsNavigationItemTable.class.php (added)
- plugins/csNavigationPlugin/trunk/lib/model/doctrine/PlugincsNavigationMenu.class.php (added)
- plugins/csNavigationPlugin/trunk/lib/model/doctrine/PlugincsNavigationMenuTable.class.php (added)
- plugins/csNavigationPlugin/trunk/lib/model/doctrine/PlugincsNavigationTable.class.php (deleted)
- plugins/csNavigationPlugin/trunk/modules/csNavigation/lib/BasecsNavigationComponents.class.php (modified) (3 diffs)
- plugins/csNavigationPlugin/trunk/modules/csNavigation/templates/_tree.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/csNavigationPlugin/trunk/config/doctrine/cs_navigation.yml
r19597 r19614 1 csNavigation :1 csNavigationItem: 2 2 actAs: 3 3 NestedSet: … … 15 15 default: false 16 16 17 # csNavigationMenu: 18 # columns: 19 # title: string(255) 20 # description: string(255) 21 # root: integer 22 # relations: 23 # csNavigationItem: 24 # local: root 25 # foreign: id 17 csNavigationMenu: 18 columns: 19 title: string(255) 20 description: string(255) 21 root_id: integer 22 relations: 23 csNavigationItem: 24 alias: NavigationRoot 25 foreignAlias: NavigationMenu 26 local: root_id 27 foreign: id plugins/csNavigationPlugin/trunk/lib/csBreadcrumbNavigation.php
r19596 r19614 23 23 { 24 24 static $instance = null; 25 protected$appended_items = array();25 protected $appended_items = array(); 26 26 static protected $session_name = 'csBreadcrumbNavigation'; 27 27 /** … … 32 32 { 33 33 $this->setRoot('Home', '@homepage'); 34 if($items)35 {36 $this->items = array_merge($this->items, $items);37 }34 if($items) 35 { 36 $this->items = array_merge($this->items, $items); 37 } 38 38 } 39 39 … … 47 47 $this->save(); 48 48 } 49 public function generateBreadcrumbFromNavigation()50 {51 $nav = csNavigationHelper::getNavigationItems();52 foreach ($nav as $item) {53 if($breadcrumb = $item->getBreadcrumbArray())54 {55 $this->appendItems($breadcrumb); 56 return; 57 }58 }59 }60 static function appendItem($item)61 {62 self::getInstance()->appended_items[] = $item;63 } 64 static function appendItems($items)65 {66 foreach ($items as $item) {67 self::appendItem($item);68 }69 }70 public function getItems($offset = 0)71 {72 return array_merge($this->items, $this->appended_items);73 }74 public static function getInstance()49 public function generateBreadcrumbFromNavigation() 50 { 51 $nav = csNavigationHelper::getNavigationItems(); 52 foreach ($nav as $item) { 53 if($breadcrumb = $item->getBreadcrumbArray()) 54 { 55 $this->appendItems($breadcrumb); 56 return; 57 } 58 } 59 } 60 static function appendItem($item) 61 { 62 self::getInstance()->appended_items[] = $item; 63 } 64 static function appendItems($items) 65 { 66 foreach ($items as $item) { 67 self::appendItem($item); 68 } 69 } 70 public function getItems($offset = 0) 71 { 72 return array_merge($this->items, $this->appended_items); 73 } 74 public static function getInstance() 75 75 { 76 76 if (is_null(self::$instance)) … … 82 82 else 83 83 { 84 self::createInstance();85 sfContext::getInstance()->getRequest()->setParameter(self::$session_name, self::$instance);86 }84 self::createInstance(); 85 sfContext::getInstance()->getRequest()->setParameter(self::$session_name, self::$instance); 86 } 87 87 } 88 88 return self::$instance; 89 89 } 90 90 91 public static function createInstance()92 {93 self::$instance = new csBreadcrumbNavigation();91 public static function createInstance() 92 { 93 self::$instance = new csBreadcrumbNavigation(); 94 94 self::$instance->save(); 95 }96 public function hasItems()97 {98 if(sizeof($this->getItems()) == 1)99 {100 return !($this->items[0]->name == 'Home' && $this->items[0]->route == '@homepage');101 }102 return parent::hasItems();103 }95 } 96 public function hasItems() 97 { 98 if(sizeof($this->getItems()) == 1) 99 { 100 return !($this->items[0]->name == 'Home' && $this->items[0]->route == '@homepage'); 101 } 102 return parent::hasItems(); 103 } 104 104 } plugins/csNavigationPlugin/trunk/lib/csNavigationHelper.class.php
r19605 r19614 27 27 { 28 28 //Populate Tree from Database 29 if(!Doctrine::getTable('csNavigation ')->isPopulated())29 if(!Doctrine::getTable('csNavigationItem')->isPopulated()) 30 30 { 31 31 //Build Database from Existing navigation.yml file 32 32 self::initDatabaseFromYaml($navigation); 33 33 } 34 //Build from nested set 35 $root = Doctrine::getTable('csNavigation')->getNavigationRoot(); 36 $nav = self::nestedSetToArray($root); 37 $tree = self::getNavigationTreeFromArray($nav['children']); 34 //Pull from database, create navigation structure 35 $tree = self::getNavigationTreeFromDatabase(); 38 36 } 39 37 else … … 42 40 $tree = self::getNavigationTreeFromYaml($navigation); 43 41 } 44 45 // Run tree's setup() method for dynamically adding branches 46 $tree->setup(); 47 42 43 $menuTable = Doctrine::getTable('csNavigationMenu'); 44 45 $menuTable->storeTree($tree); 46 47 // Run the configure() method for dynamically adding branches 48 Doctrine::getTable('csNavigationMenu')->configure(); 49 50 $tree = $menuTable->restoreTree(); 51 48 52 // Cache Tree 49 $serialized = serialize($tree );53 $serialized = serialize($tree->toArray()); 50 54 file_put_contents($cachePath, $serialized); 51 55 } 56 else 57 { 58 Doctrine::getTable('csNavigationMenu')->storeTree(self::getTree()); 59 } 52 60 self::$_settings = $settings; 53 61 } 54 62 63 /** 64 * Pulls tree from cache directory (set by self::init()) 65 * 66 * @return void 67 * @author Brent Shaffer 68 */ 69 static function getTree() 70 { 71 $cachePath = sfConfig::get('sf_cache_dir').'/navigation_tree.cache'; 72 if (!file_exists($cachePath)) 73 { 74 throw new sfException('You must add the csNavigationFilter class to your routing.yml 75 (see csNavigationPlugin\s README for more information)'); 76 } 77 78 // Pull navigation tree from cache 79 $unserialized = unserialize(file_get_contents($cachePath)); 80 81 $menus = new csNavigationCollectionManager(); 82 83 foreach ($unserialized as $menu_arr) 84 { 85 $menu = new csNavigationMenu(); 86 $menu->fromArray($menu_arr); 87 $menus[] = $menu; 88 } 89 90 return $menus; 91 } 92 55 93 56 94 static function initDatabaseFromYaml($navigation) 57 95 { 58 self::arrayToNestedSet($navigation); 59 } 60 61 // Generic Function that converts the YAML array to a doctrine nested set 62 static function arrayToNestedSet($arr, $root = null) 63 { 96 self::getNavigationTreeFromYaml($navigation, true); 97 } 98 99 static function getNavigationTreeFromYaml($arr, $commit = false) 100 { 101 $menu_tree = new csNavigationCollectionManager(); 102 64 103 foreach ($arr as $key => $value) 65 104 { 66 if (!$root) 67 { 68 $record = self::createRoot($key, $value); 69 self::setItemAttributes($record, self::parseItemAttributes($value)); 70 self::arrayToNestedSet($value, $record); 71 continue; 72 } 73 $root->refresh(); 74 $record = new csNavigation(); 75 $record->name = $key; 76 if(is_array($value)) 77 { 78 self::setItemAttributes($record, self::parseItemAttributes($value)); 79 $record->getNode()->insertAsLastChildOf($root); 80 self::arrayToNestedSet($value, $record); 81 } 82 else 83 { 84 $record->route = $value; 85 $record->getNode()->insertAsLastChildOf($root); 86 } 87 } 88 } 89 90 static function createRoot($key, $value) 91 { 92 $root = new csNavigation(); 93 $root->name = $key; 94 self::setItemAttributes($root, self::parseItemAttributes($value)); 95 $root->save(); 96 $treeObject = Doctrine::getTable('csNavigation')->getTree(); 97 $treeObject->createRoot($root); 98 return $root; 99 } 100 101 /* 102 TODO Refactor breadcrumb methods 103 */ 104 static function getBreadcrumb($level = 0) 105 { 106 $tree = self::getTree(); 107 return $tree->getBreadcrumb($level); 105 $cleaned = self::cleanItemAttributes($value); 106 107 $menu = new csNavigationMenu(); 108 109 $menu->title = $key; 110 111 $root = new csNavigationItem(); 112 113 $root->name = $key; 114 115 $root->level = 0; 116 117 $root->fromArray($cleaned['attributes']); 118 119 if($commit) 120 { 121 $menu->createRoot($root); 122 } 123 124 $root = self::getNavigationBranchFromYaml($root, $cleaned['children'], $commit); 125 126 $menu['NavigationRoot'] = $root; 127 128 $menu_tree[] = $menu; 129 } 108 130 109 // $breadcrumb = new csBreadcrumbNavigation(); 110 // $breadcrumb->generateBreadcrumbFromNavigation(); 111 // return $breadcrumb; 112 } 113 114 /* 115 TODO Deprecate this method 116 */ 117 static function getNavigationItems($level = 0, $iterations = null) 118 { 119 $nav = self::getNavigationTree($level, $iterations); 120 return $nav->getItems(); 121 } 122 123 /** 124 * getNavigationTree 125 * Returns array of the current navigation 126 * @static 127 * @access public 128 * @return array 129 */ 130 static function getNavigationTree($level = 0, $iterations = null) 131 { 132 $tree = self::getTree(); 133 return $tree->getSegment($level, $iterations); 131 return $menu_tree; 134 132 } 135 133 … … 143 141 * @author Brent Shaffer 144 142 */ 145 static function getNavigationTreeFromYaml($arr, $level = 0, &$root = null) 146 { 147 if(!$root) 148 { 149 $root = new csTreeNavigation(); 150 } 143 static function getNavigationBranchFromYaml($root, $arr, $commit = false, $level = 1) 144 { 145 $tree = new Doctrine_Collection('csNavigationItem'); 146 147 foreach ($arr as $key => $value) 148 { 149 if(is_array($value)) 150 { 151 $cleaned = self::cleanItemAttributes($value); 152 153 $item = new csNavigationItem(); 154 155 $item->name = $key; 156 157 $item->level = $level; 158 159 $item->fromArray($cleaned['attributes']); 160 161 if ($commit) 162 { 163 $item->save(); 164 $item->getNode()->insertAsLastChildOf($root); 165 } 166 167 if($cleaned['children']) 168 { 169 $item = self::getNavigationBranchFromYaml($item, $cleaned['children'], $commit, $level + 1); 170 } 171 } 172 else 173 { 174 $item = new csNavigationItem(); 175 176 $item->name = $key; 177 178 $item->route = $value; 179 180 $item->level = $level; 181 182 if ($commit) 183 { 184 $root->getNode()->addChild($item); 185 } 186 } 187 $root->addItem($item); 188 } 189 return $root; 190 } 191 192 193 static function getNavigationTreeFromDatabase() 194 { 195 $tree = new csNavigationCollectionManager(); 196 $menus = Doctrine::getTable('csNavigationMenu')->findAll(); 197 foreach ($menus as $menu) 198 { 199 $menu['NavigationRoot'] = self::getNavigationTreeFromNestedSet($menu['NavigationRoot']); 200 $tree[] = $menu; 201 } 202 return $tree; 203 } 204 /** 205 * Converts nested set from Database into array form 206 * 207 * @param string $obj 208 * @return void 209 * @author Brent Shaffer 210 */ 211 static function getNavigationTreeFromNestedSet($obj = null) 212 { 213 if($children = $obj->getNode()->getChildren()) 214 { 215 foreach ($children as $child_obj) { 216 $child = self::getNavigationTreeFromNestedSet($child_obj); 217 $obj->addChild($child); 218 } 219 } 220 return $obj; 221 } 222 223 /** 224 * Parses Navigation Item attributes in navigation.yml 225 * 226 * @param string $arr 227 * @return void 228 * @author Brent Shaffer 229 */ 230 static function cleanItemAttributes($arr) 231 { 232 $attr = array('attributes' => array(), 233 'children' => array()); 234 151 235 foreach ($arr as $key => $value) { 152 if(is_array($value)) 153 { 154 $item = new csNavigationItem($key, null, $level); 155 self::setItemAttributes($item, self::parseItemAttributes($value)); 156 if($value) 157 { 158 self::getNavigationTreeFromYaml($value, $level + 1, $item); 159 } 236 if (strpos($key, '~') === 0) 237 { 238 $attr['attributes'][substr($key, 1)] = $value; 160 239 } 161 240 else 162 241 { 163 $item = new csNavigationItem($key, $value, $level); 164 } 165 $root->addItem($item); 166 167 if ($level == 0) 168 { 169 break; // Currently no multiple-root support 170 } 171 } 172 return $root; 173 } 174 175 /** 176 * Receives a nested set in array form, converts to generic NavigationTree 177 * 178 * @param string $arr 179 * @param string $level 180 * @param string $root 181 * @return void 182 * @author Brent Shaffer 183 */ 184 static function getNavigationTreeFromArray($arr, $level = 0, &$root = null) 185 { 186 $root = $root ? $root : new csTreeNavigation(); 187 foreach ($arr as $key => $value) 188 { 189 $item = new csNavigationItem($value['name'], $value['route'], $level + 1); 190 self::setItemAttributes($item, $value); 191 if(isset($value['children'])) 192 { 193 self::getNavigationTreeFromArray($value['children'], $level + 1, $item); 194 } 195 $root->addItem($item); 196 } 197 return $root; 198 } 199 200 /** 201 * Converts nested set from Database into array form 202 * 203 * @param string $obj 204 * @return void 205 * @author Brent Shaffer 206 */ 207 static function nestedSetToArray($obj = null) 208 { 209 $arr = self::getObjArray($obj); 210 if($children = $obj->getNode()->getChildren()) 211 { 212 foreach ($children as $child_obj) { 213 $arr['children'][$child_obj['id']] = self::nestedSetToArray($child_obj); 214 } 215 } 216 return $arr; 217 } 218 219 /** 220 * Pulls tree from cache directory (set by self::init()) 221 * 222 * @return void 223 * @author Brent Shaffer 224 */ 225 static function getTree() 226 { 227 $cachePath = sfConfig::get('sf_cache_dir').'/navigation_tree.cache'; 228 if (!file_exists($cachePath)) 229 { 230 throw new sfException('You must add the csNavigationFilter class to your routing.yml 231 (see csNavigationPlugin\s README for more information)'); 232 } 233 234 // Pull navigation tree from cache 235 $serialized = file_get_contents($cachePath); 236 237 return unserialize($serialized); 238 } 239 240 /** 241 * Parses Navigation Item attributes in navigation.yml 242 * 243 * @param string $arr 244 * @return void 245 * @author Brent Shaffer 246 */ 247 static function parseItemAttributes(&$arr) 248 { 249 $attr = array(); 250 foreach ($arr as $key => $value) { 251 if (strpos($key, '~') === 0) { 252 $attr[substr($key, 1)] = $value; 253 unset($arr[$key]); 242 $attr['children'][$key] = $value; 254 243 } 255 244 } 256 245 return $attr; 257 246 } 258 259 static function setItemAttributes(&$item, $arr)260 {261 foreach ($arr as $key => $value) {262 $item->$key = $value;263 }264 }265 266 /**267 * Available object attributes in database / navigation.yml268 *269 * TODO Make Extensible - pull from csNavigation model270 *271 * @param string $obj272 * @return void273 * @author Brent Shaffer274 */275 static function getObjArray($obj)276 {277 return array( 'id' => $obj['id'],278 'name' => $obj['name'],279 'route' => $obj['route'],280 'left' => $obj['lft'],281 'right' => $obj['rgt'],282 'level' => $obj['level'],283 'protected' => $obj['protected'],284 'locked' => $obj['locked'],285 );286 }287 288 247 } plugins/csNavigationPlugin/trunk/lib/csNavigationRoute.class.php
r19597 r19614 2 2 3 3 /* 4 TODO Deprecatethis class4 TODO Refactor this class 5 5 */ 6 6 … … 67 67 $this->addRouteParam($key, $param); 68 68 } 69 elseif(method_exists(new csNavigation (), sfInflector::camelize('get_default_'.$key)))69 elseif(method_exists(new csNavigationMenu(), sfInflector::camelize('get_default_'.$key))) 70 70 { 71 71 $method = sfInflector::camelize('get_default_'.$key); 72 $this->addRouteParam($key, csNavigation ::$method($this));72 $this->addRouteParam($key, csNavigationMenu::$method($this)); 73 73 } 74 74 } plugins/csNavigationPlugin/trunk/lib/filter/csNavgationFilter.class.php
r19596 r19614 24 24 public function execute($filterChain) 25 25 { 26 if ($this->isFirstCall())27 {28 if(file_exists(sfConfig::get('sf_config_dir').'/navigation.yml'))29 {30 $path = sfConfig::get('sf_config_dir').'/navigation.yml';31 }32 else33 {34 $path = sfConfig::get('sf_app_config_dir').'/navigation.yml';35 }36 include(sfContext::getInstance()->getConfigCache()->checkConfig($path));37 csNavigationHelper::init($settings, $navigation);38 }39 $filterChain->execute();26 if ($this->isFirstCall()) 27 { 28 if(file_exists(sfConfig::get('sf_config_dir').'/navigation.yml')) 29 { 30 $path = sfConfig::get('sf_config_dir').'/navigation.yml'; 31 } 32 else 33 { 34 $path = sfConfig::get('sf_app_config_dir').'/navigation.yml'; 35 } 36 include(sfContext::getInstance()->getConfigCache()->checkConfig($path)); 37 csNavigationHelper::init($settings, $navigation); 38 } 39 $filterChain->execute(); 40 40 } 41 41 } plugins/csNavigationPlugin/trunk/modules/csNavigation/lib/BasecsNavigationComponents.class.php
r19599 r19614 33 33 } 34 34 35 36 37 35 if($nav) 38 36 { … … 50 48 $this->class = isset($this->class) ? $this->class : ''; 51 49 } 50 52 51 public function executeTree() 53 52 { … … 58 57 if(!isset($this->items)) 59 58 { 60 $nav = csNavigationHelper::getNavigationTree($this->level, $this->iterations); 59 $nav = Doctrine::getTable('csNavigationMenu')->getMenu(); 60 61 61 $this->title = isset($this->title) ? $this->title : $nav->getTitle(); 62 $this->items = $nav->getItems($this->offset); 62 63 $root = $nav->getSegment($this->level, $this->iterations); 64 65 $this->items = $root->getChildren(); 63 66 } 64 67 $this->class = isset($this->class) ? $this->class : ''; plugins/csNavigationPlugin/trunk/modules/csNavigation/templates/_tree.php
r19596 r19614 1 1 <?php if (isset($title) && $title): ?> 2 <b><?php echo $title ?></b>2 <b><?php echo $title ?></b> 3 3 <?php endif ?> 4 4 <?php if (count($items) > 0): ?> 5 5 6 <ul class="<?php echo $class ?>" id="<?php echo $id ?>"> 6 7 <?php foreach ($items as $item): ?> 7 8 <?php if ($item->isAuthenticated()): ?> 8 <?php if ($item->isActive()): ?> 9 <li class="active"><a href="#"><?php echo $item->getName() ?></a> 10 <?php elseif ($item->isActiveBranch()): ?> 11 <li class="active parent"><?php echo link_to($item->getName(), $item->getRoute()) ?></a> 12 <?php elseif($item->getRoute()): ?> 13 <li><?php echo link_to($item->getName(), $item->getRoute()) ?> 14 <?php else: ?> 15 <li><?php echo $item->getName() ?> 16 <?php endif ?> 17 <?php if ($item->hasItems() && ($item->isExpanded() || ($max_level && $item->level <= $max_level))): ?> 18 <?php include_component('csNavigation', 'tree', array('items' => $item->getItems(), 'iterations' => $iterations)) ?> 9 <?php if ($item->isActive()): ?> 10 <li class="active"><a href="#"><?php echo $item->getName() ?></a> 11 <?php elseif ($item->isActiveBranch()): ?> 12 <li class="active-parent"><?php echo link_to($item->getName(), $item->getRoute()) ?></a> 13 <?php elseif($item->getRoute()): ?> 14 <li><?php echo link_to($item->getName(), $item->getRoute()) ?> 15 <?php else: ?> 16 <li><?php echo $item->getName() ?> 19 17 <?php endif ?> 20 </li> 21 <?php endif; ?> 18 <?php if ($item->hasChildren() && ($item->isExpanded() || ($max_level && $item->level <= $max_level))): ?> 19 <?php include_component('csNavigation', 'tree', array('items' => $item->getChildren(), 'iterations' => $iterations)) ?> 20 <?php endif ?> 21 </li> 22 <?php endif; ?> 22 23 <?php endforeach ?> 23 24 </ul>