ContentTree.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. class ContentTree {
  3. static $instances;
  4. protected $locale;
  5. function getCulture(){
  6. return $this->locale;
  7. }
  8. protected function __construct($culture)
  9. {
  10. $this->locale = $culture;
  11. }
  12. /**
  13. *
  14. * @return ContentTree
  15. */
  16. static function instance($culture = null)
  17. {
  18. if (null == $culture) {
  19. $culture = sfContext::getInstance()->getUser()->getCulture();
  20. }
  21. if (empty(self::$instances[$culture])) {
  22. self::$instances[$culture] = new ContentTree($culture);
  23. }
  24. return self::$instances[$culture];
  25. }
  26. /**
  27. *
  28. * @return sfFileCache
  29. */
  30. protected function createCacheInstance()
  31. {
  32. return new sfFileCache(array('cache_dir' => sfConfig::get('sf_cache_dir') . '/ContentTree'));
  33. }
  34. protected function getCacheKey()
  35. {
  36. return 'ContentTree.' . $this->locale;
  37. }
  38. public function clearCache()
  39. {
  40. $cache = $this->createCacheInstance();
  41. $cache->remove($this->getCacheKey());
  42. $this->root = null;
  43. return true;
  44. }
  45. public function clearAllCache()
  46. {
  47. foreach(sfConfig::get('app_translations_available_languages') as $locale){
  48. ContentTree::instance($locale)->clearCache();
  49. }
  50. }
  51. protected $cacheEnabled = true;
  52. public function setCacheEnabled($value)
  53. {
  54. $this->cacheEnabled = $value;
  55. }
  56. protected $root = null;
  57. protected $currentNode = null;
  58. function build($force = false)
  59. {
  60. $default_locale = sfConfig::get('app_translations_default_language');
  61. if ($force) {
  62. $this->clearCache();
  63. }
  64. if (empty($this->root)) {
  65. // printf('building tree cache...');
  66. $cache = $this->createCacheInstance();
  67. $tree = $cache->get($this->getCacheKey());
  68. if ($tree !== null) {
  69. $this->root = unserialize($tree);
  70. } else {
  71. $criteria = new Criteria();
  72. $criteria->addAscendingOrderByColumn(NodePeer::TREE_LEFT);
  73. $nodes = NodePeer::doSelectWithI18n($criteria, $default_locale);
  74. // var_dump($nodes);exit;
  75. $root = array_shift($nodes);
  76. if (null != $root) {
  77. $parentTreeNode = new ContentTreeNode($root, null, $default_locale);
  78. $parentTreeNode->setOwnerTree($this);
  79. $parentTreeNode->setMockTranslation($this->locale != $default_locale);
  80. $this->root = $parentTreeNode;
  81. $prevTreeNode = $parentTreeNode;
  82. $prevNode = $root;
  83. $nodeStack = array();
  84. $nodeStack[] = $root;
  85. $stack = array();
  86. $stack[] = $parentTreeNode;
  87. foreach($nodes as $node) {
  88. $treeNode = new ContentTreeNode($node, $parentTreeNode, $default_locale);
  89. $treeNode->setOwnerTree($this);
  90. $treeNode->setMockTranslation($this->locale != $default_locale);
  91. if ($prevNode->getTreeParent() == $node->getTreeParent()) {
  92. // printf('1. [%d, %d, %d]<br />', $node->getId(), $node->getTreeLeft(), $node->getTreeRight());
  93. } else {
  94. if ($prevNode->getId() == $node->getTreeParent()) {
  95. // printf('2. [%d, %d, %d]<br />', $node->getId(), $node->getTreeLeft(), $node->getTreeRight());
  96. $stack[] = $parentTreeNode;
  97. $nodeStack[] = $prevNode;
  98. $parentTreeNode = $prevTreeNode;
  99. } else {
  100. // printf('3. [%d, %d, %d]<br />', $node->getId(), $node->getTreeLeft(), $node->getTreeRight());
  101. while (count($nodeStack) > 0 && ($node->getTreeParent() != $nodeStack[count($nodeStack) - 1]->getId())) {
  102. $parentTreeNode = array_pop($stack);
  103. array_pop($nodeStack);
  104. }
  105. }
  106. }
  107. $parentTreeNode->addChild($treeNode);
  108. $prevTreeNode = $treeNode;
  109. $prevNode = $node;
  110. }
  111. }
  112. unset($nodes);
  113. unset($stack);
  114. unset($prevNode);
  115. unset($prevTreeNode);
  116. unset($parentTreeNode);
  117. unset($nodeStack);
  118. unset($treeNode);
  119. if ($this->locale != $default_locale) {
  120. $localized_nodes = array();
  121. $criteria = new Criteria();
  122. $criteria->addAscendingOrderByColumn(NodePeer::TREE_LEFT);
  123. $nodes = NodePeer::doSelectWithI18n($criteria, $this->locale);
  124. foreach($nodes as /*(Node)*/$node) {
  125. $ContentTreeNode = $this->getContentTreeNodeById($node->getId());
  126. if ($ContentTreeNode) {
  127. $ContentTreeNode->setTitle($node->getTitle());
  128. $ContentTreeNode->setMenuTitle($node->getMenuTitle());
  129. $ContentTreeNode->setSubTitle($node->getSubTitle());
  130. $ContentTreeNode->setUrlIdentifier($node->getUrlIdentifier());
  131. $ContentTreeNode->setBrowserTitleType($node->getBrowserTitleType());
  132. $ContentTreeNode->setBrowserTitleContent($node->getBrowserTitleContent());
  133. $ContentTreeNode->setSeoKeywordsType($node->getSeoKeywordsType());
  134. $ContentTreeNode->setSeoKeywordsContent($node->getSeoKeywordsContent());
  135. $ContentTreeNode->setSeoDescriptionContent($node->getSeoDescriptionContent());
  136. $ContentTreeNode->setSeoDescriptionType($node->getSeoDescriptionType());
  137. $ContentTreeNode->setCulture($this->locale);
  138. $ContentTreeNode->setMockTranslation(false);
  139. }
  140. //var_dump($ContentTreeNode); exit;
  141. }
  142. //var_dump($this); exit;
  143. }
  144. if ($this->cacheEnabled) {
  145. $cache->set($this->getCacheKey(), serialize($this->root));
  146. }
  147. }
  148. unset($tree);
  149. }
  150. }
  151. function initializeCurrentNodeByPath($path)
  152. {
  153. $path = explode('/', $path);
  154. while (count($path) > 0 && empty($path[0])) {
  155. array_shift($path);
  156. }
  157. // var_dump($path);exit;
  158. $result = $this->getRoot();
  159. while (($result != null) && count($path) > 0) {
  160. $result->active = true;
  161. $pathItem = array_shift($path);
  162. // var_dump($pathItem);
  163. $result = $result->getChildByUrlIdentifier($pathItem);
  164. }
  165. if ($result != null) {
  166. $result->active = true;
  167. }
  168. $this->currentNode = $result;
  169. return $result;
  170. }
  171. function initializeCurrentNodeById($id)
  172. {
  173. $result = $this->getContentTreeNodeById($id);
  174. $node = $result;
  175. while (isset($node)) {
  176. $node->active = true;
  177. $node = $node->parent;
  178. }
  179. $this->currentNode = $result;
  180. return $result;
  181. }
  182. function initializeCurrentNode($ContentTreeNode)
  183. {
  184. $result = $ContentTreeNode;
  185. $node = $result;
  186. while (isset($node)) {
  187. $node->active = true;
  188. $node = $node->parent;
  189. }
  190. $this->currentNode = $result;
  191. return $result;
  192. }
  193. /**
  194. *
  195. * @return ContentTreeNode
  196. */
  197. function getRoot()
  198. {
  199. return $this->root;
  200. }
  201. /**
  202. * @return ContentTreeNode
  203. */
  204. function getCurrentNode()
  205. {
  206. return $this->currentNode;
  207. }
  208. // function getContentNodeByUrl($url)
  209. // {
  210. // $path = explode('/', $url);
  211. // if (empty($path[0])) {
  212. // array_shift($path);
  213. // }
  214. // $result = $this->getRoot();
  215. // while (($result != null) && count($path) > 0) {
  216. // $pathItem = array_shift($path);
  217. // // var_dump($pathItem);
  218. // $result = $result->getChildByUrlIdentifier($pathItem);
  219. // }
  220. // //var_dump($result);exit;
  221. // return $result;
  222. // }
  223. /**
  224. *
  225. * @return ContentTreeNode
  226. */
  227. function getContentTreeNodeById($id)
  228. {
  229. if(!$this->getRoot()){
  230. $this->build();
  231. }
  232. assert($this->getRoot());
  233. return $this->getRoot()->getContentTreeNodeById($id);
  234. }
  235. protected function debugChildren($nodes, $prefix = '')
  236. {
  237. foreach($nodes as $node) {
  238. echo $prefix . $node->getTitle() . '-' . $node->getUrl() . '<br />';
  239. if (count($node->children) > 0) {
  240. $this->debugChildren($node->children, $prefix . '+');
  241. }
  242. }
  243. }
  244. function dump()
  245. {
  246. $this->debugChildren(array($this->getRoot()));
  247. }
  248. function getAll(){
  249. return array_merge(array($this->getRoot()), $this->getRoot()->getAll());
  250. }
  251. public function initializeCurrentNodeByReferer($request)
  252. {
  253. //var_dump(sprintf('%s/%s', sfConfig::get('app_static_url', sfConfig::get('app_site_url')), $this->getUser()->getCulture()));exit;
  254. $urlPrefix = sprintf('%s/%s', sfConfig::get('app_static_url', sfConfig::get('app_site_url')), sfContext::getInstance()->getUser()->getCulture());
  255. //var_dump($urlPrefix);
  256. $shortPath = str_replace($urlPrefix, '', $request->getReferer());
  257. //var_dump($shortPath);
  258. return $this->initializeCurrentNodeByPath($shortPath);
  259. }
  260. }
  261. ?>