Overview
Comment:Modernisation du code : partie configuration
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dev
Files: files | file ages | folders
SHA1: 5e80c3fb203c901e57b409dbaa946680212d1705
User & Date: bohwaz on 2017-05-03 06:28:16
Other Links: branch diff | manifest | tags
Context
2017-05-03
07:07
Correction/mise à jour éditeur de squelette pour chrome check-in: 3369c1a509 user: bohwaz tags: dev
06:28
Modernisation du code : partie configuration check-in: 5e80c3fb20 user: bohwaz tags: dev
2017-05-02
06:59
Utilisation de tableau associatif pour le remplacement des tags check-in: 5a21cf2cbc user: bohwaz tags: dev
Changes

Modified src/include/lib/Garradin/Compta/Categories.php from [387c950f70] to [ae38db4253].

1
2
3
4
5
6
7
8



9
10
11
12
13
14
15
<?php

namespace Garradin\Compta;

use \Garradin\DB;
use \Garradin\Utils;
use \Garradin\UserException;




class Categories
{
    const DEPENSES = -1;
    const RECETTES = 1;
    const AUTRES = 0;

    public function importCategories()




|
|
|

>
>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php

namespace Garradin\Compta;

use Garradin\DB;
use Garradin\Utils;
use Garradin\UserException;

/**
 * Catégories comptables
 */
class Categories
{
    const DEPENSES = -1;
    const RECETTES = 1;
    const AUTRES = 0;

    public function importCategories()
27
28
29
30
31
32
33
34
35
36
37
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
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
98
99
100
101


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
        if (empty($data['compte']) || !trim($data['compte']))
        {
            throw new UserException('Le compte associé ne peut rester vide.');
        }

        $data['compte'] = trim($data['compte']);

        if (!$db->simpleQuerySingle('SELECT 1 FROM compta_comptes WHERE id = ?;', false, $data['compte']))
        {
            throw new UserException('Le compte associé n\'existe pas.');
        }

        if (!isset($data['type']) ||
            ($data['type'] != self::DEPENSES && $data['type'] != self::RECETTES))
        {
            // Catégories "autres" pas possibles pour le moment
            throw new UserException('Type de catégorie inconnu.');
        }

        $db->simpleInsert('compta_categories', [
            'intitule'  =>  $data['intitule'],
            'description'=> $data['description'],
            'compte'    =>  $data['compte'],
            'type'      =>  (int)$data['type'],
        ]);

        return $db->lastInsertRowId();
    }

    public function edit($id, $data)
    {
        $this->_checkFields($data);

        $db = DB::getInstance();

        $db->simpleUpdate('compta_categories',
            [
                'intitule'  =>  $data['intitule'],
                'description'=> $data['description'],
            ],
            'id = \''.$db->escapeString(trim($id)).'\'');



        return true;
    }

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



        // Ne pas supprimer une catégorie qui est utilisée !
        if ($db->simpleQuerySingle('SELECT 1 FROM compta_journal WHERE id_categorie = ? LIMIT 1;', false, $id))
        {
            throw new UserException('Cette catégorie ne peut être supprimée car des opérations comptables y sont liées.');
        }

        $db->simpleExec('DELETE FROM compta_categories WHERE id = ?;', $id);

