Completed
Push — master ( 88150f...580efa )
by Laurent
04:58 queued 02:17
created

modFlightLog::initTabs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
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
    /**
88
     * @var array
89
     */
90
    public $export_TypeFields_array = [];
91
92
    /**
93
     * @var array
94
     */
95
    public $menus;
96
97
    /**
98
     * @var array
99
     */
100
    public $tabs;
101
102
    /**
103
     * @var array
104
     */
105
    public $dictionaries;
106
107
    /**
108
     * Constructor. Define names, constants, directories, boxes, permissions
109
     *
110
     * @param DoliDB $db Database handler
111
     */
112
    public function __construct($db)
113
    {
114
        parent::__construct($db);
115
116
        global $conf;
117
118
        // Id for module (must be unique).
119
        $this->numero = 500000;
120
        // Key text used to identify module (for permissions, menus, etc...)
121
        $this->rights_class = 'flightlog';
122
123
        $this->family = "Belgian Balloon Club";
124
        $this->module_position = 500;
125
        $this->name = preg_replace('/^mod/i', '', get_class($this));
126
        // Module description, used if translation string 'ModuleXXXDesc' not found (where XXX is value of numeric property 'numero' of module)
127
        $this->description = "Pilots flight log";
128
        $this->descriptionlong = "Manage flights and flight types for the Belgian Balloon Club";
129
        $this->editor_name = 'De Coninck Laurent';
130
        $this->editor_url = 'http://www.dolibarr.org';
131
132
        // Possible values for version are: 'development', 'experimental', 'dolibarr', 'dolibarr_deprecated' or a version string like 'x.y.z'
133
        $this->version = '1.3';
134
        // Key used in llx_const table to save module status enabled/disabled (where MYMODULE is value of property name of module in uppercase)
135
        $this->const_name = 'MAIN_MODULE_' . strtoupper($this->name);
136
        // Name of image file used for this module.
137
        // If file is in theme/yourtheme/img directory under name object_pictovalue.png, use this->picto='pictovalue'
138
        // If file is in module/img directory under name object_pictovalue.png, use this->picto='pictovalue@module'
139
        $this->picto = 'flight@flightlog';
140
141
        $this->configureCss();
142
143
        // Data directories to create when module is enabled.
144
        $this->dirs = array();
145
146
        // Config pages. Put here list of php page, stored into mymodule/admin directory, to use to setup module.
147
        $this->config_page_url = [
148
            "vol.php@flightlog",
149
        ];
150
151
        // Dependencies
152
        $this->hidden = false;
153
        $this->depends = array('modFlightBalloon');
154
        $this->requiredby = array();
155
        $this->conflictwith = array();
156
        $this->phpmin = array(5, 5);
157
        $this->need_dolibarr_version = array(4, 0);
158
        $this->langfiles = array("mymodule@flightlog");
159
160
        // Constants
161
        $this->initConstants();
162
163
        if (!isset($conf->flightLog) || !isset($conf->flightLog->enabled)) {
164
            $conf->flightLog = new stdClass();
165
            $conf->flightLog->enabled = 0;
166
        }
167
168
        $this->boxes = [];
169
170
        $this->initTabs();
171
        $this->initDictionnaries();
172
        $this->initCronJobs();
173
        $this->initMenu();
174
        $this->initHooks();
175
        $this->initPermissions();
176
        $this->initExports();
177
178
        $this->activateTriggers();
179
        $this->initWorkflows();
180
    }
181
182
    /**
183
     *        Function called when module is enabled.
184
     *        The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
185
     *        It also creates data directories
186
     *
187
     * @param      string $options Options when enabling module ('', 'noboxes')
188
     *
189
     * @return     int                1 if OK, 0 if KO
190
     */
191
    public function init($options = '')
192
    {
193
        $sql = array();
194
195
        $this->_load_tables('/flightlog/sql/');
196
197
        return $this->_init($sql, $options);
198
    }
199
200
    /**
201
     * Function called when module is disabled.
202
     * Remove from database constants, boxes and permissions from Dolibarr database.
203
     * Data directories are not deleted
204
     *
205
     * @param      string $options Options when enabling module ('', 'noboxes')
206
     *
207
     * @return     int                1 if OK, 0 if KO
208
     */
