PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/application/plugins/files/helpers/files.php

https://github.com/fb83/Project-Pier
PHP | 161 lines | 112 code | 20 blank | 29 comment | 31 complexity | 0b0e84ef1056743d813d392f18ed48fb MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, AGPL-3.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * Render select folder box
  4. *
  5. * @param string $name Control name
  6. * @param Project $project
  7. * @param integer $selected ID of selected folder
  8. * @param array $attributes Select box attributes
  9. * @return string
  10. */
  11. function select_project_folder($name, $project = null, $selected = null, $exclude = null, $attributes = null) {
  12. if (is_null($project)) {
  13. $project = active_project();
  14. } // if
  15. if (!($project instanceof Project)) {
  16. throw new InvalidInstanceError('$project', $project, 'Project');
  17. } // if
  18. if (is_array($attributes)) {
  19. if (!isset($attributes['class'])) {
  20. $attributes['class'] = 'select_folder';
  21. }
  22. } else {
  23. $attributes = array('class' => 'select_folder');
  24. } // if
  25. $options = array(option_tag(lang('none'), 0));
  26. $folders = ProjectFolders::getProjectFolders($project);
  27. if (is_array($folders)) {
  28. $sorted = array();
  29. foreach ($folders as $folder) {
  30. $sorted[$folder->getObjectName(true)] = $folder;
  31. } // foreach
  32. ksort($sorted);
  33. foreach ($sorted as $k => $folder) {
  34. $option_attributes = $folder->getId() == $selected ? array('selected' => true) : null;
  35. $options[] = option_tag($k, $folder->getId(), $option_attributes);
  36. } // foreach
  37. } // if
  38. return select_box($name, $options, $attributes);
  39. } // select_project_folder
  40. /**
  41. * Select a single project file
  42. *
  43. * @param string $name Control name
  44. * @param Project $project
  45. * @param integer $selected ID of selected file
  46. * @param array $exclude_files Array of IDs of files that need to be excluded (already attached to object etc)
  47. * @param array $attributes
  48. * @return string
  49. */
  50. function select_project_file($name, $project = null, $selected = null, $exclude_files = null, $attributes = null) {
  51. if (is_null($project)) {
  52. $project = active_project();
  53. } // if
  54. if (!($project instanceof Project)) {
  55. throw new InvalidInstanceError('$project', $project, 'Project');
  56. } // if
  57. $all_options = array(option_tag(lang('none'), 0)); // array of options
  58. $folders = $project->getFolders();
  59. if (is_array($folders)) {
  60. foreach ($folders as $folder) {
  61. $files = $folder->getFiles();
  62. if (is_array($files)) {
  63. $options = array();
  64. foreach ($files as $file) {
  65. if (is_array($exclude_files) && in_array($file->getId(), $exclude_files)) {
  66. continue;
  67. }
  68. $option_attrbutes = $file->getId() == $selected ? array('selected' => true) : null;
  69. $options[] = option_tag($file->getFilename(), $file->getId(), $option_attrbutes);
  70. } // if
  71. if (count($options)) {
  72. $all_options[] = option_tag('', 0); // separator
  73. $all_options[] = option_group_tag($folder->getName(), $options);
  74. } // if
  75. } // if
  76. } // foreach
  77. } // if
  78. $orphaned_files = $project->getOrphanedFiles();
  79. if (is_array($orphaned_files)) {
  80. $all_options[] = option_tag('', 0); // separator
  81. foreach ($orphaned_files as $file) {
  82. if (is_array($exclude_files) && in_array($file->getId(), $exclude_files)) {
  83. continue;
  84. }
  85. $option_attrbutes = $file->getId() == $selected ? array('selected' => true) : null;
  86. $all_options[] = option_tag($file->getFilename(), $file->getId(), $option_attrbutes);
  87. } // foreach
  88. } // if
  89. return select_box($name, $all_options, $attributes);
  90. } // select_project_file
  91. /**
  92. * Render folder tree
  93. *
  94. * @param string $name Control name
  95. * @param Project $project
  96. * @param integer $selected ID of selected folder
  97. * @param array $attributes Select box attributes
  98. * @return string
  99. */
  100. function render_folder_tree($folder, $depth = 0, $project = null, $selected = null, $attributes = null) {
  101. if ($depth>5) return;
  102. if (is_null($project)) {
  103. $project = active_project();
  104. } // if
  105. if (!($project instanceof Project)) {
  106. throw new InvalidInstanceError('$project', $project, 'Project');
  107. } // if
  108. if (is_array($attributes)) {
  109. if (!isset($attributes['class'])) {
  110. $attributes['class'] = 'select_folder';
  111. }
  112. } else {
  113. $attributes = array('class' => 'select_folder');
  114. } // if
  115. $options = array(option_tag(lang('none'), 0));
  116. $html = '';
  117. if ($folder instanceof ProjectFolder) {
  118. $folders = ProjectFolders::getProjectFolderTree( $project, $folder->getId() );
  119. } else {
  120. $folders = ProjectFolders::getProjectFolderTree( $project );
  121. }
  122. if (is_array($folders)) {
  123. $html .= '<ul>';
  124. foreach ($folders as $folder) {
  125. $class = $folder->getId() == $selected ? $class = 'class="selected"' : '';
  126. //$html .= '<li>' . $folder->getName() . render_folder_tree( $folder, $depth, $project, $selected, $attributes ) . '</li>';
  127. $html .= '<li><a href="' . $folder->getBrowseUrl() . '" ' . $class . '>' . clean($folder->getName()) . '</a>';
  128. if ($folder->canEdit(logged_user())) {
  129. $html .= ' <a href="' . $folder->getEditUrl() . '" class="blank" title="' . lang('edit folder') . '"><img src="' . icon_url('edit.gif') . '" alt="" /></a>';
  130. } // if
  131. if ($folder->canDelete(logged_user())) {
  132. $html .= ' <a href="' . $folder->getDeleteUrl() . '" class="blank" title="' . lang('delete folder') . '"><img src="' . icon_url('cancel_gray.gif') . '" alt="" /></a>';
  133. } // if
  134. $html .= render_folder_tree( $folder, $depth + 1, $project, $selected, $attributes );
  135. $html .= '</li>';
  136. } // foreach
  137. $html .= '</ul>';
  138. } // if
  139. return $html;
  140. } // render_folder_tree
  141. ?>