Overview
Comment:Gérer les champs à choix multiple à l'export et import
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | stable
Files: files | file ages | folders
SHA1: f5de34b5073d5a5711e7ab35bf12c82e3bb503de
User & Date: bohwaz on 2019-11-17 17:35:48
Other Links: manifest | tags
Context
2019-11-17
18:12
Autoriser d'autres présentations du numéro de téléphone pour les autres pays check-in: c58cdbb392 user: bohwaz tags: trunk, stable
17:35
Gérer les champs à choix multiple à l'export et import check-in: f5de34b507 user: bohwaz tags: trunk, stable
16:58
Ajout colonne Projet dans l'export compta check-in: c2cddaf5e8 user: bohwaz tags: trunk
Changes

Modified src/include/lib/Garradin/Membres/Champs.php from [9b116040de] to [1e1a261c03].

173
174
175
176
177
178
179













180
181
182
183
184
185
186
    public function getList()
    {
        $champs = clone $this->champs;
        unset($champs->passe);

        return $champs;
    }














    public function getListedFields()
    {
        $champs = (array) $this->champs;

        $champs = array_filter($champs, function ($a) {
            return empty($a->list_row) ? false : true;







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







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
    public function getList()
    {
        $champs = clone $this->champs;
        unset($champs->passe);

        return $champs;
    }

    public function getMultiples()
    {
        $out = [];

        foreach ($this->champs as $id => $champ) {
            if ($champ->type == 'multiple') {
                $out[$id] = $champ;
            }
        }

        return $out;
    }

    public function getListedFields()
    {
        $champs = (array) $this->champs;

        $champs = array_filter($champs, function ($a) {
            return empty($a->list_row) ? false : true;

Modified src/include/lib/Garradin/Membres/Import.php from [7b54b35f93] to [0f6e3bb8ff].

187
188
189
190
191
192
193
194

195
196
197
198
199
200
201
202
		}

		$db = DB::getInstance();
		$db->begin();
		$membres = new Membres;

		// On récupère les champs qu'on peut importer
		$champs = Config::getInstance()->get('champs_membres')->getAll();

		$champs = array_keys((array)$champs);
		$champs[] = 'date_inscription';
		//$champs[] = 'date_connexion';
		//$champs[] = 'id';
		//$champs[] = 'id_categorie';

		$line = 0;
		$delim = Utils::find_csv_delim($fp);







|
>
|







187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
		}

		$db = DB::getInstance();
		$db->begin();
		$membres = new Membres;

		// On récupère les champs qu'on peut importer
		$champs_membres = Config::getInstance()->get('champs_membres');
		$champs_multiples = $champs_membres->getMultiples();
		$champs = $champs_membres->getKeys();
		$champs[] = 'date_inscription';
		//$champs[] = 'date_connexion';
		//$champs[] = 'id';
		//$champs[] = 'id_categorie';

		$line = 0;
		$delim = Utils::find_csv_delim($fp);
237
238
239
240
241
242
243

244



245















246
247
248
249
250
251
252
			{
				$name = trim($name);

				// Champs qui n'existent pas dans le schéma actuel
				if (!in_array($name, $champs))
					continue;


				if (trim($row[$id]) !== '')



					$data[$name] = $row[$id];















			}

			if (!empty($data['numero']) && $data['numero'] > 0)
			{
				$numero = (int)$data['numero'];
			}
			else







>
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
			{
				$name = trim($name);

				// Champs qui n'existent pas dans le schéma actuel
				if (!in_array($name, $champs))
					continue;

				// Ignorer les champs vides
				if (trim($row[$id]) === '') {
					continue;
				}

				$data[$name] = $row[$id];

				// Restitution de la valeur binaire des champs à choix multiple
				if (isset($champs_multiples[$name])) {
					$values = explode(';', $data[$name]);
					$data[$name] = 0;

					foreach ($values as $v) {
						$v = trim($v);
						$found = array_search($v, $champs_multiples[$name]->options);

						if ($found) {
							$data[$name] |= 0x01 << $found;
						}
					}
				}
			}

			if (!empty($data['numero']) && $data['numero'] > 0)
			{
				$numero = (int)$data['numero'];
			}
			else
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






















		$db = DB::getInstance();

		$champs = Config::getInstance()->get('champs_membres')->getKeys();
		$champs = array_map([$db, 'quoteIdentifier'], $champs);
		$champs_sql = 'm.' . implode(', m.', $champs);
		$where = $list ? 'WHERE ' . $db->where('m.id', $list) : '';

		$res = $db->iterate('SELECT ' . $champs_sql . ', c.nom AS "Catégorie membre" FROM membres AS m 
			INNER JOIN membres_categories AS c ON m.id_categorie = c.id
			' . $where . '
			ORDER BY c.id;');

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

	public function toCSV(array $list = null)
	{
		list($champs, $result, $name) = $this->export($list);
		return Utils::toCSV($name, $result, $champs);
	}

	public function toODS(array $list = null)
	{
		list($champs, $result, $name) = $this->export($list);
		return Utils::toODS($name, $result, $champs);
	}
}





























|














|





|

|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
		$db = DB::getInstance();

		$champs = Config::getInstance()->get('champs_membres')->getKeys();
		$champs = array_map([$db, 'quoteIdentifier'], $champs);
		$champs_sql = 'm.' . implode(', m.', $champs);
		$where = $list ? 'WHERE ' . $db->where('m.id', $list) : '';

		$res = $db->iterate('SELECT ' . $champs_sql . ', c.nom AS "Catégorie membre" FROM membres AS m
			INNER JOIN membres_categories AS c ON m.id_categorie = c.id
			' . $where . '
			ORDER BY c.id;');

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

	public function toCSV(array $list = null)
	{
		list($champs, $result, $name) = $this->export($list);
		return Utils::toCSV($name, $result, $champs, [$this, 'exportRow']);
	}

	public function toODS(array $list = null)
	{
		list($champs, $result, $name) = $this->export($list);
		return Utils::toODS($name, $result, $champs, [$this, 'exportRow']);
	}

	public function exportRow(object $row) {
		// Pas hyper efficace, il faudrait ne pas récupérer la liste pour chaque ligne... FIXME
		$champs_multiples = Config::getInstance()->get('champs_membres')->getMultiples();

		// convertir les champs à choix multiple de binaire vers liste séparée par des points virgules
		foreach ($champs_multiples as $id=>$config) {
			$out = [];

			foreach ($config->options as $b => $name)
			{
				if ($row->$id & (0x01 << $b)) {
					$out[] = $name;
				}
			}

			$row->$id = implode(';', $out);

		}

		return $row;
	}
}

Modified src/include/lib/Garradin/Template.php from [13a564ba39] to [9944aa0b81].

277
278
279
280
281
282
283
284
285
286
287
288
289
290
291

				foreach ($config->options as $b => $name)
				{
					if ($v & (0x01 << $b))
						$out[] = $name;
				}

				return implode(', ', $out);
			default:
				return htmlspecialchars($v);
		}
	}

	protected function formChampMembre($params)
	{







|







277
278
279
280
281
282
283
284
285
286
287
288
289
290
291

				foreach ($config->options as $b => $name)
				{
					if ($v & (0x01 << $b))
						$out[] = $name;
				}

				return htmlspecialchars(implode(', ', $out));
			default:
				return htmlspecialchars($v);
		}
	}

	protected function formChampMembre($params)
	{

Modified src/include/lib/Garradin/Utils.php from [5e411bcff2] to [3b510ca29b].

664
665
666
667
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
        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((array) $header);
        }

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





            $ods->add((array) $row);
        }

        $ods->output();

        return true;







|


















>
>
>
>









|



















>
>
>
>







664
665
666
667
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
        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, $row_map_callback = 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;
            }

            if (null !== $row_map_callback) {
                $row = call_user_func($row_map_callback, $row);
            }

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

        fclose($fp);

        return true;
    }

    static public function toODS($name, $iterator, $header = null, $row_map_callback = 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((array) $header);
        }

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

            if (null !== $row_map_callback) {
                $row = call_user_func($row_map_callback, $row);
            }

            $ods->add((array) $row);
        }

        $ods->output();

        return true;