/**
  * load widget
  *
  * @name    widget
  * @param   array $args - widget structure
  * @param   array $instance - widget data
  * @return  void
  */
 function widget($args, $instance)
 {
     global $wpdb;
     // show tags
     print $args['before_widget'];
     if (!empty($instance['title'])) {
         print $args['before_head'];
         print $args['before_title'] . $instance['title'] . $args['after_title'];
         print $args['after_head'];
     }
     // print $args[ 'before_body' ];
     // $termos = get_terms('setorial');
     // var_dump($termos);
     if (function_exists('get_setoriais')) {
         $setoriais = get_setoriais();
         $base = site_url('setorial/');
         echo "<ul class='menu menu-setorial'>";
         foreach ($setoriais as $key => $setorial) {
             echo "<li><i class='fa fa-angle-right'></i><a href='{$base}{$key}'>{$setorial}</a></li>";
         }
         echo "</ul>";
     }
     // print $args[ 'after_body' ];
     print $args['after_widget'];
 }
 static function create_foruns()
 {
     //rodar uma vez só
     if (get_option('foruns_inicializados') !== false) {
         return;
     }
     update_option('foruns_inicializados', true);
     $estados = get_all_states();
     $setoriais = get_setoriais();
     foreach ($estados as $uf => $estado) {
         wp_insert_term($estado, 'estado', array('slug' => $uf));
     }
     foreach ($setoriais as $slug => $setorial) {
         wp_insert_term($setorial, 'setorial', array('slug' => $slug));
     }
     foreach ($estados as $uf => $estado) {
         foreach ($setoriais as $slug => $setorial) {
             $post = array('post_title' => $uf . ' - ' . $setorial, 'post_name' => $uf . '-' . $slug, 'post_type' => 'foruns', 'post_status' => 'publish');
             $new_post = wp_insert_post($post);
             wp_set_object_terms($new_post, $uf, 'estado');
             wp_set_object_terms($new_post, $slug, 'setorial');
         }
     }
 }