209
    public function remove($options = '')
210
    {
211
        $sql = array();
212
213
        return $this->_remove($sql, $options);
214
    }
215
216
    /**
217
     * Init menu
218
     */
219
    private function initMenu()
220
    {
221
        $this->menus = array();
222
        $r = 0;
223
224
        $this->menu[$r] = array(
225
            'fk_menu' => 'fk_mainmenu=flightlog',
226
            'type' => self::MENU_TYPE_TOP,
227
            'titre' => 'Carnet de vols',
228
            'mainmenu' => 'flightlog',
229
            'leftmenu' => 'readFlight',
230
            'url' => '/flightlog/readFlights.php',
231
            'langs' => 'mylangfile',
232
            'position' => 100,
233
            'enabled' => '1',
234
            'perms' => '$user->rights->flightlog->vol->access',
235
            'target' => '',
236
            'user' => 0
237
        );
238
        $r++;
239
240
        $this->menu[$r] = array(
241
            'fk_menu' => 'fk_mainmenu=flightlog',
242
            'type' => self::MENU_TYPE_LEFT,
243
            'titre' => 'Ajouter un vol',
244
            'mainmenu' => 'flightlog',
245
            'leftmenu' => 'addFlight',
246
            'url' => '/flightlog/addFlight.php',
247
            'langs' => 'mylangfile',
248
            'position' => 101,
249
            'enabled' => '1',
250
            'perms' => '$user->rights->flightlog->vol->add',
251
            'target' => '',
252
            'user' => 2
253
        );
254
        $r++;
255
        $this->menu[$r] = array(
256
            'fk_menu' => 'fk_mainmenu=flightlog',
257
            'type' => self::MENU_TYPE_LEFT,
258
            'titre' => 'Visualisation',
259
            'mainmenu' => 'flightlog',
260
            'leftmenu' => 'showFlight',
261
            'url' => '/flightlog/readFlights.php',
262
            'langs' => 'mylangfile',
263
            'position' => 102,
264
            'enabled' => '1',
265
            'perms' => '1',
266
            'target' => '',
267
            'user' => 2
268
        );
269
        $this->menu[$r] = array(
270
            'fk_menu' => 'fk_mainmenu=flightlog',
271
            'type' => self::MENU_TYPE_LEFT,
272
            'titre' => 'Les vols',
273
            'mainmenu' => 'flightlog',
274
            'leftmenu' => 'flightlog',
275
            'url' => '/flightlog/list.php',
276
            'langs' => 'mylangfile',
277
            'position' => 105,
278
            'enabled' => '1',
279
            'perms' => '1',
280
            'target' => '',
281
            'user' => 2
282
        );
283
        $r++;
284
        $this->menu[$r] = array(
285
            'fk_menu' => 'fk_mainmenu=flightlog',
286
            'type' => self::MENU_TYPE_LEFT,
287
            'titre' => 'Gestion',
288
            'mainmenu' => 'flightlog',
289
            'leftmenu' => 'management',
290
            'url' => '',
291
            'langs' => 'mylangfile',
292
            'position' => 106,
293
            'enabled' => '1',
294
            'perms' => '$user->rights->flightlog->vol->status||$user->rights->flightlog->vol->detail',
295
            'target' => '',
296
            'user' => 2
297
        );
298
        $r++;
299
        $this->menu[$r] = array(
300
            'fk_menu' => 'fk_mainmenu=flightlog,fk_leftmenu=management',
301
            'type' => self::MENU_TYPE_LEFT,
302
            'titre' => 'Payement',
303
            'mainmenu' => 'flightlog',
304
            'leftmenu' => 'flightBilling',
305
            'url' => '/flightlog/listFact.php?view=1',
306
            'langs' => 'mylangfile',
307
            'position' => 107,
308
            'enabled' => '1',
309
            'perms' => '$user->rights->flightlog->vol->financial',
310
            'target' => '',
311
            'user' => 2
312
        );
313
        $r++;
314
        $this->menu[$r] = array(
315
            'fk_menu' => 'fk_mainmenu=flightlog,fk_leftmenu=management',
316
            'type' => self::MENU_TYPE_LEFT,
317
            'titre' => 'Aviabel',
318
            'mainmenu' => 'flightlog',
319
            'leftmenu' => 'flightAviabel',
320
            'url' => '/flightlog/listFact.php?view=2',
321
            'langs' => 'mylangfile',
322
            'position' => 108,
323
            'enabled' => '1',
324
            'perms' => '$user->rights->flightlog->vol->detail',
325
            'target' => '',
326
            'user' => 2
327
        );
328
        $r++;
329
        $this->menu[$r] = array(
330
            'fk_menu' => 'fk_mainmenu=flightlog,fk_leftmenu=management',
331
            'type' => self::MENU_TYPE_LEFT,
332
            'titre' => 'Facturation mensuelle',
333
            'mainmenu' => 'flightlog',
334
            'leftmenu' => 'monthlyBill',
335
            'url' => '/flightlog/generateMonthlyBilling.php',
336
            'langs' => 'mylangfile',
337
            'position' => 109,
338
            'enabled' => '1',
339
            'perms' => '$user->rights->flightlog->vol->financial',
340
            'target' => '',
341
            'user' => 2
342
        );
343
    }
