PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/modules/fuel/models/assets_model.php

https://github.com/be3/FUEL-CMS
PHP | 331 lines | 255 code | 53 blank | 23 comment | 26 complexity | 171dc809f7b4ce59cc027486cfd54609 MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. // not pulling from the database so just extend the normal model
  3. require_once(APPPATH.'libraries/Validator.php');
  4. class Assets_model extends CI_Model {
  5. public $filters = array('group_id' => 'images');
  6. public $filter_value = null;
  7. public $key_field = 'id';
  8. protected $_dirs = array('images', 'pdf');
  9. protected $_dir_filetypes = array('images' => 'jpg|jpe|jpeg|png|gif', 'pdf' => 'pdf');
  10. protected $validator = NULL; // the validator object
  11. private $_encoded = FALSE;
  12. function __construct()
  13. {
  14. parent::__construct();
  15. $CI =& get_instance();
  16. $CI->load->helper('directory');
  17. $this->_dirs = list_directories($CI->asset->assets_server_path, $CI->config->item('assets_excluded_dirs', 'fuel'), FALSE, TRUE);
  18. $this->_dir_filetypes = $CI->config->item('editable_asset_filetypes', 'fuel');
  19. $CI->load->helper('directory');
  20. $CI->load->helper('file');
  21. $this->validator = new Validator();
  22. $this->validator->register_to_global_errors = FALSE;
  23. }
  24. function add_filters($filters){
  25. if (empty($this->filters))
  26. {
  27. $this->filters = $filters;
  28. }
  29. else
  30. {
  31. $this->filters = array_merge($this->filters, $filters);
  32. }
  33. }
  34. function list_items($limit = null, $offset = 0, $col = 'name', $order = 'asc')
  35. {
  36. $CI =& get_instance();
  37. $CI->load->helper('array');
  38. $CI->load->helper('convert');
  39. if (!isset($this->filters['group_id'])) return array();
  40. $group_id = $this->filters['group_id'];
  41. // not encoded yet... then decode
  42. if (!$this->_encoded)
  43. {
  44. $this->filters['group_id'] = uri_safe_encode($group_id); // to pass the current folder
  45. $this->_encoded = TRUE;
  46. }
  47. else
  48. {
  49. $group_id = uri_safe_decode($group_id);
  50. }
  51. $asset_dir = $this->get_dir($group_id);
  52. $assets_path = $CI->asset->assets_server_path.$asset_dir.DIRECTORY_SEPARATOR;
  53. $tmpfiles = directory_to_array($assets_path, TRUE, $CI->config->item('assets_excluded_dirs', 'fuel'), FALSE);
  54. $files = get_dir_file_info($assets_path, TRUE);
  55. $cnt = count($tmpfiles);
  56. $return = array();
  57. $asset_type_path = WEB_PATH.$CI->config->item('assets_path').$asset_dir.'/';
  58. //for ($i = $offset; $i < $cnt - 1; $i++)
  59. for ($i = 0; $i < $cnt; $i++)
  60. {
  61. if (!empty($tmpfiles[$i]) && !empty($files[$tmpfiles[$i]]))
  62. {
  63. $key = $tmpfiles[$i];
  64. if (empty($this->filters['name']) ||
  65. (!empty($this->filters['name']) &&
  66. (strpos($files[$key]['name'], $this->filters['name']) !== FALSE || strpos($key, $this->filters['name']) !== FALSE)))
  67. {
  68. $file['id'] = uri_safe_encode(assets_server_to_web_path($files[$tmpfiles[$i]]['server_path'], TRUE));
  69. //$file['filename'] = $files[$key]['name'];
  70. $file['name'] = $key;
  71. $file['preview/kb'] = $files[$key]['size'];
  72. $file['link'] = NULL;
  73. $file['last_updated'] = english_date($files[$key]['date'], true);
  74. $return[] = $file;
  75. }
  76. }
  77. }
  78. $return = array_sorter($return, $col, $order, TRUE);
  79. // do a check for empty limit values to prevent issues found where an empty $limit value would return nothing in 5.16
  80. $return = (empty($limit)) ? array_slice($return, $offset) : array_slice($return, $offset, $limit);
  81. // after sorting add the images
  82. foreach ($return as $key => $val)
  83. {
  84. if (is_image_file($return[$key]['name']))
  85. {
  86. $return[$key]['preview/kb'] = $return[$key]['preview/kb'].' kb <div class="img_crop"><a href="'.$asset_type_path.$return[$key]['name'].'" target="_blank"><img src="'.$asset_type_path.($return[$key]['name']).'" border="0"></a></div>';
  87. $return[$key]['link'] = '<a href="'.$asset_type_path.$return[$key]['name'].'" target="_blank">'.$asset_dir.'/'.$return[$key]['name'].'</a>';
  88. }
  89. else
  90. {
  91. $return[$key]['preview/kb'] = $return[$key]['preview/kb'];
  92. $return[$key]['link'] = '<a href="'.$asset_type_path.$return[$key]['name'].'" target="_blank">'.$asset_dir.'/'.$return[$key]['name'].'</a>';
  93. }
  94. }
  95. return $return;
  96. }
  97. function list_items_total()
  98. {
  99. return count($this->list_items());
  100. }
  101. function get_dir($dir)
  102. {
  103. $dirs = (array) $this->get_dirs();
  104. return (isset($dirs[$dir])) ? $dirs[$dir] : $this->get_image_dir();
  105. }
  106. function get_dirs()
  107. {
  108. $dirs = array();
  109. if (!empty($this->_dirs))
  110. {
  111. $dirs = array_combine($this->_dirs, $this->_dirs);
  112. }
  113. ksort($dirs);
  114. return $dirs;
  115. }
  116. function get_image_dir()
  117. {
  118. $CI =& get_instance();
  119. $editable_filetypes = $CI->config->item('editable_asset_filetypes', 'fuel');
  120. foreach($editable_filetypes as $folder => $types)
  121. {
  122. if (preg_match('#(jp(e){0,1}g|gif|png)#i', $types))
  123. {
  124. return $folder;
  125. }
  126. }
  127. return key(reset($editable_filetypes));
  128. }
  129. function get_dir_filetypes()
  130. {
  131. return $this->_dir_filetypes;
  132. }
  133. function get_dir_filetype($filetype)
  134. {
  135. return (isset($this->_dir_filetypes[$filetype])) ? $this->_dir_filetypes[$filetype] : FALSE;
  136. }
  137. function find_by_key($file)
  138. {
  139. $CI =& get_instance();
  140. $asset_path = WEB_ROOT.$CI->config->item('assets_path').$file;
  141. $asset_path = str_replace('/', DIRECTORY_SEPARATOR, $asset_path); // for windows
  142. return get_file_info($asset_path);
  143. }
  144. function record_count($dir = 'images')
  145. {
  146. $CI =& get_instance();
  147. $assets_path = WEB_ROOT.$CI->config->item('assets_path').$dir.'/';
  148. $files = dir_files($assets_path, false, false);
  149. return count($files);
  150. }
  151. function delete($file)
  152. {
  153. $CI =& get_instance();
  154. $CI->load->helper('convert');
  155. $filepath = WEB_ROOT.$CI->config->item('assets_path').$file;
  156. $parent_folder = dirname($filepath).'/';
  157. if (file_exists($filepath))
  158. {
  159. $deleted = unlink($filepath);
  160. }
  161. $max_depth = 5;
  162. $i = 0;
  163. $end = FALSE;
  164. while(!$end)
  165. {
  166. // if it is the last file in a subfolder (not one of the main asset folders), then we recursively remove the folder to clean things up
  167. if (!in_array($parent_folder, $this->_get_excluded_asset_server_folders()))
  168. {
  169. $dir_files = directory_to_array($parent_folder);
  170. // if empty, now remove
  171. if (empty($dir_files))
  172. {
  173. @rmdir($parent_folder);
  174. }
  175. else
  176. {
  177. $end = TRUE;
  178. }
  179. }
  180. else
  181. {
  182. $end = TRUE;
  183. }
  184. $parent_folder = dirname($parent_folder).'/';
  185. }
  186. $i++;
  187. if ($max_depth == $i) $end = TRUE;
  188. return $deleted;
  189. }
  190. private function _get_excluded_asset_server_folders()
  191. {
  192. $CI =& get_instance();
  193. $excluded = $CI->config->item('assets_excluded_dirs', 'fuel');
  194. $return = array();
  195. foreach($excluded as $folder)
  196. {
  197. $return[] = assets_server_path($folder).'/';
  198. }
  199. return $return;
  200. }
  201. function key_field()
  202. {
  203. return $this->key_field;
  204. }
  205. // --------------------------------------------------------------------
  206. /**
  207. * Get the validation object
  208. *
  209. * @access public
  210. * @return object
  211. */
  212. public function &get_validation()
  213. {
  214. return $this->validator;
  215. }
  216. // --------------------------------------------------------------------
  217. /**
  218. * Return validation errors
  219. *
  220. * @access public
  221. * @return array
  222. */
  223. public function get_errors()
  224. {
  225. return $this->validator->get_errors();
  226. }
  227. function form_fields($values = array())
  228. {
  229. $CI =& get_instance();
  230. $fields = array();
  231. $editable_asset_types = $this->config->item('editable_asset_filetypes', 'fuel');
  232. $accepts = (!empty($editable_asset_types['media']) ? $editable_asset_types['media'] : 'jpg|jpe|jpeg|gif|png');
  233. $fields['userfile'] = array('label' => lang('form_label_file'), 'type' => 'file', 'class' => 'multifile', 'accept' => $accepts); // key is userfile because that is what CI looks for in Upload Class
  234. $fields['asset_folder'] = array('label' => lang('form_label_asset_folder'), 'type' => 'select', 'options' => $this->get_dirs(), 'comment' => lang('assets_comment_asset_folder'));
  235. $fields['userfile_filename'] = array('label' => lang('form_label_new_file_name'), 'comment' => lang('assets_comment_filename'));
  236. if ($CI->config->item('assets_allow_subfolder_creation', 'fuel'))
  237. {
  238. $fields['subfolder'] = array('label' => lang('form_label_subfolder'), 'comment' => lang('assets_comment_filename'));
  239. }
  240. $fields['overwrite'] = array('label' => lang('form_label_overwrite'), 'type' => 'checkbox', 'comment' => lang('assets_comment_overwrite'), 'checked' => true, 'value' => '1');
  241. $fields[lang('assets_heading_image_specific')] = array('type' => 'section');
  242. $fields['create_thumb'] = array('label' => lang('form_label_create_thumb'), 'type' => 'checkbox', 'comment' => lang('assets_comment_thumb'), 'value' => '1');
  243. $fields['maintain_ratio'] = array('label' => lang('form_label_maintain_ratio'), 'type' => 'checkbox', 'comment' => lang('assets_comment_aspect_ratio'), 'value' => '1');
  244. $fields['width'] = array('label' => lang('form_label_width'), 'comment' => lang('assets_comment_width'), 'size' => '3');
  245. $fields['height'] = array('label' => lang('form_label_height'), 'comment' => lang('assets_comment_height'), 'size' => '3');
  246. $fields['master_dimension'] = array('type' => 'select', 'label' => lang('form_label_master_dimension'), 'options' => array('auto' => 'auto', 'width' => 'width', 'height' => 'height'), 'comment' => lang('assets_comment_master_dim'));
  247. return $fields;
  248. }
  249. function on_after_post($values)
  250. {
  251. if (empty($values['userfile_path'])) return;
  252. // process any uploaded images files that have been specified
  253. foreach($_FILES as $file)
  254. {
  255. if (is_image_file($file['name']) AND
  256. (!empty($values['userfile_create_thumb']) OR
  257. !empty($values['userfile_maintain_ratio']) OR
  258. !empty($values['userfile_width']) OR
  259. !empty($values['userfile_height'])))
  260. {
  261. $CI =& get_instance();
  262. $CI->load->library('image_lib');
  263. $config['source_image'] = $values['userfile_path'].$file['name'];
  264. $config['create_thumb'] = $values['userfile_create_thumb'];
  265. $config['maintain_ratio'] = $values['userfile_maintain_ratio'];
  266. if (!empty($values['userfile_width'])) $config['width'] = $values['userfile_width'];
  267. if (!empty($values['userfile_height'])) $config['height'] = $values['userfile_height'];
  268. if (!empty($values['userfile_master_dim'])) $config['master_dim'] = $values['userfile_master_dim'];
  269. $this->image_lib->initialize($config);
  270. if ( ! $CI->image_lib->resize())
  271. {
  272. $error = $CI->image_lib->display_errors();
  273. $CI->validator->catch_error($error);
  274. }
  275. }
  276. }
  277. }
  278. }