api.globalmenu.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. /**
  3. * Ubilling global menu rendering and customization class
  4. */
  5. class GlobalMenu {
  6. /**
  7. * Contains globalmenu config as key=>value with sections
  8. *
  9. * @var array
  10. */
  11. protected $rawData = array();
  12. /**
  13. * Contains some preprocessed menu data
  14. *
  15. * @var array
  16. */
  17. protected $menuData = array();
  18. /**
  19. * Contains available menu categories
  20. *
  21. * @var array
  22. */
  23. protected $categories = array();
  24. /**
  25. * Contains system alter config as key=>value
  26. *
  27. * @var array
  28. */
  29. protected $altCfg = array();
  30. /**
  31. * Contains disabled menu items
  32. *
  33. * @var array
  34. */
  35. protected $disabled = array();
  36. /**
  37. * Contains fast access menu items
  38. *
  39. * @var array
  40. */
  41. protected $fastAccess = array();
  42. /**
  43. * Contains default menu icons path
  44. *
  45. * @var string
  46. */
  47. protected $iconsPath = 'skins/menuicons/';
  48. /**
  49. * Pre-rendered menu code
  50. *
  51. * @var string
  52. */
  53. protected $menuCode = '';
  54. /**
  55. * Pre-rendered fast access menu code
  56. *
  57. * @var string
  58. */
  59. protected $menuCodeFA = '';
  60. /**
  61. * Current user`s login
  62. *
  63. * @var string
  64. */
  65. protected $myLogin = '';
  66. const DEFAULT_ICON = 'defaulticon.png';
  67. const CUSTOMS_PATH = 'content/documents/glmcustoms/';
  68. /**
  69. * Creates new GlobalMenu instance
  70. */
  71. public function __construct() {
  72. $this->setLogin();
  73. $this->loadCustoms();
  74. $this->loadConfig();
  75. $this->loadData();
  76. $this->extractCategories();
  77. $this->parseData();
  78. }
  79. /**
  80. * Sets current logged in user login into private property
  81. *
  82. * @return void
  83. */
  84. protected function setLogin() {
  85. if (LOGGED_IN) {
  86. $this->myLogin = whoami();
  87. }
  88. }
  89. /**
  90. * Loads global menu custom data if its available
  91. *
  92. * @return void
  93. */
  94. protected function loadCustoms() {
  95. if (!empty($this->myLogin)) {
  96. //read and preprocess disabled modules data
  97. $disabledFilename = self::CUSTOMS_PATH . $this->myLogin . '.disabled';
  98. if (file_exists($disabledFilename)) {
  99. $tmpData = file_get_contents($disabledFilename);
  100. if (!empty($tmpData)) {
  101. $tmpData = explode(',', $tmpData);
  102. if (!empty($tmpData)) {
  103. foreach ($tmpData as $io => $modulename) {
  104. $this->disabled[$modulename] = $io;
  105. }
  106. }
  107. }
  108. }
  109. //read and preprocess fast access modules data
  110. $fastaccFilename = self::CUSTOMS_PATH . $this->myLogin . '.fastacc';
  111. if (file_exists($fastaccFilename)) {
  112. $tmpData = file_get_contents($fastaccFilename);
  113. if (!empty($tmpData)) {
  114. $tmpData = explode(',', $tmpData);
  115. if (!empty($tmpData)) {
  116. foreach ($tmpData as $io => $modulename) {
  117. $this->fastAccess[$modulename] = $io;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. /**
  125. * Loads global alter.ini into protected data prop
  126. *
  127. * @global type $ubillingConfig
  128. * @return void
  129. */
  130. protected function loadConfig() {
  131. global $ubillingConfig;
  132. $this->altCfg = $ubillingConfig->getAlter();
  133. }
  134. /**
  135. * Loads menudata into private raw data property
  136. *
  137. * @return void
  138. */
  139. protected function loadData() {
  140. $this->rawData = rcms_parse_ini_file(CONFIG_PATH . 'globalmenu.ini', true);
  141. }
  142. /**
  143. * Extracts categories from filled up rawData
  144. *
  145. * @return void
  146. */
  147. protected function extractCategories() {
  148. if (!empty($this->rawData)) {
  149. if (isset($this->rawData['ubillingglobalmenucategories'])) {
  150. $this->categories = $this->rawData['ubillingglobalmenucategories'];
  151. unset($this->rawData['ubillingglobalmenucategories']);
  152. foreach ($this->categories as $io => $each) {
  153. $this->menuData[$each] = '';
  154. }
  155. }
  156. }
  157. }
  158. /**
  159. * Parses raw menu data into ready to output menu array witch right/option checks
  160. *
  161. * @return void
  162. */
  163. protected function parseData() {
  164. if (!empty($this->rawData)) {
  165. foreach ($this->rawData as $io => $each) {
  166. $icon = (!empty($each['ICON'])) ? $each['ICON'] : self::DEFAULT_ICON;
  167. $icon = $this->iconsPath . $icon;
  168. $name = __($each['NAME']);
  169. $checkRight = (!empty($each['NEED_RIGHT'])) ? cfr($each['NEED_RIGHT']) : true;
  170. $checkOption = (!empty($each['NEED_OPTION'])) ? @$this->altCfg[$each['NEED_OPTION']] : true;
  171. if ($checkRight and $checkOption) {
  172. if (!isset($this->disabled[$io])) {
  173. $this->menuData[$each['CATEGORY']] .= wf_tag('li', false) . wf_Link($each['URL'], wf_img($icon) . ' ' . $name, false) . wf_tag('li', true);
  174. }
  175. }
  176. }
  177. }
  178. }
  179. /**
  180. * Formats existing menu data into printable HTML code
  181. *
  182. * @return void
  183. */
  184. protected function formatMenuCode() {
  185. if (!empty($this->categories)) {
  186. if (!empty($this->menuData)) {
  187. foreach ($this->categories as $eachCategoryName => $eachCategoryId) {
  188. if (!empty($this->menuData[$eachCategoryId])) {
  189. $this->menuCode .= wf_tag('h3', false) . __($eachCategoryName) . wf_tag('h3', true);
  190. $this->menuCode .= wf_tag('ul', false, 'toggleGMENU');
  191. $this->menuCode .= $this->menuData[$eachCategoryId];
  192. $this->menuCode .= wf_tag('ul', true);
  193. }
  194. }
  195. }
  196. }
  197. }
  198. /**
  199. * Returns formatted menu code
  200. *
  201. * @return string
  202. */
  203. public function render() {
  204. $this->formatMenuCode();
  205. return($this->menuCode);
  206. }
  207. /**
  208. * Formats existing menu data into printable HTML code as personalization form
  209. *
  210. * @return string
  211. */
  212. public function getEditForm() {
  213. $result = '';
  214. $tmpArr = array();
  215. if (!empty($this->categories)) {
  216. if (!empty($this->rawData)) {
  217. foreach ($this->rawData as $io => $each) {
  218. //table headers
  219. if (!isset($tmpArr[$each['CATEGORY']])) {
  220. $formCells = wf_TableCell(__('Module'), '50%');
  221. $formCells .= wf_TableCell(__('Hidden'));
  222. $formCells .= wf_TableCell(__('Fast access'));
  223. $formRows = wf_TableRow($formCells, 'row1');
  224. $tmpArr[$each['CATEGORY']] = $formRows;
  225. }
  226. //table rows
  227. $icon = (!empty($each['ICON'])) ? $each['ICON'] : self::DEFAULT_ICON;
  228. $icon = $this->iconsPath . $icon;
  229. $name = __($each['NAME']);
  230. $checkRight = (!empty($each['NEED_RIGHT'])) ? cfr($each['NEED_RIGHT']) : true;
  231. $checkOption = (!empty($each['NEED_OPTION'])) ? @$this->altCfg[$each['NEED_OPTION']] : true;
  232. if ($checkRight and $checkOption) {
  233. $formCells = wf_TableCell(wf_Link($each['URL'], wf_img($icon) . ' ' . $name, false));
  234. $disabledFlag = (isset($this->disabled[$io])) ? true : false;
  235. $formCells .= wf_TableCell(wf_CheckInput('_glmdisabled[' . $io . ']', '', false, $disabledFlag));
  236. $fastAccessFlag = (isset($this->fastAccess[$io])) ? true : false;
  237. $formCells .= wf_TableCell(wf_CheckInput('_glmfastacc[' . $io . ']', '', false, $fastAccessFlag));
  238. $formRows = wf_TableRow($formCells, 'row5');
  239. $tmpArr[$each['CATEGORY']] .= $formRows;
  240. }
  241. }
  242. }
  243. foreach ($this->categories as $eachCategoryName => $eachCategoryId) {
  244. $result .= wf_tag('h3', false) . __($eachCategoryName) . wf_tag('h3', true);
  245. $result .= wf_TableBody($tmpArr[$eachCategoryId], '100%', 0, '');
  246. }
  247. }
  248. if (!empty($result)) {
  249. $result .= wf_HiddenInput('glcustomconfedit', 'true');
  250. $result .= wf_tag('br');
  251. $result .= wf_Submit(__('Save'));
  252. $result = wf_Form('', 'POST', $result, '');
  253. }
  254. return ($result);
  255. }
  256. /**
  257. * Saves currently posted custom configs to FS
  258. *
  259. * @return void
  260. */
  261. public function saveCustomConfigs() {
  262. //disabled modules management
  263. $disabledFilename = self::CUSTOMS_PATH . $this->myLogin . '.disabled';
  264. $tmpData = '';
  265. if (ubRouting::checkPost('_glmdisabled')) {
  266. $glmDisabled = ubRouting::post('_glmdisabled');
  267. if (!empty($glmDisabled)) {
  268. foreach ($glmDisabled as $modulename => $on) {
  269. $tmpData .= trim($modulename) . ',';
  270. }
  271. $tmpData = rtrim($tmpData, ",");
  272. file_put_contents($disabledFilename, $tmpData);
  273. $tmpData = '';
  274. }
  275. } else {
  276. file_put_contents($disabledFilename, '');
  277. }
  278. //fast access modules management
  279. $fastaccFilename = self::CUSTOMS_PATH . $this->myLogin . '.fastacc';
  280. $tmpData = '';
  281. if (ubRouting::checkPost('_glmfastacc')) {
  282. $glmFastacc = ubRouting::post('_glmfastacc');
  283. if (!empty($glmFastacc)) {
  284. foreach ($glmFastacc as $modulename => $on) {
  285. $tmpData .= trim($modulename) . ',';
  286. }
  287. $tmpData = rtrim($tmpData, ",");
  288. file_put_contents($fastaccFilename, $tmpData);
  289. $tmpData = '';
  290. }
  291. } else {
  292. file_put_contents($fastaccFilename, '');
  293. }
  294. }
  295. /**
  296. * Returns fast access menu customization control
  297. *
  298. * @return string
  299. */
  300. protected function getCustomizeControl() {
  301. $result = '';
  302. if (cfr('GLMENUCONF')) {
  303. $result .= wf_tag('div', false, 'breadcrumb_divider') . wf_tag('div', true);
  304. $result .= wf_Link('?module=glmenuconf', '+', false, '', 'title="' . __('Personalize menu') . '"');
  305. }
  306. return($result);
  307. }
  308. /**
  309. * Rebuilds fast access menu data with newly saved config
  310. *
  311. * @return void
  312. */
  313. public function rebuildFastAccessMenuData() {
  314. $delimiter = wf_tag('div', false, 'breadcrumb_divider') . wf_tag('div', true);
  315. $fastaccData = self::CUSTOMS_PATH . $this->myLogin . '.fastaccdata';
  316. $tmpData = '';
  317. if (!empty($this->rawData)) {
  318. foreach ($this->rawData as $io => $each) {
  319. if (isset($this->fastAccess[$io])) {
  320. $name = __($each['NAME']);
  321. $url = $each['URL'];
  322. $tmpData .= $delimiter . wf_Link($url, $name, false);
  323. }
  324. }
  325. }
  326. //edit control here
  327. $tmpData .= $this->getCustomizeControl();
  328. file_put_contents($fastaccData, $tmpData);
  329. }
  330. /**
  331. * Loads prepared personal fast access menu data if it exists
  332. *
  333. * @return void
  334. */
  335. protected function loadFastAccesMenu() {
  336. //just single full-load of data due instance lifetime
  337. if (empty($this->menuCodeFA)) {
  338. $this->menuCodeFA .= wf_Link('?module=taskbar', __('Taskbar'), false);
  339. $fastaccData = self::CUSTOMS_PATH . $this->myLogin . '.fastaccdata';
  340. if (file_exists($fastaccData)) {
  341. //load preprocessed data
  342. $this->menuCodeFA .= file_get_contents($fastaccData);
  343. } else {
  344. //append customize link if no precached data
  345. $this->menuCodeFA .= $this->getCustomizeControl();
  346. }
  347. }
  348. }
  349. /**
  350. * Returns raw fast acces menu code
  351. *
  352. * @return string
  353. */
  354. public function renderFastAccessMenu() {
  355. $this->loadFastAccesMenu();
  356. return ($this->menuCodeFA);
  357. }
  358. }