PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/engine/classes/ElggMenuItem.php

https://github.com/fragilbert/Elgg
PHP | 590 lines | 247 code | 73 blank | 270 comment | 37 complexity | 5dd7942ff932dbb572d4d12b5ea604c2 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * Elgg Menu Item
  4. *
  5. * To create a menu item that is not a link, pass false for $href.
  6. *
  7. * @package Elgg.Core
  8. * @subpackage Navigation
  9. * @since 1.8.0
  10. */
  11. class ElggMenuItem {
  12. /**
  13. * @var array Non-rendered data about the menu item
  14. */
  15. protected $data = array(
  16. // string Identifier of the menu
  17. 'name' => '',
  18. // array Page contexts this menu item should appear on
  19. 'contexts' => array('all'),
  20. // string Menu section identifier
  21. 'section' => 'default',
  22. // int Smaller priorities float to the top
  23. 'priority' => 100,
  24. // bool Is this the currently selected menu item
  25. 'selected' => false,
  26. // string Identifier of this item's parent
  27. 'parent_name' => '',
  28. // ElggMenuItem The parent object or null
  29. 'parent' => null,
  30. // array Array of children objects or empty array
  31. 'children' => array(),
  32. // array Classes to apply to the li tag
  33. 'itemClass' => array(),
  34. // array Classes to apply to the anchor tag
  35. 'linkClass' => array(),
  36. );
  37. /**
  38. * @var string The menu display string
  39. */
  40. protected $text;
  41. /**
  42. * @var string The menu url
  43. */
  44. protected $href = null;
  45. /**
  46. * @var string Tooltip
  47. */
  48. protected $title = false;
  49. /**
  50. * @var string The string to display if link is clicked
  51. */
  52. protected $confirm = '';
  53. /**
  54. * ElggMenuItem constructor
  55. *
  56. * @param string $name Identifier of the menu item
  57. * @param string $text Display text of the menu item
  58. * @param string $href URL of the menu item (false if not a link)
  59. */
  60. public function __construct($name, $text, $href) {
  61. //$this->name = $name;
  62. $this->text = $text;
  63. if ($href) {
  64. $this->href = elgg_normalize_url($href);
  65. } else {
  66. $this->href = $href;
  67. }
  68. $this->data['name'] = $name;
  69. }
  70. /**
  71. * ElggMenuItem factory method
  72. *
  73. * This static method creates an ElggMenuItem from an associative array.
  74. * Required keys are name, text, and href.
  75. *
  76. * @param array $options Option array of key value pairs
  77. *
  78. * @return ElggMenuItem or NULL on error
  79. */
  80. public static function factory($options) {
  81. if (!isset($options['name']) || !isset($options['text'])) {
  82. return NULL;
  83. }
  84. if (!isset($options['href'])) {
  85. $options['href'] = '';
  86. }
  87. $item = new ElggMenuItem($options['name'], $options['text'], $options['href']);
  88. unset($options['name']);
  89. unset($options['text']);
  90. unset($options['href']);
  91. // special catch in case someone uses context rather than contexts
  92. if (isset($options['context'])) {
  93. $options['contexts'] = $options['context'];
  94. unset($options['context']);
  95. }
  96. // make sure contexts is set correctly
  97. if (isset($options['contexts'])) {
  98. $item->setContext($options['contexts']);
  99. unset($options['contexts']);
  100. }
  101. if (isset($options['link_class'])) {
  102. $item->setLinkClass($options['link_class']);
  103. unset($options['link_class']);
  104. }
  105. if (isset($options['item_class'])) {
  106. $item->setItemClass($options['item_class']);
  107. unset($options['item_class']);
  108. }
  109. if (isset($options['data']) && is_array($options['data'])) {
  110. $item->setData($options['data']);
  111. unset($options['data']);
  112. }
  113. foreach ($options as $key => $value) {
  114. if (isset($item->data[$key])) {
  115. $item->data[$key] = $value;
  116. } else {
  117. $item->$key = $value;
  118. }
  119. }
  120. return $item;
  121. }
  122. /**
  123. * Set a data key/value pair or a set of key/value pairs
  124. *
  125. * This method allows storage of arbitrary data with this menu item. The
  126. * data can be used for sorting, custom rendering, or any other use.
  127. *
  128. * @param mixed $key String key or an associative array of key/value pairs
  129. * @param mixed $value The value if $key is a string
  130. * @return void
  131. */
  132. public function setData($key, $value = null) {
  133. if (is_array($key)) {
  134. $this->data += $key;
  135. } else {
  136. $this->data[$key] = $value;
  137. }
  138. }
  139. /**
  140. * Get stored data
  141. *
  142. * @param string $key The key for the requested key/value pair
  143. * @return mixed
  144. */
  145. public function getData($key) {
  146. if (isset($this->data[$key])) {
  147. return $this->data[$key];
  148. } else {
  149. return null;
  150. }
  151. }
  152. /**
  153. * Set the identifier of the menu item
  154. *
  155. * @param string $name Unique identifier
  156. * @return void
  157. */
  158. public function setName($name) {
  159. $this->data['name'] = $name;
  160. }
  161. /**
  162. * Get the identifier of the menu item
  163. *
  164. * @return string
  165. */
  166. public function getName() {
  167. return $this->data['name'];
  168. }
  169. /**
  170. * Set the display text of the menu item
  171. *
  172. * @param string $text The display text
  173. * @return void
  174. */
  175. public function setText($text) {
  176. $this->text = $text;
  177. }
  178. /**
  179. * Get the display text of the menu item
  180. *
  181. * @return string
  182. */
  183. public function getText() {
  184. return $this->text;
  185. }
  186. /**
  187. * Set the URL of the menu item
  188. *
  189. * @param string $href URL or false if not a link
  190. * @return void
  191. */
  192. public function setHref($href) {
  193. $this->href = $href;
  194. }
  195. /**
  196. * Get the URL of the menu item
  197. *
  198. * @return string
  199. */
  200. public function getHref() {
  201. return $this->href;
  202. }
  203. /**
  204. * Set the contexts that this menu item is available for
  205. *
  206. * @param array $contexts An array of context strings
  207. * @return void
  208. */
  209. public function setContext($contexts) {
  210. if (is_string($contexts)) {
  211. $contexts = array($contexts);
  212. }
  213. $this->data['contexts'] = $contexts;
  214. }
  215. /**
  216. * Get an array of context strings
  217. *
  218. * @return array
  219. */
  220. public function getContext() {
  221. return $this->data['contexts'];
  222. }
  223. /**
  224. * Should this menu item be used given the current context
  225. *
  226. * @param string $context A context string (default is empty string for
  227. * current context stack).
  228. * @return bool
  229. */
  230. public function inContext($context = '') {
  231. if ($context) {
  232. return in_array($context, $this->data['contexts']);
  233. }
  234. if (in_array('all', $this->data['contexts'])) {
  235. return true;
  236. }
  237. foreach ($this->data['contexts'] as $context) {
  238. if (elgg_in_context($context)) {
  239. return true;
  240. }
  241. }
  242. return false;
  243. }
  244. /**
  245. * Set the selected flag
  246. *
  247. * @param bool $state Selected state (default is true)
  248. * @return void
  249. */
  250. public function setSelected($state = true) {
  251. $this->data['selected'] = $state;
  252. }
  253. /**
  254. * Get selected state
  255. *
  256. * @return bool
  257. */
  258. public function getSelected() {
  259. return $this->data['selected'];
  260. }
  261. /**
  262. * Set the tool tip text
  263. *
  264. * @param string $text The text of the tool tip
  265. * @return void
  266. */
  267. public function setTooltip($text) {
  268. $this->title = $text;
  269. }
  270. /**
  271. * Get the tool tip text
  272. *
  273. * @return string
  274. */
  275. public function getTooltip() {
  276. return $this->title;
  277. }
  278. /**
  279. * Set the confirm text shown when link is clicked
  280. *
  281. * @param string $text The text to show
  282. * @return void
  283. */
  284. public function setConfirmText($text) {
  285. $this->confirm = $text;
  286. }
  287. /**
  288. * Get the confirm text
  289. *
  290. * @return string
  291. */
  292. public function getConfirmText() {
  293. return $this->confirm;
  294. }
  295. /**
  296. * Set the anchor class
  297. *
  298. * @param mixed $class An array of class names, or a single string class name.
  299. * @return void
  300. */
  301. public function setLinkClass($class) {
  302. if (!is_array($class)) {
  303. $this->data['linkClass'] = array($class);
  304. } else {
  305. $this->data['linkClass'] = $class;
  306. }
  307. }
  308. /**
  309. * Get the anchor classes as text
  310. *
  311. * @return string
  312. */
  313. public function getLinkClass() {
  314. return implode(' ', $this->data['linkClass']);
  315. }
  316. /**
  317. * Add a link class
  318. *
  319. * @param mixed $class An array of class names, or a single string class name.
  320. * @return void
  321. */
  322. public function addLinkClass($class) {
  323. if (!is_array($class)) {
  324. $this->data['linkClass'][] = $class;
  325. } else {
  326. $this->data['linkClass'] += $class;
  327. }
  328. }
  329. /**
  330. * Set the li classes
  331. *
  332. * @param mixed $class An array of class names, or a single string class name.
  333. * @return void
  334. */
  335. public function setItemClass($class) {
  336. if (!is_array($class)) {
  337. $this->data['itemClass'] = array($class);
  338. } else {
  339. $this->data['itemClass'] = $class;
  340. }
  341. }
  342. /**
  343. * Get the li classes as text
  344. *
  345. * @return string
  346. */
  347. public function getItemClass() {
  348. // allow people to specify name with underscores and colons
  349. $name = strtolower($this->getName());
  350. $name = str_replace('_', '-', $name);
  351. $name = str_replace(':', '-', $name);
  352. $name = str_replace(' ', '-', $name);
  353. $class = implode(' ', $this->data['itemClass']);
  354. if ($class) {
  355. return "elgg-menu-item-$name $class";
  356. } else {
  357. return "elgg-menu-item-$name";
  358. }
  359. }
  360. /**
  361. * Set the priority of the menu item
  362. *
  363. * @param int $priority The smaller numbers mean higher priority (1 before 100)
  364. * @return void
  365. * @deprecated
  366. */
  367. public function setWeight($priority) {
  368. $this->data['priority'] = $priority;
  369. }
  370. /**
  371. * Get the priority of the menu item
  372. *
  373. * @return int
  374. * @deprecated
  375. */
  376. public function getWeight() {
  377. return $this->data['priority'];
  378. }
  379. /**
  380. * Set the priority of the menu item
  381. *
  382. * @param int $priority The smaller numbers mean higher priority (1 before 100)
  383. * @return void
  384. */
  385. public function setPriority($priority) {
  386. $this->data['priority'] = $priority;
  387. }
  388. /**
  389. * Get the priority of the menu item
  390. *
  391. * @return int
  392. */
  393. public function getPriority() {
  394. return $this->data['priority'];
  395. }
  396. /**
  397. * Set the section identifier
  398. *
  399. * @param string $section The identifier of the section
  400. * @return void
  401. */
  402. public function setSection($section) {
  403. $this->data['section'] = $section;
  404. }
  405. /**
  406. * Get the section identifier
  407. *
  408. * @return string
  409. */
  410. public function getSection() {
  411. return $this->data['section'];
  412. }
  413. /**
  414. * Set the parent identifier
  415. *
  416. * @param string $name The identifier of the parent ElggMenuItem
  417. * @return void
  418. */
  419. public function setParentName($name) {
  420. $this->data['parent_name'] = $name;
  421. }
  422. /**
  423. * Get the parent identifier
  424. *
  425. * @return string
  426. */
  427. public function getParentName() {
  428. return $this->data['parent_name'];
  429. }
  430. /**
  431. * Set the parent menu item
  432. *
  433. * @param ElggMenuItem $parent The parent of this menu item
  434. * @return void
  435. */
  436. public function setParent($parent) {
  437. $this->data['parent'] = $parent;
  438. }
  439. /**
  440. * Get the parent menu item
  441. *
  442. * @return ElggMenuItem or null
  443. */
  444. public function getParent() {
  445. return $this->data['parent'];
  446. }
  447. /**
  448. * Add a child menu item
  449. *
  450. * @param ElggMenuItem $item A child menu item
  451. * @return void
  452. */
  453. public function addChild($item) {
  454. $this->data['children'][] = $item;
  455. }
  456. /**
  457. * Set the menu item's children
  458. *
  459. * @param array $children Array of ElggMenuItems
  460. * @return void
  461. */
  462. public function setChildren($children) {
  463. $this->data['children'] = $children;
  464. }
  465. /**
  466. * Get the children menu items
  467. *
  468. * @return array
  469. */
  470. public function getChildren() {
  471. return $this->data['children'];
  472. }
  473. /**
  474. * Sort the children
  475. *
  476. * @param string $sortFunction A function that is passed to usort()
  477. * @return void
  478. */
  479. public function sortChildren($sortFunction) {
  480. foreach ($this->data['children'] as $key => $node) {
  481. $this->data['children'][$key]->data['original_order'] = $key;
  482. }
  483. usort($this->data['children'], $sortFunction);
  484. }
  485. /**
  486. * Get the menu item content (usually a link)
  487. *
  488. * @param array $vars Options to pass to output/url if a link
  489. * @return string
  490. * @todo View code in a model. How do we feel about that?
  491. */
  492. public function getContent(array $vars = array()) {
  493. if ($this->href === false) {
  494. return $this->text;
  495. }
  496. $defaults = get_object_vars($this);
  497. unset($defaults['data']);
  498. $vars += $defaults;
  499. if ($this->data['linkClass']) {
  500. if (isset($vars['class'])) {
  501. $vars['class'] = $vars['class'] . ' ' . $this->getLinkClass();
  502. } else {
  503. $vars['class'] = $this->getLinkClass();
  504. }
  505. }
  506. if (!isset($vars['rel']) && !isset($vars['is_trusted'])) {
  507. $vars['is_trusted'] = true;
  508. }
  509. if ($this->confirm) {
  510. $vars['confirm'] = $this->confirm;
  511. return elgg_view('output/confirmlink', $vars);
  512. } else {
  513. unset($vars['confirm']);
  514. }
  515. return elgg_view('output/url', $vars);
  516. }
  517. }