PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/system/cms/modules/files/plugin.php

https://github.com/JamieLomas/pyrocms
PHP | 300 lines | 198 code | 50 blank | 52 comment | 28 complexity | 013c8f6464820f9a95edf201facbbcf5 MD5 | raw file
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * Files Plugin
  4. *
  5. * Create a list of files
  6. *
  7. * @package PyroCMS
  8. * @author Marcos Coelho - PyroCMS Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, PyroCMS
  10. *
  11. */
  12. class Plugin_Files extends Plugin
  13. {
  14. private $_files = array();
  15. public function __construct()
  16. {
  17. $this->load->model(array(
  18. 'file_m',
  19. 'file_folders_m'
  20. ));
  21. }
  22. /**
  23. * Files listing
  24. *
  25. * Creates a list of files
  26. *
  27. * Usage:
  28. *
  29. * {pyro:files:listing folder="home-slider" type="i" fetch="subfolder|root"}
  30. * // your html logic
  31. * {/pyro:files:listing}
  32. *
  33. * The tags that are available to use from this method are listed below
  34. *
  35. * {id}
  36. * {folder_id}
  37. * {user_id}
  38. * {type}
  39. * {name}
  40. * {filename}
  41. * {description}
  42. * {extension}
  43. * {mimetype}
  44. * {width}
  45. * {height}
  46. * {filesize}
  47. * {date_added}
  48. *
  49. * @return array
  50. */
  51. public function listing()
  52. {
  53. if ( ! $this->content())
  54. {
  55. return '';
  56. }
  57. $folder_id = $this->attribute('folder', ''); // Id or Path
  58. $limit = $this->attribute('limit', '10');
  59. $offset = $this->attribute('offset', '');
  60. $type = $this->attribute('type', '');
  61. $fetch = $this->attribute('fetch');
  62. if ( ! empty($folder_id) && (empty($type) || in_array($type, array('a','v','d','i','o'))))
  63. {
  64. if (is_numeric($folder_id))
  65. {
  66. $folder = $this->file_folders_m->get($folder_id);
  67. }
  68. elseif (is_string($folder_id))
  69. {
  70. $folder = $this->file_folders_m->get_by_path($folder_id);
  71. }
  72. }
  73. if (empty($folder))
  74. {
  75. return array();
  76. }
  77. if (in_array($fetch, array('root', 'subfolder')) &&
  78. $subfolders = $this->file_folders_m->folder_tree(
  79. $fetch === 'root' ? $folder->root_id : $folder->id
  80. ))
  81. {
  82. $ids = array_merge(array((int) $folder->id), array_keys($subfolders));
  83. $this->file_m->where_in('folder_id', $ids);
  84. }
  85. else
  86. {
  87. $this->file_m->where('folder_id', $folder->id);
  88. }
  89. $type AND $this->file_m->where('type', $type);
  90. $limit AND $this->file_m->limit($limit);
  91. $offset AND $this->file_m->limit($offset);
  92. $files = $this->file_m->get_all();
  93. $files AND array_merge($this->_files, assoc_array_prop($files));
  94. return $files;
  95. }
  96. public function file($return = '', $type = '')
  97. {
  98. // nothing to do
  99. if ($return && ! in_array($return, array('url', 'path')))
  100. {
  101. return '';
  102. }
  103. // prepare file params
  104. $id = $this->attribute('id');
  105. $type = $type && in_array($type, array('a','v','d','i','o')) ? $type : '';
  106. // get file
  107. if (isset($this->_files[$id]))
  108. {
  109. $file = $this->_files[$id];
  110. }
  111. else
  112. {
  113. $type AND $this->file_m->where('type', $type);
  114. $file = $this->file_m->get($id);
  115. }
  116. // file not found
  117. if ( ! $file OR ($type && $file->type !== $type))
  118. {
  119. return '';
  120. }
  121. // return file fields array
  122. elseif ( ! $return && $this->content())
  123. {
  124. return (array) $file;
  125. }
  126. // make uri
  127. if ($type === 'i')
  128. {
  129. if ($size = $this->attribute('size', ''))
  130. {
  131. strpos($size, 'x') === FALSE AND $size .= 'x';
  132. list($width, $height) = explode('/', strtr($size, 'x', '/'));
  133. }
  134. else
  135. {
  136. $width = $this->attribute('width', '');
  137. $height = $this->attribute('height', '');
  138. }
  139. is_numeric($width) OR $width = 'auto';
  140. is_numeric($height) OR $height = 'auto';
  141. if ($width === 'auto' && $height === 'auto')
  142. {
  143. $dimension = '';
  144. }
  145. else
  146. {
  147. $mode = $this->attribute('mode', '');
  148. $mode = in_array($mode, array('fill', 'fit')) ? $mode : '';
  149. $dimension = trim($width . '/' . $height . '/' . $mode, '/');
  150. }
  151. $uri = $dimension ? sprintf('files/thumb/%s/%s', $file->id, $dimension) : sprintf('files/large/%s', $file->id);
  152. }
  153. else
  154. {
  155. $uri = 'files/download/' . $file->id;
  156. }
  157. // return string
  158. if ($return)
  159. {
  160. return $return === 'url' ? site_url($uri) : BASE_URI . $uri;
  161. }
  162. $attributes = $this->attributes();
  163. foreach (array('base', 'size', 'id', 'title', 'type', 'mode', 'width', 'height') as $key)
  164. {
  165. if (isset($attributes[$key]) && ($type !== 'i' OR ! in_array($key, array('width', 'height'))))
  166. {
  167. unset($attributes[$key]);
  168. }
  169. if (isset($attributes['tag-' . $key]))
  170. {
  171. $attributes[$key] = $attributes['tag-' . $key];
  172. unset($attributes['tag-' . $key]);
  173. }
  174. }
  175. $base = $this->attribute('base', 'url');
  176. // return an image tag html
  177. if ($type === 'i')
  178. {
  179. $this->load->helper('html');
  180. if (strpos($size, 'x') !== FALSE && ! isset($attributes['width'], $attributes['height']))
  181. {
  182. list($attributes['width'], $attributes['height']) = explode('x', $size);
  183. }
  184. return $this->{'_build_tag_location_' . $base}($type, $uri, array(
  185. 'attributes' => $attributes,
  186. 'index_page' => TRUE
  187. ));
  188. }
  189. // return an file anchor tag html
  190. $title = $this->attribute('title');
  191. return $this->{'_build_tag_location_' . $base}($type, $uri, compact('title', 'attributes'));
  192. }
  193. public function image()
  194. {
  195. return $this->file('', 'i');
  196. }
  197. public function image_url()
  198. {
  199. return $this->file_url('i');
  200. }
  201. public function image_path()
  202. {
  203. return $this->file_path('i');
  204. }
  205. public function file_url($type = '')
  206. {
  207. return $this->file('url', $type);
  208. }
  209. public function file_path($type = '')
  210. {
  211. return $this->file('path', $type);
  212. }
  213. public function exists()
  214. {
  215. $id = $this->attribute('id');
  216. $exists = (bool) (isset($this->_files[$id]) ? TRUE : $this->file_m->exists($id));
  217. return $exists && $this->content() ? $this->content() : $exists;
  218. }
  219. private function _build_tag_location_url($type = '', $uri = '', $extras = array())
  220. {
  221. extract($extras);
  222. if ($type === 'i')
  223. {
  224. $attributes['src'] = $uri;
  225. return img($attributes, $index_page);
  226. }
  227. return anchor($uri, $title, $attributes);
  228. }
  229. private function _build_tag_location_path($type = '', $uri = '', $extras = array())
  230. {
  231. extract($extras);
  232. // unset config base_url
  233. $base_url = $this->config->item('base_url');
  234. $this->config->set_item('base_url', '');
  235. // generate tag
  236. if ($type === 'i')
  237. {
  238. $attributes['src'] = $uri;
  239. $tag = img($attributes, $index_page);
  240. }
  241. else
  242. {
  243. $tag = anchor($uri, $title, $attributes);
  244. }
  245. // set config base_url
  246. $base_url = $this->config->set_item('base_url', $base_url);
  247. return $tag;
  248. }
  249. }
  250. /* End of file plugin.php */