Completed
Push — master ( 333ab3...9dcc44 )
by Laurent
02:27
created

modFlightLog::initHooks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2003      Rodolphe Quiedeville <[email protected]>
3
 * Copyright (C) 2004-2015 Laurent Destailleur  <[email protected]>
4
 * Copyright (C) 2005-2016 Regis Houssin        <[email protected]>
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
/**
21
 *    \defgroup   mymodule     Module MyModule
22
 *  \brief      Example of a module descriptor.
23
 *                Such a file must be copied into htdocs/mymodule/core/modules directory.
24
 *  \file       htdocs/mymodule/core/modules/modMyModule.class.php
25
 *  \ingroup    mymodule
26
 *  \brief      Description and activation file for module MyModule
27
 */
28
include_once DOL_DOCUMENT_ROOT . '/core/modules/DolibarrModules.class.php';
29
30
31
/**
32
 *  Description and activation class for module MyModule
33
 */
34
class modFlightLog extends DolibarrModules
35
{
36
    const MENU_TYPE_LEFT = 'left';
37
    const MENU_TYPE_TOP = 'top';
38
39
    /**
40
     * @var array Indexed list of export IDs
41
     *
42
     */
43
    public $export_code = array();
44
45
    /**
46
     * @var array Indexed list of export names
47
     *
48
     */
49
    public $export_label = array();
50
51
    /**
52
     * @var array Indexed list of export enabling conditions
53
     *
54
     */
55
    public $export_enabled = array();
56
57
    /**
58
     * @var array Indexed list of export required permissions
59
     *
60
     */
61
    public $export_permission = array();
62
63
    /**
64
     * @var array Indexed list of export fields
65
     *
66
     */
67
    public $export_fields_array = array();
68
69
    /**
70
     * @var array Indexed list of export entities
71
     *
72
     */
73
    public $export_entities_array = array();
74
75
    /**
76
     * @var array Indexed list of export SQL queries start
77
     *
78
     */
79
    public $export_sql_start = array();
80
81
    /**
82
     * @var array Indexed list of export SQL queries end
83
     *
84
     */
85
    public $export_sql_end = array();
86
87
    public $export_TypeFields_array = [];
88
89
    /**
90
     * @var array
91
     */
92
    public $menus;
93
94
    /**
95
     * Constructor. Define names, constants, directories, boxes, permissions
96
     *
97
     * @param DoliDB $db Database handler
98
     */
99
    public function __construct($db)
100
    {
101
        global $langs, $conf;
102
103
        $this->db = $db;
104
105
        // Id for module (must be unique).
106
        $this->numero = 500000;
107
        // Key text used to identify module (for permissions, menus, etc...)
108
        $this->rights_class = 'flightLog';
109
110
        // Family can be 'crm','financial','hr','projects','products','ecm','technic','interface','other'
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
111
        // It is used to group modules by family in module setup page
112
        $this->family = "Belgian Balloon Club";
113
        // Module position in the family
114
        $this->module_position = 500;
115
        // Gives the possibility to the module, to provide his own family info and position of this family (Overwrite $this->family and $this->module_position. Avoid this)
116
        //$this->familyinfo = array('myownfamily' => array('position' => '001', 'label' => $langs->trans("MyOwnFamily")));
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
117
118
        // Module label (no space allowed), used if translation string 'ModuleXXXName' not found (where XXX is value of numeric property 'numero' of module)
119
        $this->name = preg_replace('/^mod/i', '', get_class($this));
120
        // Module description, used if translation string 'ModuleXXXDesc' not found (where XXX is value of numeric property 'numero' of module)
121
        $this->description = "Pilots flight log";
122
        $this->descriptionlong = "Manage flights and flights type for the Belgian Balloon Club";
123
        $this->editor_name = 'De Coninck Laurent';
124
        $this->editor_url = 'http://www.dolibarr.org';
125
126
        // Possible values for version are: 'development', 'experimental', 'dolibarr', 'dolibarr_deprecated' or a version string like 'x.y.z'
127
        $this->version = '1.1';
128
        // Key used in llx_const table to save module status enabled/disabled (where MYMODULE is value of property name of module in uppercase)
129
        $this->const_name = 'MAIN_MODULE_' . strtoupper($this->name);
130
        // Name of image file used for this module.
131
        // If file is in theme/yourtheme/img directory under name object_pictovalue.png, use this->picto='pictovalue'
132
        // If file is in module/img directory under name object_pictovalue.png, use this->picto='pictovalue@module'
133
        $this->picto = 'flight@flightLog';
134
135
        $this->module_parts = array();
136
137
        // Data directories to create when module is enabled.
138
        $this->dirs = array();
139
140
        // Config pages. Put here list of php page, stored into mymodule/admin directory, to use to setup module.
141
        $this->config_page_url = array();
142
143
        // Dependencies
144
        $this->hidden = false;
145
        $this->depends = array('modFlightBalloon');
146
        $this->requiredby = array();
147
        $this->conflictwith = array();
148
        $this->phpmin = array(5, 5);
149
        $this->need_dolibarr_version = array(4, 0);
150
        $this->langfiles = array("mymodule@flightLog");
151
152
        // Constants
153
        $this->initConstants();
154
155
        // Array to add new pages in new tabs
156
        $this->tabs = [
157
        ];
158
159
        if (!isset($conf->flightLog) || !isset($conf->flightLog->enabled)) {
160
            $conf->flightLog = new stdClass();
161
            $conf->flightLog->enabled = 0;
162
        }
163
164
        $this->boxes = [];
165
166
        $this->initDictionnaries();
167
        $this->initCronJobs();
168
        $this->initMenu();
169
        $this->initHooks();
170
        $this->initPermissions();
171
172
        // Exports
173
        $r = 0;
174
175
        $this->export_code[$r] = $this->rights_class . '_' . $r;
176
        $this->export_label[$r] = 'Flights export';
177
        $this->export_enabled[$r] = '1';
178
        $this->export_permission[$r] = array(array("flightLog", "vol", "detail"));
179
        $this->export_fields_array[$r] = array(
180
            "flight.idBBC_vols"                => "Identifiant",
181
            "flight.date"                      => "Date",
182
            "flight.lieuD"                     => "Lieu décollage ",
183
            "flight.lieuA"                     => "Lieu atterissage",
184
            "flight.heureD"                    => "Heure décollage",
185
            "flight.heureA"                    => "Heure atterissage",
186
            "flight.BBC_ballons_idBBC_ballons" => "Identifiant ballon",
187
            "flight.nbrPax"                    => "# pax",
188
            "flight.remarque"                  => "Remarque",
189
            "flight.incidents"                 => "Incidents",
190
            "flight.fk_type"                   => "Identifiant type",
191
            "flight.fk_pilot"                  => "Identifiant pilote",
192
            "flight.fk_organisateur"           => "Identifiant organisateur",
193
            "flight.is_facture"                => "Facture Oui/Non",
194
            "flight.kilometers"                => "# Km",
195
            "flight.cost"                      => "Cout",
196
            "flight.fk_receiver"               => "Identifiant receveur d'argent",
197
            "flight.justif_kilometers"         => "Justificatif kilomètres",
198
            "balloon.immat"                    => "Immat.",
199
            "pilot.login"                      => "Pilote",
200
            "flightType.nom"                   => "Type de vol",
201
            "organisator.login"                => "Organisateur",
202
            "receiver.login"                   => "Percepteur",
203
        );
204
205
        $this->export_TypeFields_array[$r] = [
206
            "flight.date"                      => "Date",
207
            "flight.lieuD"                     => "Text",
208
            "flight.lieuA"                     => "Text",
209
            "flight.heureD"                    => "Text",
210
            "flight.heureA"                    => "Text",
211
            "flight.BBC_ballons_idBBC_ballons" => implode(":", ["List", "bbc_ballons", "immat", "rowid"]),
212
            "flight.nbrPax"                    => "Numeric",
213
            "flight.remarque"                  => "Text",
214
            "flight.incidents"                 => "Text",
215
            "flight.fk_type"                   => implode(":", ["List", "bbc_types", "nom", "idType"]),
216
            "flight.fk_pilot"                  => implode(":", ["List", "user", "login", "rowid"]),
217
            "flight.fk_organisateur"           => implode(":", ["List", "user", "login", "rowid"]),
218
            "flight.is_facture"                => "Boolean",
219
            "flight.kilometers"                => "Numeric",
220
            "flight.cost"                      => "Numeric",
221
            "flight.fk_receiver"               => implode(":", ["List", "user", "login", "rowid"]),
222
            "flight.justif_kilometers"         => "Text",
223
        ];
224
225
        $this->export_entities_array[$r] = array(
226
            "flight.idBBC_vols"                => "Flight",
227
            "flight.date"                      => "Flight",
228
            "flight.lieuD"                     => "Flight",
229
            "flight.lieuA"                     => "Flight",
230
            "flight.heureD"                    => "Flight",
231
            "flight.heureA"                    => "Flight",
232
            "flight.BBC_ballons_idBBC_ballons" => "Flight",
233
            "flight.nbrPax"                    => "Flight",
234
            "flight.remarque"                  => "Flight",
235
            "flight.incidents"                 => "Flight",
236
            "flight.fk_type"                   => "Flight",
237
            "flight.fk_pilot"                  => "Flight",
238
            "flight.fk_organisateur"           => "Flight",
239
            "flight.is_facture"                => "Flight",
240
            "flight.kilometers"                => "Flight",
241
            "flight.cost"                      => "Flight",
242
            "flight.fk_receiver"               => "Flight",
243
            "flight.justif_kilometers"         => "Flight",
244
            "balloon.immat"                    => "Balloon",
245
            "pilot.login"                      => "Pilot",
246
            "flightType.nom"                   => "FlightType",
247
            "organisator.login"                => "Organisator",
248
            "receiver.login"                   => "Percepteur",
249
        );
250
        $this->export_sql_start[$r] = 'SELECT DISTINCT ';
251
        $this->export_sql_end[$r] = ' FROM ' . MAIN_DB_PREFIX . 'bbc_vols as flight';
252
        $this->export_sql_end[$r] .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bbc_ballons as balloon on (flight.BBC_ballons_idBBC_ballons = balloon.rowid)';
253
        $this->export_sql_end[$r] .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bbc_types as flightType on (flight.fk_type = flightType.idType)';
254
        $this->export_sql_end[$r] .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'user as pilot on (flight.fk_pilot = pilot.rowid)';
255
        $this->export_sql_end[$r] .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'user as organisator on (flight.fk_organisateur = organisator.rowid)';
256
        $this->export_sql_end[$r] .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'user as receiver on (flight.fk_receiver = receiver.rowid)';
257
        $this->export_sql_end[$r] .= ' WHERE 1 = 1';
258
        $r++;
259
    }
260
261
    /**
262
     *        Function called when module is enabled.
263
     *        The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
264
     *        It also creates data directories
265
     *
266
     * @param      string $options Options when enabling module ('', 'noboxes')
267
     *
268
     * @return     int                1 if OK, 0 if KO
269
     */
270
    public function init($options = '')
271
    {
272
        $sql = array();
273
274
        $this->_load_tables('/flightLog/sql/');
275
276
        return $this->_init($sql, $options);
277
    }
278
279
    /**
280
     * Function called when module is disabled.
281
     * Remove from database constants, boxes and permissions from Dolibarr database.
282
     * Data directories are not deleted
283
     *
284
     * @param      string $options Options when enabling module ('', 'noboxes')
285
     *
286
     * @return     int                1 if OK, 0 if KO
287
     */
288
    public function remove($options = '')
289
    {
290
        $sql = array();
291
292
        return $this->_remove($sql, $options);
293
    }
294
295
    /**
296
     * Init menu
297
     */
298
    private function initMenu()
299
    {
300
        $this->menus = array();
301
        $r = 0;
302
303
        $this->menu[$r] = array(
304
            'fk_menu'  => 'fk_mainmenu=flightLog',
305
            'type'     => self::MENU_TYPE_TOP,
306
            'titre'    => 'Carnet de vols',
307
            'mainmenu' => 'flightLog',
308
            'leftmenu' => 'readFlight',
309
            'url'      => '/flightLog/readFlights.php',
310
            'langs'    => 'mylangfile',
311
            'position' => 100,
312
            'enabled'  => '1',
313
            'perms'    => '$user->rights->flightLog->vol->access',
314
            'target'   => '',
315
            'user'     => 0
316
        );
317
        $r++;
318
319
        $this->menu[$r] = array(
320
            'fk_menu'  => 'fk_mainmenu=flightLog',
321
            'type'     => self::MENU_TYPE_LEFT,
322
            'titre'    => 'Ajouter un vol',
323
            'mainmenu' => 'flightLog',
324
            'leftmenu' => 'addFlight',
325
            'url'      => '/flightLog/addFlight.php',
326
            'langs'    => 'mylangfile',
327
            'position' => 101,
328
            'enabled'  => '1',
329
            'perms'    => '$user->rights->flightLog->vol->add',
330
            'target'   => '',
331
            'user'     => 2
332
        );
333
        $r++;
334
        $this->menu[$r] = array(
335
            'fk_menu'  => 'fk_mainmenu=flightLog',
336
            'type'     => self::MENU_TYPE_LEFT,
337
            'titre'    => 'Visualisation',
338
            'mainmenu' => 'flightLog',
339
            'leftmenu' => 'showFlight',
340
            'url'      => '/flightLog/readFlights.php',
341
            'langs'    => 'mylangfile',
342
            'position' => 102,
343
            'enabled'  => '1',
344
            'perms'    => '1',
345
            'target'   => '',
346
            'user'     => 2
347
        );
348
        $this->menu[$r] = array(
349
            'fk_menu'  => 'fk_mainmenu=flightLog',
350
            'type'     => self::MENU_TYPE_LEFT,
351
            'titre'    => 'Les vols',
352
            'mainmenu' => 'flightLog',
353
            'leftmenu' => 'flightLog',
354
            'url'      => '/flightLog/list.php',
355
            'langs'    => 'mylangfile',
356
            'position' => 105,
357
            'enabled'  => '1',
358
            'perms'    => '1',
359
            'target'   => '',
360
            'user'     => 2
361
        );
362
        $r++;
363
        $this->menu[$r] = array(
364
            'fk_menu'  => 'fk_mainmenu=flightLog',
365
            'type'     => self::MENU_TYPE_LEFT,
366
            'titre'    => 'Gestion',
367
            'mainmenu' => 'flightLog',
368
            'leftmenu' => 'management',
369
            'url'      => '',
370
            'langs'    => 'mylangfile',
371
            'position' => 106,
372
            'enabled'  => '1',
373
            'perms'    => '$user->rights->flightLog->vol->status||$user->rights->flightLog->vol->detail',
374
            'target'   => '',
375
            'user'     => 2
376
        );
377
        $r++;
378
        $this->menu[$r] = array(
379
            'fk_menu'  => 'fk_mainmenu=flightLog,fk_leftmenu=management',
380
            'type'     => self::MENU_TYPE_LEFT,
381
            'titre'    => 'Payement',
382
            'mainmenu' => 'flightLog',
383
            'leftmenu' => 'flightBilling',
384
            'url'      => '/flightLog/listFact.php?view=1',
385
            'langs'    => 'mylangfile',
386
            'position' => 107,
387
            'enabled'  => '1',
388
            'perms'    => '$user->rights->flightLog->vol->financial',
389
            'target'   => '',
390
            'user'     => 2
391
        );
392
        $r++;
393
        $this->menu[$r] = array(
394
            'fk_menu'  => 'fk_mainmenu=flightLog,fk_leftmenu=management',
395
            'type'     => self::MENU_TYPE_LEFT,
396
            'titre'    => 'Aviabel',
397
            'mainmenu' => 'flightLog',
398
            'leftmenu' => 'flightAviabel',
399
            'url'      => '/flightLog/listFact.php?view=2',
400
            'langs'    => 'mylangfile',
401
            'position' => 108,
402
            'enabled'  => '1',
403
            'perms'    => '$user->rights->flightLog->vol->detail',
404
            'target'   => '',
405
            'user'     => 2
406
        );
407
    }
408
409
    /**
410
     * Init permissions
411
     */
412
    private function initPermissions()
413
    {
414
        $this->rights = array();        // Permission array used by this module
415
        $r = 0;
416
417
        $this->rights[$r][0] = 9993;
418
        $this->rights[$r][1] = 'Permet d\'acceder au module des vols.';
419
        $this->rights[$r][3] = 0;
420
        $this->rights[$r][4] = 'vol';
421
        $this->rights[$r][5] = 'access';
422
        $r++;
423
424
        $this->rights[$r][0] = 9998;
425
        $this->rights[$r][1] = 'Enregistrer un nouveau vol.';
426
        $this->rights[$r][3] = 0;
427
        $this->rights[$r][4] = 'vol';
428
        $this->rights[$r][5] = 'add';
429
        $r++;
430
431
        $this->rights[$r][0] = 9997;
432
        $this->rights[$r][1] = 'Permet de facturer un vol.';
433
        $this->rights[$r][3] = 0;
434
        $this->rights[$r][4] = 'vol';
435
        $this->rights[$r][5] = 'status';
436
        $r++;
437
438
        $this->rights[$r][0] = 9996;
439
        $this->rights[$r][1] = 'Permet de supprimer un vol.';
440
        $this->rights[$r][3] = 0;
441
        $this->rights[$r][4] = 'vol';
442
        $this->rights[$r][5] = 'delete';
443
        $r++;
444
445
        $this->rights[$r][0] = 9995;
446
        $this->rights[$r][1] = 'Permet de modifier tous les vols.';
447
        $this->rights[$r][3] = 0;
448
        $this->rights[$r][4] = 'vol';
449
        $this->rights[$r][5] = 'edit';
450
        $r++;
451
452
        $this->rights[$r][0] = 9994;
453
        $this->rights[$r][1] = 'affiche les details de tous les ballons et de tous les pilotes.';
454
        $this->rights[$r][3] = 0;
455
        $this->rights[$r][4] = 'vol';
456
        $this->rights[$r][5] = 'detail';
457
        $r++;
458
459
        $this->rights[$r][0] = 9999;
460
        $this->rights[$r][1] = 'Gérer les aspects financier des vols';
461
        $this->rights[$r][3] = 0;
462
        $this->rights[$r][4] = 'vol';
463
        $this->rights[$r][5] = 'financial';
464
        $r++;
465
    }
466
467
    private function initCronJobs()
468
    {
469
        $this->cronjobs = array();            // List of cron jobs entries to add
470
        // Example: $this->cronjobs=array(0=>array('label'=>'My label', 'jobtype'=>'method', 'class'=>'/dir/class/file.class.php', 'objectname'=>'MyClass', 'method'=>'myMethod', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>2, 'unitfrequency'=>3600, 'test'=>true),
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
471
        //                                1=>array('label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>1, 'unitfrequency'=>3600*24, 'test'=>true)
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
472
        // );
473
    }
474
475
    private function initDictionnaries()
476
    {
477
        $this->initFlightTypeDictionnary();
478
    }
479
480
    private function initFlightTypeDictionnary()
481
    {
482
        $this->dictionaries = array(
483
            'langs'          => 'mylangfile@mymodule',
484
            'tabname'        => array(MAIN_DB_PREFIX . "bbc_types"),
485
            'tablib'         => array("Types de vols"),
486
            'tabsql'         => array('SELECT f.idType, f.numero, f.nom, f.active FROM ' . MAIN_DB_PREFIX . 'bbc_types as f',),
487
            'tabsqlsort'     => array("numero ASC"),
488
            'tabfield'       => array("idType,numero,nom"),
489
            'tabfieldvalue'  => array("numero,nom"),
490
            'tabfieldinsert' => array("numero,nom"),
491
            'tabrowid'       => array("idType"),
492
            'tabcond'        => array('$conf->flightLog->enabled'),
493
        );
494
    }
495
496
    /**
497
     * Init hooks
498
     */
499
    private function initHooks()
500
    {
501
        if (!isset($this->module_parts["hooks"])) {
502
            $this->module_parts["hooks"] = [];
503
        }
504
505
        $this->module_parts["hooks"][] = "searchform";
506
507
    }
508
509
    private function initConstants()
510
    {
511
        $this->const = array(
512
            0 => [
513
                'BBC_FLIGHT_LOG_TAUX_REMB_KM',
514
                'chaine',
515
                '0.25',
516
                'Taux remboursement des kilomètres au BBC',
517
                true,
518
                'current',
519
                true
520
            ],
521
            1 => [
522
                'BBC_FLIGHT_LOG_UNIT_PRICE_MISSION',
523
                'chaine',
524
                '35',
525
                'Unit price special mission',
526
                true,
527
                'current',
528
                true
529
            ],
530
        );
531
    }
532
533
}
534
535