        return true;
    }

    public function get($id)
    {
        $db = DB::getInstance();
        return $db->simpleQuerySingle('SELECT * FROM compta_categories WHERE id = ?;', true, (int)$id);
    }

    public function getList($type = null)
    {
        $db = DB::getInstance();
        $type = is_null($type) ? '' : 'cat.type = '.(int)$type;
        return $db->simpleStatementFetchAssocKey('
            SELECT cat.id, cat.*, cc.libelle AS compte_libelle
            FROM compta_categories AS cat INNER JOIN compta_comptes AS cc
                ON cc.id = cat.compte
            WHERE '.$type.' ORDER BY cat.intitule;', SQLITE3_ASSOC);


    }

    public function listMoyensPaiement()
    {
        $db = DB::getInstance();
        return $db->simpleStatementFetchAssocKey('SELECT code, nom FROM compta_moyens_paiement ORDER BY nom COLLATE NOCASE;');
    }

    public function getMoyenPaiement($code)
    {
        $db = DB::getInstance();
        return $db->simpleQuerySingle('SELECT nom FROM compta_moyens_paiement WHERE code = ?;', false, $code);
    }

    protected function _checkFields(&$data)
    {
        if (empty($data['intitule']) || !trim($data['intitule']))
        {
            throw new UserException('L\'intitulé ne peut rester vide.');







|











|















|




|
>
>








>
>

|




|







|





|
|
|


|
>
>





|





|







30
31
32
33
34
35
36
37
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
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
98
99
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
        if (empty($data['compte']) || !trim($data['compte']))
        {
            throw new UserException('Le compte associé ne peut rester vide.');
        }

        $data['compte'] = trim($data['compte']);

        if (!$db->firstColumn('SELECT 1 FROM compta_comptes WHERE id = ?;', $data['compte']))
        {
            throw new UserException('Le compte associé n\'existe pas.');
        }

        if (!isset($data['type']) ||
            ($data['type'] != self::DEPENSES && $data['type'] != self::RECETTES))
        {
            // Catégories "autres" pas possibles pour le moment
            throw new UserException('Type de catégorie inconnu.');
        }

        $db->insert('compta_categories', [
            'intitule'  =>  $data['intitule'],
            'description'=> $data['description'],
            'compte'    =>  $data['compte'],
            'type'      =>  (int)$data['type'],
        ]);

        return $db->lastInsertRowId();
    }

    public function edit($id, $data)
    {
        $this->_checkFields($data);

        $db = DB::getInstance();

        $db->update('compta_categories',
            [
                'intitule'  =>  $data['intitule'],
                'description'=> $data['description'],
            ],
            'id = :id_select',
            ['id_select' => (int) $id]
        );

        return true;
    }

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

        $id = (int) $id;

        // Ne pas supprimer une catégorie qui est utilisée !
        if ($db->firstColumn('SELECT 1 FROM compta_journal WHERE id_categorie = ? LIMIT 1;', $id))
        {
            throw new UserException('Cette catégorie ne peut être supprimée car des opérations comptables y sont liées.');
        }

        $db->delete('compta_categories', 'id = ?', $id);

        return true;
    }

    public function get($id)
    {
        $db = DB::getInstance();
        return $db->first('SELECT * FROM compta_categories WHERE id = ?;', (int)$id);
    }

    public function getList($type = null)
    {
        $db = DB::getInstance();
        $where = is_null($type) ? '1' : 'cat.type = '.(int)$type;

        $query = sprintf('SELECT cat.id, cat.*, cc.libelle AS compte_libelle
            FROM compta_categories AS cat INNER JOIN compta_comptes AS cc
                ON cc.id = cat.compte
            WHERE %s ORDER BY cat.intitule;', $where);

        return $db->getAssocKey($query);
    }

    public function listMoyensPaiement()
    {
        $db = DB::getInstance();
        return $db->getAssocKey('SELECT code, nom FROM compta_moyens_paiement ORDER BY nom COLLATE NOCASE;');
    }

    public function getMoyenPaiement($code)
    {
        $db = DB::getInstance();
        return $db->firstColumn('SELECT nom FROM compta_moyens_paiement WHERE code = ?;', $code);
    }

    protected function _checkFields(&$data)
    {
        if (empty($data['intitule']) || !trim($data['intitule']))
        {
            throw new UserException('L\'intitulé ne peut rester vide.');

Modified src/include/lib/Garradin/Install.php from [2c71c5eba0] to [cbbaf6e1e8].

1
2
3
4




5
6
7
8
9
10
11
<?php

namespace Garradin;





class Install
{
	static public function install($nom_asso, $adresse_asso, $email_asso, $nom_categorie, $nom_membre, $email_membre, $passe_membre, $site_asso = WWW_URL)
	{
		$db = DB::getInstance(true);

		// Création de la base de données




>
>
>
>







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

namespace Garradin;

/**
 * Pour procéder à l'installation de l'instance Garradin
 * Utile pour automatiser l'installation sans passer par la page d'installation
 */
class Install
{
	static public function install($nom_asso, $adresse_asso, $email_asso, $nom_categorie, $nom_membre, $email_membre, $passe_membre, $site_asso = WWW_URL)
	{
		$db = DB::getInstance(true);

		// Création de la base de données

Modified src/include/lib/Garradin/Membres/Champs.php from [0ab95672fa] to [a9683b7b29].

151
152
153
154
155
156
157
158
159
160
161






162
163
164
165
166
167
168

	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;








|



>
>
>
>
>
>







151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

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

    public function getList($with_id = false)
    {
        $champs = clone $this->champs;
        unset($champs->passe);

        if ($with_id)
        {
            $champs->id = $this->get('id');
        }

        return $champs;
    }

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

Modified src/include/lib/Garradin/Sauvegarde.php from [e15be831b5] to [050c3688bc].

210
211
212
213
214
215
216

217
218







219
220
221
222
223
224
225
		}
		catch (\Exception $e)
		{
			throw new UserException('Le fichier fourni n\'est pas une base de données valide. ' .
				'Message d\'erreur de SQLite : ' . $e->getMessage());
		}


		// Regardons ensuite si la base de données n'est pas corrompue
		$check = $db->querySingle('PRAGMA integrity_check;');








		if (strtolower(trim($check)) != 'ok')
		{
			throw new UserException('Le fichier fourni est corrompu. SQLite a trouvé ' . $check . ' erreurs.');
		}

		// On ne peut pas faire de vérifications très poussées sur la structure de la base de données,







>
|
|
>
>
>
>
>
>
>







210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
		}
		catch (\Exception $e)
		{
			throw new UserException('Le fichier fourni n\'est pas une base de données valide. ' .
				'Message d\'erreur de SQLite : ' . $e->getMessage());
		}

		try {
			// Regardons ensuite si la base de données n'est pas corrompue
			$check = $db->querySingle('PRAGMA integrity_check;');
		}
		catch (\Exception $e)
		{
			// Ici SQLite peut rejeter un message type "file is encrypted or is not a db"
			throw new UserException('Le fichier fourni n\'est pas une base de données valide. ' .
				'Message d\'erreur de SQLite : ' . $e->getMessage());
		}

		if (strtolower(trim($check)) != 'ok')
		{
			throw new UserException('Le fichier fourni est corrompu. SQLite a trouvé ' . $check . ' erreurs.');
		}

		// On ne peut pas faire de vérifications très poussées sur la structure de la base de données,

Modified src/www/admin/config/_inc.php from [04927b203f] to [3f3d512f92].

1
2
3
4
5
6
7
8
9
10
11
12
<?php

namespace Garradin;

require_once __DIR__ . '/../_inc.php';

if ($user['droits']['config'] < Membres::DROIT_ADMIN)
{
    throw new UserException("Vous n'avez pas le droit d'accéder à cette page.");
}

$tpl->assign('garradin_website', WEBSITE);






|
|
<
<
<

1
2
3
4
5
6
7
8



9
<?php

namespace Garradin;

require_once __DIR__ . '/../_inc.php';

$session->requireAccess('membres', Membres::DROIT_ADMIN);




$tpl->assign('garradin_website', WEBSITE);

Modified src/www/admin/config/import.php from [84fe620fc0] to [0d90db7427].

1
2
3
4
5
6
7
8
<?php
namespace Garradin;

require_once __DIR__ . '/_inc.php';

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

?>






<
<
1
2
3
4
5
6


<?php
namespace Garradin;

require_once __DIR__ . '/_inc.php';

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


Modified src/www/admin/config/index.php from [8758269459] to [d0fb97ed05].

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
$tpl->assign('sqlite_version', $v['versionString']);

$tpl->assign('pays', Utils::getCountryList());

$cats = new Membres\Categories;
$tpl->assign('membres_cats', $cats->listSimple());

$champs_liste = array_merge(
    ['id' => ['title' => 'Numéro unique', 'type' => 'number']],
    $config->get('champs_membres')->getList()
);
$tpl->assign('champs', $champs_liste);

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

?>







<
<
|
<
<


<
<
54
55
56
57
58
59
60


61


62
63


$tpl->assign('sqlite_version', $v['versionString']);

$tpl->assign('pays', Utils::getCountryList());

$cats = new Membres\Categories;
$tpl->assign('membres_cats', $cats->listSimple());



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



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


Modified src/www/admin/config/membres.php from [d2b6067eb1] to [6f25a6449e].

1
2
3
4
5
6


7
8
9
10
11
12
13
<?php
namespace Garradin;

require_once __DIR__ . '/_inc.php';

$error = false;



// Restauration de ce qui était en session
if ($champs = $membres->sessionGet('champs_membres'))
{
    $champs = new Membres\Champs($champs);
}
else






>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
namespace Garradin;

require_once __DIR__ . '/_inc.php';

$error = false;

$membres = new Membres;

// Restauration de ce qui était en session
if ($champs = $membres->sessionGet('champs_membres'))
{
    $champs = new Membres\Champs($champs);
}
else
134
135
136
137
138
139
140
141
142
    return $types[$type];
});

$tpl->assign('csrf_name', Utils::CSRF_field_name('config_membres'));
$tpl->assign('csrf_value', Utils::CSRF_create('config_membres'));

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

?>







<
<
136
137
138
139
140
141
142


    return $types[$type];
});

$tpl->assign('csrf_name', Utils::CSRF_field_name('config_membres'));
$tpl->assign('csrf_value', Utils::CSRF_create('config_membres'));

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


Modified src/www/admin/config/site.php from [0869dac1c0] to [548ea357b2].

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
                $error = "Impossible d'enregistrer le squelette.";
            }
        }
    }

    $tpl->assign('edit', ['file' => trim(Utils::get('edit')), 'content' => $source]);
    $tpl->assign('csrf_key', $csrf_key);
    $tpl->assign('sources_json', json_encode(Squelette::listSources()));
}
else
{
    $tpl->assign('sources', Squelette::listSources());
}

$tpl->assign('reset_ok', Utils::get('reset_ok'));
$tpl->assign('error', $error);
$tpl->display('admin/config/site.tpl');







<

<
|
|
<




84
85
86
87
88
89
90

91

92
93

94
95
96
97
                $error = "Impossible d'enregistrer le squelette.";
            }
        }
    }

    $tpl->assign('edit', ['file' => trim(Utils::get('edit')), 'content' => $source]);
    $tpl->assign('csrf_key', $csrf_key);

}


$tpl->assign('sources', Squelette::listSources());


$tpl->assign('reset_ok', Utils::get('reset_ok'));
$tpl->assign('error', $error);
$tpl->display('admin/config/site.tpl');