Overview
Comment:Utilisation d'objets anonymes plutôt que des tableaux associatifs
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dev
Files: files | file ages | folders
SHA1: 7bf5e8cf66d8bfc7b72028966d0970d45e3731d0
User & Date: bohwaz on 2017-05-01 06:40:09
Other Links: branch diff | manifest | tags
Context
2017-05-01
06:42
Modernisation : utilisation d'objets au lieu de tableaux et utilisation de Session check-in: 1b35af2dbe user: bohwaz tags: dev
06:40
Utilisation d'objets anonymes plutôt que des tableaux associatifs check-in: 7bf5e8cf66 user: bohwaz tags: dev
05:45
Modernisation du code check-in: 2eb85374b2 user: bohwaz tags: dev
Changes

Modified src/include/lib/Garradin/Membres/Champs.php from [2915d1081b] to [aa1f6d39ca].

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
        }
		else
		{
			$champs = parse_ini_string((string)$champs, true);

            foreach ($champs as $key=>&$config)
            {

                $this->_checkField($key, $config);
            }

            $this->champs = $champs;
		}
	}

	public function getTypes()
	{
		return $this->types;
	}

	public function get($champ, $key = null)
	{
        if ($champ == 'id')
        {
            return ['title' => 'Numéro unique', 'type' => 'number'];
        }

        if (!array_key_exists($champ, $this->champs))
            return null;

        if ($key !== null)
        {
            if (array_key_exists($key, $this->champs[$champ]))
                return $this->champs[$champ][$key];
            else
                return null;
        }

		return $this->champs[$champ];
	}

    public function isText($champ)
    {
        if (!array_key_exists($champ, $this->champs))
            return null;

        if (in_array($this->champs[$champ]['type'], $this->text_types))
            return true;
        else
            return false;
    }

	public function getAll()
	{
        $this->champs['passe']['title'] = 'Mot de passe';
		return $this->champs;
	}

    public function getList()
    {
        $champs = $this->champs;
        unset($champs['passe']);
        return $champs;
    }

    public function getFirst()
    {
        reset($this->champs);
        return key($this->champs);
    }

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

        $champs = array_filter($champs, function ($a) {
            return empty($a['list_row']) ? false : true;
        });

        uasort($champs, function ($a, $b) {
            if ($a['list_row'] == $b['list_row'])
                return 0;

            return ($a['list_row'] > $b['list_row']) ? 1 : -1;
        });













        return $champs;

    }

    /**
     * Vérifie la cohérence et la présence des bons éléments pour un champ
     * @param  string $name     Nom du champ
     * @param  array $config    Configuration du champ
     * @return boolean true
     */
    protected function _checkField($name, &$config)
    {
        if (!preg_match('!^\w+(_\w+)*$!', $name))
        {
            throw new UserException('Le nom du champ est invalide.');
        }

        foreach ($config as $key=>&$value)
        {
            // Champ install non pris en compte
            if ($key == 'install')
            {
                unset($config[$key]);
                continue;
            }

            if (!in_array($key, $this->config_fields))
            {
                throw new \BadMethodCallException('Champ '.$key.' non valide.');
            }







>



|












|


|




|
|




|




|


|







|





|
|



<
<
<
<
<
<


|


|



|


|


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








|











|







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
        }
		else
		{
			$champs = parse_ini_string((string)$champs, true);

            foreach ($champs as $key=>&$config)
            {
                $config = (object) $config;
                $this->_checkField($key, $config);
            }

            $this->champs = (object) $champs;
		}
	}

	public function getTypes()
	{
		return $this->types;
	}

	public function get($champ, $key = null)
	{
        if ($champ == 'id')
        {
            return (object) ['title' => 'Numéro unique', 'type' => 'number'];
        }

        if (!property_exists($this->champs, $champ))
            return null;

        if ($key !== null)
        {
            if (property_exists($this->champs[$champ], $key))
                return $this->champs->$champ->$key;
            else
                return null;
        }

		return $this->champs->$champ;
	}

    public function isText($champ)
    {
        if (!property_exists($this->champs, $champ))
            return null;

        if (in_array($this->champs->$champ->type, $this->text_types))
            return true;
        else
            return false;
    }

	public function getAll()
	{
        $this->champs->passe->title = 'Mot de passe';
		return $this->champs;
	}

    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;
        });

        uasort($champs, function ($a, $b) {
            if ($a->list_row == $b->list_row)
                return 0;

            return ($a->list_row > $b->list_row) ? 1 : -1;
        });

        return (object) $champs;
    }

    public function getFirstListed()
    {
        foreach ($this->champs as $key=>$config)
        {
            if (empty($config->list_row))
            {
                continue;
            }

            return $key;
        }
    }

    /**
     * Vérifie la cohérence et la présence des bons éléments pour un champ
     * @param  string $name     Nom du champ
     * @param  array $config    Configuration du champ
     * @return boolean true
     */
    protected function _checkField($name, \stdClass &$config)
    {
        if (!preg_match('!^\w+(_\w+)*$!', $name))
        {
            throw new UserException('Le nom du champ est invalide.');
        }

        foreach ($config as $key=>&$value)
        {
            // Champ install non pris en compte
            if ($key == 'install')
            {
                unset($config->$key);
                continue;
            }

            if (!in_array($key, $this->config_fields))
            {
                throw new \BadMethodCallException('Champ '.$key.' non valide.');
            }
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
                    {
                        unset($value[$option_key]);
                    }
                }
            }
        }

        if (empty($config['title']) && $name != 'passe')
        {
            throw new UserException('Champ "'.$name.'" : Le titre est obligatoire.');
        }

        if (empty($config['type']) || !array_key_exists($config['type'], $this->types))
        {
            throw new UserException('Champ "'.$name.'" : Le type est vide ou non valide.');
        }

        if ($name == 'email' && $config['type'] != 'email')
        {
            throw new UserException('Le champ email ne peut être d\'un type différent de email.');
        }

        if ($name == 'passe' && $config['type'] != 'password')
        {
            throw new UserException('Le champ mot de passe ne peut être d\'un type différent de mot de passe.');
        }

        if (($config['type'] == 'multiple' || $config['type'] == 'select') && empty($config['options']))
        {
            throw new UserException('Le champ "'.$name.'" nécessite de comporter au moins une option possible.');
        }

        if (!array_key_exists('editable', $config))
        {
            $config['editable'] = false;
        }

        if (!array_key_exists('mandatory', $config))
        {
            $config['mandatory'] = false;
        }

        if (!array_key_exists('private', $config))
        {
            $config['private'] = false;
        }

        return true;
    }








