PageRenderTime 50ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/preload/menu.php

https://bitbucket.org/simancms/simancms
PHP | 333 lines | 293 code | 31 blank | 9 comment | 59 complexity | 16f4a6297c9fa6c3a60557d02da330f6 MD5 | raw file
  1. <?php
  2. //------------------------------------------------------------------------------
  3. //| |
  4. //| Content Management System SiMan CMS |
  5. //| |
  6. //------------------------------------------------------------------------------
  7. //==============================================================================
  8. //#ver 1.6.19
  9. //#revision 2020-09-20
  10. //==============================================================================
  11. use SM\SM;
  12. if (!defined("SIMAN_DEFINED"))
  13. exit('Hacking attempt!');
  14. class SMNavigation
  15. {
  16. protected $items=[];
  17. protected $current_item;
  18. protected $menu_id;
  19. protected $menupointer;
  20. function __construct(&$pointer)
  21. {
  22. $this->menupointer=&$pointer;
  23. }
  24. function Clear()
  25. {
  26. $this->items=Array();
  27. $this->Rebuild();
  28. }
  29. function AddRootItem($title='', $url='', $id=NULL)
  30. {
  31. if ($id===NULL)
  32. $id='mi-'.mt_rand(1111, 9999).'-'.mt_rand(1111, 9999).'-'.mt_rand(1111, 9999).'-'.mt_rand(1111, 9999);
  33. $this->items[]=Array(
  34. 'id'=>$id,
  35. 'level'=>1,
  36. 'title'=>$title,
  37. 'root'=>NULL,
  38. 'url'=>$url,
  39. 'partial'=>false
  40. );
  41. $this->current_item=&$this->items[sm_count($this->items)-1];
  42. $this->current_item['active']=$this->isActive($this->current_item);
  43. $this->current_item['opened']=$this->current_item['active'];
  44. return $this;
  45. }
  46. function AddSubItem($root_id, $title='', $url='', $id=NULL)
  47. {
  48. if (!$this->SetCurrentItemByID($root_id, $this->items))
  49. {
  50. return $this->AddRootItem($title, $url, $id);
  51. }
  52. if ($id===NULL)
  53. $id='msi-'.mt_rand(1111, 9999).'-'.mt_rand(1111, 9999).'-'.mt_rand(1111, 9999).'-'.mt_rand(1111, 9999);
  54. $this->current_item['items'][]=Array(
  55. 'id'=>$id,
  56. 'level'=>$this->current_item['level']+1,
  57. 'title'=>$title,
  58. 'root_id'=>$this->current_item['id'],
  59. 'url'=>$url,
  60. 'partial'=>false
  61. );
  62. $this->SetCurrentItemByID($id, $this->current_item['items']);
  63. $this->current_item['active']=$this->isActive($this->current_item);
  64. $this->current_item['opened']=$this->current_item['active'];
  65. if ($this->current_item['opened'])
  66. {
  67. $this->SetOpened($this->current_item['root_id']);
  68. $this->SetCurrentItemByID($id, $this->items);
  69. }
  70. return $this;
  71. }
  72. protected function CheckActive()
  73. {
  74. $this->current_item['active']=$this->isActive($this->current_item);
  75. $this->current_item['opened']=$this->current_item['active'];
  76. if ($this->current_item['opened'] && !empty($this->current_item['root_id']))
  77. {
  78. $id=$this->CurrentItemID();
  79. $this->SetOpened($this->current_item['root_id']);
  80. $this->SetCurrentItemByID($id, $this->items);
  81. }
  82. }
  83. protected function SetOpened($id=NULL)
  84. {
  85. if ($id!==NULL)
  86. $this->SetCurrentItemByID($id, $this->items);
  87. $this->current_item['opened']=true;
  88. if (!empty($this->current_item['root_id']))
  89. $this->SetOpened($this->current_item['root_id']);
  90. }
  91. protected function SetPosition($position)
  92. {
  93. $this->current_item['position']=$position;
  94. }
  95. function SetPartialActive($partial=true)
  96. {
  97. $this->current_item['partial']=$partial;
  98. $this->CheckActive();
  99. }
  100. function SetAlt($value)
  101. {
  102. $this->current_item['alt']=$value;
  103. }
  104. function SetHTMLAttributes($value)
  105. {
  106. $this->current_item['html_attributes']=$value;
  107. }
  108. function SetHTMLBegin($value)
  109. {
  110. $this->current_item['html_begin']=$value;
  111. }
  112. function SetHTMLEnd($value)
  113. {
  114. $this->current_item['html_end']=$value;
  115. }
  116. function SetOpenInNewPage($value=true)
  117. {
  118. $this->current_item['newpage']=$value;
  119. }
  120. protected function SetCurrentItemByID($id, &$items)
  121. {
  122. for ($i = 0; $i<sm_count($items); $i++)
  123. {
  124. if (sm_strcmp($items[$i]['id'], $id)==0)
  125. {
  126. $this->current_item=&$items[$i];
  127. return true;
  128. }
  129. if (isset($items[$i]['items']) && is_array($items[$i]['items']))
  130. if ($this->SetCurrentItemByID($id, $items[$i]['items']))
  131. return true;
  132. }
  133. return false;
  134. }
  135. function CurrentItemID()
  136. {
  137. return $this->current_item['id'];
  138. }
  139. protected function isActive(&$item)
  140. {
  141. global $sm;
  142. if (empty($item['url']))
  143. return false;
  144. $tmp_index = sm_strpos(sm_settings('resource_url'), '/');
  145. $main_suburl = substr(sm_settings('resource_url'), $tmp_index);
  146. if (
  147. (sm_strcmp($main_suburl.$item['url'], $sm['server']['REQUEST_URI']) == 0
  148. ||
  149. sm_strcmp($main_suburl.$item['url'], $sm['server']['REQUEST_URI'].'index.php') == 0)
  150. || (sm_is_index_page() && sm_strcmp($item['url'], 'http://'.sm_settings('resource_url')) == 0)
  151. )
  152. return true;
  153. if ($item['partial'])
  154. {
  155. if (sm_strpos($sm['server']['REQUEST_URI'], $main_suburl.$item['url']) === 0)
  156. return true;
  157. }
  158. return false;
  159. }
  160. protected function RebuildItems(&$items)
  161. {
  162. for ($i = 0; $i<sm_count($items); $i++)
  163. {
  164. if (!isset($items[$i]['items']))
  165. $items[$i]['items']=[];
  166. $index=sm_count($this->menupointer);
  167. $this->menupointer[$index]['id'] = $items[$i]['id'];
  168. $this->menupointer[$index]['first'] = $i==0?1:0;
  169. $this->menupointer[$index]['last'] = $i==sm_count($items[$i]['items'])-1?1:0;
  170. $this->menupointer[$index]['mid'] = $this->menu_id;
  171. $this->menupointer[$index]['pos'] = $items[$i]['position'];
  172. $this->menupointer[$index]['submenu_position'] = $i;
  173. $this->menupointer[$index]['add_param'] = $this->menu_id.'|'.$items[$i]['id'];
  174. $this->menupointer[$index]['level'] = $items[$i]['level'];
  175. $this->menupointer[$index]['submenu_from'] = isset($items[$i]['root_id'])?$items[$i]['root_id']:'';
  176. $this->menupointer[$index]['sublines_count'] = sm_count($items[$i]['items']);
  177. $this->menupointer[$index]['is_submenu'] = sm_count($items[$i]['items'])>0?1:0;
  178. $this->menupointer[$index]['url'] = $items[$i]['url'];
  179. $this->menupointer[$index]['caption'] = $items[$i]['title'];
  180. $this->menupointer[$index]['partial'] = $items[$i]['partial'];
  181. $this->menupointer[$index]['alt'] = $items[$i]['alt'];
  182. $this->menupointer[$index]['attr'] = $items[$i]['html_attributes'];
  183. $this->menupointer[$index]['newpage'] = $items[$i]['newpage'];
  184. $this->menupointer[$index]['html_begin'] = sm_get_array_value($items[$i], 'html_begin');
  185. $this->menupointer[$index]['html_end'] = sm_get_array_value($items[$i], 'html_end');
  186. if (intval(sm_settings('menuitems_use_image')) == 1)
  187. {
  188. if (file_exists('./files/img/menuitem'.$this->menupointer[$index]['id'].'.jpg'))
  189. $this->menupointer[$index]['image'] = 'files/img/menuitem'.$this->menupointer[$index]['id'].'.jpg';
  190. }
  191. $this->menupointer[$index]['opened']=$items[$i]['opened']?1:0;
  192. $this->menupointer[$index]['active']=$items[$i]['active']?1:0;
  193. if (is_array($items[$i]['items']))
  194. $this->RebuildItems($items[$i]['items']);
  195. }
  196. }
  197. function Rebuild()
  198. {
  199. $this->menupointer=Array();
  200. $this->RebuildItems($this->items);
  201. }
  202. function LoadFromDB($menu_id)
  203. {
  204. global $row;
  205. $this->menu_id=$menu_id;
  206. $q=new TQuery(sm_table_prefix().'menu_lines');
  207. $q->AddWhere('id_menu_ml', intval($menu_id));
  208. $q->OrderBy('submenu_from, position');
  209. $q->Open();
  210. while ($row = $q->Fetch())
  211. {
  212. sm_event('onbeforemenulineprocessing', $row['id_ml']);
  213. if (empty($row['submenu_from']))
  214. $this->AddRootItem($row['caption_ml'], $row['url'], $row['id_ml']);
  215. else
  216. $this->AddSubItem($row['submenu_from'], $row['caption_ml'], $row['url'], $row['id_ml']);
  217. $this->SetPosition($row['position']);
  218. if (intval($row['partial_select'])==1)
  219. $this->SetPartialActive();
  220. $this->SetAlt($row['alt_ml']);
  221. $this->SetHTMLAttributes($row['attr_ml']);
  222. $this->SetOpenInNewPage($row['newpage_ml']);
  223. }
  224. $this->Rebuild();
  225. return $this;
  226. }
  227. }
  228. function siman_add_modifier_menu(&$menu)
  229. {
  230. for ($i = 0; $i<sm_count($menu); $i++)
  231. {
  232. sm_add_content_modifier($menu[$i]['caption']);
  233. }
  234. }
  235. if (!function_exists('siman_load_menu'))
  236. {
  237. function siman_menu_open_detect(&$menu, $opened_id)
  238. {
  239. if (intval($opened_id) == 0)
  240. return;
  241. for ($i = 0; $i<sm_count($menu); $i++)
  242. {
  243. if (intval($menu[$i]['id']) == intval($opened_id) && intval($opened_id) != intval($menu[$i]['submenu_from']))
  244. {
  245. $menu[$i]['opened'] = true;
  246. siman_menu_open_detect($menu, $menu[$i]['submenu_from']);
  247. }
  248. }
  249. }
  250. function siman_load_menu($menu_id, $maxlevel = -1)
  251. {
  252. $rmenu=[];
  253. (new SMNavigation($rmenu))->LoadFromDB($menu_id);
  254. return $rmenu;
  255. }
  256. }
  257. function sm_add_menuitem(&$menu, $title, $url, $level = 1, $partial_select = '', $alt_text = '', $newpage = 0)
  258. {
  259. global $sm;
  260. $i = sm_count($menu);
  261. $menu[$i]['url'] = $url;
  262. $menu[$i]['caption'] = $title;
  263. $menu[$i]['partial'] = $partial_select;
  264. $menu[$i]['level'] = $level;
  265. $menu[$i]['alt'] = $alt_text;
  266. $menu[$i]['newpage'] = $newpage;
  267. $menu[$i]['active'] = 0;
  268. if ($level == 1)
  269. {
  270. $menu[$i]['last'] = 1;
  271. if ($i == 0)
  272. $menu[$i]['first'] = 1;
  273. else
  274. $menu[$i-1]['last'] = 0;
  275. }
  276. $tmp_index = sm_strpos(sm_settings('resource_url'), '/');
  277. $main_suburl = substr(sm_settings('resource_url'), $tmp_index);
  278. if (
  279. (sm_strcmp($main_suburl.$menu[$i]['url'], $sm['server']['REQUEST_URI']) == 0
  280. ||
  281. sm_strcmp($main_suburl.$menu[$i]['url'], $sm['server']['REQUEST_URI'].'index.php') == 0)
  282. || (sm_is_index_page() && sm_strcmp($menu[$i]['url'], 'http://'.sm_settings('resource_url')) == 0)
  283. )
  284. $menu[$i]['active'] = '1';
  285. if ($menu[$i]['active'] != '1' && $menu[$i]['partial'] == 1)
  286. {
  287. if (sm_strpos($sm['server']['REQUEST_URI'], $main_suburl.$menu[$i]['url']) === 0)
  288. $menu[$i]['active'] = '1';
  289. }
  290. if (empty($menu[$i]['url']))
  291. $menu[$i]['active'] = '0';
  292. }
  293. if (!sm_empty_settings('upper_menu_id'))
  294. {
  295. SM::TopNavigation()->LoadFromDB(sm_settings('upper_menu_id'));
  296. siman_add_modifier_menu($sm['s']['uppermenu']);
  297. }
  298. if (!sm_empty_settings('bottom_menu_id'))
  299. {
  300. SM::BottomNavigation()->LoadFromDB(sm_settings('bottom_menu_id'));
  301. siman_add_modifier_menu($sm['s']['bottommenu']);
  302. }