PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/system/cms/modules/navigation/models/navigation_m.php

https://github.com/marcoscoelho/pyrocms
PHP | 492 lines | 297 code | 59 blank | 136 comment | 31 complexity | cfe65a29db14c43aa8397784856b529a MD5 | raw file
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * Navigation model for the navigation module.
  4. *
  5. * @package PyroCMS
  6. * @subpackage Navigation Module
  7. * @category Modules
  8. * @author Phil Sturgeon - PyroCMS Development Team
  9. *
  10. */
  11. class Navigation_m extends MY_Model
  12. {
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $this->_table = 'navigation_links';
  17. }
  18. /**
  19. * Get a navigation link
  20. *
  21. * @access public
  22. * @param int $id The ID of the item
  23. * @return mixed
  24. */
  25. public function get_link($id = 0)
  26. {
  27. $query = $this->db->get_where('navigation_links', array('id'=>$id));
  28. if ($query->num_rows() == 0)
  29. {
  30. return FALSE;
  31. }
  32. else
  33. {
  34. return $query->row();
  35. }
  36. }
  37. /**
  38. * Get a navigation link with all the trimmings
  39. *
  40. * @access public
  41. * @param int $id The ID of the item
  42. * @return mixed
  43. */
  44. public function get_url($id = 0)
  45. {
  46. $query = $this->db->get_where('navigation_links', array('id'=>$id));
  47. if ($query->num_rows() == 0)
  48. {
  49. return FALSE;
  50. }
  51. else
  52. {
  53. return $this->make_url($query->result());
  54. }
  55. }
  56. /**
  57. * Create a new Navigation Link
  58. *
  59. * @access public
  60. * @param array $input The data to insert
  61. * @return int
  62. */
  63. public function insert_link($input = array())
  64. {
  65. $input = $this->_format_array($input);
  66. $row = $this->db->order_by('position', 'desc')
  67. ->limit(1)
  68. ->get_where('navigation_links', array('navigation_group_id' => (int) $input['navigation_group_id']))
  69. ->row();
  70. $position = isset($row->position) ? $row->position + 1 : 1;
  71. $this->db->insert('navigation_links', array(
  72. 'title' => $input['title'],
  73. 'link_type' => $input['link_type'],
  74. 'url' => isset($input['url']) ? $input['url'] : '',
  75. 'uri' => isset($input['uri']) ? $input['uri'] : '',
  76. 'module_name' => isset($input['module_name']) ? $input['module_name'] : '',
  77. 'page_id' => (int) $input['page_id'],
  78. 'position' => $position,
  79. 'target' => isset($input['target']) ? $input['target'] : '',
  80. 'class' => isset($input['class']) ? $input['class'] : '',
  81. 'navigation_group_id' => (int) $input['navigation_group_id'],
  82. 'restricted_to' => empty($input['restricted_to']) ? 0 : $input['restricted_to']
  83. ));
  84. return $this->db->insert_id();
  85. }
  86. /**
  87. * Update a Navigation Link
  88. *
  89. * @access public
  90. * @param int $id The ID of the link to update
  91. * @param array $input The data to update
  92. * @return bool
  93. */
  94. public function update_link($id = 0, $input = array())
  95. {
  96. $input = $this->_format_array($input);
  97. $insert = array(
  98. 'title' => $input['title'],
  99. 'link_type' => $input['link_type'],
  100. 'url' => $input['url'] == 'http://' ? '' : $input['url'], // Do not insert if only http://
  101. 'uri' => $input['uri'],
  102. 'module_name' => $input['module_name'],
  103. 'page_id' => (int) $input['page_id'],
  104. 'target' => $input['target'],
  105. 'class' => $input['class'],
  106. 'navigation_group_id' => (int) $input['navigation_group_id'],
  107. 'restricted_to' => empty($input['restricted_to']) ? 0 : $input['restricted_to']
  108. );
  109. // if it was changed to a different group we need to reset the parent > child
  110. if ($input['current_group_id'] != $input['navigation_group_id'])
  111. {
  112. // modify the link update array to reset this link in case it's a child
  113. $insert['parent'] = 0;
  114. // reset all of this link's children
  115. $this->db->where('parent', $id)->update($this->_table, array('parent' => 0));
  116. }
  117. return $this->db->update('navigation_links', $insert, array('id' => $id));
  118. }
  119. /**
  120. * Update links by group
  121. *
  122. * @author Jerel Unruh - PyroCMS Dev Team
  123. * @access public
  124. * @param int $group
  125. * @param array $data
  126. * @return boolean
  127. */
  128. public function update_by_group($group = 0, $data = array())
  129. {
  130. return $this->db->where_in('navigation_group_id', $group)
  131. ->set($data)
  132. ->update($this->_table);
  133. }
  134. /**
  135. * Build a multi-array of parent > children.
  136. *
  137. * @author Jerel Unruh - PyroCMS Dev Team
  138. * @access public
  139. * @param string $group Either the group abbrev or the group id
  140. * @return array An array representing the link tree
  141. */
  142. public function get_link_tree($group, $params = array())
  143. {
  144. // the plugin passes the abbreviation
  145. if ( ! is_numeric($group))
  146. {
  147. $row = $this->get_group_by('abbrev', $group);
  148. $group = $row ? $row->id : null;
  149. }
  150. if ( ! empty($params['order']))
  151. {
  152. $this->db->order_by($params['order']);
  153. }
  154. else
  155. {
  156. $this->db->order_by('position');
  157. }
  158. if (isset($params['front_end']) AND $params['front_end'])
  159. {
  160. $front_end = TRUE;
  161. }
  162. else
  163. {
  164. $front_end = FALSE;
  165. }
  166. if (isset($params['user_group']))
  167. {
  168. $user_group = $params['user_group'];
  169. }
  170. else
  171. {
  172. $user_group = FALSE;
  173. }
  174. $all_links = $this->db->where('navigation_group_id', $group)
  175. ->get($this->_table)
  176. ->result_array();
  177. $this->load->helper('url');
  178. $links = array();
  179. // we must reindex the array first and build urls
  180. $all_links = $this->make_url_array($all_links, $user_group, $front_end);
  181. foreach ($all_links AS $row)
  182. {
  183. $links[$row['id']] = $row;
  184. }
  185. unset($all_links);
  186. $link_array = array();
  187. // build a multidimensional array of parent > children
  188. foreach ($links AS $row)
  189. {
  190. if (array_key_exists($row['parent'], $links))
  191. {
  192. // add this link to the children array of the parent link
  193. $links[$row['parent']]['children'][] =& $links[$row['id']];
  194. }
  195. if ( ! isset($links[$row['id']]['children']))
  196. {
  197. $links[$row['id']]['children'] = array();
  198. }
  199. // this is a root link
  200. if ($row['parent'] == 0)
  201. {
  202. $link_array[] =& $links[$row['id']];
  203. }
  204. }
  205. return $link_array;
  206. }
  207. /**
  208. * Set the parent > child relations and child order
  209. *
  210. * @author Jerel Unruh - PyroCMS Dev Team
  211. * @param array $link
  212. * @return void
  213. */
  214. public function _set_children($link)
  215. {
  216. if (isset($link['children']))
  217. {
  218. foreach ($link['children'] as $i => $child)
  219. {
  220. $this->db->where('id', str_replace('link_', '', $child['id']));
  221. $this->db->update($this->_table, array('parent' => str_replace('link_', '', $link['id']), 'position' => $i));
  222. //repeat as long as there are children
  223. if (isset($child['children']))
  224. {
  225. $this->_set_children($child);
  226. }
  227. }
  228. }
  229. }
  230. /**
  231. * Format an array
  232. *
  233. * @access public
  234. * @param array $input The data to format
  235. * @return array
  236. */
  237. public function _format_array($input)
  238. {
  239. // If the url is not empty and not just the default http://
  240. if(!empty($input['url']) && $input['url'] != 'http://')
  241. {
  242. $input['uri'] = '';
  243. $input['module_name'] = '';
  244. $input['page_id'] = 0;
  245. }
  246. // If the uri is empty reset the others
  247. if(!empty($input['uri']))
  248. {
  249. $input['url'] = '';
  250. $input['module_name'] = '';
  251. $input['page_id'] = 0;
  252. }
  253. // You get the idea...
  254. if(!empty($input['module_name']))
  255. {
  256. $input['url'] = '';
  257. $input['uri'] = '';
  258. $input['page_id'] = 0;
  259. }
  260. if(!empty($input['page_id']))
  261. {
  262. $input['url'] = '';
  263. $input['uri'] = '';
  264. $input['module_name'] = '';
  265. }
  266. return $input;
  267. }
  268. /**
  269. * Delete a Navigation Link
  270. *
  271. * @access public
  272. * @param int $id The ID of the link to delete
  273. * @return array
  274. */
  275. public function delete_link($id = 0)
  276. {
  277. $params = is_array($id) ? $id : array('id' => $id);
  278. return $this->db->delete('navigation_links', $params);
  279. }
  280. /**
  281. * Make URL
  282. *
  283. * @access public
  284. * @param array $row Navigation record
  285. * @return mixed Valid url
  286. */
  287. public function make_url($result)
  288. {
  289. foreach($result as $key => &$row)
  290. {
  291. // If its any other type than a URL, it needs some help becoming one
  292. switch($row->link_type)
  293. {
  294. case 'uri':
  295. $row->url = site_url($row->uri);
  296. break;
  297. case 'module':
  298. $row->url = site_url($row->module_name);
  299. break;
  300. case 'page':
  301. if ($page = $this->page_m->get_by(array_filter(array(
  302. 'id' => $row->page_id,
  303. 'status' => (is_subclass_of(ci(), 'Public_Controller') ? 'live' : NULL)
  304. ))))
  305. {
  306. $row->url = site_url($page->uri);
  307. $row->is_home = $page->is_home;
  308. }
  309. else
  310. {
  311. unset($result[$key]);
  312. }
  313. break;
  314. }
  315. }
  316. return $result;
  317. }
  318. /**
  319. * Make a URL array
  320. *
  321. * @access public
  322. * @param array $row Array of links
  323. * @return mixed Array of links with valid urls
  324. */
  325. public function make_url_array($links, $user_group = FALSE, $front_end = FALSE)
  326. {
  327. // We have to fetch it ourselves instead of just using $current_user because this
  328. // will all be cached per user group
  329. $group = $this->db->select('id')->where('name', $user_group)->get('groups')->row();
  330. foreach($links as $key => &$row)
  331. {
  332. // Looks like it's restricted. Let's find out who
  333. if ($row['restricted_to'] AND $front_end)
  334. {
  335. $row['restricted_to'] = (array) explode(',', $row['restricted_to']);
  336. if ( ! $user_group OR
  337. ($user_group != 'admin' AND
  338. ! in_array($group->id, $row['restricted_to']))
  339. )
  340. {
  341. unset($links[$key]);
  342. }
  343. }
  344. // If its any other type than a URL, it needs some help becoming one
  345. switch($row['link_type'])
  346. {
  347. case 'uri':
  348. $row['url'] = site_url($row['uri']);
  349. break;
  350. case 'module':
  351. $row['url'] = site_url($row['module_name']);
  352. break;
  353. case 'page':
  354. if ($page = $this->page_m->get_by(array_filter(array(
  355. 'id' => $row['page_id'],
  356. 'status' => ($front_end ? 'live' : NULL)
  357. ))))
  358. {
  359. $row['url'] = site_url($page->uri);
  360. $row['is_home'] = $page->is_home;
  361. // But wait. If we're on the front-end and they don't have access to the page then we'll remove it anyway.
  362. if ($front_end AND $page->restricted_to)
  363. {
  364. $page->restricted_to = (array) explode(',', $page->restricted_to);
  365. if ( ! $user_group OR
  366. ($user_group != 'admin' AND
  367. ! in_array($group->id, $page->restricted_to))
  368. )
  369. {
  370. unset($links[$key]);
  371. }
  372. }
  373. }
  374. else
  375. {
  376. unset($links[$key]);
  377. }
  378. break;
  379. }
  380. }
  381. return $links;
  382. }
  383. /**
  384. * Get group by..
  385. *
  386. * @access public
  387. * @param string $what What to get
  388. * @param string $value The value
  389. * @return mixed
  390. */
  391. public function get_group_by($what, $value)
  392. {
  393. return $this->db->where($what, $value)->get('navigation_groups')->row();
  394. }
  395. /**
  396. * Return an array of Navigation Groups
  397. *
  398. * @access public
  399. * @return void
  400. */
  401. public function get_groups()
  402. {
  403. return $this->db->get('navigation_groups')->result();
  404. }
  405. /**
  406. *
  407. * Insert a new group into the DB
  408. *
  409. * @param array $input The data to insert
  410. * @return int
  411. */
  412. public function insert_group($input = array())
  413. {
  414. $this->db->insert('navigation_groups', array(
  415. 'title' => $input['title'],
  416. 'abbrev' => $input['abbrev']
  417. ));
  418. return $this->db->insert_id();
  419. }
  420. /**
  421. * Delete a Navigation Group
  422. *
  423. * @access public
  424. * @param int $id The ID of the group to delete
  425. * @return array
  426. */
  427. public function delete_group($id = 0)
  428. {
  429. $params = is_array($id) ? $id : array('id'=>$id);
  430. $this->db->delete('navigation_groups', $params);
  431. return $this->db->affected_rows();
  432. }
  433. }