344
345
    /**
346
     * Init permissions
347
     */
348
    private function initPermissions()
349
    {
350
        $this->rights = array();        // Permission array used by this module
351
        $r = 0;
352
353
        $this->rights[$r][0] = 9993;
354
        $this->rights[$r][1] = 'Permet d\'acceder au module des vols.';
355
        $this->rights[$r][3] = 0;
356
        $this->rights[$r][4] = 'vol';
357
        $this->rights[$r][5] = 'access';
358
        $r++;
359
360
        $this->rights[$r][0] = 9998;
361
        $this->rights[$r][1] = 'Enregistrer un nouveau vol.';
362
        $this->rights[$r][3] = 0;
363
        $this->rights[$r][4] = 'vol';
364
        $this->rights[$r][5] = 'add';
365
        $r++;
366
367
        $this->rights[$r][0] = 9997;
368
        $this->rights[$r][1] = 'Permet de facturer un vol.';
369
        $this->rights[$r][3] = 0;
370
        $this->rights[$r][4] = 'vol';
371
        $this->rights[$r][5] = 'status';
372
        $r++;
373
374
        $this->rights[$r][0] = 9996;
375
        $this->rights[$r][1] = 'Permet de supprimer un vol.';
376
        $this->rights[$r][3] = 0;
377
        $this->rights[$r][4] = 'vol';
378
        $this->rights[$r][5] = 'delete';
379
        $r++;
380
381
        $this->rights[$r][0] = 9995;
382
        $this->rights[$r][1] = 'Permet de modifier tous les vols.';
383
        $this->rights[$r][3] = 0;
384
        $this->rights[$r][4] = 'vol';
385
        $this->rights[$r][5] = 'edit';
386
        $r++;
387
388
        $this->rights[$r][0] = 9994;
389
        $this->rights[$r][1] = 'affiche les details de tous les ballons et de tous les pilotes.';
390
        $this->rights[$r][3] = 0;
391
        $this->rights[$r][4] = 'vol';
392
        $this->rights[$r][5] = 'detail';
393
        $r++;
394
395
        $this->rights[$r][0] = 9999;
396
        $this->rights[$r][1] = 'Gérer les aspects financier des vols';
397
        $this->rights[$r][3] = 0;
398
        $this->rights[$r][4] = 'vol';
399
        $this->rights[$r][5] = 'financial';
400
        $r++;
401
402
        $this->rights[$r][0] = 10000;
403
        $this->rights[$r][1] = 'Gérer des documents financiers';
404
        $this->rights[$r][3] = 0;
405
        $this->rights[$r][4] = 'vol';
406
        $this->rights[$r][5] = 'financialGenerateDocuments';
407
    }
408
409
    private function initCronJobs()
410
    {
411
        $this->cronjobs = [
412
            0=>array('label'=>'bbcMonthlyFlightsBill', 'jobtype'=>'method', 'class'=>'flightlog/core/cron/BbcMonthlyFlightsBillCron.php', 'objectname'=>'BbcMonthlyFlightsBillCron', 'method'=>'run', 'parameters'=>'', 'comment'=>'Generate month bill.', 'frequency'=>1, 'unitfrequency'=>2592000),
413
        ];
414
    }
