PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/SM/UI/Navigation.php

https://bitbucket.org/simancms/simancms
PHP | 218 lines | 194 code | 21 blank | 3 comment | 31 complexity | 9a166024a80ef051809a8ec7a7ed6a3d MD5 | raw file
  1. <?php
  2. //==============================================================================
  3. //#revision 2020-09-20
  4. //==============================================================================
  5. namespace SM\UI;
  6. sm_add_cssfile('common_adminnavigation.css');
  7. class Navigation
  8. {
  9. protected $nav = [
  10. 'class'=>'',
  11. 'style'=>'',
  12. 'items'=>[],
  13. ];
  14. protected $currentitem;
  15. function __construct()
  16. {
  17. global $sm;
  18. if (!empty($sm['adminnavigation']['globalclass']))
  19. $this->AddClassnameGlobal($sm['adminnavigation']['globalclass']);
  20. }
  21. function AddItem($title, $url, $name='')
  22. {
  23. global $sm;
  24. if (empty($name))
  25. $name = md5(rand(1, 999999));
  26. $this->SetActiveItem($name);
  27. $this->SetTitle($title);
  28. $this->SetURL($url);
  29. $this->currentitem['level']=1;
  30. $this->currentitem['active']=false;
  31. $this->currentitem['active_on_partial']=false;
  32. if (!empty($sm['adminnavigation']['item_li_class']))
  33. $this->currentitem['container']['attrs']['class']=$sm['adminnavigation']['item_li_class'];
  34. if (!empty($sm['adminnavigation']['item_a_class']))
  35. {
  36. $this->AddClassname($sm['adminnavigation']['item_a_class']);
  37. }
  38. return $this;
  39. }
  40. protected function SetActiveItem($name)
  41. {
  42. $this->currentitem=&$this->nav['items'][$name];
  43. if (!isset($this->currentitem))
  44. {
  45. $this->currentitem['name']=$name;
  46. $this->currentitem['class']='';
  47. $this->currentitem['style']='';
  48. $this->currentitem['onclick']='';
  49. $this->currentitem['container']['attrs']['class']='';
  50. }
  51. return $this;
  52. }
  53. function SetURL($url, $name='')
  54. {
  55. if (!empty($name))
  56. $this->SetActiveItem($name);
  57. $this->currentitem['attrs']['href']=$url;
  58. return $this;
  59. }
  60. function SetTitle($title, $name='')
  61. {
  62. if (!empty($name))
  63. $this->SetActiveItem($name);
  64. $this->currentitem['title']=$title;
  65. return $this;
  66. }
  67. function SetImage($image, $name='')
  68. {
  69. if (!empty($name))
  70. $this->SetActiveItem($name);
  71. if (sm_strlen($image)>0 && sm_strpos($image, '://')===false && sm_strpos($image, '.')===false)
  72. $image.='.png';
  73. if (!empty($image) && sm_strpos($image, '/')===false)
  74. {
  75. if (!file_exists('themes/'.sm_current_theme().'/images/adminnavigation/'.$image))
  76. $image='themes/default/images/adminnavigation/'.$image;
  77. else
  78. $image='themes/'.sm_current_theme().'/images/adminnavigation/'.$image;
  79. }
  80. $this->currentitem['image']=$image;
  81. return $this;
  82. }
  83. function AddClassname($classname, $name='')
  84. {
  85. if (!empty($name))
  86. $this->SetActiveItem($name);
  87. $this->currentitem['class'].=(empty($this->currentitem['class'])?'':' ').$classname;
  88. return $this;
  89. }
  90. function AddClassnameGlobal($classname)
  91. {
  92. $this->nav['class'].=(empty($this->nav['class'])?'':' ').$classname;
  93. return $this;
  94. }
  95. function SetStyle($style, $name='')
  96. {
  97. if (!empty($name))
  98. $this->SetActiveItem($name);
  99. $this->currentitem['style']=$style;
  100. return $this;
  101. }
  102. function SetFA($fa_tag, $name='')
  103. {
  104. if (!empty($name))
  105. $this->SetActiveItem($name);
  106. $this->currentitem['fa']=$fa_tag;
  107. return $this;
  108. }
  109. function SetActive($name='')
  110. {
  111. if (!empty($name))
  112. $this->SetActiveItem($name);
  113. $this->currentitem['active']=true;
  114. $this->currentitem['container']['attrs']['class'].=(empty($this->currentitem['container']['attrs']['class'])?'':' ').' active';
  115. return $this;
  116. }
  117. function SetAutodetectionPartialMode($name='')
  118. {
  119. if (!empty($name))
  120. $this->SetActiveItem($name);
  121. $this->currentitem['active_on_partial']=true;
  122. return $this;
  123. }
  124. function OpenInNewWindow($name='')
  125. {
  126. if (!empty($name))
  127. $this->SetActiveItem($name);
  128. $this->currentitem['attrs']['target']='_blank';
  129. return $this;
  130. }
  131. function SetStyleGlobal($style)
  132. {
  133. $this->nav['style']=$style;
  134. return $this;
  135. }
  136. function AutoDetectActive()
  137. {
  138. global $sm;
  139. if (is_array($this->nav['items']))
  140. {
  141. $tmp_index=sm_strpos($sm['_s']['resource_url'], '/');
  142. $main_suburl=substr($sm['_s']['resource_url'], $tmp_index);
  143. foreach ($this->nav['items'] as $itemname=>&$itemparams)
  144. {
  145. if (
  146. (sm_strcmp($main_suburl.$itemparams['attrs']['href'], $sm['server']['REQUEST_URI'])==0
  147. ||
  148. sm_strcmp($main_suburl.$itemparams['attrs']['href'], $sm['server']['REQUEST_URI'].'index.php')==0)
  149. || (sm_is_index_page() && sm_strcmp($itemparams['attrs']['href'], $sm['s']['page']['scheme'].'://'.$sm['_s']['resource_url'])==0)
  150. )
  151. $this->SetActive($itemname);
  152. if (!$itemparams['active'] && $itemparams['active_on_partial'])
  153. {
  154. if (sm_strpos($sm['server']['REQUEST_URI'], $main_suburl.$itemparams['attrs']['href'])===0)
  155. $this->SetActive($itemname);
  156. }
  157. }
  158. }
  159. return $this;
  160. }
  161. function Count()
  162. {
  163. return sm_count($this->nav['items']);
  164. }
  165. function Output()
  166. {
  167. global $sm;
  168. if (is_array($this->nav['items']))
  169. {
  170. foreach ($this->nav['items'] as $itemname=>&$itemparams)
  171. {
  172. $itemparams['container']['attrs']['class'].=(empty($itemparams['container']['attrs']['class'])?'':' ').'anav-item';
  173. $itemparams['class'].=(empty($itemparams['class'])?'':' ').'anav-a';
  174. if ($itemparams['active'])
  175. {
  176. $itemparams['class'].=' anav-a-active';
  177. if (!empty($sm['adminnavigation']['item_a_class_active']))
  178. $itemparams['class'].=' '.$sm['adminnavigation']['item_a_class_active'];
  179. }
  180. $itemparams['html']=$itemparams['title'];
  181. if (!empty($itemparams['fa']))
  182. $itemparams['html']='<span class="anav-fa">'.FA::EmbedCodeFor($itemparams['fa']).'</span> '.$itemparams['html'];
  183. $itemparams['attrs']['style']=$itemparams['style'];
  184. $itemparams['attrs']['onclick']=$itemparams['onclick'];
  185. $itemparams['attrs']['class']=$itemparams['class'];
  186. }
  187. $items=Array();
  188. foreach ($this->nav['items'] as $itemname=>&$itemparams)
  189. {
  190. $items[]=$itemparams;
  191. }
  192. $this->nav['items']=$items;
  193. }
  194. $this->nav['count']=sm_count($this->nav['items']);
  195. return $this->nav;
  196. }
  197. }