<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Install extends Admin_Controller
{
    private $tabelas_sistema = array(
        'nwl_campanha_email',
        'nwl_config_campanha',
        'nwl_config_listas',
        'nwl_config_tb_rel',
        'nwl_config_views',
        'nwl_desinscricao',
        'nwl_emails',
        'nwl_lista_custom',
        'nwl_local_cadastro',
        'nwl_log_emails',
        'nwl_log_error',
        'nwl_notifications',
        'nwl_arquivo' 
             ); 
             
    //'nwl_desinscricao','nwl_local_cadastro', 'nwl_log_error',
             
     //falta incluir tabela  nwl_arquivo no sql     
    
    function __construct() 
    {
        parent::__construct();
    }
     
    public function index()
    {
        //Voltar Bk
        /*$tabelas_sistema = $this->tabelas_sistema;
        $this->load->dbforge();
        foreach($tabelas_sistema as $tb)
        {
            $cont = 1;
            $new_name = 'bk_'.$cont.'_'.$tb;
            if ($this->db->table_exists($new_name) )
            {
                $this->dbforge->rename_table($new_name, $tb);
            }
        }
        die();*/
        
         
        if($this->session->perfil != 1)
        die('');
        
        $module = @$this->db->where('name', 'NewsLetter')->where('nickname', 'newsletter')->get('sys_modules')->row();
        if($module == null)
        {
            if(!is_dir(FCPATH.'img')){
                mkdir(FCPATH.'img');
            }
            
            //$this->cria_db();
            $sections = $this->cria_modulos_sessoes();
            $this->set_controle_acesso($sections);
        }
        redirect(base_url().'newsletter/painel/config_campanha/edit/1');
        
    }
    
    private function cria_db()
    {
        $tabelas_sistema = $this->tabelas_sistema;
             
        $this->load->dbforge();
         foreach($tabelas_sistema as $tb)
         {
             if ($this->db->table_exists($tb) )
             {
                $cont = 1;
                $new_name = 'bk_'.$cont.'_'.$tb;
                while($this->db->table_exists($new_name))
                {
                    $cont++;
                    $new_name = 'bk_'.$cont.'_'.$tb;
                } 
                $this->dbforge->rename_table($tb, $new_name);
             }
         }
         $this->sql_db();
         
    }
  
   
// actions  
//1 Adicionar,2 Editar,3 Deletar ,4 exportar,5 imprimir,6 visualizar
//session   
//'id_module','location','name','icon','type','class','method','visible','active','priority','section_actions','subsections'    
//subsession
//'id_module', 'id_section', 'name', 'priority', 'class', 'method', 'visible', 'active','subsection_actions'


    private function cria_modulos_sessoes()
    {
        $sql = "INSERT INTO `sys_modules` (`name`, `nickname`, `icon`, `visible`, `active`, `type`, `priority`) 
         VALUES ('NewsLetter', 'newsletter', 'fa fa-calendar', 1, 1, 2, 2);";
        if($this->db->query($sql))
        {
            $id_module = $this->db->insert_id();
            
            $sessoes_ini = array(
                
                $this->montaSession(array($id_module, 2, 'Enviar', 'fa fa-envelope-o', 1, 'painel', 'enviar_email', 1, 1, 0, array(6), array())),
                
                $this->montaSession(array($id_module, 2, 'Arquivo', 'fa fa-calendar', 1, 'painel', 'arquivo', 1, 1, 1, array(1,2,6), array())),
                
                $this->montaSession(array($id_module, 2, 'Campanhas', 'fa fa-calendar', 1, 'painel', 'campanhas', 1, 1, 2, array(1,2,6), array())),
                
                $this->montaSession(array($id_module, 2, 'Configurações', 'fa fa-cogs', 2, '', '', 1, 1, 3, array(),array(
                        $this->montaSubSession(array($id_module, 0, 'Envio', 0, 'painel', 'config_campanha', 1, 1, array(2))),
                        $this->montaSubSession(array($id_module, 0, 'Tabelas', 0, 'painel', 'config_tabelas', 1, 1, array(1,2,3))),
                        $this->montaSubSession(array($id_module, 0, 'Views', 0, 'painel', 'config_views', 1, 1, array(1,2))),
                        $this->montaSubSession(array($id_module, 0, 'Listas', 0, 'painel', 'config_listas', 1, 1, array(1,2,3,6))),
                    ))),
                    
                );
            
            $sections = array();
            foreach($sessoes_ini as $sessao_ini)
            {
                $section_actions = $sessao_ini['section_actions'];
                $subsections_ini = $sessao_ini['subsections'];
                unset($sessao_ini['section_actions']);
                unset($sessao_ini['subsections']);
                
                $this->db->insert('sys_module_sections',$sessao_ini);
                $id_section = $this->db->insert_id();
                
                foreach($section_actions as $section_action){
                    $this->db->query("INSERT INTO `sys_section_action` (`id_section`, `id_action`) VALUES($id_section, $section_action)");
                }
                
                $subsections = array();
                foreach($subsections_ini as $subsessao_ini)
                {
                    $subsessao_ini['id_section'] = $id_section;
                    $subsection_actions = $subsessao_ini['subsection_actions'];
                    unset($subsessao_ini['subsection_actions']);
                    
                    $this->db->insert('sys_module_subsections',$subsessao_ini);
                    $id_subsection = $this->db->insert_id(); 
                    
                    foreach($subsection_actions as $subsection_action){
                        $this->db->query("INSERT INTO `sys_subsection_action` (`id_subsection`, `id_action`) VALUES ($id_subsection, $subsection_action)");
                    }
                    
                    $subsections[$id_subsection] = $subsessao_ini;
                    $subsections[$id_subsection]['subsection_actions'] = $subsection_actions;
                }
                
                $sections[$id_section] = $sessao_ini;
                $sections[$id_section]['section_actions'] = $section_actions;
                $sections[$id_section]['subsections'] = $subsections;
            }
            return $sections;
        }
    }
    
    private function montaSession($array)
    {
        $estrutura = array('id_module','location','name','icon','type','class','method','visible','active','priority','section_actions','subsections');
        $cont = 0;
        $new = array();
        foreach($estrutura as $campo){
            $new[$campo] = $array[$cont];
            $cont++;
        }
        return $new;
    }
    
    private function montaSubSession($array)
    {
        $estrutura = array('id_module', 'id_section', 'name', 'priority', 'class', 'method', 'visible', 'active','subsection_actions');
        $cont = 0;
        $new = array();
        foreach($estrutura as $campo){
            $new[$campo] = $array[$cont];
            $cont++;
        }
        return $new;
    }
    
    private function set_controle_acesso($sections)
    {
        foreach($sections as $id_section=>$section)
        {
            if($section['type'] == 1)
            {
                foreach($section['section_actions'] as $id_action)
                {
                     $data = array(
                         'id_perfil' => 1,
                         'id_module' => $section['id_module'],
                         'id_section' => $id_section,
                         'id_subsection' => null,
                         'id_action' => $id_action
                         );
                    $this->db->insert('sys_rel_perfil',$data);
                    if($section['name'] == 'Arquivo' || $section['name'] == 'Campanhas' || $section['name'] == 'Enviar')
                    {
                       $data['id_perfil'] = 2;
                       $this->db->insert('sys_rel_perfil',$data);
                    }
                 }
             }
             else
             {
                foreach($section['subsections'] as $id_subsection=>$subsection)
                {
                    foreach($subsection['subsection_actions'] as $id_action)
                    {
                        $data = array(
                            'id_perfil' => 1,
                            'id_module' => $section['id_module'],
                            'id_section' => $id_section,
                            'id_subsection' => $id_subsection,
                            'id_action' => $id_action
                            );
                        $this->db->insert('sys_rel_perfil',$data);
                    }
                }
             }
        }
    }
    
    private function sql_db()
    {
    
        $this->db->query("
            
            --
-- Estrutura para tabela `nwl_arquivo`
--

CREATE TABLE IF NOT EXISTS `nwl_arquivo` (
  `id_arquivo` int(11) NOT NULL,
  `nome` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `tipo` enum('Interno','Externo') COLLATE utf8_unicode_ci NOT NULL,
  `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `status` int(11) NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; "); $this->db->query(" 

-- --------------------------------------------------------

--
-- Estrutura para tabela `nwl_campanha_email`
--

CREATE TABLE IF NOT EXISTS `nwl_campanha_email` (
  `id_campanha` int(11) NOT NULL,
  `nome` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `campanha` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `subject` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `mensagem` text COLLATE utf8_unicode_ci NOT NULL,
  `view_html` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `link_destino` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `enviados` int(11) NOT NULL,
  `abertos` int(11) NOT NULL,
  `clicados` int(11) NOT NULL,
  `falhou` int(11) NOT NULL,
  `criado_em` datetime NOT NULL,
  `clicou_site` int(11) NOT NULL,
  `tipo` enum('Interno','Externo') CHARACTER SET utf8 NOT NULL DEFAULT 'Interno',
  `envio` enum('Unico','Multiplo') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Multiplo',
  `config_smtp` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; "); $this->db->query(" 

--
-- Fazendo dump de dados para tabela `nwl_campanha_email`
--

INSERT INTO `nwl_campanha_email` (`id_campanha`, `nome`, `campanha`, `subject`, `mensagem`, `view_html`, `link_destino`, `enviados`, `abertos`, `clicados`, `falhou`, `criado_em`, `clicou_site`, `tipo`, `envio`, `config_smtp`) VALUES
(2, 'Teste', 'Primeira campanha', 'Primeira camampanha', '<p>\r\n	Msg de terste</p>\r\n', '', '', 0, 0, 0, 0, '0000-00-00 00:00:00', 0, 'Interno', 'Unico', 1),
(1, 'Padrão', 'Default', 'Default', '<p>Default</p>', '', '', 0, 0, 0, 0, '0000-00-00 00:00:00', 0, 'Interno', 'Multiplo', 0); "); $this->db->query(" 

-- --------------------------------------------------------

--
-- Estrutura para tabela `nwl_config_campanha`
--

CREATE TABLE IF NOT EXISTS `nwl_config_campanha` (
  `id` int(11) NOT NULL,
  `from_email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `from_titulo` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `base_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `redirect_desiscricao` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `img_cont_view` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `img_logo_email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `cor_principal` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `smtp_host` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `smtp_user` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `smtp_pass` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `smtp_port` varchar(255) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; "); $this->db->query(" 

--
-- Fazendo dump de dados para tabela `nwl_config_campanha`
--

INSERT INTO `nwl_config_campanha` (`id`, `from_email`, `from_titulo`, `base_url`, `redirect_desiscricao`, `img_cont_view`, `img_logo_email`, `cor_principal`, `smtp_host`, `smtp_user`, `smtp_pass`, `smtp_port`) VALUES
(1, 'contato@viewbrasil.com.br', 'View Brasil', 'http://viewbrasil.com.br/', 'http://viewbrasil.com.br/viewbrasil/desinscricao', '6c8df-img_view.jpg', '31f6f-logo_emkt.png', '#2b90d9', 'email-smtp.us-west-2.amazonaws.com', 'AKIAJQFMVPSXBQWOUQ3Q', 'Asvbq6GWZOeMalQyww05H5bfjvn/ewOUMRhX7HwNlewv', '587'); "); $this->db->query(" 

-- --------------------------------------------------------

--
-- Estrutura para tabela `nwl_config_listas`
--

CREATE TABLE IF NOT EXISTS `nwl_config_listas` (
  `id` int(11) NOT NULL,
  `tabela` varchar(100) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; "); $this->db->query(" 

-- --------------------------------------------------------

--
-- Estrutura para tabela `nwl_config_tb_rel`
--

CREATE TABLE IF NOT EXISTS `nwl_config_tb_rel` (
  `id` int(11) NOT NULL,
  `nome` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `tabela` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `campo` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `tipo` enum('Interno','Externo') COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; "); $this->db->query(" 

--
-- Fazendo dump de dados para tabela `nwl_config_tb_rel`
--

INSERT INTO `nwl_config_tb_rel` (`id`, `nome`, `tabela`, `campo`, `tipo`) VALUES
(1, 'Usuarios', 'sys_users', 'email', 'Interno'),
(2, 'NewsLetter', 'nwl_emails', 'email', 'Interno'); "); $this->db->query(" 

-- --------------------------------------------------------

--
-- Estrutura para tabela `nwl_config_views`
--

CREATE TABLE IF NOT EXISTS `nwl_config_views` (
  `id` int(11) NOT NULL,
  `nome` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `view` varchar(255) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; "); $this->db->query(" 

-- --------------------------------------------------------

--
-- Estrutura para tabela `nwl_emails`
--

CREATE TABLE IF NOT EXISTS `nwl_emails` (
  `id` int(11) NOT NULL,
  `id_anunciante` int(11) NOT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `nome` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `sobrenome` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `sexo` tinyint(4) NOT NULL,
  `origem` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `status` int(11) NOT NULL,
  `mailchimp` int(11) NOT NULL,
  `activation_key` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `criado_em` datetime NOT NULL,
  `ativado_em` datetime NOT NULL,
  `desativado_em` datetime NOT NULL,
  `conf_1` int(11) NOT NULL,
  `abriu_conf_1` datetime NOT NULL,
  `conf_2` int(11) NOT NULL,
  `abriu_conf_2` datetime NOT NULL,
  `reenvio` int(11) NOT NULL,
  `clicou_maps` int(11) NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=15053 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; "); $this->db->query(" 

-- --------------------------------------------------------

--
-- Estrutura para tabela `nwl_lista_custom`
--

CREATE TABLE IF NOT EXISTS `nwl_lista_custom` (
  `id` int(11) NOT NULL,
  `nome` varchar(255) NOT NULL,
  `sql` text NOT NULL,
  `campo_email` varchar(255) NOT NULL,
  `tipo` enum('Interno','Externo') CHARACTER SET utf8 NOT NULL DEFAULT 'Interno'
) ENGINE=InnoDB DEFAULT CHARSET=latin1; "); $this->db->query(" 

-- --------------------------------------------------------

--
-- Estrutura para tabela `nwl_log_emails`
--

CREATE TABLE IF NOT EXISTS `nwl_log_emails` (
  `id` int(11) NOT NULL,
  `key_log` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `id_arquivo` int(11) NOT NULL,
  `id_campanha` int(11) NOT NULL,
  `enviado` int(11) NOT NULL,
  `abriu` int(11) NOT NULL,
  `clicou` int(11) NOT NULL,
  `clicou_site` int(11) NOT NULL,
  `historico_clique` text COLLATE utf8_unicode_ci NOT NULL,
  `enviado_em` datetime NOT NULL,
  `abriu_em` datetime NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=24910 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; "); $this->db->query(" 

-- --------------------------------------------------------

--
-- Estrutura para tabela `nwl_notifications`
--

CREATE TABLE IF NOT EXISTS `nwl_notifications` (
  `id` int(11) NOT NULL,
  `notificationType` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `emailAddress` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `action` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `status` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `diagnosticCode` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM AUTO_INCREMENT=5193 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; "); $this->db->query(" 

--
-- Índices de tabelas apagadas
--

--
-- Índices de tabela `nwl_arquivo`
--
ALTER TABLE `nwl_arquivo`
  ADD PRIMARY KEY (`id_arquivo`); "); $this->db->query(" 

--
-- Índices de tabela `nwl_campanha_email`
--
ALTER TABLE `nwl_campanha_email`
  ADD PRIMARY KEY (`id_campanha`); "); $this->db->query(" 

--
-- Índices de tabela `nwl_config_campanha`
--
ALTER TABLE `nwl_config_campanha`
  ADD PRIMARY KEY (`id`); "); $this->db->query(" 

--
-- Índices de tabela `nwl_config_listas`
--
ALTER TABLE `nwl_config_listas`
  ADD PRIMARY KEY (`id`); "); $this->db->query(" 

--
-- Índices de tabela `nwl_config_tb_rel`
--
ALTER TABLE `nwl_config_tb_rel`
  ADD PRIMARY KEY (`id`); "); $this->db->query(" 

--
-- Índices de tabela `nwl_config_views`
--
ALTER TABLE `nwl_config_views`
  ADD PRIMARY KEY (`id`); "); $this->db->query(" 

--
-- Índices de tabela `nwl_emails`
--
ALTER TABLE `nwl_emails`
  ADD PRIMARY KEY (`id`); "); $this->db->query(" 

--
-- Índices de tabela `nwl_lista_custom`
--
ALTER TABLE `nwl_lista_custom`
  ADD PRIMARY KEY (`id`); "); $this->db->query(" 

--
-- Índices de tabela `nwl_log_emails`
--
ALTER TABLE `nwl_log_emails`
  ADD PRIMARY KEY (`id`); "); $this->db->query(" 

--
-- Índices de tabela `nwl_notifications`
--
ALTER TABLE `nwl_notifications`
  ADD PRIMARY KEY (`id`); "); $this->db->query(" 

--
-- AUTO_INCREMENT de tabelas apagadas
--

--
-- AUTO_INCREMENT de tabela `nwl_arquivo`
--
ALTER TABLE `nwl_arquivo`
  MODIFY `id_arquivo` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=6; "); $this->db->query(" 
--
-- AUTO_INCREMENT de tabela `nwl_campanha_email`
--
ALTER TABLE `nwl_campanha_email`
  MODIFY `id_campanha` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=15; "); $this->db->query(" 
--
-- AUTO_INCREMENT de tabela `nwl_config_campanha`
--
ALTER TABLE `nwl_config_campanha`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3; "); $this->db->query(" 
--
-- AUTO_INCREMENT de tabela `nwl_config_listas`
--
ALTER TABLE `nwl_config_listas`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; "); $this->db->query(" 
--
-- AUTO_INCREMENT de tabela `nwl_config_tb_rel`
--
ALTER TABLE `nwl_config_tb_rel`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=5; "); $this->db->query(" 
--
-- AUTO_INCREMENT de tabela `nwl_config_views`
--
ALTER TABLE `nwl_config_views`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; "); $this->db->query(" 
--
-- AUTO_INCREMENT de tabela `nwl_emails`
--
ALTER TABLE `nwl_emails`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=15053; "); $this->db->query(" 
--
-- AUTO_INCREMENT de tabela `nwl_lista_custom`
--
ALTER TABLE `nwl_lista_custom`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; "); $this->db->query(" 
--
-- AUTO_INCREMENT de tabela `nwl_log_emails`
--
ALTER TABLE `nwl_log_emails`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=24910; "); $this->db->query(" 
--
-- AUTO_INCREMENT de tabela `nwl_notifications`
--
ALTER TABLE `nwl_notifications`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=5193; ");
    
    }
    
    
    
    private function cria_modulos_sessoes_bk()
    {
        $sql = "INSERT INTO `sys_modules` (`name`, `nickname`, `icon`, `visible`, `active`, `type`, `priority`) 
         VALUES ('NewsLetter', 'newsletter', 'fa fa-calendar', 1, 1, 2, 2);";
         if($this->db->query($sql))
         {
            $id_module = $this->db->insert_id();
            
            $sqlP1 =  "INSERT INTO `sys_module_sections` (`id_module`, `location`, `name`, `icon`, `type`, `class`, `method`, `visible`, `active`, `priority`) ";
            $sqlP2s = array(
                "VALUES ($id_module, 2, 'Configurações', 'fa fa-cogs', 2, '', '', 1, 1, 0)",
                "VALUES ($id_module, 2, 'Campanhas', 'fa fa-calendar', 1, 'painel', 'campanhas', 1, 1, 0)",
                "VALUES ($id_module, 2, 'Enviar', 'fa fa-envelope-o', 1, 'painel', 'enviar_email', 1, 1, 0)",
                );
            
            $section_actions = array(
                array(),
                array(1,2),
                array(6),
                );
                
            $id_sections = array();
            foreach($sqlP2s as $key=>$sqlP2)
            {
                $this->db->query($sqlP1.$sqlP2);
                $id_sections[] = $this->db->insert_id();
            }
            
            foreach($id_sections as $key=>$id_section)
            {
                foreach($section_actions[$key] as $section_action)
                {
                    $this->db->query("INSERT INTO `sys_section_action` (`id_section`, `id_action`) VALUES($id_section, $section_action)");
                    
                }
            }
            
            $sqlP1 =  "INSERT INTO `sys_module_subsections` (`id_module`, `id_section`, `name`, `priority`, `class`, `method`, `visible`, `active`) ";
            $sqlP2s = array(
                "VALUES ($id_module, ".$id_sections[0].", 'Email', 0, 'painel', 'config_campanha', 1, 1)",
                "VALUES ($id_module, ".$id_sections[0].", 'Tabelas', 0, 'painel', 'config_tabelas', 1, 1)",
                "VALUES ($id_module, ".$id_sections[0].", 'Views', 0, 'painel', 'config_views', 1, 1)",
                "VALUES ($id_module, ".$id_sections[0].", 'Listas', 0, 'painel', 'config_listas', 1, 1)",
                );
                
            $subsection_actions = array(
                array(2),
                array(1,2,3),
                array(1,2),
                array(1,2,3,6)
                );
            
            $id_subsections = array();
            foreach($sqlP2s as $sqlP2)
            {
                $this->db->query($sqlP1.$sqlP2);
                $id_subsections[] = $this->db->insert_id();
            }
                
            foreach($id_subsections as $key=>$id_subsection)
            {
                foreach($subsection_actions[$key] as $subsection_action)
                {
                    $this->db->query("INSERT INTO `sys_subsection_action` (`id_subsection`, `id_action`) VALUES ($id_subsection, $subsection_action)");
                }
                
            }
        }
    }
    
}