Overview
Comment:Corrections et améliorations des formulaires de membres
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dev
Files: files | file ages | folders
SHA1: 01e8abeba8f07dbfe595848170fb0da9965cac8e
User & Date: bohwaz on 2017-05-26 06:32:32
Other Links: branch diff | manifest | tags
Context
2017-05-30
00:28
Renvoyer la taille du fichier pour permettre d'avoir une progression du téléchargement check-in: f286f9d40f user: bohwaz tags: dev
2017-05-26
06:32
Corrections et améliorations des formulaires de membres check-in: 01e8abeba8 user: bohwaz tags: dev
06:27
Utiliser les options du cookie directement check-in: c1e0f2b273 user: bohwaz tags: dev
Changes

Modified src/include/lib/Garradin/Form.php from [974d7760bb] to [fc2bdd0ba9].

38
39
40
41
42
43
44
45
46
47





48
49
50
51
52





53
54
55
56
57
58
59
..
76
77
78
79
80
81
82




83
84
85
86
87
88
89
90
91
92
	}

	public function addError($msg)
	{
		$this->errors[] = $msg;
	}

	public function getErrorMessages()
	{
		$errors = [];






		foreach ($this->errors as $error)
		{
			if (is_array($error))
			{





				$errors[] = $this->getErrorMessage($error['rule'], $error['name'], $error['params']);
			}
			else
			{
				$errors[] = $error;
			}
		}
................................................................................
		{
			case 'required':
				return sprintf('Le champ %s est vide.', $element);
			case 'min':
				return sprintf('Le champ %s doit faire au moins %d caractères.', $element, $params[0]);
			case 'file':
				return sprintf('Le fichier envoyé n\'est pas valide.');




			default:
				return sprintf('Erreur "%s" dans le champ "%s"', $rule, $element);
		}
	}

	public function __invoke($key)
	{
		return \KD2\Form::get($key);
	}
}







|


>
>
>
>
>





>
>
>
>
>







 







>
>
>
>










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
..
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
	}

	public function addError($msg)
	{
		$this->errors[] = $msg;
	}

	public function getErrorMessages($membre = false)
	{
		$errors = [];

		if ($membre)
		{
			$champs = Config::getInstance()->get('champs_membres');
		}

		foreach ($this->errors as $error)
		{
			if (is_array($error))
			{
				if ($membre)
				{
					$error['name'] = $champs->get($error['name'], 'title');
				}

				$errors[] = $this->getErrorMessage($error['rule'], $error['name'], $error['params']);
			}
			else
			{
				$errors[] = $error;
			}
		}
................................................................................
		{
			case 'required':
				return sprintf('Le champ %s est vide.', $element);
			case 'min':
				return sprintf('Le champ %s doit faire au moins %d caractères.', $element, $params[0]);
			case 'file':
				return sprintf('Le fichier envoyé n\'est pas valide.');
			case 'confirmed':
				return sprintf('La vérification du champ %s n\'est pas identique au champ lui-même.', $element);
			case 'date_format':
				return sprintf('Format de date invalide dans le champ %s.', $element);
			default:
				return sprintf('Erreur "%s" dans le champ "%s"', $rule, $element);
		}
	}

	public function __invoke($key)
	{
		return \KD2\Form::get($key);
	}
}

