Overview
Comment:Mise en commun export CSV/ODS (+ simplification)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dev
Files: files | file ages | folders
SHA1: 72b8ff6c0fafa8653aedab0791771d0c7a405016
User & Date: bohwaz on 2018-10-01 18:00:48
Other Links: manifest | tags
Context
2018-10-01
21:28
Refactorisation export CSV/ODS fonctionnel check-in: 789d2ab164 user: bohwaz tags: dev
18:00
Mise en commun export CSV/ODS (+ simplification) check-in: 72b8ff6c0f user: bohwaz tags: dev
17:36
Ajout recherche sur catégorie de membre check-in: b648e8d1ff user: bohwaz tags: dev
Changes

Modified src/include/lib/Garradin/Compta/Import.php from [19a1972b2f] to [8af5b583d1].

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
				LEFT JOIN compta_comptes AS debit ON debit.id = journal.compte_debit
				LEFT JOIN compta_comptes AS credit ON credit.id = journal.compte_credit
				LEFT JOIN compta_moyens_paiement AS moyen ON moyen.code = journal.moyen_paiement
			WHERE id_exercice = '.(int)$exercice.'
			ORDER BY journal.date;
		')->execute();
	}






	public function toCSV($exercice)
	{
		$res = $this->export($exercice);

		$fp = fopen('php://output', 'w');

		fputcsv($fp, $this->header);

		while ($row = $res->fetchArray(SQLITE3_ASSOC))
		{
			fputcsv($fp, $row);
		}

		fclose($fp);

		return true;
	}

    public function toODS($exercice)
    {
    	$result = $this->export($exercice);
        $ods = new ODSWriter;
        $ods->table_name = 'Journal';


        $ods->add($this->header);

        while ($row = $result->fetchArray(SQLITE3_ASSOC))
        {
        	unset($row->passe);
        	$ods->add($row);
        }

        $ods->output();
    }

	public function fromCSV($path)
	{
		if (!file_exists($path) || !is_readable($path))
		{
			throw new \RuntimeException('Fichier inconnu : '.$path);
		}







>
>
>
>
>



|
|
<

<
<
<
<
<
<
<
<
<
<
<
<
|
|
<
<
<
>
|
<
<
<
<
<
<
<
<
<
<







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
				LEFT JOIN compta_comptes AS debit ON debit.id = journal.compte_debit
				LEFT JOIN compta_comptes AS credit ON credit.id = journal.compte_credit
				LEFT JOIN compta_moyens_paiement AS moyen ON moyen.code = journal.moyen_paiement
			WHERE id_exercice = '.(int)$exercice.'
			ORDER BY journal.date;
		')->execute();
	}

	protected function exportName()
	{
		return sprintf('Export comptabilité - %s - %s', Config::getInstance()->get('nom_asso'), date('Y-m-d'));
	}

	public function toCSV($exercice)
	{
		return Utils::toCSV($this->exportName(), $this->export());
	}














	public function toODS($exercice)
	{



		return Utils::toODS($this->exportName(), $this->export());
	}











	public function fromCSV($path)
	{
		if (!file_exists($path) || !is_readable($path))
		{
			throw new \RuntimeException('Fichier inconnu : '.$path);
		}

Modified src/include/lib/Garradin/Membres/Import.php from [6fc6eacc71] to [b0e7e2ae10].

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
...
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

		$db->commit();
		return true;
	}

	/**
	 * Importer un CSV de la liste des membres depuis un export Garradin
	 * @param  string $path 	Chemin vers le CSV
	 * @param  int    $current_user_id
	 * @return boolean          TRUE en cas de succès
	 */
	public function fromGarradinCSV($path, $current_user_id)
	{
		if (!file_exists($path) || !is_readable($path))
		{
................................................................................

		fclose($fp);
		return true;
	}

	protected function export()
	{
        $db = DB::getInstance();

        $champs = Config::getInstance()->get('champs_membres')->getKeys();
        $champs_sql = 'm.' . implode(', m.', $champs);

        $res = $db->prepare('SELECT ' . $champs_sql . ', c.nom AS categorie FROM membres AS m 
            LEFT JOIN membres_categories AS c ON m.id_categorie = c.id ORDER BY c.id;')->execute();


        return [array_merge($champs, ['categorie']), $res];



	}

    public function toCSV()
    {
    	list($champs, $result) = $this->export();

        $fp = fopen('php://output', 'w');
        $header = false;

        while ($row = $result->fetchArray(SQLITE3_ASSOC))
        {
            unset($row->passe);

            if (!$header)
            {
                fputs($fp, Utils::row_to_csv(array_keys($row)));
                $header = true;
            }

            fputs($fp, Utils::row_to_csv($row));
        }

        fclose($fp);

        return true;
    }

    public function toODS()
    {
    	list($champs, $result) = $this->export();
        $ods = new ODSWriter;
        $ods->table_name = 'Membres';


        $ods->add($champs);

        while ($row = $result->fetchArray(SQLITE3_ASSOC))
        {
        	unset($row->passe);
        	$ods->add($row);
        }

        $ods->output();
    }
}







|







 







|

|
|

|
|

>
|
>
>
>


|
|
|
<
<
<
<
|
<
<
|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
|
|
|
<
<
>
|
<
|
<
<
<
<
<
<
<
<
<
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
...
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










		$db->commit();
		return true;
	}

	/**
	 * Importer un CSV de la liste des membres depuis un export Garradin
	 * @param  string $path     Chemin vers le CSV
	 * @param  int    $current_user_id
	 * @return boolean          TRUE en cas de succès
	 */
	public function fromGarradinCSV($path, $current_user_id)
	{
		if (!file_exists($path) || !is_readable($path))
		{
................................................................................

		fclose($fp);
		return true;
	}

	protected function export()
	{
		$db = DB::getInstance();

		$champs = Config::getInstance()->get('champs_membres')->getKeys();
		$champs_sql = 'm.' . implode(', m.', $champs);

		$res = $db->iterate('SELECT ' . $champs_sql . ', c.nom AS categorie FROM membres AS m 
			LEFT JOIN membres_categories AS c ON m.id_categorie = c.id ORDER BY c.id;');

		return [
			array_merge($champs, ['categorie']),
			$res,
			sprintf('Export membres - %s - %s', Config::getInstance()->get('nom_asso'), date('Y-m-d')),
		];
	}

	public function toCSV()
	{
		list($champs, $result, $name) = $this->export();




		return Utils::toCSV($name, $result, $champs);


	}














	public function toODS()
	{
		list($champs, $result, $name) = $this->export();


		return Utils::toODS($name, $result, $champs);
	}

}









Modified src/include/lib/Garradin/Utils.php from [c76bd4d270] to [1b2b85687d].

668
669
670
671
672
673
674

























































675
676
677
678
679
680
681

        array_walk($row, function (&$field) {
            $field = strtr($field, ['"' => '""', "\r\n" => "\n"]);
        });

        return sprintf("\"%s\"\r\n", implode('","', $row));
    }


























































    static public function sendEmail($recipient, $subject, $content, $id_membre = null, $pgp_key = null)
    {
        // Ne pas envoyer de mail à des adresses invalides
        if (!SMTP::checkEmailIsValid($recipient, false))
        {
            throw new UserException('Adresse email invalide: ' . $recipient);







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738

        array_walk($row, function (&$field) {
            $field = strtr($field, ['"' => '""', "\r\n" => "\n"]);
        });

        return sprintf("\"%s\"\r\n", implode('","', $row));
    }

    static public function toCSV($name, $iterator, $header = null)
    {
        header('Content-type: application/csv');
        header(sprintf('Content-Disposition: attachment; filename="%s.csv"', $name));

        $fp = fopen('php://output', 'w');

        if ($header)
        {
            fputs($fp, self::row_to_csv($header));
        }

        foreach ($iterator as $row)
        {
            if (!$header)
            {
                fputs($fp, self::row_to_csv(array_keys($row)));
                $header = true;
            }

            fputs($fp, self::row_to_csv($row));
        }

        fclose($fp);

        return true;
    }

    static public function toODS($name, $iterator, $header = null)
    {
        header('Content-type: application/vnd.oasis.opendocument.spreadsheet');
        header(sprintf('Content-Disposition: attachment; filename="%s.ods"', $name));

        $ods = new ODSWriter;
        $ods->table_name = $name;

        if ($header)
        {
            $ods->add($header);
        }

        foreach ($iterator as $row)
        {
            if (!$header)
            {
                $ods->add(array_keys($row));
                $header = true;
            }

            $ods->add($row);
        }

        $ods->output();

        return true;
    }

    static public function sendEmail($recipient, $subject, $content, $id_membre = null, $pgp_key = null)
    {
        // Ne pas envoyer de mail à des adresses invalides
        if (!SMTP::checkEmailIsValid($recipient, false))
        {
            throw new UserException('Adresse email invalide: ' . $recipient);

Modified src/www/admin/membres/import.php from [87bae42958] to [b710d208b2].

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

$import = new Membres\Import;

$tpl->assign('tab', null !== qg('export') ? 'export' : 'import');

if (qg('export') == 'csv')
{
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename="Export membres - ' . $config->get('nom_asso') . ' - ' . date('Y-m-d') . '.csv"');
    $import->toCSV();
    exit;
}
elseif (qg('export') == 'ods')
{
    header('Content-type: application/vnd.oasis.opendocument.spreadsheet');
    header('Content-Disposition: attachment; filename="Export membres - ' . $config->get('nom_asso') . ' - ' . date('Y-m-d') . '.ods"');
    $import->toODS();
    exit;
}

$champs = $config->get('champs_membres')->getAll();
$champs->date_inscription = (object) ['title' => 'Date inscription', 'type' => 'date'];








<
<





<
<







7
8
9
10
11
12
13


14
15
16
17
18


19
20
21
22
23
24
25

$import = new Membres\Import;

$tpl->assign('tab', null !== qg('export') ? 'export' : 'import');

if (qg('export') == 'csv')
{


    $import->toCSV();
    exit;
}
elseif (qg('export') == 'ods')
{


    $import->toODS();
    exit;
}

$champs = $config->get('champs_membres')->getAll();
$champs->date_inscription = (object) ['title' => 'Date inscription', 'type' => 'date'];