· 8 years ago · Mar 29, 2018, 03:28 PM
1CREATE TABLE IF NOT EXISTS `sys_jstree` (
2`id` INT(11) NOT NULL,
3`texto` VARCHAR(100) NOT NULL,
4`tipo` VARCHAR(100) NOT NULL,
5`parent_id` VARCHAR(20) NOT NULL,
6`menu_id` INT(11) NOT NULL,
7PRIMARY KEY (`id`))
8ENGINE = InnoDB
9DEFAULT CHARSET = utf8;
10
11INSERT INTO `sys_jstree` (`id`, `texto`, `tipo`, `parent_id`, `menu_id`)
12VALUES
13(1, 'Dashboard', 'grupo', '#', 0),
14(2, 'Configuração', 'grupo', '#', 0),
15(3, 'Usuário', 'funcao', 2, 0),
16(4, 'Criar', 'acao', 3, 0),
17(5, 'Editar', 'acao', 3, 0),
18(6, 'Excluir', 'acao', 3, 0),
19(8, 'Visualizar', 'acao', 3, 0);
20
21// get_all_jstree
22public function get_all_jstree_funcoes()
23{
24 $jstree = $this->permissao->get_all_jstree_funcoes();
25 echo json_encode($jstree, JSON_NUMERIC_CHECK);
26}
27
28// get_all_jstree_funcoes
29public function get_all_jstree_funcoes()
30{
31 $this->db->select('id, texto text, tipo type, parent_id parent');
32 $this->db->from('sys_jstree');
33 $query = $this->db->get();
34 return array_map(function($item)
35 {
36 return $item;
37 },
38 $query->result_array());
39}
40
41CREATE TABLE IF NOT EXISTS `tb_nivel`(
42`id` INT(11) NOT NULL AUTO_INCREMENT,
43`nome` VARCHAR(80) NOT NULL,
44`grupo_id` INT(11) NOT NULL,
45PRIMARY KEY (`id`))
46ENGINE = InnoDB
47DEFAULT CHARSET = utf8;
48
49INSERT INTO `tb_nivel` (`id`,`nome`,`grupo_id`) VALUES
50(1,'Administrador',1),
51(2,'Usuário',2);
52
53CREATE TABLE IF NOT EXISTS `tb_jstree` (
54`id` INT(11) NOT NULL,
55`texto` VARCHAR(100) NOT NULL,
56`tipo` VARCHAR(100) NOT NULL,
57`acesso` INT(11) NOT NULL, -- BIT(1) NOT NULL DEFAULT 1,
58 `parent_id` VARCHAR(20) NOT NULL,
59`menu_id` INT(11) NOT NULL,
60`permissao_id` INT(11) NOT NULL,
61PRIMARY KEY (`id`))
62ENGINE = InnoDB
63DEFAULT CHARSET = utf8;
64
65
66INSERT INTO `tb_jstree` (`id`, `texto`, `tipo`, `acesso`, `parent_id`,
67menu_id`, `permissao_id`) VALUES
68(1, 'Dashboard', 'grupo', 1, '#', 1, 1),
69(2, 'Configuração', 'grupo', 0, '#', 2, 1),
70(3, 'Usuário', 'funcao', 0, 2, 3, 1),
71(4, 'Criar', 'acao', 1, 3, 4, 1),
72(5, 'Editar', 'acao', 1, 3, 59, 1),
73(6, 'Excluir', 'acao', 0, 3, 60, 1),
74(8, 'Visualizar', 'acao', 1, 3, 83, 1),
75(9, 'Dashboard', 'grupo', 1, '#', 1, 2),
76(10, 'Configuração', 'grupo', 0, '#', 2, 2),
77(11, 'Usuário', 'funcao', 0, 10, 1, 2),
78(12, 'Criar', 'acao', 0, 11, 4, 2),
79(13, 'Editar', 'acao', 0, 11, 59, 2),
80(14, 'Excluir', 'acao', 0, 11, 60, 2),
81(15, 'Visualizar', 'acao', 0, 11, 83, 2);
82
83// set
84public function set_objeto()
85{
86 $objeto = $_POST['objeto'];
87 $js_tree = $_POST['jstree'];
88 $objeto_decode = json_decode($objeto);
89 $jstree_decode = json_decode($js_tree);
90 $id = $objeto_decode->id;
91
92 /*** lança o objeto ***/
93 foreach($jstree_decode as $key => $value)
94 {
95 $jstree_decode[$key] = (array) $value;
96 }
97 /*** exibe o resultado ***/
98 //print_r( $jstree_decode );
99
100 if ($id == 0)
101 {
102 $permissao_id = $this->permissao->add($objeto_decode);
103
104 $this->permissao->add_jstree($jstree_decode, $permissao_id);
105 }
106 else
107 {
108 $permissao_id = $objeto_decode->id;
109 $this->permissao->edit(array('id' => $permissao_id), $objeto_decode);
110
111 $this->permissao->edit_jstree($jstree_decode);
112 }
113
114 $objeto_encode = json_encode($objeto_decode);
115 echo ($objeto_encode);
116}
117
118// add
119public function add($dados)
120{
121 $permissao = [
122 'nome' => $dados->nome,
123 'grupo_id' => $dados->grupo_id,
124 ];
125 $permissao_id = $this->db->insert($this->tabela, $permissao);
126 return $permissao_id;
127}
128// add_jstree
129public function add_jstree($dados, $permissao_id)
130{
131 print_r($dados);
132 $array = (array) $dados;
133 foreach ($dados as $js)
134 {
135 $jstree = [
136 'acesso' => $js['state']->selected,
137 'permissao_id' => $permissao_id,
138 ];
139 $jstree_id = $this->db->insert('tb_jstree', $permissao);
140 return $jstree_id;
141 }
142}