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

/system/expressionengine/libraries/api/Api_channel_categories.php

https://bitbucket.org/mbaily/tremain
PHP | 404 lines | 225 code | 74 blank | 105 comment | 36 complexity | e52a5f2de0f861c9afeb3f7c5faf3fda 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 - 2013, 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 Channel Categories API Class
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Core
  19. * @category Core
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. class Api_channel_categories extends Api {
  24. /**
  25. * Just a note, these really should be protected/private and set by the API itself
  26. * However, we're abusing them in other places, so they are set to public for now.
  27. * Third-party devs, don't rely on these being public, m'kay?
  28. */
  29. public $assign_cat_parent = TRUE;
  30. public $categories = array();
  31. public $cat_parents = array();
  32. public $cat_array = array();
  33. /**
  34. * Constructor
  35. */
  36. public function __construct()
  37. {
  38. parent::__construct();
  39. ee()->load->model('channel_model');
  40. $this->assign_cat_parent = (ee()->config->item('auto_assign_cat_parents') == 'n') ? FALSE : TRUE;
  41. }
  42. // --------------------------------------------------------------------
  43. /**
  44. * Category tree
  45. *
  46. * This function (and the next) create a higherarchy tree
  47. * of categories. There are two versions of the tree. The
  48. * "text" version is a list of links allowing the categories
  49. * to be edited. The "form" version is displayed in a
  50. * multi-select form on the new entry page.
  51. *
  52. * @param mixed int or array of category group ids
  53. * @param
  54. * @param string c for custom, a alphabetical
  55. *
  56. * @return mixed FALSE if no results or cat array of results.
  57. */
  58. public function category_tree($group_id, $selected = '', $order = 'c')
  59. {
  60. // reset $this->categories
  61. $this->initialize(array(
  62. 'categories' => array(),
  63. 'cat_parents' => array(),
  64. 'cat_array' => array(),
  65. )
  66. );
  67. // Fetch the category group ID number
  68. // Drop it into an array for where_in()
  69. $group_ids = $group_id;
  70. if ( ! is_array($group_id))
  71. {
  72. $group_ids = explode('|', $group_id);
  73. }
  74. $catarray = array();
  75. if (is_array($selected))
  76. {
  77. foreach ($selected as $key => $val)
  78. {
  79. $catarray[$val] = $val;
  80. }
  81. }
  82. else
  83. {
  84. $catarray[$selected] = $selected;
  85. }
  86. $order = ($order == 'a') ? "cat_name" : "cat_order";
  87. $query = ee()->db->select('cat_name, cat_id, parent_id, cat_image, cat_description, g.group_id, group_name, cat_url_title')
  88. ->from('category_groups g, categories c')
  89. ->where('g.group_id', 'c.group_id', FALSE)
  90. ->where_in('g.group_id', $group_ids)
  91. ->order_by('group_id, parent_id, '.$order, 'asc')
  92. ->get();
  93. if ($query->num_rows() === 0)
  94. {
  95. return FALSE;
  96. }
  97. // Assign the query result to a multi-dimensional array
  98. foreach($query->result_array() as $row)
  99. {
  100. $cat_array[$row['cat_id']] = array(
  101. $row['parent_id'],
  102. $row['cat_name'],
  103. $row['group_id'],
  104. $row['group_name'],
  105. $row['cat_image'],
  106. $row['cat_description'],
  107. $row['cat_url_title']
  108. );
  109. }
  110. // Build our output...
  111. foreach($cat_array as $key => $val)
  112. {
  113. if (0 == $val[0])
  114. {
  115. $sel = (isset($catarray[$key])) ? TRUE : FALSE;
  116. $depth = 1;
  117. $this->categories[$key] = array($key, $val[1], (int) $val[2], $val[3], $sel, $depth, FALSE, $val[4], $val[5], $val[6]);
  118. //$this->categories[$key] = array('cat_id' => $key, 'cat_name' => $val['1'], 'group_id' => $val['2'], 'group_name' => $val['3'], 'selected' => $sel, 'depth' => $depth);
  119. $this->_category_subtree($key, $cat_array, $depth, $selected);
  120. }
  121. }
  122. return $this->categories;
  123. }
  124. // --------------------------------------------------------------------
  125. /**
  126. * Category sub-tree
  127. *
  128. * This function works with the preceeding one to show a
  129. * hierarchical display of categories
  130. *
  131. * @param mixed
  132. * @param array
  133. * @param int
  134. * @param
  135. */
  136. protected function _category_subtree($cat_id, $cat_array, $depth, $selected = array())
  137. {
  138. // Just as in the function above, we'll figure out which items are selected.
  139. $catarray = array();
  140. if (is_array($selected))
  141. {
  142. foreach ($selected as $key => $val)
  143. {
  144. $catarray[$val] = $val;
  145. }
  146. }
  147. $depth++;
  148. foreach ($cat_array as $key => $val)
  149. {
  150. if ($cat_id == $val['0'])
  151. {
  152. $sel = (isset($catarray[$key])) ? TRUE : FALSE;
  153. $this->categories[$key] = array($key, $val['1'], (int) $val['2'], $val['3'], $sel, $depth, (int) $val[0], $val[4], $val[5]);
  154. //$this->categories[$key] = array('cat_id' => $key, 'cat_name' => $val['1'], 'group_id' => $val['2'], 'group_name' => $val['3'], 'selected' => $sel, 'depth' => $depth);
  155. $this->_category_subtree($key, $cat_array, $depth, $selected);
  156. }
  157. }
  158. }
  159. // --------------------------------------------------------------------
  160. /**
  161. * Category Form Tree
  162. *
  163. * @param string
  164. * @param mixed
  165. * @param boolean
  166. */
  167. public function category_form_tree($nested = 'y', $categories = FALSE, $sites = FALSE)
  168. {
  169. $order = ($nested == 'y') ? 'group_id, parent_id, cat_name' : 'cat_name';
  170. ee()->db->select('categories.group_id, categories.parent_id, categories.cat_id, categories.cat_name');
  171. ee()->db->from('categories');
  172. if ($sites == FALSE)
  173. {
  174. ee()->db->where('site_id', ee()->config->item('site_id'));
  175. }
  176. elseif ($sites != 'all')
  177. {
  178. if (is_array($sites))
  179. {
  180. $sites = implode('|', $sites);
  181. }
  182. ee()->functions->ar_andor_string($sites, 'site_id');
  183. }
  184. if ($categories !== FALSE)
  185. {
  186. if (is_array($categories))
  187. {
  188. $categories = implode('|', $categories);
  189. }
  190. ee()->functions->ar_andor_string($categories, 'cat_id', 'exp_categories');
  191. }
  192. ee()->db->order_by($order);
  193. $query = ee()->db->get();
  194. // Load the text helper
  195. ee()->load->helper('text');
  196. if ($query->num_rows() > 0)
  197. {
  198. $categories = array();
  199. foreach ($query->result_array() as $row)
  200. {
  201. $categories[] = array($row['group_id'], $row['cat_id'], entities_to_ascii($row['cat_name']), $row['parent_id']);
  202. }
  203. if ($nested == 'y')
  204. {
  205. foreach($categories as $key => $val)
  206. {
  207. if (0 == $val['3'])
  208. {
  209. $this->cat_array[] = array($val['0'], $val['1'], $val['2']);
  210. $this->category_form_subtree($val['1'], $categories, $depth=1);
  211. }
  212. }
  213. }
  214. else
  215. {
  216. $this->cat_array = $categories;
  217. }
  218. }
  219. return $this->cat_array;
  220. }
  221. // --------------------------------------------------------------------
  222. /**
  223. * Category Edit Sub-tree
  224. *
  225. * @param string category id
  226. * @param array array of categories
  227. * @param int
  228. * @return void
  229. */
  230. public function category_form_subtree($cat_id, $categories, $depth)
  231. {
  232. $spcr = '!-!';
  233. $indent = $spcr.$spcr.$spcr.$spcr;
  234. if ($depth == 1)
  235. {
  236. $depth = 4;
  237. }
  238. else
  239. {
  240. $indent = str_repeat($spcr, $depth).$indent;
  241. $depth = $depth + 4;
  242. }
  243. $sel = '';
  244. foreach ($categories as $key => $val)
  245. {
  246. if ($cat_id == $val['3'])
  247. {
  248. $pre = ($depth > 2) ? $spcr : '';
  249. $this->cat_array[] = array($val['0'], $val['1'], $pre.$indent.$spcr.$val['2']);
  250. $this->category_form_subtree($val['1'], $categories, $depth);
  251. }
  252. }
  253. }
  254. // --------------------------------------------------------------------
  255. /**
  256. * Fetch Parent Category ID
  257. *
  258. * @param array category array
  259. */
  260. public function fetch_category_parents($cat_array)
  261. {
  262. if (count($cat_array) == 0)
  263. {
  264. return;
  265. }
  266. $sql = "SELECT parent_id FROM exp_categories WHERE site_id = '".ee()->db->escape_str(ee()->config->item('site_id'))."' AND (";
  267. foreach($cat_array as $val)
  268. {
  269. $sql .= " cat_id = '$val' OR ";
  270. }
  271. $sql = substr($sql, 0, -3).")";
  272. $query = ee()->db->query($sql);
  273. if ($query->num_rows() == 0)
  274. {
  275. return;
  276. }
  277. $temp = array();
  278. foreach ($query->result_array() as $row)
  279. {
  280. if ($row['parent_id'] != 0)
  281. {
  282. $this->cat_parents[] = $row['parent_id'];
  283. $temp[] = $row['parent_id'];
  284. }
  285. }
  286. $this->fetch_category_parents($temp);
  287. }
  288. // --------------------------------------------------------------------
  289. /**
  290. * Fetch Allowed Category Groups
  291. *
  292. * @param string String of cat groups. either one, or a piped list
  293. * @return mixed
  294. */
  295. public function fetch_allowed_category_groups($cat_group)
  296. {
  297. if (ee()->cp->allowed_group('can_admin_channels') OR ee()->cp->allowed_group('can_edit_categories'))
  298. {
  299. if (! is_array($cat_group))
  300. {
  301. $cat_group = explode('|', $cat_group);
  302. }
  303. ee()->load->model('category_model');
  304. $catg_query = ee()->category_model->get_category_group_name($cat_group);
  305. $link_info = array();
  306. foreach($catg_query->result_array() as $catg_row)
  307. {
  308. $link_info[] = array('group_id' => $catg_row['group_id'], 'group_name' => $catg_row['group_name']);
  309. }
  310. return $link_info;
  311. }
  312. else
  313. {
  314. return FALSE;
  315. }
  316. }
  317. // --------------------------------------------------------------------
  318. /**
  319. * Get Categories
  320. *
  321. * @return array
  322. */
  323. public function get_categories()
  324. {
  325. return $this->categories;
  326. }
  327. }
  328. // END CLASS
  329. /* End of file Api_channel_categories.php */
  330. /* Location: ./system/expressionengine/libraries/api/Api_channel_categories.php */