<?php
checkLogin('adm');
include(ROOT."/includes/modules/servizi/functions.inc.php");

//popolo gruppi
if(isset($_GET['populate'])){
	foreach(get_sottogruppi(dbClean($_GET['populate'],false)) as $grp_id => $v){
		$childrens = array();
		foreach($v['sub'] as $sub_id => $sub_name)
			$childrens[] = array('label'=>entities($sub_name),'value'=>$sub_id);
			
		$row[] = array('title'=>entities($v['name']),'children'=>$childrens);
	}

	echo json_encode($row);	
	die();
}

//invio SMS
if(isset($_POST['servizio'])){
	
	//ho selezionato un servizio
	if($_POST['servizio'] <> 'all'){
		
		checkEnteService($_POST['servizio']);
		$serv_cod = $_POST['servizio'];

		$table = "serv_".$serv_cod;
		$join = "LEFT JOIN ".$table." ON anagrafiche.anagr_id = ".$table.".anagr_id";
		$w .= " AND ".$table.".classe_id IS NOT NULL";
		
		//ho selezionato dei gruppi
		if(count($_POST['grp'])>0){
			foreach($_POST['grp'] as $id){
				$ids[] = intval($id);
			}
			$w .= " AND ".$table.".classe_id IN (".implode(",",$ids).")";
		}
	}
	
	$result = $mysqli->query($q="
	SELECT user_email FROM users 
	LEFT JOIN anagrafiche ON users.user_id = anagrafiche.user_id
	".$join."
	WHERE 
	users.user_email IS NOT NULL AND
	anagrafiche.ente_id = ".$_SESSION['user']['ente_id']." 
	".$w."
	GROUP BY users.user_id
	");
	
	$n = $result->num_rows;
	
	while($row = $result->fetch_assoc())
		$email[] = $row['user_email'];
	
	header('Content-Type: text/plain');
	header('Content-Disposition: attachment;filename="indirizzi_email.txt"');
	header('Cache-Control: max-age=0');
	header('Cache-Control: max-age=1');
	header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
	header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
	header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
	header ('Pragma: public'); // HTTP/1.0
		
	echo implode(",",$email);
	die();
}

$p = new Page('Scarica indirizzi e-mail');
$p->addMenu('logged');
$p->addModuleJs('script');
$p->addJs('bootstrap-multiselect');
$p->addCss('bootstrap-multiselect');
$p->printHeader();
?>
<h3 class="p-title">Scarica indirizzi e-mail</h3>
    
    <form data-role="form" id="SMSform" class="form-horizontal panel panel-default" action="./" method="post" enctype="application/x-www-form-urlencoded">

        <div class="panel-heading">
                <div class="row">
                    <label class="control-label col-sm-1">Servizio</label>
                    <div class="col-sm-5">
                        <select class="form-control" name="servizio" onChange="populateGrp(this.value)">
                            <option value="all">Tutti</option>
                            <?php
                                foreach(get_ente_servizi() as $v)
                                echo '<option value="'.$v['serv_cod'].'">'.entities($v['serv_name']).'</option>';
                            ?>
                        </select>
                    </div>
			
                    <label class="control-label col-sm-1">Gruppi</label>
                    <div class="col-sm-5">
                        <select class="multiselect" disabled id="sel-grp" name="grp[]" multiple size="5"></select>
                    </div>

                </div>
        </div>
    
        <div class="panel-body">
        	<button type="submit" class="btn btn-primary">Scarica indirizzi</button>
        </div>
        
    </form>
<?php
$p->printFooter();
?>