AdminMenu.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecadmin\models;
  10. use Yii;
  11. use yii\db\ActiveRecord;
  12. use fec\helpers\CUrl;
  13. /**
  14. * @author Terry Zhao <2358269014@qq.com>
  15. * @since 1.0
  16. */
  17. class AdminMenu extends ActiveRecord
  18. {
  19. private $_admin_menu;
  20. private $_admin_menu_tree_arr;
  21. public $_ajaxMenuEditUrl;
  22. private $_active_menu_ids;
  23. public static function tableName()
  24. {
  25. return '{{%admin_menu}}';
  26. }
  27. public function rules()
  28. {
  29. $current_rules = [
  30. [['name', 'parent_id','url_key',], 'required'],
  31. ['sort_order','validateSortOrder'],
  32. ];
  33. return $current_rules;
  34. }
  35. public function validateSortOrder($attribute,$params){
  36. if($order = $this->sort_order){
  37. if(!is_numeric($order)){
  38. $this->addError($attribute,"sort_order must be numeric");
  39. }
  40. }
  41. }
  42. public function getAdminMenu(){
  43. if($this->_admin_menu === null ){
  44. $this->_admin_menu = new self;
  45. }
  46. return $this->_admin_menu;
  47. }
  48. public function getActiveMenuIds(){
  49. if($this->_active_menu_ids === null){
  50. $user = Yii::$app->user->identity;
  51. $user_id = $user['id'];
  52. $roles = AdminUserRole::find()->asArray()->where([
  53. 'user_id' => $user_id
  54. ])->all();
  55. $role_ids = [];
  56. if(!empty($roles)){
  57. foreach($roles as $one){
  58. $role_ids[] = $one['role_id'];
  59. }
  60. }
  61. //var_dump($user);exit;
  62. $menu_ids = [];
  63. if(!empty($role_ids)){
  64. $AdminRoleMenus = AdminRoleMenu::find()
  65. ->asArray()
  66. ->select(['menu_id'])
  67. ->where(['in','role_id',$role_ids])
  68. ->all();
  69. if(!empty($AdminRoleMenus)){
  70. foreach($AdminRoleMenus as $menu){
  71. $menu_ids[] = $menu['menu_id'];
  72. }
  73. }
  74. }
  75. $menu_ids = array_unique($menu_ids);
  76. $this->_active_menu_ids = $menu_ids;
  77. }
  78. return $this->_active_menu_ids;
  79. }
  80. # 得到后台显示菜单(左侧)
  81. public function getLeftMenuTreeHtml($treeArr='',$i=1){
  82. $active_menu_ids = $this->getActiveMenuIds();
  83. $str = '';
  84. if(!$treeArr){
  85. $treeArr = $this->getMenuTreeArray();
  86. }
  87. foreach($treeArr as $node){
  88. $name = $node["name"];
  89. $id = $node["id"];
  90. if(!in_array($id,$active_menu_ids)){
  91. continue;
  92. }
  93. $url_key = $node["url_key"];
  94. if($i == 1){
  95. $str .= '<div class="accordionHeader">
  96. <h2><span>Folder</span>'.$name .'</h2>
  97. </div>
  98. <div class="accordionContent">';
  99. if($this->hasChild($node)){
  100. $str .='<ul class="tree treeFolder">';
  101. $str .= $this->getLeftMenuTreeHtml($node['child'],$i+1);
  102. $str .='</ul>';
  103. }
  104. $str .= '</div>';
  105. }else{
  106. if($this->hasChild($node)){
  107. //$str .= '<li><a href="'.CUrl::getUrl($url_key).'" target="navTab" rel="page1">'.$name.'</a>';
  108. $str .= '<li><a href="javascript:void(0)" >'.$name.'</a>';
  109. $str .= '<ul>';
  110. $str .= $this->getLeftMenuTreeHtml($node['child'],$i+1);
  111. $str .= '</ul>';
  112. $str .= '</li>';
  113. }else{
  114. $str .='<li><a href="'.CUrl::getUrl($url_key).'" target="navTab" rel="page1">'.$name.'</a></li>';
  115. }
  116. }
  117. }
  118. return $str;
  119. }
  120. public function getRoloEditMenuTreeHtml($selected_menu_ids,$treeArr=''){
  121. $str ='';
  122. if(!$treeArr){
  123. $treeArr = $this->getMenuTreeArray();
  124. }
  125. foreach($treeArr as $node){
  126. $name = $node["name"];
  127. $id = $node["id"];
  128. $url_key = $node["url_key"];
  129. if($this->hasChild($node)){
  130. $str .= '<li><a '.( in_array($id,$selected_menu_ids) ? 'checked="true"' : '').' tvalue="'.$id.'" tname="name">'.$name.'</a>';
  131. $str .= '<ul>';
  132. $str .= $this->getRoloEditMenuTreeHtml($selected_menu_ids,$node['child']);
  133. $str .= '</ul>';
  134. $str .= '</li>';
  135. }else{
  136. $str .= '<li><a '.( in_array($id,$selected_menu_ids) ? 'checked="true"' : '').' tvalue="'.$id.'" tname="name">'.$name.'</a></li>';
  137. }
  138. }
  139. return $str;
  140. }
  141. # 得到menu的html
  142. public function getEditMenuTreeHtml($treeArr=''){
  143. $str ='';
  144. //$str = '<ul class="tree treeFolder">';
  145. if(!$treeArr){
  146. $treeArr = $this->getMenuTreeArray();
  147. }
  148. foreach($treeArr as $node){
  149. $name = $node["name"];
  150. $id = $node["id"];
  151. $url_key = $node["url_key"];
  152. if($this->hasChild($node)){
  153. $str .= '<li><a href="'.$this->_ajaxMenuEditUrl.'?id='.$id.'" target="ajax" rel="jbsxBox">'.$name.'</a>';
  154. $str .= '<ul>';
  155. $str .= $this->getEditMenuTreeHtml($node['child']);
  156. $str .= '</ul>';
  157. $str .= '</li>';
  158. }else{
  159. $url = CUrl::getUrl($url_key);
  160. $str .= '<li><a href="'.$this->_ajaxMenuEditUrl.'?id='.$id.'" target="ajax" rel="jbsxBox">'.$name.'</a></li>';
  161. }
  162. }
  163. //$str .= '</ul>';
  164. return $str;
  165. }
  166. public function hasChild($node){
  167. if(isset($node['child']) && !empty($node['child'])){
  168. return true;
  169. }
  170. return false;
  171. }
  172. # 得到tree的数组
  173. public function getMenuTreeArray(){
  174. $menu_tree_array = $this->getMenuTree();
  175. return $menu_tree_array;
  176. }
  177. public function getMenuTree($parent_id = 0){
  178. $menuArr = [];
  179. $parentArr = AdminMenu::find()->asArray()
  180. ->where(['parent_id' => $parent_id])
  181. ->orderBy(['sort_order' => SORT_DESC])
  182. ->all();
  183. if(is_array($parentArr) && !empty($parentArr)){
  184. foreach($parentArr as $node){
  185. $name = $node["name"];
  186. $id = $node["id"];
  187. $url_key = $node["url_key"];
  188. $menuArr[$id] = [
  189. 'id' => $id,
  190. 'name' => $name,
  191. 'url_key' => $url_key,
  192. ];
  193. $menuArr[$id]['child'] = $this->getMenuTree($id);
  194. }
  195. }
  196. return $menuArr;
  197. }
  198. }