|




|




|




|




|




|




|




|







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
                    {
                        unset($value[$option_key]);
                    }
                }
            }
        }

        if (empty($config->title) && $name != 'passe')
        {
            throw new UserException('Champ "'.$name.'" : Le titre est obligatoire.');
        }

        if (empty($config->type) || !array_key_exists($config->type, $this->types))
        {
            throw new UserException('Champ "'.$name.'" : Le type est vide ou non valide.');
        }

        if ($name == 'email' && $config->type != 'email')
        {
            throw new UserException('Le champ email ne peut être d\'un type différent de email.');
        }

        if ($name == 'passe' && $config->type != 'password')
        {
            throw new UserException('Le champ mot de passe ne peut être d\'un type différent de mot de passe.');
        }

        if (($config->type == 'multiple' || $config->type == 'select') && empty($config->options))
        {
            throw new UserException('Le champ "'.$name.'" nécessite de comporter au moins une option possible.');
        }

        if (!property_exists($config, 'editable'))
        {
            $config['editable'] = false;
        }

        if (!property_exists($config, 'mandatory'))
        {
            $config['mandatory'] = false;
        }

        if (!property_exists($config, 'private'))
        {
            $config['private'] = false;
        }

        return true;
    }

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
    	];

        $anciens_champs = $config->get('champs_membres');
    	$anciens_champs = is_null($anciens_champs) ? $this->champs : $anciens_champs->getAll();

    	foreach ($this->champs as $key=>$cfg)
    	{
    		if ($cfg['type'] == 'number')
    			$type = 'FLOAT';
    		elseif ($cfg['type'] == 'multiple' || $cfg['type'] == 'checkbox')
    			$type = 'INTEGER';
    		elseif ($cfg['type'] == 'file')
    			$type = 'BLOB';
    		else
    			$type = 'TEXT';

    		$line = $key . ' ' . $type . ',';

            if (!empty($cfg['title']))
            {
                $line .= ' -- ' . str_replace(["\n", "\r"], '', $cfg['title']);
            }

            $create[] = $line;

    		if (array_key_exists($key, $anciens_champs))
    		{
    			$copy[] = $key;
    		}
    	}

    	$create = array_merge($create, $create_keys);








