Controller_Tree.class.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Controller for displaying a tree
  4. *
  5. * @author Christopher Han <xiphux@gmail.com>
  6. * @copyright Copyright (c) 2010 Christopher Han
  7. * @package GitPHP
  8. * @subpackage Controller
  9. */
  10. class GitPHP_Controller_Tree extends GitPHP_ControllerBase
  11. {
  12. /**
  13. * Initialize controller
  14. */
  15. public function Initialize()
  16. {
  17. parent::Initialize();
  18. if (!(isset($this->params['hashbase']) || isset($this->params['hash']))) {
  19. $this->params['hashbase'] = 'HEAD';
  20. }
  21. if (isset($this->params['output']) && ($this->params['output'] == 'js')) {
  22. $this->DisableLogging();
  23. }
  24. }
  25. /**
  26. * Gets the template for this controller
  27. *
  28. * @return string template filename
  29. */
  30. protected function GetTemplate()
  31. {
  32. if (isset($this->params['output']) && ($this->params['output'] == 'js')) {
  33. return 'treelist.tpl';
  34. }
  35. return 'tree.tpl';
  36. }
  37. /**
  38. * Gets the cache key for this controller
  39. *
  40. * @return string cache key
  41. */
  42. protected function GetCacheKey()
  43. {
  44. return (isset($this->params['hashbase']) ? $this->params['hashbase'] : '') . '|' . (isset($this->params['hash']) ? $this->params['hash'] : '') . '|' . (isset($this->params['file']) ? sha1($this->params['file']) : '');
  45. }
  46. /**
  47. * Gets the name of this controller's action
  48. *
  49. * @param boolean $local true if caller wants the localized action name
  50. * @return string action name
  51. */
  52. public function GetName($local = false)
  53. {
  54. if ($local && $this->resource) {
  55. return $this->resource->translate('tree');
  56. }
  57. return 'tree';
  58. }
  59. /**
  60. * Loads data for this template
  61. */
  62. protected function LoadData()
  63. {
  64. if (!isset($this->params['hashbase'])) {
  65. // TODO: write a lookup for hash (tree) -> hashbase (commithash) and remove this
  66. throw new Exception('Hashbase is required');
  67. }
  68. $commit = $this->GetProject()->GetCommit($this->params['hashbase']);
  69. $this->tpl->assign('commit', $commit);
  70. if (!isset($this->params['hash'])) {
  71. if (isset($this->params['file'])) {
  72. $this->params['hash'] = $commit->GetTree()->PathToHash($this->params['file']);
  73. if (empty($this->params['hash']))
  74. throw new GitPHP_DirectoryNotFoundException($this->params['file']);
  75. } else {
  76. $this->params['hash'] = $commit->GetTreeHash();
  77. }
  78. }
  79. $tree = $this->GetProject()->GetObjectManager()->GetTree($this->params['hash']);
  80. if (!$tree->GetCommit()) {
  81. $tree->SetCommit($commit);
  82. }
  83. if (isset($this->params['file'])) {
  84. $tree->SetPath($this->params['file']);
  85. }
  86. $this->tpl->assign('tree', $tree);
  87. }
  88. }