Differences From Artifact [9cbfc1c785]:

To Artifact [e8bc40a4a8]:


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
...
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
    }

    public function getIDWithNumero($numero)
    {
        return DB::getInstance()->firstColumn('SELECT id FROM membres WHERE numero = ?;', (int) $numero);
    }

    public function buildSQLSearchQuery(array $groups, $order, $desc = false, $limit = 100)
    {
        $db = DB::getInstance();
        $config = Config::getInstance();

        $champs = $config->get('champs_membres');
        $colonnes = [];

        $query_groups = [];

        foreach ($groups as $group)
        {
            if (!isset($group['conditions'], $group['operator'])
                || !is_array($group['conditions'])
                || ($group['operator'] != 'AND' && $group['operator'] != 'OR'))
            {
                // Ignorer les groupes de conditions invalides
                continue;
            }

            $query_group_conditions = [];

            foreach ($group['conditions'] as $condition)
            {
                if (!isset($condition['column'], $condition['operator'])
                    || (isset($condition['values']) && !is_array($condition['values'])))
                {
                    // Ignorer les conditions invalides
                    continue;
                }

                if (!$champs->get($condition['column']))
                {
                    // Ignorer une condition qui se rapporte à une colonne
                    // qui n'existe pas, cas possible si on reprend une recherche
                    // après avoir modifié les fiches de membres
                    continue;
                }

                $colonnes[] = $condition['column'];
                $champ = $champs->get($condition['column']);

                if ($champs->isText($condition['column']))
                {
                    $query = sprintf('transliterate_to_ascii(%s) COLLATE NOCASE %s', $db->quoteIdentifier($condition['column']), $condition['operator']);
                }
                else
                {
                    $query = sprintf('%s %s', $db->quoteIdentifier($condition['column']), $condition['operator']);
                }

                $values = isset($condition['values']) ? $condition['values'] : [];

                $values = array_map(['Garradin\Utils', 'transliterateToAscii'], $values);
                
                if ($champ->type == 'tel')
                {
                    // Normaliser le numéro de téléphone
                    $values = array_map(['Garradin\Utils', 'normalizePhoneNumber'], $values);
                }

                if ($condition['operator'] == '&')
                {
                    $new_query = [];

                    foreach ($values as $value)
                    {
                        $new_query[] = sprintf('%s (1 << %d)', $query, (int) $value);
                    }

                    $query = '(' . implode(' AND ', $new_query) . ')';
                }
                elseif (strpos($query, '??') !== false)
                {
                    $values = array_map([$db, 'quote'], $values);
                    $query = str_replace('??', implode(', ', $values), $query);
                }
                elseif (preg_match('/%\?%|%\?|\?%/', $query, $match))
                {
                    $value = str_replace(['%_'], ['\\%', '\\_'], reset($values));
                    $value = str_replace('?', $value, $match[0]);
                    $query = str_replace($match[0], sprintf('%s ESCAPE \'\\\'', $db->quote($value)), $query);
                }
                elseif (strpos($query, '?') !== false)
                {
                    $expected = substr_count($query, '?');
                    $found = count($values);

                    if ($expected != $found)
                    {
                        throw new \RuntimeException(sprintf('Operator %s expects at least %d parameters, only %d supplied', $condition['operator'], $expected, $found));
                    }

                    for ($i = 0; $i < $expected; $i++)
                    {
                        $pos = strpos($query, '?');
                        $query = substr_replace($query, $db->quote(array_shift($values)), $pos, 1);
                    }
                }

                $query_group_conditions[] = $query;
            }

            $query_groups[] = implode(' ' . $group['operator'] . ' ', $query_group_conditions);
        }

        $colonnes = array_unique($colonnes);

        if (!in_array($config->get('champ_identite'), $colonnes))
        {
            array_unshift($colonnes, $config->get('champ_identite'));
        }

        $colonnes = array_map([$db, 'quoteIdentifier'], $colonnes);

        if ($champs->isText($order))
        {
            $order = sprintf('transliterate_to_ascii(%s) COLLATE NOCASE', $db->quoteIdentifier($order));
        }
        else
        {
            $order = $db->quoteIdentifier($order);
        }

        $sql_query = sprintf('SELECT id, %s FROM membres WHERE %s ORDER BY %s %s LIMIT %d;',
            implode(', ', $colonnes),
            '(' . implode(') AND (', $query_groups) . ')',
            $order,
            $desc ? 'DESC' : 'ASC',
            (int) $limit);

        return $sql_query;
    }

    public function getSearchHeaderFields(array $result)
    {
        if (!count($result))
        {
            return false;
        }

................................................................................
                $fields[$field] = $config;
            }
        }

        return $fields;
    }

    public function searchSQL($query)
    {
        $db = DB::getInstance();

        if (!preg_match('/LIMIT\s+/i', $query))
        {
            $query = preg_replace('/;?\s*$/', '', $query);
            $query .= ' LIMIT 100';
        }

        if (preg_match('/;\s*(.+?)$/', $query))
        {
            throw new UserException('Une seule requête peut être envoyée en même temps.');
        }

        $st = $db->prepare($query);

        if (!$st->readOnly())
        {
            throw new UserException('Seules les requêtes en lecture sont autorisées.');
        }

        $res = $st->execute();
        $out = [];

        while ($row = $res->fetchArray(SQLITE3_ASSOC))
        {
            if (array_key_exists('passe', $row))
            {
                unset($row['passe']);
            }
            
            $out[] = (object) $row;
        }

        return $out;
    }

    public function schemaSQL()
    {
        $db = DB::getInstance();

        $tables = [
            'membres'   =>  $db->firstColumn('SELECT sql FROM sqlite_master WHERE type = \'table\' AND name = \'membres\';'),
            'categories'=>  $db->firstColumn('SELECT sql FROM sqlite_master WHERE type = \'table\' AND name = \'membres_categories\';'),
        ];

        return $tables;
    }
    public function listByCategory($cat, $fields, $page = 1, $order = null, $desc = false)
    {
        $begin = ($page - 1) * self::ITEMS_PER_PAGE;

        $db = DB::getInstance();
        $config = Config::getInstance();








<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







290
291
292
293
294
295
296






































































































































297
298
299
300
301
302
303
...
311
312
313
314
315
316
317

















































318
319
320
321
322
323
324
    }

    public function getIDWithNumero($numero)
    {
        return DB::getInstance()->firstColumn('SELECT id FROM membres WHERE numero = ?;', (int) $numero);
    }







































































































































    public function getSearchHeaderFields(array $result)
    {
        if (!count($result))
        {
            return false;
        }

................................................................................
                $fields[$field] = $config;
            }
        }

        return $fields;
    }


















































    public function listByCategory($cat, $fields, $page = 1, $order = null, $desc = false)
    {
        $begin = ($page - 1) * self::ITEMS_PER_PAGE;

        $db = DB::getInstance();
        $config = Config::getInstance();