PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/web/system/BlocksModule/Block/ExtmenuBlock.php

https://github.com/antoniom/core
PHP | 419 lines | 271 code | 57 blank | 91 comment | 51 complexity | d1d2f52a1cddc732504c908fe0ae3677 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\Block;
  15. use UserUtil, ModUtil, SecurityUtil, LogUtil, DataUtil, System, ZLanguage, CategoryRegistryUtil, CategoryUtil;
  16. use PageUtil, ThemeUtil, BlockUtil;
  17. class ExtmenuBlock extends \Zikula\Framework\Controller\AbstractBlock
  18. {
  19. /**
  20. * initialise block
  21. */
  22. public function init()
  23. {
  24. SecurityUtil::registerPermissionSchema('ExtendedMenublock::', 'Block ID:Link ID:');
  25. }
  26. /**
  27. * get information on block
  28. *
  29. * @return array The block information
  30. */
  31. public function info()
  32. {
  33. return array('module' => $this->name,
  34. 'text_type' => $this->__('Extended menu'),
  35. 'text_type_long' => $this->__('Extended menu block'),
  36. 'allow_multiple' => true,
  37. 'form_content' => false,
  38. 'form_refresh' => false,
  39. 'show_preview' => true,
  40. 'admin_tableless' => true);
  41. }
  42. /**
  43. * display block
  44. *
  45. * @param array $blockinfo a blockinfo structure
  46. * @return output the rendered bock
  47. */
  48. public function display($blockinfo)
  49. {
  50. // security check
  51. if (!SecurityUtil::checkPermission('ExtendedMenublock::', $blockinfo['bid'] . '::', ACCESS_READ)) {
  52. return;
  53. }
  54. // Break out options from our content field
  55. $vars = BlockUtil::varsFromContent($blockinfo['content']);
  56. // template to use
  57. if (empty($vars['template'])) {
  58. $vars['template'] = 'Block/extmenu.tpl';
  59. }
  60. // stylesheet to use
  61. if (empty($vars['stylesheet'])) {
  62. $vars['stylesheet'] = 'extmenu.css';
  63. }
  64. // add the stylesheet to the header
  65. PageUtil::addVar('stylesheet', ThemeUtil::getModuleStylesheet('Blocks', $vars['stylesheet']));
  66. // if cache is enabled, checks for a cached output
  67. if ($this->view->getCaching()) {
  68. // set the cache id
  69. $this->view->setCacheId($blockinfo['bkey'].'/bid'.$blockinfo['bid'].'/'.UserUtil::getGidCacheString());
  70. // check out if the contents are cached
  71. if ($this->view->is_cached($vars['template'])) {
  72. $blockinfo['content'] = $this->view->fetch($vars['template']);
  73. return BlockUtil::themeBlock($blockinfo);
  74. }
  75. }
  76. // create default block variables
  77. if (!isset($vars['blocktitles'])) {
  78. $vars['blocktitles'] = array();
  79. }
  80. if (!isset($vars['links'])) {
  81. $vars['links'] = array();
  82. }
  83. if (!isset($vars['stylesheet'])) {
  84. $vars['stylesheet'] = '';
  85. }
  86. if (!isset($vars['menuid'])) {
  87. $vars['menuid'] = 0;
  88. }
  89. // get language and default to en
  90. $thislang = ZLanguage::getLanguageCode();
  91. if (!array_key_exists($thislang, $vars['links'])) {
  92. $thislang = 'en';
  93. }
  94. // if specific blocktitle for selected language exists, use it
  95. if (array_key_exists($thislang, $vars['blocktitles']) && !empty($vars['blocktitles'][$thislang])) {
  96. $blockinfo['title'] = $vars['blocktitles'][$thislang];
  97. }
  98. // Content
  99. $menuitems = array();
  100. if (!empty($vars['links'][$thislang])) {
  101. $blocked = array();
  102. foreach ($vars['links'][$thislang] as $linkid => $link) {
  103. $link['parentid'] = isset($link['parentid']) ? $link['parentid'] : null;
  104. $denied = !SecurityUtil::checkPermission('ExtendedMenublock::', $blockinfo['bid'] . ':' . $linkid . ':', ACCESS_READ);
  105. if ($denied || (!is_null($link['parentid']) && in_array($link['parentid'], $blocked))) {
  106. $blocked[] = $linkid;
  107. } elseif (!isset($link['active']) || $link['active'] != '1') {
  108. $blocked[] = $linkid;
  109. } else {
  110. // pre zk1.2 check
  111. if (!isset($link['id'])) {
  112. $link['id'] = $linkid;
  113. }
  114. $link['url'] = ModUtil::apiFunc('BlocksModule', 'user', 'encodebracketurl', $link['url']);
  115. // check for multiple options in image
  116. $this->checkImage($link);
  117. $menuitems[] = $link;
  118. }
  119. }
  120. }
  121. // Modules
  122. if (!empty($vars['displaymodules'])) {
  123. $newmods = ModUtil::getModulesCapableOf('user');
  124. $mods = array();
  125. foreach ($newmods as $module) {
  126. if (!preg_match('#(?:error|blocks)#', strtolower($module['name']))) {
  127. $mods[] = $module;
  128. }
  129. }
  130. // Separate from current content, if any
  131. if (count($menuitems) > 0) {
  132. $menuitems[] = array('name' => '&nbsp;',
  133. 'url' => '',
  134. 'title' => '',
  135. 'level' => 0,
  136. 'parentid' => null,
  137. 'image' => '');
  138. if (SecurityUtil::checkPermission('ExtendedMenublock::', $blockinfo['bid'] . '::', ACCESS_ADMIN)) {
  139. $menuitems[] = array('name' => $this->__('--Installed modules--'),
  140. 'url' => ModUtil::url('Blocks', 'admin', 'modify', array('bid' => $blockinfo['bid'])),
  141. 'title' => '',
  142. 'level' => 0,
  143. 'parentid' => null,
  144. 'image' => '');
  145. }
  146. }
  147. foreach ($mods as $mod) {
  148. // prepare image
  149. if (SecurityUtil::checkPermission("$mod[name]::", '::', ACCESS_OVERVIEW)) {
  150. $menuitems[] = array('name' => $mod['displayname'],
  151. 'url' => ModUtil::url($mod['name'], 'user', 'index'),
  152. 'title' => $mod['description'],
  153. 'level' => 0,
  154. 'parentid' => null,
  155. 'image' => '');
  156. }
  157. }
  158. }
  159. // check for any empty result set
  160. if (empty($menuitems)) {
  161. return;
  162. }
  163. $currenturi = urlencode(str_replace(System::getBaseUri() . '/', '', System::getCurrentUri()));
  164. // assign the items
  165. $this->view->assign('menuitems', $menuitems)
  166. ->assign('blockinfo', $blockinfo)
  167. ->assign('currenturi', $currenturi)
  168. ->assign('access_edit', Securityutil::checkPermission('ExtendedMenublock::', $blockinfo['bid'] . '::', ACCESS_EDIT));
  169. // get the block content
  170. $blockinfo['content'] = $this->view->fetch($vars['template']);
  171. // pass the block array back to the theme for display
  172. return BlockUtil::themeBlock($blockinfo);
  173. }
  174. /**
  175. * do a simple check .. to see if the current URL is the menu item
  176. *
  177. * @param none
  178. * @return boolean
  179. */
  180. public function is_recent_page($url)
  181. {
  182. if (!empty($url)) {
  183. $uri = System::getCurrentUri();
  184. if (is_integer(strpos($uri, $url))) {
  185. return true;
  186. }
  187. }
  188. return false;
  189. }
  190. /**
  191. * modify block settings
  192. *
  193. * @param array $blockinfo a blockinfo structure
  194. * @return output the bock form
  195. */
  196. public function modify($blockinfo)
  197. {
  198. // Break out options from our content field
  199. $vars = BlockUtil::varsFromContent($blockinfo['content']);
  200. $blockinfo['content'] = '';
  201. // Defaults
  202. if (empty($vars['displaymodules'])) {
  203. $vars['displaymodules'] = 0;
  204. }
  205. // template to use
  206. if (empty($vars['template'])) {
  207. $vars['template'] = 'Block/extmenu.tpl';
  208. }
  209. // create default block variables
  210. if (!isset($vars['blocktitles'])) {
  211. $vars['blocktitles'] = array();
  212. }
  213. if (!isset($vars['links'])) {
  214. $vars['links'] = array();
  215. }
  216. if (!isset($vars['stylesheet'])) {
  217. $vars['stylesheet'] = '';
  218. }
  219. if (!isset($vars['menuid'])) {
  220. $vars['menuid'] = 0;
  221. }
  222. $languages = ZLanguage::getInstalledLanguages();
  223. $userlanguage = ZLanguage::getLanguageCode();
  224. // filter out invalid languages
  225. foreach ($vars['blocktitles'] as $k => $v) {
  226. if (!in_array($k, $languages)) {
  227. unset($vars['blocktitles'][$k]);
  228. unset($vars['links'][$k]);
  229. }
  230. }
  231. // check if the users wants to add a new link via the "Add current url" link in the block
  232. $addurl = $this->request->query->get('addurl', 0);
  233. // or if we come from the normal "edit this block" link
  234. $fromblock = $this->request->query->get('fromblock', null);
  235. $redirect = '';
  236. if ($addurl == 1) {
  237. // set a marker for redirection later on
  238. $newurl = System::serverGetVar('HTTP_REFERER');
  239. $redirect = urlencode($newurl);
  240. $newurl = str_replace(System::getBaseUrl(), '', $newurl);
  241. if (empty($newurl)) {
  242. $newurl = System::getHomepageUrl();
  243. }
  244. foreach ($languages as $singlelanguage) {
  245. $vars['links'][$singlelanguage][] = array('name' => $this->__('--New link--'),
  246. 'url' => $newurl,
  247. 'title' => $this->__('--New link--'),
  248. 'level' => 0,
  249. 'parentid' => null,
  250. 'image' => '',
  251. 'active' => 1);
  252. }
  253. } elseif (isset($fromblock)) {
  254. $redirect = urlencode(System::serverGetVar('HTTP_REFERER'));
  255. }
  256. // add new languages to the blocktitles and link arrays
  257. // we need to know which language has the most links, this language will be the "master"
  258. // for new languages to be added. this ensures that all links for the new language
  259. // are prepared.
  260. $link_master = array();
  261. foreach ($languages as $lang) {
  262. if (isset($vars['links'][$lang]) && count($link_master) < count($vars['links'][$lang])) {
  263. $link_master = $vars['links'][$lang];
  264. }
  265. }
  266. foreach ($languages as $lang) {
  267. // create an empty blocktitle string
  268. if (!array_key_exists($lang, $vars['blocktitles'])) {
  269. $vars['blocktitles'][$lang] = '';
  270. }
  271. if (!array_key_exists($lang, $vars['links'])) {
  272. $vars['links'][$lang] = $link_master;
  273. }
  274. }
  275. // menuitems are sorted by language per default for easier
  276. // access when showing them (which is more often necessary than
  277. // editing them), but for editing them we need them sorted by id
  278. $menuitems = array();
  279. foreach ($vars['links'] as $lang => $langlinks) {
  280. // langlinks now contains an array of links for a certain language
  281. // sorted by key=id
  282. foreach ($langlinks as $linkid => $link) {
  283. // pre zk1.2 check
  284. if (!isset($link['id'])) {
  285. $link['id'] = $linkid;
  286. }
  287. $link['errors'] = array();
  288. $this->checkImage($link);
  289. $menuitems[$linkid][$lang] = $link;
  290. }
  291. }
  292. $vars['links'] = $menuitems;
  293. $this->view->setCaching(\Zikula_View::CACHE_DISABLED);
  294. // assign the vars
  295. $this->view->assign($vars)
  296. ->assign('languages', $languages)
  297. ->assign('userlanguage', $userlanguage)
  298. ->assign('redirect', $redirect)
  299. ->assign('blockinfo', $blockinfo);
  300. // return the output
  301. return $this->view->fetch('Block/extmenu_modify.tpl');
  302. }
  303. /**
  304. * update block settings
  305. *
  306. * @param array $blockinfo a blockinfo structure
  307. * @return $blockinfo the modified blockinfo structure
  308. */
  309. public function update($blockinfo)
  310. {
  311. $vars['displaymodules'] = $this->request->get('displaymodules');
  312. $vars['stylesheet'] = $this->request->get('stylesheet');
  313. $vars['template'] = $this->request->get('template');
  314. $vars['blocktitles'] = $this->request->get('blocktitles');
  315. // Defaults
  316. if (empty($vars['displaymodules'])) {
  317. $vars['displaymodules'] = 0;
  318. }
  319. if (empty($vars['template'])) {
  320. $vars['template'] = 'Block/extmenu.tpl';
  321. }
  322. if (empty($vars['stylesheet'])) {
  323. $vars['stylesheet'] = 'extmenu.css';
  324. }
  325. // User links
  326. $content = array();
  327. $vars['links'] = $this->request->get('links');
  328. $vars['blockversion'] = 1;
  329. // Save links hierarchy
  330. $linksorder = $this->request->get('linksorder');
  331. $linksorder = json_decode($linksorder, true);
  332. if (is_array($linksorder) && !empty($linksorder)) {
  333. foreach ((array)$vars['links'] as $lang => $langlinks) {
  334. foreach ($langlinks as $linkid => $link) {
  335. $vars['links'][$lang][$linkid]['parentid'] = $linksorder[$linkid]['parentid'];
  336. $vars['links'][$lang][$linkid]['haschildren'] = $linksorder[$linkid]['haschildren'];
  337. }
  338. }
  339. }
  340. $blockinfo['content'] = BlockUtil::varsToContent($vars);
  341. // clear the block cache
  342. $this->view->clear_cache(null, $blockinfo['bkey'].'/bid'.$blockinfo['bid']);
  343. // and clear the theme cache
  344. \Zikula_View_Theme::getInstance()->clear_cache();
  345. return $blockinfo;
  346. }
  347. protected function checkImage(&$link)
  348. {
  349. if (!empty($link['image'])) {
  350. $osimg = DataUtil::formatForOS($link['image']);
  351. if (is_readable($osimg)) {
  352. $link['image'] = $osimg;
  353. $link['imagedata'] = @getimagesize($osimg);
  354. } else {
  355. $link['errors'][] = DataUtil::formatForDisplay($link['image']) . ': invalid image data';
  356. $link['image'] = '';
  357. $link['imagedata'] = false;
  358. }
  359. }
  360. return;
  361. }
  362. }