Modified src/include/lib/Garradin/Membres.php from [ad31ed57c8] to [615bdf928b].

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
...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
...
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
                {
                    continue;
                }
            }

            if (isset($data[$key]))
            {
                if ($config->type == 'email' && trim($data[$key]) !== '' && !filter_var($data[$key], FILTER_VALIDATE_EMAIL))
                {
                    throw new UserException('Adresse e-mail invalide dans le champ "' . $config->title . '".');
                }
                elseif ($config->type == 'url' && trim($data[$key]) !== '' && !filter_var($data[$key], FILTER_VALIDATE_URL))
                {
                    throw new UserException('Adresse URL invalide dans le champ "' . $config->title . '".');
                }
                elseif ($config->type == 'date' && trim($data[$key]) !== '' && !Utils::checkDate($data[$key]))
                {
                    throw new UserException('Date invalide "' . $config->title . '", format attendu : AAAA-MM-JJ.');
                }
                elseif ($config->type == 'datetime' && trim($data[$key]) !== '')
                {
                    if (!Utils::checkDateTime($data[$key]) || !($dt = new DateTime($data[$key])))
                    {
                        throw new UserException('Date invalide "' . $config->title . '", format attendu : AAAA-MM-JJ HH:mm.');
                    }

                    $data[$key] = $dt->format('Y-m-d H:i');
                }
                elseif ($config->type == 'tel')
                {
                    $data[$key] = Utils::normalizePhoneNumber($data[$key]);
                }
                elseif ($config->type == 'country')
................................................................................
                }
                elseif ($config->type == 'number' && trim($data[$key]) !== '')
                {
                    if (empty($data[$key]))
                    {
                        $data[$key] = 0;
                    }

                    if (!is_numeric($data[$key]))
                        throw new UserException('Le champ "' . $config->title . '" doit contenir un chiffre.');
                }
                elseif ($config->type == 'select' && !in_array($data[$key], $config->options))
                {
                    throw new UserException('Le champ "' . $config->title . '" ne correspond pas à un des choix proposés.');
                }
                elseif ($config->type == 'multiple')
                {
................................................................................
                if (is_string($data[$key]) && trim($data[$key]) === '')
                {
                    $data[$key] = null;
                }
            }
        }

        if (isset($data['code_postal']) && trim($data['code_postal']) != '')
        {
            if (!empty($data['pays']) && $data['pays'] == 'FR' && !preg_match('!^\d{5}$!', $data['code_postal']))
            {
                throw new UserException('Code postal invalide.');
            }
        }

        if (!empty($data['passe']) && strlen($data['passe']) < 5)
        {
            throw new UserException('Le mot de passe doit faire au moins 5 caractères.');
        }

        return true;
    }

    public function add($data = [])
    {
        $this->_checkFields($data);
        $db = DB::getInstance();







<
<
<
<
<
<
<
<
<
<
<
<
|

|
<
<
<
<







 







<
<
<







 







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







73
74
75
76
77
78
79












80
81
82




83
84
85
86
87
88
89
..
96
97
98
99
100
101
102



103
104
105
106
107
108
109
...
130
131
132
133
134
135
136













137
138
139
140
141
142
143
                {
                    continue;
                }
            }

            if (isset($data[$key]))
            {












                if ($config->type == 'datetime' && trim($data[$key]) !== '')
                {
                    $dt = new DateTime($data[$key]);




                    $data[$key] = $dt->format('Y-m-d H:i');
                }
                elseif ($config->type == 'tel')
                {
                    $data[$key] = Utils::normalizePhoneNumber($data[$key]);
                }
                elseif ($config->type == 'country')
................................................................................
                }
                elseif ($config->type == 'number' && trim($data[$key]) !== '')
                {
                    if (empty($data[$key]))
                    {
                        $data[$key] = 0;
                    }



                }
                elseif ($config->type == 'select' && !in_array($data[$key], $config->options))
                {
                    throw new UserException('Le champ "' . $config->title . '" ne correspond pas à un des choix proposés.');
                }
                elseif ($config->type == 'multiple')
                {
................................................................................
                if (is_string($data[$key]) && trim($data[$key]) === '')
                {
                    $data[$key] = null;
                }
            }
        }














        return true;
    }

    public function add($data = [])
    {
        $this->_checkFields($data);
        $db = DB::getInstance();

Modified src/include/lib/Garradin/Membres/Champs.php from [ae54e63bd0] to [ddca1d72f4].

42
43
44
45
46
47
48
49

50
51
52
53
54
55
56
...
194
195
196
197
198
199
200





































































201
202
203
204
205
206
207
        'type',
        'title',
        'help',
        'editable',
        'list_row',
        'mandatory',
        'private',
        'options'

    ];

    static protected $presets = null;

	public function __toString()
	{
		return Utils::write_ini_string($this->champs);
................................................................................
            {
                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
     */







|
>







 







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







42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
...
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
        'type',
        'title',
        'help',
        'editable',
        'list_row',
        'mandatory',
        'private',
        'options',
        'rules'
    ];

    static protected $presets = null;

	public function __toString()
	{
		return Utils::write_ini_string($this->champs);
................................................................................
            {
                continue;
            }

            return $key;
        }
    }

    public function getValidationRules($mode = 'edit')
    {
        assert(in_array($mode, ['edit', 'create', 'user_edit']));

        $all_rules = [];

        foreach ($this->champs as $name => $config)
        {
            if (empty($config->editable) && $mode == 'user_edit')
            {
                $all_rules[$name] = 'absent';
                break;
            }

            $rules = [];

            if (!empty($config->mandatory) && !($name == 'passe' && $mode != 'create'))
            {
                $rules[] = 'required';
            }
            
            if ($config->type == 'email')
            {
                $rules[] = 'email';
            }
            elseif ($config->type == 'url')
            {
                $rules[] = 'url';
            }
            elseif ($config->type == 'date')
            {
                $rules[] = 'date_format:Y-m-d';
            }
            elseif ($config->type == 'date')
            {
                $rules[] = 'date_format:Y-m-d H\:i';
            }
            elseif ($config->type == 'number' || $config->type == 'multiple')
            {
                $rules[] = 'numeric';
            }
            elseif ($config->type == 'select')
            {
                $rules[] = 'in:' . range(0, count($this->options) - 1);
            }
            elseif ($config->type == 'checkbox')
            {
                $rules[] = 'boolean';
            }

            if ($name == 'passe')
            {
                $rules[] = 'min:6';
            }

            if (isset($config->rules))
            {
                $rules[] = $config->rules;
            }

            if (count($rules))
            {
                $all_rules[$name] = implode('|', $rules);
            }
        }

        return $all_rules;
    }

    /**
     * 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
     */