function resumo_setorial_estado_page_callback_function()
{
    // setorial, uf, qtd_candidatos, qtd_eleitores, qtd_votantes)
    if (!current_user_can('curate')) {
        return false;
    }
    $data[] = ['setorial', 'uf', 'qtd_candidatos', 'qtd_eleitores', 'qtd_votantes'];
    $uf_selected = $_GET['uf'];
    $setorial_selected = $_GET['setorial'];
    $confirm = $_GET['confirm'];
    $states = get_all_states();
    $setoriais = get_setoriais();
    if (!in_array($uf_selected, array_keys($states))) {
        if ($uf_selected != 'todos') {
            $uf_selected = '';
        }
    } else {
        $data[] = 'uf';
    }
    if (!in_array($setorial_selected, array_keys($setoriais))) {
        if ($setorial_selected != 'todos') {
            $setorial_selected = '';
        }
    } else {
        $data[] = 'setorial';
    }
    ?>
    <div class="wrap span-20">

    <h2>Resumo das setoriais e estados</h2>
    
    <h4>Selecione a UF:</h4>
    <select class="select-state-v" id="filtrar_uf">
      <option></option>
      <?php 
    foreach ($states as $uf_item => $state_item) {
        ?>
      <option value="<?php 
        echo $uf_item;
        ?>
" <?php 
        if ($uf_item == $uf_selected) {
            echo "selected";
        }
        ?>
><?php 
        echo $state_item;
        ?>
</option>
<?php 
    }
    ?>
      <option value="todos" <?php 
    if ($uf_selected == 'todos') {
        echo "selected";
    }
    ?>
>TODOS</option>
    </select>
      
    <h4>Selecione a Setorial:</h4>
    <select class="select-setorial-v" id="filtrar_setorial">
      <option></option>
      <?php 
    foreach ($setoriais as $slug => $setorial_item) {
        ?>
      <option value="<?php 
        echo $slug;
        ?>
" <?php 
        if ($slug == $setorial_selected) {
            echo "selected";
        }
        ?>
><?php 
        echo $setorial_item;
        ?>
</option>
      <?php 
    }
    ?>
      <option value="todos" <?php 
    if ($setorial_selected == 'todos') {
        echo "selected";
    }
    ?>
>TODOS</option>      
    </select>
      <br/>
      <input type="button" value="buscar" id="resumo_setorial_estado" class="filtrar_relatorio">
      <br/><br/>
      
<?php 
    if ($uf_selected != '' || $setorial_selected != '') {
        if ($uf_selected == 'todos') {
            $uf_selected = '';
        }
        if ($setorial_selected == 'todos') {
            $setorial_selected = '';
        }
        $nome_relatorio = "";
        if ($uf_selected && $setorial_selected) {
            $nome_relatorio = "resumo-{$uf_selected}-{$setorial_selected}";
        } else {
            if ($uf_selected && !$setorial_selected) {
                $nome_relatorio = "resumo-resumo-{$uf_selected}";
            } else {
                if (!$uf_selected && $setorial_selected) {
                    $nome_relatorio = "resumo-{$setorial_selected}";
                } else {
                    $nome_relatorio = "resumo-nacional";
                }
            }
        }
        // testa arquivo
        $filename = $nome_relatorio . ".csv";
        $file_web = get_template_directory_uri() . '/relatorios/arquivos/' . $filename;
        $file_path = get_template_directory() . '/relatorios/arquivos/' . $filename;
        clearstatcache();
        if (file_exists($file_path)) {
            echo "<strong>Já existe um relatório disponível para essa consulta:</strong> <br/>";
            echo "<a href='{$file_web}'>{$filename}</a>";
            if (!$confirm) {
                echo "<br/><br/>";
                echo "<h3><a href='" . basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING'] . '&confirm=1' . "'>desejo gerar uma nova consulta</a></h3>";
            }
        }
        if ($confirm == 1 || !file_exists($file_path)) {
            if ($uf_selected == '' && $setorial_selected == '') {
                /* listagem TODOS */
                foreach ($states as $uf => $state_item) {
                    foreach ($setoriais as $setorial => $setorial_item) {
                        $qtd_candidatos = get_count_candidates($uf, $setorial, true);
                        $qtd_eleitores = get_count_users_setorial_uf($uf, $setorial);
                        $qtd_votantes = get_votos_estado_setorial($uf, $setorial);
                        $data[] = [$setoriais[$setorial], $states[$uf], $qtd_candidatos, $qtd_eleitores, $qtd_votantes];
                    }
                }
            } else {
                if ($uf_selected != '' && $setorial_selected == '') {
                    // relatorio por só por uf
                    foreach ($setoriais as $setorial => $setorial_item) {
                        $qtd_candidatos = get_count_candidates($uf, $setorial, true);
                        $qtd_eleitores = get_count_users_setorial_uf($uf, $setorial);
                        $qtd_votantes = get_votos_estado_setorial($uf, $setorial);
                        $data[] = [$setorial_item, $states[$uf_selected], $qtd_candidatos, $qtd_eleitores, $qtd_votantes];
                    }
                } else {
                    if ($uf_selected == '' && $setorial_selected != '') {
                        // relatorio só por setorial
                        foreach ($states as $uf => $state_item) {
                            $qtd_candidatos = get_count_candidates($uf, $setorial, true);
                            $qtd_eleitores = get_count_users_setorial_uf($uf, $setorial);
                            $qtd_votantes = get_votos_estado_setorial($uf, $setorial);
                            $data[] = [$setoriais[$setorial_selected], $state_item, $qtd_candidatos, $qtd_eleitores, $qtd_votantes];
                        }
                    } else {
                        if ($uf_selected != '' && $setorial_selected != '') {
                            // relatorio por uf e setorial
                            $qtd_candidatos = get_count_candidates($uf, $setorial, true);
                            $qtd_eleitores = get_count_users_setorial_uf($uf, $setorial);
                            $qtd_votantes = get_votos_estado_setorial($uf, $setorial);
                            $data[] = [$setoriais[$setorial_selected], $states[$uf_selected], $qtd_candidatos, $qtd_eleitores, $qtd_votantes];
                        }
                    }
                }
            }
            ?>
     
          <iframe id="iframeExportar" frameborder="0" src="<?php 
            echo get_template_directory_uri();
            ?>
/baixar-csv.php" data_filename='<?php 
            echo $nome_relatorio;
            ?>
' data_csv='<?php 
            echo json_encode($data);
            ?>
' data_output_path='arquivo'>
      </div>    
<?php 
        }
    }
}
function get_count_candidates_setoriais_afrodesc_by_uf($uf)
{
    global $wpdb;
    if (empty($uf)) {
        return false;
    }
    $setorais = get_setoriais();
    $count = array();
    $results = array();
    foreach ($setorais as $key => $setorial) {
        $count[$key] = $wpdb->get_results($wpdb->prepare("SELECT COUNT(u.umeta_id) as count," . "mm.meta_value AS race " . "FROM {$wpdb->posts} as p " . "INNER JOIN {$wpdb->postmeta} as m ON p.ID = m.post_id " . "INNER JOIN {$wpdb->usermeta} as u ON p.post_author = u.user_id " . "INNER JOIN {$wpdb->postmeta} as mm ON p.ID = mm.post_id " . "INNER JOIN {$wpdb->usermeta} as uu ON p.post_author = uu.user_id " . "WHERE p.post_type = 'projetos' " . "AND m.meta_key = 'subscription-valid' " . "AND u.meta_key = 'setorial' AND u.meta_value = %s " . "AND mm.meta_key = 'candidate-race'" . "AND uu.meta_key = 'uf' AND uu.meta_value = %s" . "GROUP BY race", $key, $uf));
        if (!empty($count[$key])) {
            foreach ($count[$key] as $item) {
                if ($item->race == 'true') {
                    $results[$key]['afro'] = $item->count;
                } else {
                    $results[$key]['outros'] = $item->count;
                }
            }
        }
    }
    return $results;
}
/**
 * show a menu dropdown from the setorial
 *
 * @name    dropdown_states
 * @return  string
 */
