PageRenderTime 86ms CodeModel.GetById 28ms RepoModel.GetById 5ms app.codeStats 0ms

/system/expressionengine/models/file_upload_preferences_model.php

https://bitbucket.org/tdevonshire/hoolux
PHP | 282 lines | 150 code | 41 blank | 91 comment | 22 complexity | fa4ff001484d7c5beff7d932684c770d MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author EllisLab Dev Team
  7. * @copyright Copyright (c) 2003 - 2012, EllisLab, Inc.
  8. * @license http://ellislab.com/expressionengine/user-guide/license.html
  9. * @link http://ellislab.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Admin Model
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Core
  19. * @category Model
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. class File_upload_preferences_model extends CI_Model
  24. {
  25. /**
  26. * Get Upload Preferences
  27. *
  28. * @deprecated 2.4, use get_file_upload_preferences() instead to support
  29. * config variable overrides
  30. * @param int
  31. * @return object
  32. */
  33. function get_upload_preferences($group_id = NULL, $id = NULL)
  34. {
  35. $this->load->library('logger');
  36. $this->logger->deprecated('2.4', 'File_upload_preferences_model::get_file_upload_preferences() to support config variable overrides');
  37. // for admins, no specific filtering, just give them everything
  38. if ($group_id == 1)
  39. {
  40. // there a specific upload location we're looking for?
  41. if ($id != '')
  42. {
  43. $this->db->where('id', $id);
  44. }
  45. $this->db->from('upload_prefs');
  46. $this->db->where('site_id', $this->config->item('site_id'));
  47. $this->db->order_by('name');
  48. $upload_info = $this->db->get();
  49. }
  50. else
  51. {
  52. // non admins need to first be checked for restrictions
  53. // we'll add these into a where_not_in() check below
  54. $this->db->select('upload_id');
  55. $no_access = $this->db->get_where('upload_no_access', array('member_group'=>$group_id));
  56. if ($no_access->num_rows() > 0)
  57. {
  58. $denied = array();
  59. foreach($no_access->result() as $result)
  60. {
  61. $denied[] = $result->upload_id;
  62. }
  63. $this->db->where_not_in('id', $denied);
  64. }
  65. // there a specific upload location we're looking for?
  66. if ($id)
  67. {
  68. $this->db->where('id', $id);
  69. }
  70. $this->db->from('upload_prefs');
  71. $this->db->where('site_id', $this->config->item('site_id'));
  72. $this->db->order_by('name');
  73. $upload_info = $this->db->get();
  74. }
  75. return $upload_info;
  76. }
  77. /**
  78. * Get Upload Preferences
  79. *
  80. * @access public
  81. * @param int $group_id Member group ID specified when returning allowed upload
  82. * directories only for that member group
  83. * @param int $id Specific ID of upload destination to return
  84. * @param bool $ignore_site_id If TRUE, returns upload destinations for all sites
  85. * @return array Result array of DB object, possibly merged with custom
  86. * file upload settings
  87. */
  88. function get_file_upload_preferences($group_id = NULL, $id = NULL, $ignore_site_id = FALSE, $parameters = array())
  89. {
  90. // for admins, no specific filtering, just give them everything
  91. if ($group_id != 1)
  92. {
  93. // non admins need to first be checked for restrictions
  94. // we'll add these into a where_not_in() check below
  95. $this->db->select('upload_id');
  96. $no_access = $this->db->get_where('upload_no_access', array('member_group'=>$group_id));
  97. if ($no_access->num_rows() > 0)
  98. {
  99. $denied = array();
  100. foreach($no_access->result() as $result)
  101. {
  102. $denied[] = $result->upload_id;
  103. }
  104. $this->db->where_not_in('id', $denied);
  105. }
  106. }
  107. // Is there a specific upload location we're looking for?
  108. if ( ! empty($id))
  109. {
  110. $this->db->where('id', $id);
  111. }
  112. $this->db->from('upload_prefs');
  113. // By default, we will return upload destinations for the current site
  114. // unless we are to ignore the site ID and return all
  115. if ( ! $ignore_site_id)
  116. {
  117. $this->db->where('site_id', $this->config->item('site_id'));
  118. }
  119. // Check for order_by parameters
  120. if (isset($parameters['order_by']))
  121. {
  122. foreach ($parameters['order_by'] as $column => $direction)
  123. {
  124. $this->db->order_by($column, $direction);
  125. }
  126. }
  127. $this->db->order_by('name');
  128. // If we were passed an ID, just return the row
  129. $result_array = ( ! empty($id)) ? $this->db->get()->row_array() : $this->db->get()->result_array();
  130. // Has the user set overrides in the upload_preferences config variable?
  131. if ($this->config->item('upload_preferences') !== FALSE && count($result_array) > 0)
  132. {
  133. $upload_preferences = $this->config->item('upload_preferences');
  134. // If we are dealing with a single row
  135. if (isset($result_array['id']))
  136. {
  137. // If there is an override preference set for this row
  138. if (isset($upload_preferences[$result_array['id']]))
  139. {
  140. $result_array = array_merge($result_array, $upload_preferences[$result_array['id']]);
  141. }
  142. }
  143. else // Multiple upload preference rows returned
  144. {
  145. // Loop through our results and see if any items need to be overridden
  146. foreach ($result_array as &$upload_dir)
  147. {
  148. if (isset($upload_preferences[$upload_dir['id']]))
  149. {
  150. // Merge the database result with the custom result, custom keys
  151. // overwriting database keys
  152. $upload_dir = array_merge($upload_dir, $upload_preferences[$upload_dir['id']]);
  153. }
  154. }
  155. }
  156. }
  157. // Use upload destination ID as key for row for easy traversing
  158. $return_array = ( ! empty($id)) ? $result_array : array();
  159. if (empty($return_array))
  160. {
  161. foreach ($result_array as $row)
  162. {
  163. $return_array[$row['id']] = $row;
  164. }
  165. }
  166. return $return_array;
  167. }
  168. // --------------------------------------------------------------------
  169. /**
  170. * Builds an array suitable for dropdown lists
  171. *
  172. * @param integer $group_id The group id to get file preferences for
  173. * @param integer $id Specific upload directory ID if you just want settings for that
  174. * @param array $prefs_array Optional existing array to add the preferences to
  175. * @return array Associative array with ids as the keys and names as the values
  176. */
  177. public function get_dropdown_array($group_id = NULL, $id = NULL, $prefs_array = array())
  178. {
  179. $prefs = $this->get_file_upload_preferences($group_id, $id);
  180. if (isset($prefs['id']))
  181. {
  182. $prefs = array($prefs);
  183. }
  184. foreach ($prefs as $pref)
  185. {
  186. $prefs_array[$pref['id']] = $pref['name'];
  187. }
  188. return $prefs_array;
  189. }
  190. // --------------------------------------------------------------------
  191. /**
  192. * Delete Upload Preferences
  193. *
  194. * @access public
  195. * @param int
  196. * @return string
  197. */
  198. function delete_upload_preferences($id = '')
  199. {
  200. // There are no permission checks- I don't really think there should be
  201. $this->db->where('upload_id', $id);
  202. $this->db->delete('upload_no_access');
  203. // get the name we're going to delete so that we can return it when we're done
  204. $this->db->select('name');
  205. $this->db->where('id', $id);
  206. $deleting = $this->db->get('upload_prefs');
  207. // Delete the files associated with this preference
  208. // Note we aren't doing anything to the files/folders yet
  209. $this->db->where('upload_location_id', $id);
  210. $this->db->delete('files');
  211. // ok, now remove the pref
  212. $this->db->where('id', $id);
  213. $this->db->delete('upload_prefs');
  214. return $deleting->row('name');
  215. }
  216. // --------------------------------------------------------------------
  217. /**
  218. * Get the category groups for one or more upload directories
  219. *
  220. * @param array $id Either an array of upload directory IDs or just one
  221. * @return array Array of category group IDs
  222. */
  223. public function get_category_groups($id = array())
  224. {
  225. if ( ! is_array($id))
  226. {
  227. $id = array($id);
  228. }
  229. $cat_groups = array();
  230. $this->db->select('cat_group');
  231. $this->db->where_in('id', $id);
  232. $upload_pref_query = $this->db->get('upload_prefs');
  233. foreach ($upload_pref_query->result() as $upload_pref)
  234. {
  235. $cat_groups = array_merge($cat_groups, explode('|', $upload_pref->cat_group));
  236. }
  237. return array_unique($cat_groups);
  238. }
  239. }
  240. /* End of file file_model.php */
  241. /* Location: ./system/expressionengine/models/file_model.php */