PageRenderTime 60ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/web/system/BlocksModule/Api/MenutreeApi.php

https://github.com/antoniom/core
PHP | 798 lines | 560 code | 62 blank | 176 comment | 95 complexity | 92bb212c187a8e103bcb98588a294cb2 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MIT
  1. <?php
  2. /**
  3. * Copyright Zikula Foundation 2009 - Zikula Application Framework
  4. *
  5. * This work is contributed to the Zikula Foundation under one or more
  6. * Contributor Agreements and licensed to You under the following license:
  7. *
  8. * @license GNU/LGPLv3 (or at your option, any later version).
  9. * @package Zikula
  10. *
  11. * Please see the NOTICE file distributed with this source code for further
  12. * information regarding copyright and licensing.
  13. */
  14. namespace BlocksModule\Api;
  15. use ModUtil, SecurityUtil, LogUtil, DataUtil, System, ZLanguage, CategoryRegistryUtil, CategoryUtil;
  16. use UserUtil;
  17. use BlocksModule\MenutreeUtil;
  18. class Blocks_Api_MenutreeApi extends Zikula_AbstractApi
  19. {
  20. /**
  21. * Return list of admin modules
  22. *
  23. * Syntax used in menutree
  24. * {ext:Blocks:adminlinks:[flat,category]}
  25. * Last param is optional. It can be flat and/or category separated by a comma.
  26. * 'flat' will add the admin links in the current menu. Without 'flat' the links are grouped one level down
  27. * 'category' additionally groups the admin links by their category.
  28. * You can combine 'flat' and 'category' to have the category links added in the current menu.
  29. *
  30. * @param array $args['item'] menu node to be replaced
  31. * @param string $args['lang'] current menu language
  32. * @param string $args['extrainfo'] additional params - if 'flat' then return links ungrouped. if 'category' module links grouped by category
  33. * @return mixed array of links if successful, false otherwise
  34. */
  35. public function adminlinks($args)
  36. {
  37. $item = isset($args['item']) && !empty($args['item']) ? $args['item'] : null;
  38. $lang = isset($args['lang']) && !empty($args['lang']) ? $args['lang'] : null;
  39. $extrainfo = isset($args['extrainfo']) && !empty($args['extrainfo']) ? $args['extrainfo'] : null;
  40. // $item ang lang params are required
  41. if (!$item || !$lang) {
  42. return false;
  43. }
  44. // Convert extrainfo into flags
  45. $extrainfo = ($extrainfo) ? preg_split("/[\s]*,[\s]*/", trim($extrainfo)) : array();
  46. $flag = array();
  47. $flag['flat'] = in_array("flat", $extrainfo); //now true or false
  48. $flag['category'] = in_array("category", $extrainfo); //now true or false
  49. // Make sure admin API is loaded
  50. if (!ModUtil::loadApi('Admin','admin',true)) {
  51. return false;
  52. }
  53. if (!SecurityUtil::checkPermission('Admin::', "::", ACCESS_EDIT)) {
  54. return array(); // Since no permission, return empty links
  55. }
  56. // get id for first element, use api func to aviod id conflicts inside menu
  57. $idoffset = MenutreeUtil::getIdOffset($item['id']);
  58. $lineno = 0;
  59. $links = array();
  60. // if not flat, group the links into a single menu entry
  61. if (!$flag['flat']) {
  62. $links['adminlinks'] = array(
  63. $lang => array(
  64. 'id' => $idoffset++,
  65. 'name' => $item['name'],
  66. 'href' => ModUtil::url('Admin', 'admin', 'adminpanel'),
  67. 'title' => $item['title'],
  68. 'className' => $item['className'],
  69. 'state' => $item['state'],
  70. 'lang' => $lang,
  71. 'lineno' => $lineno++,
  72. 'parent' => $item['parent']
  73. )
  74. );
  75. }
  76. // need to set parent node id - if links are grouped - use your_accont item id
  77. // otherwise parent id of replaced menu node
  78. $parentNode = (!$flag['flat']) ? $links['adminlinks'][$lang]['id'] : $item['parent'];
  79. // First work on the Admin module categories
  80. $catinfo = array(); // used to store menu information for the categories
  81. $catlinks = array();
  82. if ($flag['category']) {
  83. // Get all the Categories
  84. $categories = ModUtil::apiFunc('AdminModule', 'admin', 'getall');
  85. foreach ($categories as $item) {
  86. if (SecurityUtil::checkPermission('Admin::', "$item[catname]::$item[cid]", ACCESS_EDIT)) {
  87. // Set up the menu information for this category
  88. $catinfo[$item['cid']] = array (
  89. 'id' => $idoffset, // will need this to be a parent
  90. 'no' => 0 // start with 0 sub menu items
  91. );
  92. $catlinks[] = array(
  93. $lang => array(
  94. 'id' => $idoffset++,
  95. 'name' => $item['catname'],
  96. 'href' => ModUtil::url('Admin','admin','adminpanel', array('acid' => $item['cid'])),
  97. 'title' => $item['description'],
  98. 'className' => '',
  99. 'state' => 1,
  100. 'lang' => $lang,
  101. 'lineno' => $lineno++,
  102. 'parent' => $parentNode
  103. )
  104. );
  105. }
  106. }
  107. }
  108. // Now work on admin capable modules
  109. $adminmodules = ModUtil::getModulesCapableOf('admin');
  110. $displayNameType = ModUtil::getVar('Admin', 'displaynametype', 1);
  111. $default_cid = ModUtil::getVar('Admin', 'startcategory');
  112. $adminlinks = array();
  113. foreach ($adminmodules as $adminmodule) {
  114. if (SecurityUtil::checkPermission("$adminmodule[name]::", '::', ACCESS_EDIT)) {
  115. $cid = ModUtil::apiFunc('AdminModule', 'admin', 'getmodcategory',
  116. array('mid' => ModUtil::getIdFromName($adminmodule['name'])));
  117. $cid = (isset($catinfo[$cid])) ? $cid : $default_cid; // make sure each module is assigned a category
  118. $modinfo = ModUtil::getInfo(ModUtil::getIdFromName($adminmodule['name']));
  119. if ($modinfo['type'] == 2 || $modinfo['type'] == 3) {
  120. $menutexturl = ModUtil::url($modinfo['name'], 'admin');
  121. } else {
  122. $menutexturl = 'admin.php?module=' . $modinfo['name'];
  123. }
  124. if ($displayNameType == 1) {
  125. $menutext = $modinfo['displayname'];
  126. } elseif ($displayNameType == 2) {
  127. $menutext = $modinfo['name'];
  128. } elseif ($displayNameType == 3) {
  129. $menutext = $modinfo['displayname'] . ' (' . $modinfo['name'] . ')';
  130. }
  131. $adminlinks[] = array(
  132. $lang => array(
  133. 'id' => $idoffset++,
  134. 'name' => $menutext,
  135. 'href' => $menutexturl,
  136. 'title' => $modinfo['description'],
  137. 'className' => '',
  138. 'state' => 1,
  139. 'lang' => $lang,
  140. 'lineno' => ($flag['category']) ? $catinfo[$cid]['no']++ :$lineno++,
  141. 'parent' => ($flag['category']) ? $catinfo[$cid]['id'] :$parentNode
  142. )
  143. );
  144. }
  145. }
  146. $links = array_merge($links, $catlinks, $adminlinks);
  147. return $links;
  148. }
  149. /**
  150. * "Blank", sample plugin
  151. *
  152. * @param array $args['item'] menu node to be replaced
  153. * @param string $args['lang'] current menu language
  154. * @param string $args['extrainfo'] additional params - if 'flat' then return links ungrouped
  155. * @return mixed array of links if successful, false otherwise
  156. */
  157. public function blank($args)
  158. {
  159. /**
  160. * args may look like this:
  161. * Array
  162. * (
  163. * [item] => Array
  164. * (
  165. * [id] => 999
  166. * [name] => Name given in menutree form
  167. * [href] => {ext:blank:foo=1&bar=2}
  168. * [title] => Some title
  169. * [className] => important
  170. * [state] => 1
  171. * [lang] => eng
  172. * [lineno] => 99
  173. * [parent] => 0
  174. * )
  175. * [lang] => pol
  176. * [bid] => 999
  177. * [extrainfo] => foo=1&bar=2
  178. * )
  179. *
  180. */
  181. $dom = ZLanguage::getModuleDomain('menutree');
  182. $item = isset($args['item']) && !empty($args['item']) ? $args['item'] : null;
  183. $lang = isset($args['lang']) && !empty($args['lang']) ? $args['lang'] : null;
  184. $bid = isset($args['bid']) && !empty($args['bid']) ? $args['bid'] : null;
  185. $extrainfo = isset($args['extrainfo']) && !empty($args['extrainfo']) ? $args['extrainfo'] : null;
  186. // $item ang lang params are required
  187. if (!$item || !$lang) {
  188. return false;
  189. }
  190. // is there is extrainfo - convert it into array, parse_str is quite handy
  191. if ($extrainfo) {
  192. parse_str($extrainfo, $extrainfo);
  193. }
  194. // get id for first element, use api func to aviod id conflicts inside menu
  195. $idoffset = MenutreeUtil::getIdOffset($item['id']);
  196. $links = array();
  197. // build some link
  198. // you may use associative array keys
  199. $links['first'] = array(
  200. $lang => array(
  201. 'id' => $idoffset++, // always use id returned by api func for first element
  202. 'name' => $item['name'], // you may use name given by user - but do not have to
  203. 'href' => ModUtil::url('News'),
  204. 'title' => $item['title'], // the same as for name - you may use user input
  205. 'className' => $item['className'],
  206. 'state' => $item['state'],
  207. 'lang' => $lang,
  208. 'lineno' => $item['lineno'],
  209. 'parent' => $item['parent'] // always use replaced item parent for element at first level
  210. )
  211. );
  212. // build second link - this one will be child of the first element
  213. $secondLink = isset($extrainfo['foo']) ? ModUtil::url('News','user','display',array('sid' => $extrainfo['foo'])) : ModUtil::url('News');
  214. $links['second'] = array(
  215. $lang => array(
  216. 'id' => $idoffset++, // using this syntax you're always will have proper ids
  217. 'name' => 'Second blank link',
  218. 'href' => $secondLink,
  219. 'title' => __('Title', $dom), // you may also use translated content
  220. 'className' => '',
  221. 'state' => 1, // for child nodes set state = 1
  222. 'lang' => $lang,
  223. 'lineno' => 0,
  224. 'parent' => $links['first'][$lang]['id'] // use first element id while we want to set this node as child node
  225. )
  226. );
  227. // build third link - this one will be on the same level as first link
  228. $thirdLink = isset($extrainfo['bar']) ? ModUtil::url('News','user','display',array('sid' => $extrainfo['bar'])) : ModUtil::url('News');
  229. $links['third'] = array(
  230. $lang => array(
  231. 'id' => $idoffset++,
  232. 'name' => 'Third blank link',
  233. 'href' => $thirdLink,
  234. 'title' => '',
  235. 'className' => '',
  236. 'state' => $item['state'], // always use replaced item state for element at first level
  237. 'lang' => $lang,
  238. 'lineno' => $item['lineno']+1,
  239. 'parent' => $item['parent'] // always use replaced item parent for element at first level
  240. )
  241. );
  242. return $links;
  243. }
  244. /**
  245. * Return Content pages
  246. *
  247. * Syntax used in menutree
  248. * {ext:Blocks:Content:[groupby=page&parent=1]}
  249. * Params in [] are optional and
  250. * groupby = menuitem (default) or page, all other values stands for none
  251. * parent - id of parent node - this allows to get specified node of Content pages
  252. *
  253. *
  254. * @param array $args['item'] menu node to be replaced
  255. * @param string $args['lang'] current menu language
  256. * @param string $args['extrainfo'] additional params
  257. * @return mixed array of links if successful, false otherwise
  258. */
  259. public function Content($args)
  260. {
  261. $item = isset($args['item']) && !empty($args['item']) ? $args['item'] : null;
  262. $lang = isset($args['lang']) && !empty($args['lang']) ? $args['lang'] : null;
  263. $extrainfo = isset($args['extrainfo']) && !empty($args['extrainfo']) ? $args['extrainfo'] : null;
  264. // $item ang lang params are required
  265. if (!$item || !$lang) {
  266. return false;
  267. }
  268. // is there is extrainfo - convert it into array, parse_str is quite handy
  269. if ($extrainfo) {
  270. parse_str($extrainfo, $extrainfo);
  271. }
  272. $extrainfo['parent'] = isset($extrainfo['parent']) ? (int)$extrainfo['parent'] : 0;
  273. $extrainfo['groupby'] = isset($extrainfo['groupby'])? $extrainfo['groupby'] : 'menuitem';
  274. // get id for first element, use api func to aviod id conflicts inside menu
  275. $idoffset = MenutreeUtil::getIdOffset($item['id']);
  276. $lineno = 0;
  277. $links = array();
  278. // if $extrainfo['group'] if false - don't group pages
  279. if ($extrainfo['groupby'] == 'menuitem') {
  280. $links['content'] = array(
  281. $lang => array(
  282. 'id' => $idoffset++,
  283. 'name' => $item['name'],
  284. 'href' => ModUtil::url('Content'),
  285. 'title' => $item['title'],
  286. 'className' => $item['className'],
  287. 'state' => $item['state'],
  288. 'lang' => $lang,
  289. 'lineno' => $lineno++,
  290. 'parent' => $item['parent']
  291. )
  292. );
  293. }
  294. // need to set parent node id according to groupby mode
  295. $parentNode = $extrainfo['groupby'] == 'menuitem' ? $links['content'][$lang]['id'] : $item['parent'];
  296. // set option and get page list
  297. $options = array(
  298. 'orderBy' => 'setLeft',
  299. 'makeTree' => false,
  300. 'language' => $lang,
  301. 'filter' => array(
  302. 'superParentId' => $extrainfo['parent']
  303. )
  304. );
  305. $pages = ModUtil::apiFunc('ContentModule', 'page', 'getPages', $options);
  306. $blocked = array();
  307. foreach ((array)$pages as $page) {
  308. // grouping - skip first page if pages are filtered by parent id
  309. // and grouping is not set to page
  310. if ($extrainfo['parent'] == $page['id'] && $extrainfo['groupby'] != 'page') {
  311. continue;
  312. }
  313. // skip pages which are disabled for display in menu
  314. if (in_array($page['parentPageId'], $blocked) || !$page['isInMenu']) {
  315. $blocked[] = $page['id'];
  316. continue;
  317. }
  318. $links[$page['id']] = array(
  319. $lang => array(
  320. 'id' => $idoffset+$page['id'],
  321. 'name' => isset($page['translatedTitle']) && !empty($page['translatedTitle']) ? $page['translatedTitle'] : $page['title'],
  322. 'href' => ModUtil::url('Content', 'user', 'view', array('pid' => $page['id'])),
  323. 'title' => isset($page['translatedTitle']) && !empty($page['translatedTitle']) ? $page['translatedTitle'] : $page['title'],
  324. 'className' => '',
  325. 'state' => $page['isInMenu'],
  326. 'lang' => $lang,
  327. 'lineno' => $page['position'],
  328. 'parent' => isset($links[$page['parentPageId']][$lang]['id']) ? $links[$page['parentPageId']][$lang]['id'] : $parentNode
  329. )
  330. );
  331. }
  332. return $links;
  333. }
  334. /**
  335. * Return list of user modules
  336. *
  337. * Syntax used in menutree
  338. * {ext:Blocks:modules:[flat]}
  339. * Last param is optional
  340. *
  341. * @param array $args['item'] menu node to be replaced
  342. * @param string $args['lang'] current menu language
  343. * @param string $args['extrainfo'] additional params - if 'flat' then return links ungrouped
  344. * @return mixed array of links if successful, false otherwise
  345. */
  346. public function modules($args)
  347. {
  348. $item = isset($args['item']) && !empty($args['item']) ? $args['item'] : null;
  349. $lang = isset($args['lang']) && !empty($args['lang']) ? $args['lang'] : null;
  350. $extrainfo = isset($args['extrainfo']) && !empty($args['extrainfo']) ? $args['extrainfo'] : null;
  351. // $item ang lang params are required
  352. if (!$item || !$lang) {
  353. return false;
  354. }
  355. // get id for first element, use api func to aviod id conflicts inside menu
  356. $idoffset = MenutreeUtil::getIdOffset($item['id']);
  357. $lineno = 0;
  358. $links = array();
  359. // if $extrainfo if 'flat' - don't group links
  360. if ($extrainfo != 'flat') {
  361. $links['modules'] = array(
  362. $lang => array(
  363. 'id' => $idoffset++,
  364. 'name' => $item['name'],
  365. 'href' => '',
  366. 'title' => $item['title'],
  367. 'className' => $item['className'],
  368. 'state' => $item['state'],
  369. 'lang' => $lang,
  370. 'lineno' => $lineno++,
  371. 'parent' => $item['parent']
  372. )
  373. );
  374. }
  375. // need to set parent node id - if links are grouped - use your_accont item id
  376. // otherwise parent id of replaced menu node
  377. $parentNode = $extrainfo != 'flat' ? $links['modules'][$lang]['id'] : $item['parent'];
  378. $mods = ModUtil::getModulesCapableOf('user');
  379. foreach ($mods as $mod) {
  380. if (SecurityUtil::checkPermission("$mod[name]::", '::', ACCESS_OVERVIEW)) {
  381. $links[] = array(
  382. $lang => array(
  383. 'id' => $idoffset++,
  384. 'name' => $mod['displayname'],
  385. 'href' => ModUtil::url($mod['name'], 'user', 'index'),
  386. 'title' => $mod['description'],
  387. 'className' => '',
  388. 'state' => 1,
  389. 'lang' => $lang,
  390. 'lineno' => $lineno++,
  391. 'parent' => $parentNode
  392. )
  393. );
  394. }
  395. }
  396. return $links;
  397. }
  398. /**
  399. * Return some useful News links
  400. *
  401. * Syntax used in menutree
  402. * {ext:Blocks:news:[flat=BOOL&links=view,add,cat,arch|ALL]}
  403. * Params in [] are optional and
  404. * flat - true or false, if set to true links are ungrouped (default is false)
  405. * links - list of elements, default is ALL, avaiable items:
  406. * - view - link to main News view
  407. * - add - link do Submit News form
  408. * - cat - list of News categories
  409. * - arch - link to News archive
  410. * Items are displayed in order provided in menutree
  411. *
  412. * @param array $args['item'] menu node to be replaced
  413. * @param string $args['lang'] current menu language
  414. * @param string $args['extrainfo'] additional params
  415. * @return mixed array of links if successful, false otherwise
  416. */
  417. public function news($args)
  418. {
  419. $dom = ZLanguage::getModuleDomain('menutree');
  420. $item = isset($args['item']) && !empty($args['item']) ? $args['item'] : null;
  421. $lang = isset($args['lang']) && !empty($args['lang']) ? $args['lang'] : null;
  422. $bid = isset($args['bid']) && !empty($args['bid']) ? $args['bid'] : null;
  423. $extrainfo = isset($args['extrainfo']) && !empty($args['extrainfo']) ? $args['extrainfo'] : null;
  424. // $item ang lang params are required
  425. if (!$item || !$lang) {
  426. return false;
  427. }
  428. // is there is extrainfo - convert it into array, parse_str is quite handy
  429. if ($extrainfo) {
  430. parse_str($extrainfo, $extrainfo);
  431. }
  432. $extrainfo['flat'] = isset($extrainfo['flat'])? (bool)$extrainfo['flat'] : false;
  433. $extrainfo['links'] = isset($extrainfo['links'])? explode(',',$extrainfo['links']) : array('all');
  434. // get id for first element, use api func to aviod id conflicts inside menu
  435. $idoffset = MenutreeUtil::getIdOffset($item['id']);
  436. $lineno = 0;
  437. // load plugin language file
  438. $modinfo = ModUtil::getInfo(ModUtil::getIdFromName('News'));
  439. $links = array();
  440. // build some link
  441. // you may use associative array keys
  442. if (!$extrainfo['flat']) {
  443. $links['news'] = array(
  444. $lang => array(
  445. 'id' => $idoffset++,
  446. 'name' => $item['name'],
  447. 'href' => ModUtil::url('News'),
  448. 'title' => $item['title'],
  449. 'className' => $item['className'],
  450. 'state' => $item['state'],
  451. 'lang' => $lang,
  452. 'lineno' => $lineno++,
  453. 'parent' => $item['parent']
  454. )
  455. );
  456. }
  457. $parentNode = !$extrainfo['flat'] ? $links['news'][$lang]['id'] : $item['parent'];
  458. if (in_array('all',$extrainfo['links']) || in_array('view',$extrainfo['links'])) {
  459. $links['view'] = array(
  460. $lang => array(
  461. 'id' => $idoffset++,
  462. 'name' => $modinfo['displayname'],
  463. 'href' => ModUtil::url('News'),
  464. 'title' => __('View news', $dom),
  465. 'className' => '',
  466. 'state' => 1,
  467. 'lang' => $lang,
  468. 'lineno' => $lineno++,
  469. 'parent' => $parentNode
  470. )
  471. );
  472. }
  473. if (in_array('all',$extrainfo['links']) || in_array('arch',$extrainfo['links'])) {
  474. $links['arch'] = array(
  475. $lang => array(
  476. 'id' => $idoffset++,
  477. 'name' => __('Archive', $dom),
  478. 'href' => ModUtil::url('News','user','archives'),
  479. 'title' => __('Archive', $dom),
  480. 'className' => '',
  481. 'state' => 1,
  482. 'lang' => $lang,
  483. 'lineno' => $lineno++,
  484. 'parent' => $parentNode
  485. )
  486. );
  487. }
  488. if (in_array('all',$extrainfo['links']) || in_array('add',$extrainfo['links'])) {
  489. $links['add'] = array(
  490. $lang => array(
  491. 'id' => $idoffset++,
  492. 'name' => __('Submit news', $dom),
  493. 'href' => ModUtil::url('News','user','new'),
  494. 'title' => __('Submit news', $dom),
  495. 'className' => '',
  496. 'state' => 1,
  497. 'lang' => $lang,
  498. 'lineno' => $lineno++,
  499. 'parent' => $parentNode
  500. )
  501. );
  502. }
  503. if (in_array('all',$extrainfo['links']) || in_array('cat',$extrainfo['links'])) {
  504. if (!$extrainfo['flat']) {
  505. $links['cat'] = array(
  506. $lang => array(
  507. 'id' => $idoffset++,
  508. 'name' => __('Categories', $dom),
  509. 'href' => ModUtil::url('News'),
  510. 'title' => __('Categories', $dom),
  511. 'className' => '',
  512. 'state' => 1,
  513. 'lang' => $lang,
  514. 'lineno' => $lineno++,
  515. 'parent' => $parentNode
  516. )
  517. );
  518. }
  519. $catParentNode = !$extrainfo['flat'] ? $links['cat'][$lang]['id'] : $item['parent'];
  520. $catregistry = CategoryRegistryUtil::getRegisteredModuleCategories ('News', 'stories');
  521. if (!empty($catregistry)) {
  522. $multicategory = count($catregistry) > 1;
  523. $catLinks = array();
  524. foreach ($catregistry as $prop => $catid) {
  525. if ($multicategory && !$extrainfo['flat']) {
  526. $parentCategory = CategoryUtil::getCategoryByID ($catid);
  527. $catLinks[$catid] = array(
  528. $lang => array(
  529. 'id' => $idoffset++,
  530. 'name' => isset($parentCategory['display_name'][$lang]) && !empty($parentCategory['display_name'][$lang]) ? $parentCategory['display_name'][$lang] : $parentCategory['name'],
  531. 'href' => '',
  532. 'title' => isset($parentCategory['display_name'][$lang]) && !empty($parentCategory['display_name'][$lang]) ? $parentCategory['display_name'][$lang] : $parentCategory['name'],
  533. 'className' => '',
  534. 'state' => 1,
  535. 'lang' => $lang,
  536. 'lineno' => $lineno++,
  537. 'parent' => $catParentNode
  538. )
  539. );
  540. }
  541. $categories = CategoryUtil::getSubCategories($catid);
  542. foreach ($categories as $cat) {
  543. $catLinks[$cat['id']] = array(
  544. $lang => array(
  545. 'id' => $idoffset++,
  546. 'name' => isset($cat['display_name'][$lang]) && !empty($cat['display_name'][$lang]) ? $cat['display_name'][$lang] : $cat['name'],
  547. 'href' => ModUtil::url('News','user','view',array('prop' => $prop, 'cat' => $cat['name'])),
  548. 'title' => isset($cat['display_name'][$lang]) && !empty($cat['display_name'][$lang]) ? $cat['display_name'][$lang] : $cat['name'],
  549. 'className' => '',
  550. 'state' => 1,
  551. 'lang' => $lang,
  552. 'lineno' => $lineno++,
  553. 'parent' => isset($catLinks[$cat['parent_id']]) ? $catLinks[$cat['parent_id']][$lang]['id'] : $catParentNode
  554. )
  555. );
  556. }
  557. }
  558. } elseif (!$extrainfo['flat']) {
  559. unset($links['cat']);
  560. }
  561. }
  562. // sort links in order provided in menutree
  563. if (!in_array('all',$extrainfo['links'])) {
  564. $sortedLinks = array();
  565. if (!$extrainfo['flat']) {
  566. $sortedLinks[] = $links['news'];
  567. }
  568. foreach ($extrainfo['links'] as $l) {
  569. if (isset($links[$l]) && !empty($links[$l])) {
  570. $sortedLinks[] = $links[$l];
  571. }
  572. if ($l == 'cat') {
  573. $sortedLinks = array_merge((array)$sortedLinks, (array)$catLinks);
  574. }
  575. }
  576. $links = $sortedLinks;
  577. }
  578. return $links;
  579. }
  580. /**
  581. * Return some common user links
  582. *
  583. * Syntax used in menutree
  584. * {ext:Blocks:userlinks:[flat]}
  585. * Last param is optional
  586. *
  587. * This plugin generates a list of some common user links. The list looks as follows:
  588. * for loggedin users:
  589. * Your Account
  590. * Profile
  591. * Private Messages (if there is some message module)
  592. * Logout
  593. * for anonymous users:
  594. * Your Account
  595. * Login
  596. * Register
  597. * Lost Password
  598. *
  599. * If you provide an additional param extrainfo = flat, then the links are not grouped within
  600. * Your Accont element
  601. *
  602. * @param array $args['item'] menu node to be replaced
  603. * @param string $args['lang'] current menu language
  604. * @param string $args['extrainfo'] additional params - if 'flat' then return links ungrouped
  605. * @return mixed array of links if successful, false otherwise
  606. */
  607. public function userlinks($args)
  608. {
  609. $dom = ZLanguage::getModuleDomain('menutree');
  610. $item = isset($args['item']) && !empty($args['item']) ? $args['item'] : null;
  611. $lang = isset($args['lang']) && !empty($args['lang']) ? $args['lang'] : null;
  612. $extrainfo = isset($args['extrainfo']) && !empty($args['extrainfo']) ? $args['extrainfo'] : null;
  613. // $item ang lang params are required
  614. if (!$item || !$lang) {
  615. return false;
  616. }
  617. // get id for first element, use api func to aviod id conflicts inside menu
  618. $idoffset = MenutreeUtil::getIdOffset($item['id']);
  619. $lineno = 0;
  620. // module config
  621. $profileModule = System::getVar('profilemodule') ? System::getVar('profilemodule') : 'Profile';
  622. $profileModule = ModUtil::available($profileModule) ? $profileModule : null;
  623. $messageModule = System::getVar('messagemodule') ? System::getVar('messagemodule') : 'InterCom';
  624. $messageModule = ModUtil::available($messageModule) ? $messageModule : null;
  625. $links = array();
  626. // if $extrainfo if 'flat' - don't group links in your_account node
  627. if ($extrainfo != 'flat') {
  628. $links['your_account'] = array(
  629. $lang => array(
  630. 'id' => $idoffset++,
  631. 'name' => $item['name'],
  632. 'href' => ModUtil::url($profileModule),
  633. 'title' => $item['title'],
  634. 'className' => $item['className'],
  635. 'state' => $item['state'],
  636. 'lang' => $lang,
  637. 'lineno' => $lineno++,
  638. 'parent' => $item['parent']
  639. )
  640. );
  641. }
  642. // need to set parent node id - if links are grouped - use your_accont item id
  643. // otherwise parent id of replaced menu node
  644. $parentNode = $extrainfo != 'flat' ? $links['your_account'][$lang]['id'] : $item['parent'];
  645. if (UserUtil::isLoggedIn()) {
  646. $links['profile'] = array(
  647. $lang => array(
  648. 'id' => $idoffset++,
  649. 'name' => __('Profile', $dom),
  650. 'href' => ModUtil::url($profileModule),
  651. 'title' => __('Profile', $dom),
  652. 'className' => '',
  653. 'state' => 1,
  654. 'lang' => $lang,
  655. 'lineno' => $lineno++,
  656. 'parent' => $parentNode
  657. )
  658. );
  659. if (!is_null($messageModule)) {
  660. $links['messages'] = array(
  661. $lang => array(
  662. 'id' => $idoffset++,
  663. 'name' => __('Private messages', $dom),
  664. 'href' => ModUtil::url($messageModule),
  665. 'title' => __('Private messages', $dom),
  666. 'className' => '',
  667. 'state' => 1,
  668. 'lang' => $lang,
  669. 'lineno' => $lineno++,
  670. 'parent' => $parentNode
  671. )
  672. );
  673. }
  674. $links['logout'] = array(
  675. $lang => array(
  676. 'id' => $idoffset++,
  677. 'name' => __('Logout', $dom),
  678. 'href' => ModUtil::url('Users','user','logout'),
  679. 'title' => __('Logout', $dom),
  680. 'className' => '',
  681. 'state' => 1,
  682. 'lang' => $lang,
  683. 'lineno' => $lineno++,
  684. 'parent' => $parentNode
  685. )
  686. );
  687. } else {
  688. $request = $this->container->get('request');
  689. $loginArgs = array();
  690. if ($request->isMethod('GET')) {
  691. $loginArgs['returnpage'] = urlencode(System::getCurrentUri());
  692. }
  693. $links['login'] = array(
  694. $lang => array(
  695. 'id' => $idoffset++,
  696. 'name' => __('Login', $dom),
  697. 'href' => ModUtil::url('Users', 'user', 'login', $loginArgs),
  698. 'title' =>__('Login', $dom),
  699. 'className' => '',
  700. 'state' => 1,
  701. 'lang' => $lang,
  702. 'lineno' => $lineno++,
  703. 'parent' => $parentNode
  704. )
  705. );
  706. $links['register'] = array(
  707. $lang => array(
  708. 'id' => $idoffset++,
  709. 'name' => __('Register', $dom),
  710. 'href' => ModUtil::url('Users','user','register'),
  711. 'title' => __('Register', $dom),
  712. 'className' => '',
  713. 'state' => 1,
  714. 'lang' => $lang,
  715. 'lineno' => $lineno++,
  716. 'parent' => $parentNode
  717. )
  718. );
  719. $links['lostpassword'] = array(
  720. $lang => array(
  721. 'id' => $idoffset++,
  722. 'name' => __('Lost password', $dom),
  723. 'href' => ModUtil::url('Users','user','lostpassword'),
  724. 'title' => __('Lost password', $dom),
  725. 'className' => '',
  726. 'state' => 1,
  727. 'lang' => $lang,
  728. 'lineno' => $lineno++,
  729. 'parent' => $parentNode
  730. )
  731. );
  732. }
  733. return $links;
  734. }
  735. }