415
416
    private function initDictionnaries()
417
    {
418
        $this->initFlightTypeDictionnary();
419
    }
420
421
    private function initFlightTypeDictionnary()
422
    {
423
        $this->dictionaries = array(
424
            'langs' => 'mylangfile@mymodule',
425
            'tabname' => array(MAIN_DB_PREFIX . "bbc_types"),
426
            'tablib' => array("Types de vols"),
427
            'tabsql' => array('SELECT f.idType, f.numero, f.nom, f.active FROM ' . MAIN_DB_PREFIX . 'bbc_types as f',),
428
            'tabsqlsort' => array("numero ASC"),
429
            'tabfield' => array("idType,numero,nom"),
430
            'tabfieldvalue' => array("numero,nom"),
431
            'tabfieldinsert' => array("numero,nom"),
432
            'tabrowid' => array("idType"),
433
            'tabcond' => array('$conf->flightlog->enabled'),
434
        );
435
    }
436
437
    /**
438
     * Init hooks
439
     */
440
    private function initHooks()
441
    {
442
        if (!isset($this->module_parts["hooks"])) {
443
            $this->module_parts["hooks"] = [];
444
        }
445
446
        $this->module_parts["hooks"][] = "searchform";
447
        $this->module_parts["hooks"][] = "showLinkToObjectBlock";
448
        $this->module_parts["hooks"][] = "criccar";
449
450
    }
451
452
    private function initConstants()
453
    {
454
        $this->const = array(
455
            0 => [
456
                'BBC_FLIGHT_LOG_TAUX_REMB_KM',
457
                'chaine',
458
                '0.25',
459
                'Taux remboursement des kilomètres au BBC',
460
                true,
461
                'current',
462
                true
463
            ],
464
            1 => [
465
                'BBC_FLIGHT_LOG_UNIT_PRICE_MISSION',
466
                'chaine',
467
                '35',
468
                'Unit price special mission',
469
                true,
470
                'current',
471
                true
472
            ],
473
        );
474
    }
475
476
    /**
477
     * Init exports
478
     */
479
    private function initExports()
480
    {
481
        $r = 0;
482
        $this->addFullFlightsExport($r);
483
    }
484
485
    /**
486
     * @param int $r
487
     */
488
    private function addFullFlightsExport($r)