|

|

|






|

|




|







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
    	];

        $anciens_champs = $config->get('champs_membres');
    	$anciens_champs = is_null($anciens_champs) ? $this->champs : $anciens_champs->getAll();

    	foreach ($this->champs as $key=>$cfg)
    	{
    		if ($cfg->type == 'number')
    			$type = 'FLOAT';
    		elseif ($cfg->type == 'multiple' || $cfg->type == 'checkbox')
    			$type = 'INTEGER';
    		elseif ($cfg->type == 'file')
    			$type = 'BLOB';
    		else
    			$type = 'TEXT';

    		$line = $key . ' ' . $type . ',';

            if (!empty($cfg->title))
            {
                $line .= ' -- ' . str_replace(["\n", "\r"], '', $cfg->title);
            }

            $create[] = $line;

    		if (property_exists($anciens_champs, $key))
    		{
    			$copy[] = $key;
    		}
    	}

    	$create = array_merge($create, $create_keys);

Modified src/include/lib/Garradin/Template.php from [fe21e67b41] to [438b56c467].

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

function tpl_html_champ_membre($params)
{
    if (empty($params['config']) || empty($params['name']))
        throw new \BadFunctionCallException('Paramètres type et name obligatoires.');

    $config = $params['config'];
    $type = $config['type'];

    if ($params['name'] == 'passe' || (!empty($params['user_mode']) && !empty($config['private'])))
    {
        return '';
    }



    if ($type == 'select')
    {
        if (empty($config['options']))

            throw new \BadFunctionCallException('Paramètre options obligatoire pour champ de type select.');



    }
    elseif ($type == 'country')
    {
        $type = 'select';
        $config['options'] = Utils::getCountryList();
        $params['default'] = Config::getInstance()->get('pays');
    }
    elseif ($type == 'date')
    {
        $params['pattern'] = '\d{4}-\d{2}-\d{2}';
    }
    elseif ($type == 'multiple')
    {
        if (empty($config['options']))
            throw new \BadFunctionCallException('Paramètre options obligatoire pour champ de type multiple.');
    }

    $field = '';
    $value = tpl_form_field($params);
    $attributes = 'name="' . htmlspecialchars($params['name'], ENT_QUOTES, 'UTF-8') . '" ';
    $attributes .= 'id="f_' . htmlspecialchars($params['name'], ENT_QUOTES, 'UTF-8') . '" ';

    if (!empty($params['disabled']))
    {
        $attributes .= 'disabled="disabled" ';
    }

    if (!empty($config['mandatory']))
    {
        $attributes .= 'required="required" ';
    }

    if (!empty($params['user_mode']) && empty($config['editable']))
    {
        $out = '<dt>' . htmlspecialchars($config['title'], ENT_QUOTES, 'UTF-8') . '</dt>';
        $out .= '<dd>' . (trim($value) === '' ? 'Non renseigné' : tpl_display_champ_membre($value, $config)) . '</dd>';
        return $out;
    }

    if ($type == 'select')
    {
        $field .= '<select '.$attributes.'>';
        foreach ($config['options'] as $k=>$v)
        {
            if (is_int($k))
                $k = $v;

            $field .= '<option value="' . htmlspecialchars($k, ENT_QUOTES, 'UTF-8') . '"';

            if ($value == $k || empty($value) && !empty($params['default']))







|

|




>
>
|

|
>
|
>
>
>




|






<
<
<
<
<











|




|

|







|







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

function tpl_html_champ_membre($params)
{
    if (empty($params['config']) || empty($params['name']))
        throw new \BadFunctionCallException('Paramètres type et name obligatoires.');

    $config = $params['config'];
    $type = $config->type;

    if ($params['name'] == 'passe' || (!empty($params['user_mode']) && !empty($config->private)))
    {
        return '';
    }

    $options = [];

    if ($type == 'select' || $type == 'multiple')
    {
        if (empty($config->options))
        {
            throw new \BadFunctionCallException('Paramètre options obligatoire pour champ de type ' . $type);
        }

        $options = (array) $config->options;
    }
    elseif ($type == 'country')
    {
        $type = 'select';
        $options = Utils::getCountryList();
        $params['default'] = Config::getInstance()->get('pays');
    }
    elseif ($type == 'date')
    {
        $params['pattern'] = '\d{4}-\d{2}-\d{2}';
    }






    $field = '';
    $value = tpl_form_field($params);
    $attributes = 'name="' . htmlspecialchars($params['name'], ENT_QUOTES, 'UTF-8') . '" ';
    $attributes .= 'id="f_' . htmlspecialchars($params['name'], ENT_QUOTES, 'UTF-8') . '" ';

    if (!empty($params['disabled']))
    {
        $attributes .= 'disabled="disabled" ';
    }

    if (!empty($config->mandatory))
    {
        $attributes .= 'required="required" ';
    }

    if (!empty($params['user_mode']) && empty($config->editable))
    {
        $out = '<dt>' . htmlspecialchars($config->title, ENT_QUOTES, 'UTF-8') . '</dt>';
        $out .= '<dd>' . (trim($value) === '' ? 'Non renseigné' : tpl_display_champ_membre($value, $config)) . '</dd>';
        return $out;
    }

    if ($type == 'select')
    {
        $field .= '<select '.$attributes.'>';
        foreach ($options as $k=>$v)
        {
            if (is_int($k))
                $k = $v;

            $field .= '<option value="' . htmlspecialchars($k, ENT_QUOTES, 'UTF-8') . '"';

            if ($value == $k || empty($value) && !empty($params['default']))
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
    {
        if (is_array($value))
        {
            $binary = 0;

            foreach ($value as $k => $v)
            {
                if (array_key_exists($k, $config['options']) && !empty($v))
                {
                    $binary |= 0x01 << $k;
                }
            }

            $value = $binary;
        }

        foreach ($config['options'] as $k=>$v)
        {
            $b = 0x01 << (int)$k;
            $field .= '<label><input type="checkbox" name="' 
                . htmlspecialchars($params['name'], ENT_QUOTES, 'UTF-8') . '[' . (int)$k . ']" value="1" '
                . (($value & $b) ? 'checked="checked"' : '') . ' ' . $attributes . '/> ' 
                . htmlspecialchars($v, ENT_QUOTES, 'UTF-8') . '</label><br />';
        }







|








|







459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
    {
        if (is_array($value))
        {
            $binary = 0;

            foreach ($value as $k => $v)
            {
                if (array_key_exists($k, $options) && !empty($v))
                {
                    $binary |= 0x01 << $k;
                }
            }

            $value = $binary;
        }

        foreach ($options as $k=>$v)
        {
            $b = 0x01 << (int)$k;
            $field .= '<label><input type="checkbox" name="' 
                . htmlspecialchars($params['name'], ENT_QUOTES, 'UTF-8') . '[' . (int)$k . ']" value="1" '
                . (($value & $b) ? 'checked="checked"' : '') . ' ' . $attributes . '/> ' 
                . htmlspecialchars($v, ENT_QUOTES, 'UTF-8') . '</label><br />';
        }
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

    if ($type == 'checkbox')
    {
        $out .= $field . ' ';
    }

    $out .= '<label for="f_' . htmlspecialchars($params['name'], ENT_QUOTES, 'UTF-8') . '">'
        . htmlspecialchars($config['title'], ENT_QUOTES, 'UTF-8') . '</label>';

    if (!empty($config['mandatory']))
    {
        $out .= ' <b title="(Champ obligatoire)">obligatoire</b>';
    }

    $out .= '</dt>';

    if (!empty($config['help']))
    {
        $out .= '
    <dd class="help">' . htmlspecialchars($config['help'], ENT_QUOTES, 'UTF-8') . '</dd>';
    }

    if ($type != 'checkbox')
    {
        $out .= '
    <dd>' . $field . '</dd>';
    }

    return $out;
}

function tpl_display_champ_membre($v, $config)
{
    if ($config['type'] == 'checkbox')
    {

        return $v ? 'Oui' : 'Non';
    }
    elseif ($config['type'] == 'email')
    {
        return '<a href="mailto:' . rawurlencode($v) . '">' . htmlspecialchars($v) . '</a>';
    }
    elseif ($config['type'] == 'tel')
    {
        return '<a href="tel:' . rawurlencode($v) . '">' . htmlspecialchars($v) . '</a>';
    }
    elseif ($config['type'] == 'url')
    {
        return '<a href="' . htmlspecialchars($v) . '">' . htmlspecialchars($v) . '</a>';
    }
    elseif ($config['type'] == 'country') 
    {
        return Utils::getCountryName($v);
    }
    elseif ($config['type'] == 'multiple')
    {
        $out = [];

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

        return implode(', ', $out);
    }
    else
    {
        return htmlspecialchars($v);
    }
}

$tpl->register_function('csrf_field', 'Garradin\tpl_csrf_field');
$tpl->register_function('form_field', 'Garradin\tpl_form_field');
$tpl->register_function('select_compte', 'Garradin\tpl_select_compte');








|

|






|


|













|

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

|
|
|
|
|

|
<
|
<
|







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

    if ($type == 'checkbox')
    {
        $out .= $field . ' ';
    }

    $out .= '<label for="f_' . htmlspecialchars($params['name'], ENT_QUOTES, 'UTF-8') . '">'
        . htmlspecialchars($config->title, ENT_QUOTES, 'UTF-8') . '</label>';

    if (!empty($config->mandatory))
    {
        $out .= ' <b title="(Champ obligatoire)">obligatoire</b>';
    }

    $out .= '</dt>';

    if (!empty($config->help))
    {
        $out .= '
    <dd class="help">' . htmlspecialchars($config->help, ENT_QUOTES, 'UTF-8') . '</dd>';
    }

    if ($type != 'checkbox')
    {
        $out .= '
    <dd>' . $field . '</dd>';
    }

    return $out;
}

function tpl_display_champ_membre($v, $config)
{
    switch ($config->type)
    {
        case 'checkbox':
            return $v ? 'Oui' : 'Non';

        case 'email':

            return '<a href="mailto:' . rawurlencode($v) . '">' . htmlspecialchars($v) . '</a>';

        case 'tel':

            return '<a href="tel:' . rawurlencode($v) . '">' . htmlspecialchars($v) . '</a>';

        case 'url':

            return '<a href="' . htmlspecialchars($v) . '">' . htmlspecialchars($v) . '</a>';

        case 'country':

            return Utils::getCountryName($v);

        case 'multiple':

            $out = [];

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

            return implode(', ', $out);

        default:

            return htmlspecialchars($v);
    }
}

$tpl->register_function('csrf_field', 'Garradin\tpl_csrf_field');
$tpl->register_function('form_field', 'Garradin\tpl_form_field');
$tpl->register_function('select_compte', 'Garradin\tpl_select_compte');