/engine/classes/ElggMenuItem.php

https://github.com/wangaiying/elgg4ysu · PHP · 568 lines · 238 code · 71 blank · 259 comment · 37 complexity · a1b72352b46bd1eab22c6b90686ffbeb MD5 · raw file

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