PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/system/cms/modules/navigation/src/Pyro/Module/Navigation/Model/Link.php

https://github.com/asalem/pyrocms
PHP | 379 lines | 191 code | 56 blank | 132 comment | 26 complexity | 869b33f32d1cb715cef25e02a0786bec MD5 | raw file
Possible License(s): CC-BY-3.0, BSD-3-Clause, CC0-1.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, MIT
  1. <?php namespace Pyro\Module\Navigation\Model;
  2. use Pyro\Model\Eloquent;
  3. use Pyro\Module\Navigation\Model\Group;
  4. use Pyro\Module\Pages\Model\Page;
  5. use Pyro\Module\Users;
  6. /**
  7. * Navigation model for the navigation module.
  8. *
  9. * @author PyroCMS Dev Team
  10. * @package PyroCMS\Core\Modules\Navigation\Models
  11. */
  12. class Link extends Eloquent
  13. {
  14. /**
  15. * Define the table name
  16. *
  17. * @var string
  18. */
  19. protected $table = 'navigation_links';
  20. /**
  21. * Cache minutes
  22. * @var int
  23. */
  24. public $cacheMinutes = 30;
  25. /**
  26. * The attributes that aren't mass assignable
  27. *
  28. * @var array
  29. */
  30. protected $guarded = array();
  31. /**
  32. * Disable updated_at and created_at on table
  33. *
  34. * @var boolean
  35. */
  36. public $timestamps = false;
  37. /**
  38. * Relationship: Children
  39. *
  40. * @return Illuminate\Database\Eloquent\Relations\HasMany
  41. */
  42. public function children()
  43. {
  44. return $this->hasMany('Pyro\Module\Navigation\Model\Link', 'parent')->orderBy('position', 'ASC');
  45. }
  46. /**
  47. * Relationship: Page
  48. *
  49. * @return Illuminate\Database\Eloquent\Relations\BelongsTo
  50. */
  51. public function page()
  52. {
  53. return $this->belongsTo('Pyro\Module\Pages\Model\Page');
  54. }
  55. /**
  56. * Get a navigation link with all the trimmings
  57. *
  58. *
  59. * @param int $id The ID of the item
  60. * @return mixed
  61. */
  62. public static function getUrl($id = 0)
  63. {
  64. $link = parent::find($id);
  65. if (! $link) {
  66. return;
  67. }
  68. return self::makeUrl($link);
  69. }
  70. /**
  71. * Find link by Group ID and order by position
  72. *
  73. * @param int $group_id The group_id of the link
  74. * @param string $direction The direction of the sort
  75. *
  76. * @return void
  77. */
  78. public static function findByGroupIdAndOrderByPosition($group_id, $direction = 'asc')
  79. {
  80. return static::where('navigation_group_id', '=', $group_id)->orderBy('position',$direction)->first();
  81. }
  82. /**
  83. * Get links by Group ID and order by position
  84. *
  85. * @param int $group_id The group_id of the links
  86. * @param string $direction The direction of the sort
  87. *
  88. * @return void
  89. */
  90. public static function findManyByGroupIdAndOrderByPosition($group_id, $direction = 'asc')
  91. {
  92. return static::where('navigation_group_id', '=', $group_id)->orderBy('position',$direction)->get();
  93. }
  94. /**
  95. * Reset the parent > child
  96. *
  97. * @param int $parent_id The parent_id of the link
  98. *
  99. * @return void
  100. */
  101. public static function resetChildByParentId($parent_id)
  102. {
  103. return static::where('parent', '=', $parent_id)->update(array('parent'=> 0));
  104. }
  105. /**
  106. * Build a multi-array of parent > children.
  107. *
  108. * @param string $group Either the group abbrev or the group id
  109. * @return array An array representing the link tree
  110. */
  111. public static function getTreeByGroup($group, $params = array())
  112. {
  113. $front_end = (isset($params['front_end']) and $params['front_end']);
  114. $user_groups = (isset($params['user_groups'])) ? $params['user_groups'] : false;
  115. // By wuuut?
  116. if (is_numeric($group)) {
  117. $group = Group::with('links.children', 'links.page')->where('id', '=', $group)->first();
  118. } else {
  119. $group = Group::with('links.children', 'links.page')->where('abbrev', '=', $group)->first();
  120. }
  121. $all_links = $group->links;
  122. $all_links = self::makeUrlArray($all_links, $user_groups, $front_end);
  123. return $all_links;
  124. }
  125. /**
  126. * Set order of links
  127. *
  128. * @param array $link
  129. * @return void
  130. */
  131. public static function setOrder($order = array(), $group = null)
  132. {
  133. //reset all parent > child relations
  134. $group = Group::find($group);
  135. foreach ($group->links as $link) {
  136. $link->parent = 0;
  137. $link->save();
  138. }
  139. foreach ($order as $i => $link) {
  140. //set the order of the root links
  141. ci()->pdb
  142. ->table('navigation_links')
  143. ->where('id', str_replace('link_', '', $link['id']))
  144. ->update(array('position' => $i));
  145. //iterate through children and set their order and parent
  146. self::setChildren($link);
  147. }
  148. }
  149. /**
  150. * Set the parent > child relations and child order
  151. *
  152. * @param array $link
  153. * @return void
  154. */
  155. public static function setChildren($link)
  156. {
  157. if (isset($link['children'])) {
  158. foreach ($link['children'] as $i => $child) {
  159. ci()->pdb
  160. ->table('navigation_links')
  161. ->where('id', str_replace('link_', '', $child['id']))
  162. ->update(
  163. array(
  164. 'parent' => str_replace('link_', '', $link['id']),
  165. 'position' => $i
  166. )
  167. );
  168. //repeat as long as there are children
  169. if (isset($child['children'])) {
  170. self::setChildren($child);
  171. }
  172. }
  173. }
  174. }
  175. /**
  176. * Get a link's children IDs
  177. *
  178. * @param array $link
  179. * @return void
  180. */
  181. public static function getChildrenIds($parent_id = null)
  182. {
  183. $children = ci()->pdb->table('navigation_links')
  184. ->where('parent', '=', $parent_id)->get();
  185. $ids = array();
  186. foreach ($children as $child) {
  187. $ids[] = $child->id;
  188. $ids = array_merge($ids, self::getChildrenIds($child->id));
  189. }
  190. return $ids;
  191. }
  192. /**
  193. * Delete a link and its children
  194. *
  195. * @param int $id The ID of the link to delete
  196. * @return array
  197. */
  198. public static function deleteLinkChildren($id = 0)
  199. {
  200. $ids = array($id);
  201. $ids = array_merge($ids, self::getChildrenIds($id));
  202. return self::whereIn('id', $ids)->delete();
  203. }
  204. /**
  205. * Format an array
  206. *
  207. * @param array $input The data to format
  208. * @return array
  209. */
  210. public static function formatArray($input)
  211. {
  212. // If the url is not empty and not just the default http://
  213. if ( ! empty($input['url']) && $input['url'] != 'http://') {
  214. $input['uri'] = '';
  215. $input['module_name'] = '';
  216. $input['page_id'] = 0;
  217. }
  218. // If the uri is empty reset the others
  219. if ( ! empty($input['uri'])) {
  220. $input['url'] = '';
  221. $input['module_name'] = '';
  222. $input['page_id'] = 0;
  223. }
  224. // You get the idea...
  225. if ( ! empty($input['module_name'])) {
  226. $input['url'] = '';
  227. $input['uri'] = '';
  228. $input['page_id'] = 0;
  229. }
  230. if ( ! empty($input['page_id'])) {
  231. $input['url'] = '';
  232. $input['uri'] = '';
  233. $input['module_name'] = '';
  234. }
  235. return $input;
  236. }
  237. /**
  238. * Make URL
  239. *
  240. * @param array $row Navigation record
  241. * @return mixed Valid url
  242. */
  243. public static function makeUrl($row)
  244. {
  245. // If its any other type than a URL, it needs some help becoming one
  246. switch ($row->link_type) {
  247. case 'uri':
  248. $row->url = site_url($row->uri);
  249. break;
  250. case 'module':
  251. $row->url = site_url($row->module_name);
  252. break;
  253. case 'page':
  254. if ($status = (is_subclass_of(ci(), 'Public_Controller') ? 'live' : null)) {
  255. $page = Page::findByIdAndStatus($row->page_id, $status);
  256. } else {
  257. $page = Page::find($row->page_id);
  258. }
  259. $row->url = site_url($page->uri);
  260. $row->is_home = $page->is_home;
  261. break;
  262. }
  263. return $row;
  264. }
  265. /**
  266. * Make a URL array
  267. *
  268. * @param array $row Array of links
  269. * @return mixed Array of links with valid urls
  270. */
  271. public static function makeUrlArray($links, $user_groups = false, $front_end = false)
  272. {
  273. foreach ($links as $key => &$row) {
  274. // Looks like it's restricted. Let's find out who
  275. if ($row->restricted_to and $front_end) {
  276. // Explode!
  277. $row->restricted_to = (array) explode(',', $row->restricted_to);
  278. // Get the similarities
  279. $matches = $user_groups != false ? array_intersect($row->restricted_to, $user_groups) : $row->restricted_to;
  280. // Test it..
  281. if (empty($user_groups) or (in_array(1, $user_groups) == false and empty($matches))) {
  282. unset($links[$key]);
  283. }
  284. }
  285. // If its any other type than a URL, it needs some help becoming one
  286. switch ($row->link_type) {
  287. case 'uri':
  288. $row->url = site_url($row->uri);
  289. break;
  290. case 'module':
  291. $row->url = site_url($row->module_name);
  292. break;
  293. case 'page':
  294. if ($row->page and $row->page->status == 'live') {
  295. $page = $row->page;
  296. } else {
  297. $page = false;
  298. }
  299. // Fuck this then
  300. if (! $page) {
  301. unset($links[$key]);
  302. break;
  303. }
  304. $row->url = site_url($page->uri);
  305. $row->is_home = $page->is_home;
  306. // But wait. If we're on the front-end and they don't have access to the page then we'll remove it anyway.
  307. if ($front_end and $page->restricted_to) {
  308. // EXPLODE again!
  309. $page->restricted_to = (array) explode(',', $page->restricted_to);
  310. // Get the similarities
  311. $matches = array_intersect($row->restricted_to, $user_groups);
  312. if ( (empty($user_groups) and $page->restricted_to) or (in_array(1, $user_groups) == false and empty($matches))) {
  313. unset($links[$key]);
  314. }
  315. }
  316. break;
  317. }
  318. // Process all the little children
  319. $row->children = self::makeUrlArray($row->children, $user_groups, $front_end);
  320. }
  321. return $links;
  322. }
  323. }