401 lines
13 KiB
PHP
401 lines
13 KiB
PHP
<?php
|
|
|
|
include_once 'definition.php';
|
|
|
|
define("TEMP_FOLDER", "admin/survey/modules/mod_MJU/temp", true);
|
|
define("RESULTS_FOLDER", "admin/survey/modules/mod_MJU/results", true);
|
|
define("SCRIPT_FOLDER", "admin/survey/modules/mod_MJU/R", true);
|
|
|
|
class SurveyMJUEnote{
|
|
|
|
private $anketa; // id ankete
|
|
private $db_table = '';
|
|
|
|
|
|
function __construct($anketa){
|
|
global $site_url;
|
|
|
|
if ((int)$anketa > 0){
|
|
$this->anketa = $anketa;
|
|
|
|
# polovimo vrsto tabel (aktivne / neaktivne)
|
|
SurveyInfo :: getInstance()->SurveyInit($this->anketa);
|
|
if (SurveyInfo::getInstance()->getSurveyColumn('db_table') == 1) {
|
|
$this->db_table = '_active';
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// Prikaz nastavitev za izvoz
|
|
public function displaySettings(){
|
|
|
|
$enote = array(
|
|
"Ajdovščina", "Brežice", "Celje", "Cerknica", "Črnomelj", "Domžale", "Dravograd", "Gornja Radgona", "Grosuplje",
|
|
"Hrastnik", "Idrija", "Ilirska Bistrica", "Izola", "Jesenice", "Kamnik", "Kočevje", "Koper", "Kranj", "Krško", "Laško", "Lenart", "Lendava",
|
|
"Litija", "Ljubljana", "Ljutomer","Logatec", "Maribor", "Metlika", "Mozirje", "Murska Sobota", "Nova Gorica", "Novo mesto",
|
|
"Ormož", "Pesnica", "Piran", "Postojna", "Ptuj", "Radlje ob Dravi", "Radovljica", "Ravne na Koroškem",
|
|
"Ribnica", "Ruše", "Sevnica", "Sežana", "Slovenj Gradec", "Slovenska Bistrica", "Slovenske Konjice", "Šentjur pri Celju",
|
|
"Škofja Loka", "Šmarje pri Jelšah", "Tolmin", "Trbovlje", "Trebnje", "Tržič", "Velenje", "Vrhnika", "Zagorje ob Savi",
|
|
"Žalec", "Ljubljana-Bežigrad", "Ljubljana-Center", "Ljubljana-Moste Polje", "Ljubljana-SUNZ", "Ljubljana-Šiška", "Ljubljana-Vič Rudnik"
|
|
);
|
|
|
|
|
|
// Stari izvozi za enote
|
|
echo '<fieldset><legend>Izvozi za upravne enote</legend>';
|
|
|
|
// Warning da zna trajati
|
|
echo '<p class="bold">Izvoz poročila za posamezno enoto traja približno 30 sekund!</p>';
|
|
|
|
echo '<span class="bold spaceRight">Tip poročila:</span>';
|
|
echo '<span class="spaceLeft"><label for="type_4"><input type="radio" name="type" id="type_4" value="4" checked="checked" onClick="mju_enote_export_type();">Skupno poročilo</label></span>';
|
|
echo '<span class="spaceLeft"><label for="type_1"><input type="radio" name="type" id="type_1" value="1" onClick="mju_enote_export_type();">Posamezna enota</label></span>';
|
|
echo '<span class="spaceLeft"><label for="type_2"><input type="radio" name="type" id="type_2" value="2" onClick="mju_enote_export_type();">UE Ljubljana</label></span>';
|
|
echo '<span class="spaceLeft"><label for="type_3"><input type="radio" name="type" id="type_3" value="3" onClick="mju_enote_export_type();">Paket 63 posameznih poročil</label></span>';
|
|
|
|
echo '<br /><br />';
|
|
|
|
echo '<div id="enote_holder" style="display: none; margin: 0 0 10px 0;">';
|
|
echo ' <span class="bold spaceRight" style="margin-right:55px!important;">Enota:</span><select name="enote">';
|
|
foreach($enote as $key => $enota){
|
|
echo ' <option value="'.($key+1).'">'.$enota.'</option>';
|
|
}
|
|
echo ' </select>';
|
|
echo '</div>';
|
|
|
|
echo '<span id="enote_warning" class="red bold" style="display:none;">Opozorilo! Izvoz paketa vseh poročil lahko traja tudi 15 min ali več!<br /><br /></span>';
|
|
|
|
//echo '<br />';
|
|
|
|
echo '<span class="floatLeft"><div class="buttonwrapper">';
|
|
echo ' <a class="ovalbutton ovalbutton_orange btn_savesettings" href="#" onclick="mju_enote_export(); return false;">';
|
|
echo ' <span style="color:white;">Izvozi</span>';
|
|
echo ' </a>';
|
|
echo '</div></span>';
|
|
|
|
echo '</fieldset>';
|
|
|
|
|
|
echo '<br />';
|
|
|
|
// Novi izvozi za enote
|
|
echo '<fieldset><legend>NOVI izvozi za upravne enote - kakovost</legend>';
|
|
|
|
// Warning da zna trajati
|
|
echo '<p class="bold">Izvoz poročila za posamezno enoto traja približno 30 sekund!</p>';
|
|
|
|
echo '<span class="bold spaceRight">Tip poročila:</span>';
|
|
echo '<span class="spaceLeft"><label for="type2_4"><input type="radio" name="type2" id="type2_4" value="4" checked="checked" onClick="mju2_enote_export_type();">Skupno poročilo</label></span>';
|
|
echo '<span class="spaceLeft"><label for="type2_1"><input type="radio" name="type2" id="type2_1" value="1" onClick="mju2_enote_export_type();">Posamezna enota</label></span>';
|
|
echo '<span class="spaceLeft"><label for="type2_2"><input type="radio" name="type2" id="type2_2" value="2" onClick="mju2_enote_export_type();">UE Ljubljana</label></span>';
|
|
echo '<span class="spaceLeft"><label for="type2_3"><input type="radio" name="type2" id="type2_3" value="3" onClick="mju2_enote_export_type();">Paket 63 posameznih poročil</label></span>';
|
|
|
|
echo '<br /><br />';
|
|
|
|
echo '<div id="enote_holder2" style="display: none; margin: 0 0 10px 0;">';
|
|
echo ' <span class="bold spaceRight" style="margin-right:55px!important;">Enota:</span><select name="enote2">';
|
|
foreach($enote as $key => $enota){
|
|
echo ' <option value="'.($key+1).'">'.$enota.'</option>';
|
|
}
|
|
echo ' </select>';
|
|
echo '</div>';
|
|
|
|
echo '<span id="enote_warning2" class="red bold" style="display:none;">Opozorilo! Izvoz paketa vseh poročil lahko traja tudi 15 min ali več!<br /><br /></span>';
|
|
|
|
//echo '<br />';
|
|
|
|
echo '<span class="floatLeft"><div class="buttonwrapper">';
|
|
echo ' <a class="ovalbutton ovalbutton_orange btn_savesettings" href="#" onclick="mju2_enote_export(); return false;">';
|
|
echo ' <span style="color:white;">Izvozi</span>';
|
|
echo ' </a>';
|
|
echo '</div></span>';
|
|
|
|
echo '</fieldset>';
|
|
|
|
|
|
// Include js
|
|
echo '<script src="modules/mod_MJU/js/mju.js" type="text/javascript"></script>';
|
|
}
|
|
|
|
// Zgeneriramo pdf analizo
|
|
public function executeExport($type, $enota='0'){
|
|
global $site_path;
|
|
global $site_url;
|
|
global $lang;
|
|
global $admin_type;
|
|
|
|
// Zgeneriramo zacasne csv datoteke
|
|
$this->prepareCSV();
|
|
|
|
// Poklicemo R skripto in zgeneriramo pdf
|
|
$script = $site_path . SCRIPT_FOLDER . '/Reports_MJU.R';
|
|
$params = $type;
|
|
$params .= ($type == 1 && $enota > 0) ? ' '.$enota : '';
|
|
|
|
$out = exec('Rscript '.$script.' '.$params.' 2>&1', $output, $return_var);
|
|
|
|
// Testiranje - izpis errorjev
|
|
/*if($admin_type == 0){
|
|
echo '<div>';
|
|
echo 'Rscript '.$script;
|
|
echo '<br />'.$out.'<br />';
|
|
var_dump($output);
|
|
echo '</div>';
|
|
}*/
|
|
|
|
|
|
// Ime datoteke ion priprava zip paketa
|
|
if($type == 1 || $type == 2){
|
|
$doc_name = 'Upravna_enota.docx';
|
|
}
|
|
elseif($type == 3){
|
|
$doc_name = 'Upravne_enote.zip';
|
|
|
|
$zip = new ZipArchive;
|
|
if ($zip->open($site_path . RESULTS_FOLDER . '/'.$doc_name, ZIPARCHIVE::CREATE) !== TRUE)
|
|
die ("Could not open archive");
|
|
|
|
if($handle = opendir($site_path . RESULTS_FOLDER.'/')){
|
|
// Dodamo vse doc file v paket
|
|
while(false !== ($entry = readdir($handle))){
|
|
if(pathinfo($entry, PATHINFO_EXTENSION) == 'docx'){
|
|
$zip->addFile($site_path . RESULTS_FOLDER.'/'.$entry, $entry);
|
|
}
|
|
}
|
|
closedir($handle);
|
|
}
|
|
|
|
$zip->close();
|
|
}
|
|
else{
|
|
$doc_name = 'Vse_upravne_enote.docx';
|
|
}
|
|
|
|
// Pripravimo file za download
|
|
if(file_exists($site_path . RESULTS_FOLDER . '/'.$doc_name)){
|
|
|
|
$file = $site_path . RESULTS_FOLDER . '/'.$doc_name;
|
|
|
|
header('Content-Description: File Transfer');
|
|
//header('Content-Type: application/octet-stream');
|
|
header('Content-Disposition: attachment; filename='.basename(''.$doc_name.''));
|
|
|
|
header("Content-type: text/x-csv; charset=utf-8");
|
|
//header("Content-type: text/csv");
|
|
|
|
header('Content-Transfer-Encoding: binary');
|
|
header('Expires: 0');
|
|
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
|
|
header('Pragma: public');
|
|
header('Content-Length: ' . filesize($file));
|
|
|
|
ob_clean();
|
|
flush();
|
|
|
|
readfile($file);
|
|
}
|
|
|
|
// Na koncu pobrisemo zacasne datoteke
|
|
$this->deleteTemp();
|
|
|
|
// Ugasnemo skripto:)
|
|
die();
|
|
}
|
|
|
|
// Zgeneriramo pdf analizo za nove izvoze
|
|
public function executeExport2($type, $enota='0'){
|
|
global $site_path;
|
|
global $site_url;
|
|
global $lang;
|
|
global $admin_type;
|
|
|
|
// Zgeneriramo zacasne csv datoteke
|
|
$this->prepareCSV();
|
|
|
|
// Poklicemo R skripto in zgeneriramo pdf
|
|
$script = $site_path . SCRIPT_FOLDER . '/00_Reports_MJU.r';
|
|
$params = $type;
|
|
$params .= ($type == 1 && $enota > 0) ? ' '.$enota : '';
|
|
|
|
$out = exec('Rscript '.$script.' '.$params.' 2>&1', $output, $return_var);
|
|
|
|
// Testiranje - izpis errorjev
|
|
// if($admin_type == 0){
|
|
// echo '<div>';
|
|
// echo 'Rscript '.$script;
|
|
// echo '<br />'.$out.'<br />';
|
|
// var_dump($output);
|
|
// echo '</div>';
|
|
// }
|
|
|
|
|
|
// Ime datoteke ion priprava zip paketa
|
|
if($type == 1 || $type == 2){
|
|
$doc_name = 'Upravna_enota.docx';
|
|
}
|
|
elseif($type == 3){
|
|
$doc_name = 'Upravne_enote.zip';
|
|
|
|
$zip = new ZipArchive;
|
|
if ($zip->open($site_path . RESULTS_FOLDER . '/'.$doc_name, ZIPARCHIVE::CREATE) !== TRUE)
|
|
die ("Could not open archive");
|
|
|
|
if($handle = opendir($site_path . RESULTS_FOLDER.'/')){
|
|
// Dodamo vse doc file v paket
|
|
while(false !== ($entry = readdir($handle))){
|
|
if(pathinfo($entry, PATHINFO_EXTENSION) == 'docx'){
|
|
$zip->addFile($site_path . RESULTS_FOLDER.'/'.$entry, $entry);
|
|
}
|
|
}
|
|
closedir($handle);
|
|
}
|
|
|
|
$zip->close();
|
|
}
|
|
else{
|
|
$doc_name = 'Vse_upravne_enote.docx';
|
|
}
|
|
|
|
// Pripravimo file za download
|
|
if(file_exists($site_path . RESULTS_FOLDER . '/'.$doc_name)){
|
|
|
|
$file = $site_path . RESULTS_FOLDER . '/'.$doc_name;
|
|
|
|
header('Content-Description: File Transfer');
|
|
//header('Content-Type: application/octet-stream');
|
|
header('Content-Disposition: attachment; filename='.basename(''.$doc_name.''));
|
|
|
|
header("Content-type: text/x-csv; charset=utf-8");
|
|
//header("Content-type: text/csv");
|
|
|
|
header('Content-Transfer-Encoding: binary');
|
|
header('Expires: 0');
|
|
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
|
|
header('Pragma: public');
|
|
header('Content-Length: ' . filesize($file));
|
|
|
|
ob_clean();
|
|
flush();
|
|
|
|
readfile($file);
|
|
}
|
|
|
|
// Na koncu pobrisemo zacasne datoteke
|
|
$this->deleteTemp();
|
|
|
|
// Ugasnemo skripto:)
|
|
die();
|
|
}
|
|
|
|
|
|
// Pripravimo zacasne datoteke
|
|
private function prepareCSV(){
|
|
global $site_path;
|
|
|
|
$temp_folder = $site_path . TEMP_FOLDER.'/';
|
|
|
|
|
|
// Poskrbimo za datoteko s podatki
|
|
$SDF = SurveyDataFile::get_instance();
|
|
$SDF->init($this->anketa);
|
|
$SDF->prepareFiles();
|
|
|
|
$_headFileName = $SDF->getHeaderFileName();
|
|
$_dataFileName = $SDF->getDataFileName();
|
|
|
|
if ($_headFileName != null && $_headFileName != '') {
|
|
$_HEADERS = unserialize(file_get_contents($_headFileName));
|
|
}
|
|
else {
|
|
echo 'Error! Empty file name!';
|
|
}
|
|
|
|
// Zaenkrat dopuscamo samo status 6 in brez lurkerjev
|
|
$status_filter = '('.STATUS_FIELD.' ~ /6|5/)&&('.LURKER_FIELD.'==0)';
|
|
|
|
$start_sequence = 2;
|
|
$end_sequence = $_HEADERS['_settings']['metaSequence']-1;
|
|
|
|
$field_delimit = ';';
|
|
|
|
// Filtriramo podatke po statusu in jih zapisemo v temp folder
|
|
if (IS_WINDOWS) {
|
|
$out = shell_exec('awk -F"|" "BEGIN {{OFS=\",\"} {ORS=\"\n\"}} '.$status_filter.'" '.$_dataFileName.' | cut -d "|" -f '.$start_sequence.'-'.$end_sequence.' >> '.$temp_folder.'/temp_data_'.$this->anketa.'.dat');
|
|
}
|
|
else {
|
|
$out = shell_exec('awk -F"|" \'BEGIN {{OFS=","} {ORS="\n"}} '.$status_filter.'\' '.$_dataFileName.' | cut -d \'|\' -f '.$start_sequence.'-'.$end_sequence.' >> '.$temp_folder.'/temp_data_'.$this->anketa.'.dat');
|
|
}
|
|
|
|
|
|
// Ustvarimo koncni CSV
|
|
if ($fd = fopen($temp_folder.'/temp_data_'.$this->anketa.'.dat', "r")) {
|
|
|
|
$fd2 = fopen($temp_folder.'/mju_baza.csv', "w");
|
|
|
|
$convertType = 1; // kateri tip konvertiranja uporabimo
|
|
$convertTypes[1] = array('charSet' => 'windows-1250',
|
|
'delimit' => ';',
|
|
'newLine' => "\n",
|
|
'BOMchar' => "\xEF\xBB\xBF");
|
|
# dodamo boomchar za utf-8
|
|
fwrite($fd2, $convertTypes[$convertType]['BOMchar']);
|
|
|
|
# naredimo header row
|
|
foreach ($_HEADERS AS $spid => $spremenljivka) {
|
|
if (count($spremenljivka['grids']) > 0) {
|
|
foreach ($spremenljivka['grids'] AS $gid => $grid) {
|
|
foreach ($grid['variables'] AS $vid => $variable ){
|
|
if ($spremenljivka['tip'] !== 'sm' && !($variable['variable'] == 'uid' && $variable['naslov'] == 'User ID')){
|
|
$output1 .= strip_tags($variable['variable']).$field_delimit;
|
|
$output2 .= '"'.strip_tags($variable['naslov']).'"'.$field_delimit;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fwrite($fd2, $output1."\r\n");
|
|
fwrite($fd2, $output2."\r\n");
|
|
|
|
while ($line = fgets($fd)) {
|
|
|
|
$temp = array();
|
|
$temp = explode('|', $line);
|
|
|
|
// Spremenimo encoding v windows-1250
|
|
//$line = iconv("UTF-8","Windows-1250//TRANSLIT", $line);
|
|
|
|
$line = '"' . str_replace(array("\r","\n","\"","|"), array("","","",'";"'), $line) . '"';
|
|
|
|
fwrite($fd2, $line);
|
|
fwrite($fd2, "\r\n");
|
|
}
|
|
|
|
fclose($fd2);
|
|
}
|
|
fclose($fd);
|
|
}
|
|
|
|
// Pobrisemo zacasne datoteke
|
|
private function deleteTemp(){
|
|
global $site_path;
|
|
|
|
$temp_folder = $site_path . TEMP_FOLDER.'/';
|
|
|
|
// Pobrisemo zacasno CSV datoteko s podatki
|
|
if (file_exists($temp_folder.'/mju_baza.csv')) {
|
|
unlink($temp_folder.'/mju_baza.csv');
|
|
}
|
|
if (file_exists($temp_folder.'/temp_data_'.$this->anketa.'.dat')) {
|
|
unlink($temp_folder.'/temp_data_'.$this->anketa.'.dat');
|
|
}
|
|
|
|
// Pobrisemo še vse rezultatih
|
|
$files = glob($site_path . RESULTS_FOLDER . '/*');
|
|
foreach($files as $file){
|
|
if(is_file($file))
|
|
unlink($file);
|
|
}
|
|
}
|
|
|
|
} |