489
    {
490
        $this->export_code[$r] = $this->rights_class . '_' . $r;
491
        $this->export_label[$r] = 'Flights export';
492
        $this->export_enabled[$r] = '1';
493
        $this->export_permission[$r] = array(array("flightlog", "vol", "detail"));
494
        $this->export_fields_array[$r] = array(
495
            "flight.idBBC_vols" => "Identifiant",
496
            "flight.date" => "Date",
497
            "flight.lieuD" => "Lieu décollage ",
498
            "flight.lieuA" => "Lieu atterissage",
499
            "flight.heureD" => "Heure décollage",
500
            "flight.heureA" => "Heure atterissage",
501
            "flight.BBC_ballons_idBBC_ballons" => "Identifiant ballon",
502
            "flight.nbrPax" => "# pax",
503
            "flight.remarque" => "Remarque",
504
            "flight.incidents" => "Incidents",
505
            "flight.fk_type" => "Identifiant type",
506
            "flight.fk_pilot" => "Identifiant pilote",
507
            "flight.fk_organisateur" => "Identifiant organisateur",
508
            "flight.is_facture" => "Facture Oui/Non",
509
            "flight.kilometers" => "# Km",
510
            "flight.cost" => "Cout",
511
            "flight.fk_receiver" => "Identifiant receveur d'argent",
512
            "flight.justif_kilometers" => "Justificatif kilomètres",
513
            "balloon.immat" => "Immat.",
514
            "pilot.login" => "Pilote",
515
            "flightType.nom" => "Type de vol",
516
            "organisator.login" => "Organisateur",
517
            "receiver.login" => "Percepteur",
518
        );
519
520
        $this->export_TypeFields_array[$r] = [
521
            "flight.date" => "Date",
522
            "flight.lieuD" => "Text",
523
            "flight.lieuA" => "Text",
524
            "flight.heureD" => "Text",
525
            "flight.heureA" => "Text",
526
            "flight.BBC_ballons_idBBC_ballons" => implode(":", ["List", "bbc_ballons", "immat", "rowid"]),
527
            "flight.nbrPax" => "Numeric",
528
            "flight.remarque" => "Text",
529
            "flight.incidents" => "Text",
530
            "flight.fk_type" => implode(":", ["List", "bbc_types", "nom", "idType"]),
531
            "flight.fk_pilot" => implode(":", ["List", "user", "login", "rowid"]),
532
            "flight.fk_organisateur" => implode(":", ["List", "user", "login", "rowid"]),
533
            "flight.is_facture" => "Boolean",
534
            "flight.kilometers" => "Numeric",
535
            "flight.cost" => "Numeric",
536
            "flight.fk_receiver" => implode(":", ["List", "user", "login", "rowid"]),
537
            "flight.justif_kilometers" => "Text",
538
        ];
539
540
        $this->export_entities_array[$r] = array(
541
            "flight.idBBC_vols" => "Flight",
542
            "flight.date" => "Flight",
543
            "flight.lieuD" => "Flight",
544
            "flight.lieuA" => "Flight",
545
            "flight.heureD" => "Flight",
546
            "flight.heureA" => "Flight",
547
            "flight.BBC_ballons_idBBC_ballons" => "Flight",
548
            "flight.nbrPax" => "Flight",
549
            "flight.remarque" => "Flight",
550
            "flight.incidents" => "Flight",
551
            "flight.fk_type" => "Flight",
552
            "flight.fk_pilot" => "Flight",
553
            "flight.fk_organisateur" => "Flight",
554
            "flight.is_facture" => "Flight",
555
            "flight.kilometers" => "Flight",
556
            "flight.cost" => "Flight",
557
            "flight.fk_receiver" => "Flight",
558
            "flight.justif_kilometers" => "Flight",
559
            "balloon.immat" => "Balloon",
560
            "pilot.login" => "Pilot",
561
            "flightType.nom" => "FlightType",
562
            "organisator.login" => "Organisator",
563
            "receiver.login" => "Percepteur",
564
        );
565
        $this->export_sql_start[$r] = 'SELECT DISTINCT ';
566
        $this->export_sql_end[$r] = ' FROM ' . MAIN_DB_PREFIX . 'bbc_vols as flight';
567
        $this->export_sql_end[$r] .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bbc_ballons as balloon on (flight.BBC_ballons_idBBC_ballons = balloon.rowid)';
568
        $this->export_sql_end[$r] .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bbc_types as flightType on (flight.fk_type = flightType.idType)';
569
        $this->export_sql_end[$r] .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'user as pilot on (flight.fk_pilot = pilot.rowid)';
570
        $this->export_sql_end[$r] .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'user as organisator on (flight.fk_organisateur = organisator.rowid)';
571
        $this->export_sql_end[$r] .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'user as receiver on (flight.fk_receiver = receiver.rowid)';
572
        $this->export_sql_end[$r] .= ' WHERE 1 = 1';
573
    }
574
575
    /**
576
     * Activate triggers for this module
577
     */
578
    private function activateTriggers()
579
    {
580
        $this->module_parts['triggers'] = 1;
581
    }
582
583
    /**
584
     * Initialize all workflows
585
     */
586
    private function initWorkflows()
587
    {
588
        $this->module_parts['workflow'] = [
589
            "WORKFLOW_BBC_FLIGHTLOG_SEND_MAIL_ON_INCIDENT" => [
590
                'family' => 'create',
591
                'position' => 10,
592
                'enabled' => '1',
593
                'picto' => 'order'
594
            ],
595
        ];
596
    }
597
598
    /**
599
     * Add stylesheets
600
     */
601
    private function configureCss()
602
    {
603
        $this->module_parts['css'] = [
604
            '/flightlog/css/flightlog.css',
605
        ];
606
    }
607
608
    /**
609
     * Init tabs to inject in other modules.
610
     */
611
    private function initTabs()
612
    {
613
        $this->tabs = [];
614
        $this->tabs[] = [
615
            'data'=>'project:+instruction:Vol d\'instructions:mymodule@flightlog:1:/flightlog/tabs/project/instructions.php?id=__ID__'
616
        ];
617
    }
618
619
}
620
621