function dropdown_setoriais($name, $selected = null, $all = false, $extra = null)
{
    $setoriais = get_setoriais();
    $output = "<select id='{$name}' name='{$name}' {$extra}>";
    if ($all) {
        $output .= "<option value=''>----------</option>";
    }
    foreach ($setoriais as $acronym => $setorial) {
        $att = '';
        if ($acronym == $selected) {
            $att = 'selected="selected"';
        }
        $output .= "<option value='{$acronym}' {$att}>{$setorial}</option>";
    }
    $output .= "</select>";
    return $output;
}
function get_number_of_votes_setorial_race_by_uf($uf)
{
    global $wpdb;
    $setorais = get_setoriais();
    $count = array();
    $results = array();
    foreach ($setorais as $key => $setorial) {
        $count[$key] = $wpdb->get_results($wpdb->prepare("SELECT COUNT(u.umeta_id) as count, pm.meta_value as race " . "FROM {$wpdb->usermeta} as u " . "INNER JOIN {$wpdb->posts} as p ON p.post_author = u.user_id " . "INNER JOIN {$wpdb->usermeta} as uu ON u.user_id = uu.user_id " . "INNER JOIN {$wpdb->usermeta} as uuu ON u.user_id = uuu.user_id " . "INNER JOIN {$wpdb->postmeta} as pm ON p.ID = pm.post_id " . "WHERE u.meta_key = 'vote-project-id' " . "AND uu.meta_key = 'setorial' AND uu.meta_value = %s " . "AND uuu.meta_key = 'uf' AND uuu.meta_value = %s " . "AND pm.meta_key = 'candidate-race' " . "GROUP BY race ", $key, $uf));
        if (!empty($count[$key])) {
            foreach ($count[$key] as $item) {
                if ($item->race == 'true') {
                    $results[$key]['afro'] = $item->count;
                } else {
                    $results[$key]['outros'] = $item->count;
                }
            }
        }
        return $results;
    }
}
<div id="mapa">
	
	<?php 
include get_template_directory() . '/assets/mapa_do_brasil.svg';
?>

    <?php 
if (function_exists('get_setoriais')) {
    $setoriais = get_setoriais();
    ?>

        <div class="menu-setoriais-container">
            <div class="menu-head">
                <h2 class="menu-title">Setoriais</h2>
            </div>
            <ul id="menu-setoriais" class="menu">
                <?php 
    foreach ($setoriais as $key => $setorial) {
        ?>
                    <li><i class="fa fa-angle-right"></i><a id="<?php 
        echo $key;
        ?>
" href="<?php 
        echo site_url();
        ?>
/foruns/uf-<?php 
        echo $key;
        ?>
"><?php 
        echo $setorial;
        ?>