PageRenderTime 53ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/system/cms/modules/files/models/file_folders_m.php

https://github.com/marcoscoelho/pyrocms
PHP | 257 lines | 146 code | 42 blank | 69 comment | 18 complexity | db5b669c52ae64a7d7030bf2e8ae6244 MD5 | raw file
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * PyroCMS
  4. *
  5. * An open source CMS based on CodeIgniter
  6. *
  7. * @package PyroCMS
  8. * @author PyroCMS Dev Team
  9. * @license Apache License v2.0
  10. * @link http://pyrocms.com
  11. * @since Version 1.0-dev
  12. * @filesource
  13. */
  14. /**
  15. * PyroCMS File Folders Model
  16. *
  17. * Interacts with the file_folders table in the database.
  18. *
  19. * @author Dan Horrigan <dan@dhorrigan.com>
  20. * @author Eric Barnes <eric@pyrocms.com>
  21. * @package PyroCMS
  22. * @subpackage Files
  23. */
  24. class File_folders_m extends MY_Model {
  25. private $_folders = array();
  26. /**
  27. * Exists
  28. *
  29. * Checks if a given folder exists.
  30. *
  31. * @access public
  32. * @param int The folder id or slug
  33. * @return bool If the folder exists
  34. */
  35. public function exists($folder = 0)
  36. {
  37. if (is_numeric($folder))
  38. {
  39. $count = array('id' => $folder);
  40. }
  41. else
  42. {
  43. $count = array('slug' => $folder);
  44. }
  45. return (bool) (parent::count_by($count) > 0);
  46. }
  47. // ------------------------------------------------------------------------
  48. /**
  49. * Has Children
  50. *
  51. * Checks if a given folder has children or not.
  52. *
  53. * @access public
  54. * @param int The folder id
  55. * @return bool If the folder has children
  56. */
  57. public function has_children($folder_id = 0)
  58. {
  59. return (bool) (parent::count_by(array('parent_id' => $folder_id)) > 0);
  60. }
  61. // ------------------------------------------------------------------------
  62. /**
  63. * Folder Tree
  64. *
  65. * Get folder in an array
  66. *
  67. * @uses folder_subtree
  68. */
  69. public function folder_tree($parent_id = 0, $depth = 0, &$arr = array())
  70. {
  71. $arr = $arr ? $arr : array();
  72. if ($parent_id === 0)
  73. {
  74. $arr = array();
  75. $depth = 0;
  76. }
  77. $folders = $this
  78. ->order_by('name')
  79. ->get_many_by(array('parent_id' => $parent_id));
  80. if ( ! $folders)
  81. {
  82. return $arr;
  83. }
  84. static $root = NULL;
  85. foreach ($folders as $folder)
  86. {
  87. if ($depth < 1)
  88. {
  89. $root = $folder->id;
  90. }
  91. // $folder->name_indent = repeater('&raquo; ', $depth) . $folder->name;
  92. $folder->root_id = $root;
  93. $folder->depth = $depth;
  94. $folder->count_files = sizeof($this->db->get_where('files', array('folder_id' => $folder->id))->result());
  95. $arr[$folder->id] = $folder;
  96. $old_size = sizeof($arr);
  97. $this->folder_tree($folder->id, $depth+1, $arr);
  98. $folder->count_subfolders = sizeof($arr) - $old_size;
  99. }
  100. if ($parent_id === 0)
  101. {
  102. foreach ($arr as $id => &$folder)
  103. {
  104. $folder->virtual_path = $this->_build_asc_segments($id, array(
  105. 'segments' => $arr,
  106. 'separator' => '/',
  107. 'attribute' => 'slug'
  108. ));
  109. }
  110. $this->_folders = $arr;
  111. }
  112. if ($parent_id > 0 && $depth < 1)
  113. {
  114. foreach ($arr as $id => &$folder)
  115. {
  116. $folder->virtual_path = $this->_folders[$id]->virtual_path;
  117. }
  118. }
  119. return $arr;
  120. }
  121. // ------------------------------------------------------------------------
  122. /**
  123. * Get Folders
  124. *
  125. * Get the full array of folders
  126. *
  127. * @return array
  128. */
  129. public function get_folders($id = 0)
  130. {
  131. if ($id)
  132. {
  133. $this->folder_tree($id);
  134. }
  135. elseif (empty($this->_folder))
  136. {
  137. $this->folder_tree();
  138. }
  139. return $this->_folders;
  140. }
  141. // ------------------------------------------------------------------------
  142. /**
  143. * Breadcrumb
  144. *
  145. * Generates a breadcrumb nav for folders
  146. *
  147. * @param int $node The current folder id
  148. * @param int $lev The current level
  149. * @return array
  150. */
  151. public function breadcrumb($id, $separator = '&raquo;', $limit = 3)
  152. {
  153. if ( ! $this->_folders)
  154. {
  155. $this->folder_tree();
  156. }
  157. return $this->_build_asc_segments($id, array(
  158. 'segments' => $this->_folders,
  159. 'attribute' => 'name',
  160. 'separator' => ' ' . trim($separator) . ' ',
  161. 'limit' => $limit
  162. ));
  163. }
  164. public function _build_asc_segments($id, $options = array())
  165. {
  166. if ( ! isset($options['segments']))
  167. {
  168. return;
  169. }
  170. $defaults = array(
  171. 'attribute' => 'name',
  172. 'separator' => ' &raquo; ',
  173. 'limit' => 0
  174. );
  175. $options = array_merge($defaults, $options);
  176. extract($options);
  177. $arr = array();
  178. while (isset($segments[$id]))
  179. {
  180. array_unshift($arr, $segments[$id]->{$attribute});
  181. $id = $segments[$id]->parent_id;
  182. }
  183. if (is_int($limit) && $limit > 0 && sizeof($arr) > $limit)
  184. {
  185. array_splice($arr, 1, -($limit-1), '&#8230;');
  186. }
  187. return implode($separator, $arr);
  188. }
  189. public function get_by_path($path)
  190. {
  191. if (is_array($path))
  192. {
  193. $path = implode('/', $path);
  194. }
  195. $path = trim($path, '/');
  196. if (empty($this->_folders))
  197. {
  198. $this->get_folders();
  199. }
  200. foreach ($this->_folders as $id => $folder)
  201. {
  202. if ($folder->virtual_path == $path)
  203. {
  204. return $folder;
  205. }
  206. }
  207. return array();
  208. }
  209. public function delete($id = 0)
  210. {
  211. $this->file_m->delete_files($id);
  212. return parent::delete($id);
  213. }
  214. }
  215. /* End of file file_folders_m.php */