Modified src/include/lib/Garradin/Template.php from [fb886d7d5c] to [aad5f09e9f].

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
        $form = $this->getTemplateVars('form');

        if (!$form->hasErrors())
        {
            return '';
        }

        return '<div class="error"><ul><li>' . implode('</li><li>', $form->getErrorMessages()) . '</li></ul></div>';
    }

    protected function showError($params)
    {
        if (!$params['if'])
        {
            return '';







|







40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
        $form = $this->getTemplateVars('form');

        if (!$form->hasErrors())
        {
            return '';
        }

        return '<div class="error"><ul><li>' . implode('</li><li>', $form->getErrorMessages(!empty($params['membre']) ? true : false)) . '</li></ul></div>';
    }

    protected function showError($params)
    {
        if (!$params['if'])
        {
            return '';

Modified src/templates/admin/mes_infos.tpl from [701b28771a] to [136897ffcd].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

16
17
18
19
20
21
22
{include file="admin/_head.tpl" title="Mes informations personnelles" current="mes_infos" js=1}

{if $error}
    <p class="error">
        {$error}
    </p>
{/if}

<ul class="actions">
    <li class="current"><a href="{$admin_url}mes_infos.php">Mes informations personnelles</a></li>
    <li><a href="{$admin_url}mes_infos_securite.php">Mot de passe et options de sécurité</a></li>
</ul>

<form method="post" action="{$self_url}">



    <fieldset>
        <legend>Informations personnelles</legend>
        <dl>
            {foreach from=$champs item="champ" key="nom"}
            {if empty($champ.private) && $nom != 'passe'}
                {html_champ_membre config=$champ name=$nom data=$membre user_mode=true}


<
<
<
<
<
<





|

>







1
2






3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{include file="admin/_head.tpl" title="Mes informations personnelles" current="mes_infos" js=1}







<ul class="actions">
    <li class="current"><a href="{$admin_url}mes_infos.php">Mes informations personnelles</a></li>
    <li><a href="{$admin_url}mes_infos_securite.php">Mot de passe et options de sécurité</a></li>
</ul>

{form_errors membre=1}

<form method="post" action="{$self_url}">

    <fieldset>
        <legend>Informations personnelles</legend>
        <dl>
            {foreach from=$champs item="champ" key="nom"}
            {if empty($champ.private) && $nom != 'passe'}
                {html_champ_membre config=$champ name=$nom data=$membre user_mode=true}

Modified src/www/admin/mes_infos.php from [7c839aa4bc] to [ff993f6636].

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
<?php
namespace Garradin;

require_once __DIR__ . '/_inc.php';

$error = false;

if (!empty($_POST['save']))
{
    if (!Utils::CSRF_check('edit_me'))
    {
        $error = 'Une erreur est survenue, merci de renvoyer le formulaire.';
    }
    else
    {
        try {
            $data = [];

            foreach ($config->get('champs_membres')->getAll() as $key=>$c)
            {
                if (!empty($c['editable']))
                {
                    $data[$key] = Utils::post($key);
                }
            }

            $session->editUser($data);

            Utils::redirect('/admin/');
        }
................................................................................
        catch (UserException $e)
        {
            $error = $e->getMessage();
        }
    }
}

$tpl->assign('error', $error);

$tpl->assign('champs', $config->get('champs_membres')->getAll());

$tpl->assign('membre', $session->getUser());

$tpl->display('admin/mes_infos.tpl');





|

|

|
|
|
<
<




|

|

|







 







<
<
|




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
<?php
namespace Garradin;

require_once __DIR__ . '/_inc.php';

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

if (f('save'))
{
    $form->check('edit_me', $champs->getValidationRules('user_edit'));

    if (!$form->hasErrors())


    {
        try {
            $data = [];

            foreach ($champs->getAll() as $key=>$c)
            {
                if (!empty($c->editable))
                {
                    $data[$key] = f($key);
                }
            }

            $session->editUser($data);

            Utils::redirect('/admin/');
        }
................................................................................
        catch (UserException $e)
        {
            $error = $e->getMessage();
        }
    }
}



$tpl->assign('champs', $champs->getAll());

$tpl->assign('membre', $session->getUser());

$tpl->display('admin/mes_infos.tpl');