PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/system/expressionengine/third_party/matrix/celltypes/file.php

https://bitbucket.org/tdevonshire/guinness-jazz-festival
PHP | 490 lines | 305 code | 85 blank | 100 comment | 55 complexity | 8d52e6da73f06e68a957dc73f731f1f0 MD5 | raw file
  1. <?php if (! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * File Celltype Class for EE2
  4. *
  5. * @package Matrix
  6. * @author Brandon Kelly <brandon@pixelandtonic.com>
  7. * @copyright Copyright (c) 2011 Pixel & Tonic, Inc
  8. */
  9. class Matrix_file_ft {
  10. var $info = array(
  11. 'name' => 'File'
  12. );
  13. var $default_settings = array(
  14. 'content_type' => 'any',
  15. 'directory' => 'all'
  16. );
  17. /**
  18. * Constructor
  19. */
  20. function __construct()
  21. {
  22. $this->EE =& get_instance();
  23. // Load the file_field library
  24. $this->EE->load->library('file_field');
  25. // -------------------------------------------
  26. // Prepare Cache
  27. // -------------------------------------------
  28. if (! isset($this->EE->session->cache['matrix']['celltypes']['file']))
  29. {
  30. $this->EE->session->cache['matrix']['celltypes']['file'] = array();
  31. }
  32. $this->cache =& $this->EE->session->cache['matrix']['celltypes']['file'];
  33. }
  34. /**
  35. * Prep Settings
  36. */
  37. private function _prep_settings(&$settings)
  38. {
  39. $settings = array_merge($this->default_settings, $settings);
  40. }
  41. // --------------------------------------------------------------------
  42. /**
  43. * Get Upload Preferences
  44. * @param int $group_id Member group ID specified when returning allowed upload directories only for that member group
  45. * @param int $id Specific ID of upload destination to return
  46. * @return array Result array of DB object, possibly merged with custom file upload settings (if on EE 2.4+)
  47. */
  48. private function _get_upload_preferences($group_id = NULL, $id = NULL)
  49. {
  50. if (version_compare(APP_VER, '2.4', '>='))
  51. {
  52. $this->EE->load->model('file_upload_preferences_model');
  53. return $this->EE->file_upload_preferences_model->get_file_upload_preferences($group_id, $id);
  54. }
  55. if (version_compare(APP_VER, '2.1.5', '>='))
  56. {
  57. $this->EE->load->model('file_upload_preferences_model');
  58. $result = $this->EE->file_upload_preferences_model->get_upload_preferences($group_id, $id);
  59. }
  60. else
  61. {
  62. $this->EE->load->model('tools_model');
  63. $result = $this->EE->tools_model->get_upload_preferences($group_id, $id);
  64. }
  65. // If an $id was passed, just return that directory's preferences
  66. if ( ! empty($id))
  67. {
  68. return $result->row_array();
  69. }
  70. // Use upload destination ID as key for row for easy traversing
  71. $return_array = array();
  72. foreach ($result->result_array() as $row)
  73. {
  74. $return_array[$row['id']] = $row;
  75. }
  76. return $return_array;
  77. }
  78. // --------------------------------------------------------------------
  79. /**
  80. * Display Cell Settings
  81. */
  82. function display_cell_settings($data)
  83. {
  84. $this->_prep_settings($data);
  85. if (version_compare(APP_VER, '2.2', '>='))
  86. {
  87. $directory_options['all'] = lang('all');
  88. $filedirs = $this->_get_upload_preferences(1);
  89. foreach ($filedirs as $filedir)
  90. {
  91. $directory_options[$filedir['id']] = $filedir['name'];
  92. }
  93. $r[] = array(
  94. lang('allowed_dirs_file'),
  95. form_dropdown('directory', $directory_options, $data['directory'])
  96. );
  97. }
  98. $content_type_options = array('all' => lang('all'), 'image' => lang('type_image'));
  99. $r[] = array(
  100. str_replace(' ', '&nbsp;', lang('field_content_file')),
  101. form_dropdown('content_type', $content_type_options, $data['content_type'])
  102. );
  103. return $r;
  104. }
  105. // --------------------------------------------------------------------
  106. /**
  107. * Display Cell
  108. */
  109. function display_cell($data)
  110. {
  111. if (REQ == 'PAGE')
  112. {
  113. return 'File cells don’t work within SafeCracker. Use SafeCracker File instead.';
  114. }
  115. $this->_prep_settings($this->settings);
  116. if (! isset($this->cache['displayed']))
  117. {
  118. if (isset($this->var_id))
  119. {
  120. // Load the file browser (thanks Rob!)
  121. $this->EE->file_field->browser();
  122. }
  123. // include matrix_text.js
  124. $theme_url = $this->EE->session->cache['matrix']['theme_url'];
  125. $this->EE->cp->add_to_foot('<script type="text/javascript" src="'.$theme_url.'scripts/matrix_file.js"></script>');
  126. $this->EE->lang->loadfile('matrix');
  127. $this->cache['displayed'] = TRUE;
  128. }
  129. $r['class'] = 'matrix-file';
  130. // -------------------------------------------
  131. // Get the upload directories
  132. // -------------------------------------------
  133. $upload_dirs = array();
  134. $upload_prefs = $this->_get_upload_preferences($this->EE->session->userdata('group_id'));
  135. foreach ($upload_prefs as $row)
  136. {
  137. $upload_dirs[$row['id']] = $row['name'];
  138. }
  139. // -------------------------------------------
  140. // Existing file?
  141. // -------------------------------------------
  142. if ($data)
  143. {
  144. if (is_array($data) && ! empty($data['filedir']) && ! empty($data['filename']))
  145. {
  146. $filedir = $data['filedir'];
  147. $filename = $data['filename'];
  148. }
  149. else if (is_string($data) && preg_match('/^{filedir_([0-9]+)}(.*)/', $data, $matches))
  150. {
  151. $filedir = $matches[1];
  152. $filename = $matches[2];
  153. }
  154. }
  155. if (isset($filedir))
  156. {
  157. if (version_compare(APP_VER, '2.1.5', '>='))
  158. {
  159. $this->EE->load->library('filemanager');
  160. $thumb_info = $this->EE->filemanager->get_thumb($filename, $filedir);
  161. $thumb_url = $thumb_info['thumb'];
  162. if (! isset($thumb_info['thumb_path']))
  163. {
  164. $filedir_info = $this->_get_upload_preferences(1, $filedir);
  165. $thumb_info['thumb_path'] = $filedir_info['server_path'].'_thumb/'.$filename;
  166. }
  167. if (file_exists($thumb_info['thumb_path']))
  168. {
  169. $thumb_size = getimagesize($thumb_info['thumb_path']);
  170. }
  171. else
  172. {
  173. $thumb_url = PATH_CP_GBL_IMG.'default.png';
  174. $thumb_size = array(64, 64);
  175. }
  176. }
  177. else
  178. {
  179. $filedir_info = $this->_get_upload_preferences(1, $filedir);
  180. $thumb_filename = $filedir_info['server_path'].'_thumbs/thumb_'.$filename;
  181. if (file_exists($thumb_filename))
  182. {
  183. $thumb_url = $filedir_info['url'].'_thumbs/thumb_'.$filename;
  184. $thumb_size = getimagesize($thumb_filename);
  185. }
  186. else
  187. {
  188. $thumb_url = PATH_CP_GBL_IMG.'default.png';
  189. $thumb_size = array(64, 64);
  190. }
  191. }
  192. $r['data'] = '<div class="matrix-thumb" style="width: '.$thumb_size[0].'px;">'
  193. . '<a title="'.lang('remove_file').'"></a>'
  194. . '<img src="'.$thumb_url.'" width="'.$thumb_size[0].'" height="'.$thumb_size[1].'" />'
  195. . '</div>'
  196. . '<div class="matrix-filename">'.$filename.'</div>';
  197. $add_style = ' style="display: none;"';
  198. }
  199. else
  200. {
  201. $filedir = '';
  202. $filename = '';
  203. $r['data'] = '';
  204. $add_style = '';
  205. }
  206. $add_line = ($this->settings['content_type'] != 'image') ? 'add_file' : 'add_image';
  207. $r['data'] .= '<input type="hidden" name="'.$this->cell_name.'[filedir]" value="'.$filedir .'" class="filedir" />'
  208. . '<input type="hidden" name="'.$this->cell_name.'[filename]" value="'.$filename.'" class="filename" />'
  209. . '<a class="matrix-btn matrix-add"'.$add_style.'>'.$this->EE->lang->line($add_line).'</a>';
  210. // pass along the EE version in the settings
  211. $r['settings']['ee22plus'] = version_compare(APP_VER, '2.2', '>=');
  212. if (APP_VER == '2.1.5')
  213. {
  214. $this->EE->cp->add_js_script(array(
  215. 'plugin' => array('tmpl')
  216. )
  217. );
  218. }
  219. return $r;
  220. }
  221. // --------------------------------------------------------------------
  222. /**
  223. * Validate Cell
  224. */
  225. function validate_cell($data)
  226. {
  227. // is this a required column?
  228. if ($this->settings['col_required'] == 'y' && (empty($data['filename']) || empty($data['filedir'])))
  229. {
  230. return lang('col_required');
  231. }
  232. return TRUE;
  233. }
  234. /**
  235. * Save Cell
  236. */
  237. function save_cell($data)
  238. {
  239. return $this->EE->file_field->format_data($data['filename'], $data['filedir']);
  240. }
  241. // --------------------------------------------------------------------
  242. /**
  243. * Pre-processes the field data for replace_tag().
  244. * @param string $data The file path in "{filedir_X}filename.ext" format
  245. * @return array Info about the file
  246. */
  247. function pre_process($data)
  248. {
  249. return $this->EE->file_field->parse_field($data);
  250. }
  251. /**
  252. * Replaces a File cell tag.
  253. * @param array $file_info Whatever was returned by pre_process()
  254. * @param array $params
  255. * @param string $tagdata
  256. * @return string
  257. */
  258. function replace_tag($file_info, $params = array(), $tagdata = FALSE)
  259. {
  260. // Ignore if there's no image
  261. if (! $file_info) return;
  262. if (isset($params['raw_output']) && $params['raw_output'] == 'yes')
  263. {
  264. return $file_info['raw_output'];
  265. }
  266. if (!empty($params['manipulation']))
  267. {
  268. $file_info['path'] .= '_'.$params['manipulation'].'/';
  269. }
  270. // Make sure we have file_info to work with
  271. if ($tagdata)
  272. {
  273. // Parse legacy {filesize} tags
  274. if (strpos($tagdata, 'filesize') !== FALSE)
  275. {
  276. $file_info['filesize'] = $this->_format_filesize($file_info['upload_location_id'], $file_info['file_size'], array('format' => 'no'));
  277. }
  278. // Parse conditionals
  279. $tagdata = $this->EE->functions->prep_conditionals($tagdata, $file_info);
  280. // Parse date variables
  281. $this->file_info = $file_info;
  282. $tagdata = preg_replace_callback('/'.LD.'(upload_date|modified_date)\s+format=([\'"])(.*?)\2'.RD.'/s', array($this, '_replace_date_tag'), $tagdata);
  283. unset($this->file_info);
  284. // Parse any remaining tags
  285. $tagdata = $this->EE->functions->var_swap($tagdata, $file_info);
  286. // Backspace param
  287. if (isset($params['backspace']))
  288. {
  289. $tagdata = substr($tagdata, 0, -$params['backspace']);
  290. }
  291. return $tagdata;
  292. }
  293. else if ($file_info['path'] && !empty($file_info['filename']) && $file_info['extension'] !== FALSE)
  294. {
  295. $full_path = $file_info['path'].$file_info['filename'].'.'.$file_info['extension'];
  296. if (isset($params['wrap']))
  297. {
  298. if ($params['wrap'] == 'link')
  299. {
  300. return '<a href="'.$full_path.'">'.$file_info['filename'].'</a>';
  301. }
  302. elseif ($params['wrap'] == 'image')
  303. {
  304. return '<img src="'.$full_path.'" alt="'.$file_info['filename'].'" />';
  305. }
  306. }
  307. return $full_path;
  308. }
  309. }
  310. /**
  311. * Replaces a date tag with a formatted date.
  312. * @access private
  313. * @param array $match
  314. * @return string
  315. */
  316. private function _replace_date_tag($match)
  317. {
  318. $var = $match[1];
  319. if (! isset($this->file_info[$var])) return;
  320. $dvars = $this->EE->localize->fetch_date_params($match[3]);
  321. if (!$dvars) return;
  322. $return = $match[3];
  323. foreach ($dvars as $dvar)
  324. {
  325. $formatted_dvar = $this->EE->localize->convert_timestamp($dvar, $this->file_info[$var], TRUE);
  326. $return = str_replace($dvar, $formatted_dvar, $return);
  327. }
  328. return $return;
  329. }
  330. /**
  331. * Replace File Name
  332. */
  333. function replace_filename($file_info)
  334. {
  335. return $file_info['file_name'];
  336. }
  337. /**
  338. * Replace Extension
  339. */
  340. function replace_extension($file_info)
  341. {
  342. return $file_info['extension'];
  343. }
  344. /**
  345. * Replaces a file manipulation tag, e.g. {my_file_col:thumbnail}
  346. * @param array $file_info
  347. * @param array $params
  348. * @param string $tagdata
  349. * @param string $modifier
  350. */
  351. function replace_tag_catchall($file_info, $params, $tagdata, $modifier)
  352. {
  353. $params['manipulation'] = $modifier;
  354. return $this->replace_tag($file_info, $params, $tagdata);
  355. }
  356. // --------------------------------------------------------------------
  357. /**
  358. * Get Filesize
  359. */
  360. private function _format_filesize($upload_dir, $filename, $params)
  361. {
  362. $this->EE->db->select('server_path');
  363. $query = $this->EE->db->get_where('upload_prefs', array('id' => $upload_dir));
  364. if ($query->num_rows())
  365. {
  366. $full_path = $query->row('server_path') . $filename;
  367. if (file_exists($full_path))
  368. {
  369. // get the filesize in bytes
  370. $filesize = filesize($full_path);
  371. // unit conversions
  372. if (isset($params['unit']))
  373. {
  374. switch (strtolower($params['unit']))
  375. {
  376. case 'kb': $filesize /= 1024; break;
  377. case 'mb': $filesize /= 1048576; break;
  378. case 'gb': $filesize /= 1073741824; break;
  379. }
  380. }
  381. // commas
  382. if (! isset($params['format']) || $params['format'] == 'yes')
  383. {
  384. $decimals = isset($params['decimals']) ? $params['decimals'] : 0;
  385. $dec_point = isset($params['dec_point']) ? $params['dec_point'] : '.';
  386. $thousands_sep = isset($params['thousands_sep']) ? $params['thousands_sep'] : ',';
  387. $filesize = number_format($filesize, $decimals, $dec_point, $thousands_sep);
  388. }
  389. return $filesize;
  390. }
  391. }
  392. return '';
  393. }
  394. /**
  395. * Replace File Size
  396. */
  397. function replace_filesize($data, $params = array())
  398. {
  399. if (preg_match('/^{filedir_(\d+)}(.*)$/', $data, $matches))
  400. {
  401. return $this->_get_filesize($matches[1], $matches[2], $params);
  402. }
  403. return '';
  404. }
  405. }