aboutsummaryrefslogtreecommitdiffstats
path: root/mayor-mod/www/include/modules/naplo/share/file.php
blob: 1e6c7595bf4d419d0816361782fdef95639eb25a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
<?php

    require_once('include/share/print/pdf.php');    

    if (file_exists("lang/$lang/module-naplo/share/file.php")) {
	require_once("lang/$lang/module-naplo/share/file.php");
    } elseif (file_exists('lang/'._DEFAULT_LANG.'/module-naplo/share/file.php')) {
	require_once('lang/'._DEFAULT_LANG.'/module-naplo/share/file.php');
    } else {
	echo $lang;
    }

    $Attrs = array(

	'diakid' => _ATTR_DIAKID,
	'oid' => _ATTR_OID,
	'diakigazolvanyszam' => _ATTR_IGAZOLVANYSZAM,
	'viseltnevelotag' => _ATTR_VNE,
	'viseltcsaladinev' => _ATTR_VCSN,
	'viseltutonev' => _ATTR_VUN,
	'szuleteskorinevelotag' => _ATTR_SZNE,
	'szuleteskoricsaladinev' => _ATTR_SZCSN,
	'szuleteskoriutonev' => _ATTR_SZUN,
	'szuletesihely' => _ATTR_SZH,
	'szuletesiido' => _ATTR_SZI,
	'anyanevelotag' => _ATTR_ANE,
	'anyacsaladinev' => _ATTR_ACSN,
	'anyautoneve' => _ATTR_AUN,
	'kezdotanev' => _ATTR_KEZDO_TANEV,
	'kezdoszemeszter' => _ATTR_KEZDO_SZEMESZTER,
	'vegzotanev' => _ATTR_VEGZO_TANEV,
	'vegzoszemeszter' => _ATTR_VEGZO_SZEMESZTER,
	'adoazonosito' => _ATTR_ADOAZONOSITO,
	'allampolgarsag' => _ATTR_ALLAMPOLGARSAG,
	'anyaid' => _ATTR_ANYAID,
	'apaid' => _ATTR_APAID,
	'gondviseloid' => _ATTR_GONDVISELOID,
	'neveloid' => _ATTR_NEVELOID,
	'diaknaplosorszam' => _ATTR_DIAKNAPLOSORSZAM,
	'elozoiskolaomkod' => _ATTR_ELOZOISKOLAOMKOD,
	'email' => _ATTR_EMAIL,
	'fogyatekossag' => _ATTR_FOGYATEKOSSAG,
	'gondozasiszam' => _ATTR_GONDOZASISZAM,
	'jogviszonykezdete' => _ATTR_JOGVISZONYKEZDETE,
	'jogviszonyvege' => _ATTR_JOGVISZONYVEGE,

	'lakhelyorszag' => _ATTR_LAKHELY_ORSZAG,
	'lakhelyirsz' => _ATTR_LAKHELY_IRSZ,
	'lakhelyhelyseg' => _ATTR_LAKHELY_HELYSEG,
	'lakhelykozteruletnev' => _ATTR_LAKHELY_KOZTERULETNEV,
	'lakhelykozteruletjelleg' => _ATTR_LAKHELY_KOZTERULETJELLEG,
	'lakhelyhazszam' => _ATTR_LAKHELY_HAZSZAM,
	'lakhelyemelet' => _ATTR_LAKHELY_EMELET,
	'lakhelyajto' => _ATTR_LAKHELY_AJTO,

	'tartorszag' => _ATTR_TART_ORSZAG,
	'tartirsz' => _ATTR_TART_IRSZ,
	'tarthelyseg' => _ATTR_TART_HELYSEG,
	'tartkozteruletnev' => _ATTR_TART_KOZTERULETNEV,
	'tartkozteruletjelleg' => _ATTR_TART_KOZTERULETJELLEG,
	'tarthazszam' => _ATTR_TART_HAZSZAM,
	'tartemelet' => _ATTR_TART_EMELET,
	'tartajto' => _ATTR_TART_AJTO,

	'tajszam' => _ATTR_TAJSZAM,
	'osztalyjel' => _ATTR_OSZTALYJEL,
	'penzugyistatusz' => _ATTR_PENZUGYISTATUSZ,
	'szemelyiigazolvanyszam' => _ATTR_SZEMELYIIGAZOLVANYSZAM,
	'szocialishelyzet' => _ATTR_SZOCIALISHELYZET,
	'statusz' => _ATTR_STATUSZ,
	'tartozkodasiokiratszam' => _ATTR_TARTOZKODASIOKIRATSZAM,
	'torvenyeskepviselo' => _ATTR_TORVENYESKEPVISELO,
	'telefon' => _ATTR_TELEFON,
	'mobil' => _ATTR_MOBIL,
	'nem' => _ATTR_NEM,
	'lakohelyijellemzo' => _ATTR_LAKOHELYIJELLEMZO,
	'megjegyzes' => _ATTR_MEGJEGYZES,
    );

    function readUpdateFile($fileName, $mezo_elvalaszto = '	') {


	$ADATOK = array();
	if ($fp = @fopen($fileName,'r')) {
		// Az első 50 sor beolvasása - minta a mező-hozzárendeléshez
		$i=0;
		while (($sor = fgets($fp,1024)) and ($i<50)) {
			$ADATOK[$i] = explode($mezo_elvalaszto,chop($sor));
			$i++;
		}
		fclose($fp);
	} else {
		$_SESSION['alert'][] = 'message:file_open_error:'.$fileName;
	}
	
	return $ADATOK;
    }
/*
 * Lekérdezi egy adatbázis (naplo_intezmeny, naplo (tanév)) egy adott táblájának mezőit.
 * Ha szükséges ezek listáját kiegészíti az extraAttrs tömbben felsorolt mezőkkel.
 * A mezőnevekhez nyelvi konstansokat rendelhetünk ($Attrs tömb és lang/.../share/file.php)
 */
    function getTableFields($table, $db = 'naplo_intezmeny', $extraAttrs = array(), $SET = array('withType' => false)) {

	global $Attrs;
	
	$return = $type = $name = array();
	$q = "SHOW FIELDS FROM `%s`";
	$r = db_query($q, array('fv' => 'getTableFields','modul' => $db, 'result' => 'indexed', 'values' => array($table)));
	for ($i = 0; $i < count($r); $i++) {
	    if ($SET['withType']) {
		if (substr($r[$i]['Type'],0,7) == 'varchar') $type[ $r[$i]['Field'] ] = 'string';
		elseif (substr($r[$i]['Type'],0,4) == 'enum') $type[ $r[$i]['Field'] ] = 'enum';
		elseif (substr($r[$i]['Type'],0,4) == 'date') $type[ $r[$i]['Field'] ] = 'date';
		elseif (strpos($r[$i]['Type'],'int(') !== false) $type[ $r[$i]['Field'] ] = 'int';
		else $type[ $r[$i]['Field'] ] = 'string';
	    }
	    if ($Attrs[kisbetus($r[$i]['Field'])] != '') $name[$r[$i]['Field']] = $Attrs[kisbetus($r[$i]['Field'])];
	    else $name[$r[$i]['Field']] = $r[$i]['Field'];
	}
	for ($i = 0; $i < count($extraAttrs); $i++) {
		if (!isset($name[$extraAttrs[$i]])) {
			if ($Attrs[kisbetus($extraAttrs[$i])] != '') $name[$extraAttrs[$i]] = $Attrs[kisbetus($extraAttrs[$i])];
			else $name[$extraAttrs[$i]] = $extraAttrs[$i];
		}
	}
	ksort($name);

	if ($SET['withType']) return array('names' => $name, 'types' => $type);
	return $name;

    }


    function getEnumField($modul, $table, $field) {

	$table = '`'.str_replace('.','`.`',$table).'`';
        $q = "SHOW FIELDS FROM %s LIKE '%s'";
	$v = array($table, $field);
        $field = db_query($q, array('fv' => 'getEnumField', 'modul' => $modul, 'result' => 'record', 'values' => $v));
        $enum = substr($field['Type'], 6, -2);
        $values = explode("','", $enum);

        return $values;
    }

    function getSetField($modul, $table, $field) {

	$table = '`'.str_replace('.','`.`',$table).'`';
        $q = "SHOW FIELDS FROM %s LIKE '%s'";
	$v = array($table, $field);
        $field = db_query($q, array('fv' => 'getSetField', 'modul' => $modul, 'result' => 'record', 'values' => $v));
        $set = substr($field['Type'], 5, -2);
        $values = explode("','", $set);

        return $values;
    }


    function updateTable($table, $file, $MEZO_LISTA, $KULCS_MEZOK, $mezo_elvalaszto = '	', $rovatfej = false, $db = 'naplo_intezmeny') {


	if (!file_exists($file)) {
		$_SESSION['alert'][] = 'message:file_not_found:updateTable:'.$file;
		return false;
	}

	if (!is_array($MEZO_LISTA)) {
		$_SESSION['alert'][] = 'message:wrong_data:updateTable:MEZO_LISTA';
		return false;
	}

	if (!is_array($KULCS_MEZOK)) {
		$_SESSION['alert'][] = 'message:wrong_data:updateTable:KULCS_MEZOK';
		return false;
	}

	// A frissítendő attribútumok listája
	$attrList = array_values(array_filter($MEZO_LISTA));

	$fp = fopen($file,'r');
	if (!$fp) {
		$_SESSION['alert'][] = 'message:file_open_error:updateTable:'.$file;
		return false;
	}

	$lr = db_connect($db, array('fv' => 'updateTable'));
	if (!$lr) {
		$_SESSION['alert'][] = 'message:db_connect_failure:updateTable';
		fclose($fp);
		return false;
	}
	db_start_trans($lr);

	// Az első sor kihagyása
	if ($rovatfej) $sor = fgets($fp,1024);
	while ($sor = fgets($fp,1024)) {

		$insertValues = $insertPatterns = array();
		$adatSor = explode($mezo_elvalaszto,chop($sor));
		$update = false;

		// keresési feltétel összerakása
		$where = $v = $vw = array();
		for ($i = 0; $i < count($KULCS_MEZOK); $i++) {
			if ($adatSor[$KULCS_MEZOK[$i]] != '') {
				$where[] = "`%s`='%s'";
				array_push($vw, $MEZO_LISTA[$KULCS_MEZOK[$i]], $adatSor[$KULCS_MEZOK[$i]]);
			}
		}
		$num = 0;
		if (count($where) != 0) {
			$q = 'SELECT COUNT(*) FROM `%s` WHERE '.implode(' AND ', $where);
			array_unshift($vw, $table);
			$num = db_query($q, array('fv' => 'updateTable', 'values' => $vw, 'result' => 'value', 'modul' => $db), $lr);
		}
		if ($num == 1 && _SKIP_ON_DUP === true) { $_SESSION['alert'][] = 'info:_SKIP_ON_DUP:'.serialize($sor); continue; }
		if ($num == 1) { // update
		    $v = $vw;
		    array_shift($v); //$table kivétele
		    $UPDATE = array();
		    for ($i = 0; $i < count($MEZO_LISTA); $i++) {
			if (
			    $MEZO_LISTA[$i] != ''
			    and $adatSor[$i] != ''
			    and !in_array($i,$KULCS_MEZOK)
			) {
			    if ($adatSor[$i] == '\N') {
				array_unshift($UPDATE, "`%s`=NULL");
				array_unshift($v, $MEZO_LISTA[$i]);
			    } else {
				array_unshift($UPDATE, "`%s`='%s'");
				array_unshift($v, $MEZO_LISTA[$i], $adatSor[$i]);
			    }
			}
		    }
		    if (count($UPDATE) > 0) {
			array_unshift($v, $table);
			$q = 'UPDATE `%s` SET '.implode(',',$UPDATE).' WHERE '.implode(' AND ', $where);
			$r = db_query($q, array('fv' => 'updateTable/update', 'values' => $v, 'modul' => $db, 'rollback' => true), $lr);
			if (!$r) {
			    db_close($lr);
			    return false;
			}
		    }
		} elseif ($num == 0) { // insert
		    for ($i = 0; $i < count($MEZO_LISTA); $i++) {
			if ($MEZO_LISTA[$i] != '') {
			    if ($adatSor[$i] == '\N') {
				$insertValues[] = 'NULL';
				$insertPatterns[] = '%s';
			    } else {
				$insertValues[] = $adatSor[$i];
				$insertPatterns[] = "'%s'";
			    }
			}
		    }
		    $q = 'INSERT INTO `%s` ('.implode(',', array_fill(0, count($attrList), '%s')).') 
				    VALUES ('.implode(',', $insertPatterns).')';
		    $v = mayor_array_join(array($table), $attrList, $insertValues);
		    $r = db_query($q, array('fv' => 'updateTable/insert', 'modul' => $db, 'values' => $v, 'rollback' => true), $lr);
		    if (!$r) {
			db_close($lr);
			return false;
		    }
		} else {
		    $_SESSION['alert'][] = 'message:wrong_data:updateTable:több illeszkedő rekord is van, túl laza a kulcs feltétel ('
				.call_user_func_array('sprintf', array_merge(array('%s tábla, '.implode(' AND ',$where)), $vw)).')';
		    db_rollback($lr);
		    db_close($lr);
		    return false;
		}
	} // while
	db_commit($lr);
	db_close($lr);

	fclose($fp);

    }

    function generatePDF($outputFile, $outputDir, $str, $booklet=false) {


	// A szöveg file-ba írása
	if (!$fp = fopen($outputDir.'/'.$outputFile.'-u8.tex', 'w')) {
	    $_SESSION['alert'][] = 'message:file_open_failure:generatePDF:'.$outputDir.'/'.$outputFile.'-u8.tex';
	    return false;
        }
	if (!fwrite($fp, $str)) {
	    $_SESSION['alert'][] = 'message:file_write_failure:generatePDF:'.$outputDir.'/'.$outputFile.'-u8.tex';
	    return false;	
        }
	fclose($fp);
	if (__NYOMTATAS_XETEX===true) {
    	    $ret = exec('cd '.$outputDir.'; cat <<EOF > '.$outputFile.'.tex
%\font\kicsi=ecrm0500
%\font\nagy=ecbx1200
%\font\vastag=ecsx0800
%\font\nagyss=ecsx1200
%\font\normal=ecss0800
%\font\dolt=ecsi0800

\font\kicsi="Linux Libertine O" at 5pt
\font\nagy="Linux Libertine O/B" at 12pt
\font\nagyss="Arial/B" at 12pt
\font\normal="Linux Biolinum O" at 8pt
\font\dolt="Linux Biolinum O/I" at 8pt
\normal

EOF
');
    	    $ret = exec('cd '.$outputDir.'; cat '.$outputFile.'-u8.tex >> '.$outputFile.'.tex');
    	    $ret = exec('cd '.$outputDir.'; xetex -fmt '._MAYOR_DIR.'/print/module-naplo/xetex/mayor-xetex '.$outputFile.'.tex');
#ex -fmt '._MAYOR_DIR.'/print/module-naplo/tex/mayor '.$outputFile.'.tex');
	} else {
	    // utf8 --> cork (t1)
    	    $ret = exec('cd '.$outputDir.'; cat '.$outputFile.'-u8.tex | sed -e "s/\xc2\xa0/\x20/g" | recode u8..T1 > '.$outputFile.'.tex');
	    // DVI, PS, PFD generálás (a rotate miatt nem megy a pdftex közvetlenül :o(
    	    $ret = exec('cd '.$outputDir.'; tex -fmt '._MAYOR_DIR.'/print/module-naplo/tex/mayor '.$outputFile.'.tex');
	    if ($ret === false) { $_SESSION['alert'][] = 'message:futási_hiba:generatePDF:tex'; return false; }
# LOG        	$ret = exec('cd '.$outputDir.'; dvips '.$outputFile.'.dvi  2>&1 | tee -a /tmp/x.log   ');
    	    $ret = exec('HOME=/tmp && export HOME && cd '.$outputDir.'; dvips '.$outputFile.'.dvi ');
	    if ($ret === false) { $_SESSION['alert'][] = 'message:futási_hiba:generatePDF:ps'; return false; }
    	    $ret = exec('cd '.$outputDir.'; ps2pdf -sPAPERSIZE=a4 -dAutoRotatePages=/None '.$outputFile.'.ps');
	    if (strpos($ret, 'error') !== false) { $_SESSION['alert'][] = 'message:futási_hiba:generatePDF:pdf'; return false; }

	}
	if ($booklet) {
    	    $ret = exec('cd '.$outputDir.'; mv '.$outputFile.'.pdf '.$outputFile.'-A4.pdf; pdfbook --short-edge --outfile '.$outputFile.'.pdf '.$outputFile.'-A4.pdf');
	}
	return true;
    }

    function generateXLS($fileName, $Table, $title) {

	global $policy, $page, $sub, $f;

	if (dirname($fileName) == '.') $fileName = _DOWNLOADDIR."/$policy/$page/$sub/$f/$fileName";
	$fp = fopen($fileName, 'w');
	if (!$fp) {
	    $_SESSION['alert'][] = 'message:file_open_failure:'.$fileName;
	    return false;
	}

	fputs($fp, '<?xml version="1.0"?>'."\r\n");
        fputs($fp, '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">'."\r\n");
        fputs($fp, ' <Styles>'."\r\n"
                .'  <Style ss:ID="s21">'."\r\n".'   <NumberFormat ss:Format="Short Date" />'."\r\n".'  </Style>'."\r\n"
                .'  <Style ss:ID="s22">'."\r\n".'   <NumberFormat ss:Format="yyyy\.m\.d\.\ h:mm" />'."\r\n".'  </Style>'."\r\n"
                ."\r\n".' </Styles>'."\r\n");

        fputs($fp, '<Worksheet ss:Name="'.$title.'">'."\r\n");
        fputs($fp, '<Table>'."\r\n");

	for ($i = 0; $i < count($Table); $i++) {
	    fputs($fp, '  <Row>'."\r\n");
	    foreach ($Table[$i] as $index => $value) {
                if (is_numeric($value))
                    fputs($fp, "    <Cell><Data ss:Type=\"Number\">".$value."</Data></Cell>\r\n");
                elseif (strtotime($value))
                    if (strlen($value) > 10) {
                        fputs($fp, "    <Cell ss:StyleID=\"s22\"><Data ss:Type=\"DateTime\">".str_replace(' ','T',$value).'.000'."</Data></Cell>\r\n");
                    } else {
                        fputs($fp, "    <Cell ss:StyleID=\"s21\"><Data ss:Type=\"DateTime\">".$value.'T08:40:00.000'."</Data></Cell>\r\n");
                    }
                else
                    fputs($fp, "    <Cell><Data ss:Type=\"String\">".$value."</Data></Cell>\r\n");
            }
	    fputs($fp, '  </Row>'."\r\n");
	}

        fputs($fp, '</Table>'."\r\n");
        fputs($fp, '</Worksheet>'."\r\n");
        fputs($fp, '</Workbook>'."\r\n");

        fclose($fp);
        return true;

    }

    function generateCSV($fileName, $Table, $title, $mezoElvalaszto='	') {

	global $policy, $page, $sub, $f;

	if (dirname($fileName) == '.') $fileName = _DOWNLOADDIR."/$policy/$page/$sub/$f/$fileName";
	$fp = fopen($fileName, 'w');
	if (!$fp) {
	    $_SESSION['alert'][] = 'message:file_open_failure:'.$fileName;
	    return false;
	}

        if ($title !='') fputs($fp, $title."\n");
        for ($i = 0; $i < count($Table); $i++) fputs($fp, implode($mezoElvalaszto, $Table[$i])."\n");

        fclose($fp);
        return true;

    }

    function generateODS($fileName, $Table, $title) {

	global $policy, $page, $sub, $f;

	if (dirname($fileName) == '.') $fileName = _DOWNLOADDIR."/$policy/$page/$sub/$f/$fileName";
	define('TMPZIP','/tmp/'.substr(basename($fileName), 0, strpos(basename($fileName), '.')));
	define('ODS_MIMETIPE','application/vnd.oasis.opendocument.spreadsheet');
	define('ODS_MANIFEST','<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">
 <manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.spreadsheet" manifest:version="1.2" manifest:full-path="/"/>
 <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="content.xml"/>
</manifest:manifest>');
	define('ODS_START_XMLDOCUMENT','<?xml version="1.0" encoding="UTF-8"?>
<office:document-content
 xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
 xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
 xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
 xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
 xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
 xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
 xmlns:xlink="http://www.w3.org/1999/xlink"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
 xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
 xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"
 xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
 xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
 xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
 xmlns:math="http://www.w3.org/1998/Math/MathML"
 xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
 xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
 xmlns:ooo="http://openoffice.org/2004/office"
 xmlns:ooow="http://openoffice.org/2004/writer"
 xmlns:oooc="http://openoffice.org/2004/calc"
 xmlns:dom="http://www.w3.org/2001/xml-events"
 xmlns:xforms="http://www.w3.org/2002/xforms"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:rpt="http://openoffice.org/2005/report"
 xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2"
 xmlns:xhtml="http://www.w3.org/1999/xhtml"
 xmlns:grddl="http://www.w3.org/2003/g/data-view#"
 xmlns:tableooo="http://openoffice.org/2009/table"
 xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0"
 xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0"
 office:version="1.2" grddl:transformation="http://docs.oasis-open.org/office/1.2/xslt/odf2rdf.xsl">');
	define('ODS_ASTYLES','<office:automatic-styles>
    <number:date-style style:name="N84">
      <number:year number:style="long"/>
      <number:text>-</number:text>
      <number:month number:style="long"/>
      <number:text>-</number:text>
      <number:day number:style="long"/>
    </number:date-style>
    <style:style style:name="ce1" style:family="table-cell" style:parent-style-name="Default" style:data-style-name="N84"/>
  </office:automatic-styles>');
	define('ODS_START_SPREADSHEET','<office:body><office:spreadsheet>');
	define('ODS_START_TABLE','<table:table table:name="Export">');
	define('ODS_START_ROW','<table:table-row>');
	define('ODS_END_ROW','</table:table-row>');
	define('ODS_END_TABLE','</table:table>');
	define('ODS_END_SPREADSHEET','</office:spreadsheet></office:body>');
	define('ODS_END_XMLDOCUMENT','</office:document-content>');

	$content = ODS_START_XMLDOCUMENT . ODS_ASTYLES . ODS_START_SPREADSHEET . ODS_START_TABLE;
	for ($i = 0; $i < count($Table); $i++) {
	    $content .= ODS_START_ROW;
	    foreach ($Table[$i] as $index => $value) {
                if (is_numeric($value))
		    $content .= '<table:table-cell office:value-type="float" office:value="'.$value.'"/>'."\n";
                elseif (strtotime($value))
                    if (strlen($value) > 10) {
			$content .= '<table:table-cell table:style-name="ce1" office:value-type="date" office:date-value="'.$value.'"/>'."\n";
                    } else {
			$content .= '<table:table-cell table:style-name="ce1" office:value-type="date" office:date-value="'.$value.'"/>'."\n";
                    }
                elseif ($value[0] == '=') $content .= '<table:table-cell table:formula="'.$value.'" office:value-type="float"/>'."\n";
		// formula példa: $value = 'of:=SUM([.A1:.B1])*2+[.A1]'
		else $content .= '<table:table-cell office:value-type="string"><text:p>'.$value.'</text:p></table:table-cell>'."\n";
            }
	    $content .= ODS_END_ROW;
	}
	$content .= ODS_END_TABLE . ODS_END_SPREADSHEET . ODS_END_XMLDOCUMENT;
	
	mkdir(TMPZIP);
	mkdir(TMPZIP."/META-INF");
	file_put_contents(TMPZIP."/META-INF/manifest.xml", ODS_MANIFEST);
	file_put_contents(TMPZIP."/content.xml", $content);
	file_put_contents(TMPZIP."/mimetype", ODS_MIMETIPE);
	system("cd ".TMPZIP."; zip -mr ".$fileName." mimetype META-INF/* content.xml >/dev/null");
	rmdir(TMPZIP."/META-INF");
	rmdir(TMPZIP);

	return true;
    }

/* --------------------------------------- */

    function _template2array($fp, $type, &$aTeX) {
        $vege = false;
        $aTeX[$type] = array();
        while (!$vege && ($sor = fgets($fp, 1024))) {
            $sor = chop($sor);
            if ($sor == "%}$type") {
                $vege = true;
            } elseif (substr($sor, 0, 2) != '%!') { // A feldolgozást végző függvény megadása
                if (substr($sor, 0, 2) == '%}') {
		    echo "HIBA #1 Megnyitás előtti blokk lezárás a {$type} blokkban: $sor<hr>";
                } else {
                    if (substr($sor, 0, 2) == '%{') {
                        $_type = substr($sor, 2);
                        _template2array($fp, $_type, $aTeX);
                        $aTeX[$type][] = '%{'.$_type.'}';
                    } else {
			// feltételes szövegrészek
			$condArray = explode('%?', $sor);
			for ($i = 1; $i < count($condArray); $i = $i + 2) {
			    $str = $condArray[$i];
			    $tmpArray = explode('|', $str);
			    $j = 0; while (is_array($aTeX['conditional']["$j".$tmpArray[0]])) $j++;
			    $newCondition = "$j".$tmpArray[0];
			    $aTeX['conditional'][$newCondition] = array(true => $tmpArray[1], false => $tmpArray[2], 'orig' => $tmpArray[0]);
			    $sor = str_replace($str.'%?', $newCondition, $sor);
			}
//                        $aTeX[$type][] = $sor;
			// lezáró eset
			$finalArray = explode('%>', $sor);
			for ($i = 1; $i < count($finalArray); $i = $i + 2) {
			    $str = $finalArray[$i];
			    $tmpArray = explode('<!>', $str);
			    $j = 0; while (is_array($aTeX['finalCase']["$j".$type])) $j++;
			    $newCondition = "$j".$type;
			    $aTeX['finalCase'][$newCondition] = array(true => $tmpArray[0], false => $tmpArray[1]);
			    $sor = str_replace($str.'%>', $newCondition, $sor);
			}
                        $aTeX[$type][] = $sor;
                    }
                }
            }
        }
    }

    function _array2text($type, $id, $mit, $mire, $aTeX, $ADAT, $flag = null) {
        $ret = '';
        if (is_null($id)) $A = $ADAT[$type];
        else $A = $ADAT[$type][$id];

        // A cserélendő attribútumok
        if (is_array($A)) foreach ($A as $attr => $value) {
            if (!is_array($value)) {
		if (true || !is_bool($value)) { // feltételes szövegrészek külön kezelendők ??? Miért is? Az általánosabb feltételes kiíráshoz kell!
            	    if (in_array('%$'.$attr, $mit)) { // A már szereplő mintát felülírjuk!
			$key = array_search('%$'.$attr, $mit);
			$mit[$key] = '%$'.$attr;
            		$mire[$key] = $value;
		    } else {
			$mit[] = '%$'.$attr;
            		$mire[] = $value;
		    }
		}
            }
        }

        // aTeX feldolgozása
        $TeX = $aTeX[$type];
        for ($i = 0; $i < count($TeX); $i++) {
            $sor = $TeX[$i];
            if (substr($sor, 0, 2) == '%{') {
                // Almodul feldolgozása
                $_type = substr($sor, 2, -1);
                if (is_array($A[$_type])) {
                    if (is_null($id)) {
                        foreach ($A[$_type] as $key => $_id) $ret .= _array2text($_type, $_id, $mit, $mire, $aTeX, $ADAT);
                    } else {
			$count = count($A[$_type]); $db = 0;
                        foreach ($A[$_type] as $_id => $_data) {
			    $db++; if ($count == $db) $_flag = 'final'; else $_flag = null;
                            if (!is_array($ADAT[$_type][$_id]) && !is_array($_data)) {
                                echo '<br>HIBA#2!!! '.$_type.':'.$_id.':'.$_data.'<hr />';
//                                return false;
                            } else {
				if (!is_array($ADAT[$_type][$_id])) $ADAT[$_type][$_id] = array();
				elseif (!is_array($_data)) $_data = array();
                        	$ADAT[$_type][$_id] = $ADAT[$_type][$_id] + $_data;
                        	$ret .= _array2text($_type, $_id, $mit, $mire, $aTeX, $ADAT, $_flag);
			    }
                        }
                    }
                } elseif (__DEBUG === true)  { echo '<br>HIBA#3: '.$sor.'<br />'.$_type.':'; var_dump($A[$_type]); echo '<hr />';}
            } else {
                    // Csere - lezáró eset
		    if (strpos($sor, '%>') !== false) foreach ($aTeX['finalCase'] as $attr => $values) {
			$sor = str_replace('%>'.$attr, $values[ $flag === 'final' ], $sor);
		    }
		    // Csete - feltételes kiírás
                    if (strpos($sor, '%?') !== false) foreach ($aTeX['conditional'] as $attr => $values) {
			// Nem csak az adott szintről veszi az értéket, hanem feljebbről is (a feljebbi a meghatározó - ez nem biztos, hogy jó...)
			if ($key = array_search('%$'.$values['orig'], $mit)) $_val = $mire[$key];
			else $_val = $A[$values['orig']];
			$sor = str_replace('%?'.$attr, $values[ $_val==true ], $sor);
			//$sor = str_replace('%?'.$attr, $values[ $A[$values['orig']]==true ], $sor);
		    }
		    // Kiírás
                    $ret .= str_replace($mit, $mire, $sor)."\n";
            }
        }

        return $ret;
    }

    function template2text($templateFile, $ADAT) {

	$mit = $mire = array();
	$aTeX = array('conditional' => array());

	$fp = fopen($templateFile, 'r');
	_template2array($fp, 'base', $aTeX);
	fclose($fp);
	return _array2text('base', null, $mit, $mire, $aTeX, $ADAT);

    }

    function template2file($templateFile, $ADAT) {

	global $policy, $page, $sub, $f;

	$mit = $mire = array();
	$aTeX = array('conditional' => array());

	$fp = fopen($templateFile, 'r');
	// A feldolgozást végző függvény neve
	$sor = fgets($fp, 1024);
	rewind($fp);
	if (substr($sor, 0, 2) == '%!') {
	    list($func,$ext,$opt) = explode(' ', substr(chop($sor), 2));
	    if (!function_exists($func)) unset($func);
	}
	if ($ext=='') $ext = 'txt';
	_template2array($fp, 'base', $aTeX);
	fclose($fp);
	$text = _array2text('base', null, $mit, $mire, $aTeX, $ADAT);
//die();
	if ($text === false) return false;
	$success = true;
	if (isset($func)) {
	    $success = $func($text, $ADAT['file'], $opt);
	} else {
	    $fp = fopen(_DOWNLOADDIR."/$policy/$page/$sub/$f/".$ADAT['file'].'.'.$ext, 'w');
	    fputs($fp, $text);
	    fclose($fp);
	}
	if ($success) return $ADAT['file'].".$ext";
	else return false;

    }



?>