cartable.php 12.2 KB
Newer Older
Bastien Ho's avatar
Bastien Ho committed
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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php

require_once 'cartable.civix.php';
// phpcs:disable
use CRM_Cartable_ExtensionUtil as E;
// phpcs:enable

/**
 * Implements hook_civicrm_config().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
 */
function cartable_civicrm_config(&$config) {
  _cartable_civix_civicrm_config($config);
}

/**
 * Implements hook_civicrm_xmlMenu().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_xmlMenu
 */
function cartable_civicrm_xmlMenu(&$files) {
  _cartable_civix_civicrm_xmlMenu($files);
}

/**
 * Implements hook_civicrm_install().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
 */
function cartable_civicrm_install() {
  _cartable_civix_civicrm_install();
}

/**
 * Implements hook_civicrm_postInstall().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall
 */
function cartable_civicrm_postInstall() {
  _cartable_civix_civicrm_postInstall();
}

/**
 * Implements hook_civicrm_uninstall().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall
 */
function cartable_civicrm_uninstall() {
  _cartable_civix_civicrm_uninstall();
}

/**
 * Implements hook_civicrm_enable().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
 */
function cartable_civicrm_enable() {
  _cartable_civix_civicrm_enable();
}

/**
 * Implements hook_civicrm_disable().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable
 */
function cartable_civicrm_disable() {
  _cartable_civix_civicrm_disable();
}

/**
 * Implements hook_civicrm_upgrade().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_upgrade
 */
function cartable_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
  return _cartable_civix_civicrm_upgrade($op, $queue);
}

/**
 * Implements hook_civicrm_managed().
 *
 * Generate a list of entities to create/deactivate/delete when this module
 * is installed, disabled, uninstalled.
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_managed
 */
function cartable_civicrm_managed(&$entities) {
  _cartable_civix_civicrm_managed($entities);
}

/**
 * Implements hook_civicrm_caseTypes().
 *
 * Add CiviCase types provided by this extension.
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_caseTypes
 */
function cartable_civicrm_caseTypes(&$caseTypes) {
  _cartable_civix_civicrm_caseTypes($caseTypes);
}

/**
 * Implements hook_civicrm_angularModules().
 *
 * Add Angular modules provided by this extension.
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_angularModules
 */
function cartable_civicrm_angularModules(&$angularModules) {
  // Auto-add module files from ./ang/*.ang.php
  _cartable_civix_civicrm_angularModules($angularModules);
}

/**
 * Implements hook_civicrm_alterSettingsFolders().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterSettingsFolders
 */
function cartable_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
  _cartable_civix_civicrm_alterSettingsFolders($metaDataFolders);
}

/**
 * Implements hook_civicrm_entityTypes().
 *
 * Declare entity types provided by this module.
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes
 */
function cartable_civicrm_entityTypes(&$entityTypes) {
  _cartable_civix_civicrm_entityTypes($entityTypes);
}

/**
 * Implements hook_civicrm_themes().
 */
function cartable_civicrm_themes(&$themes) {
  _cartable_civix_civicrm_themes($themes);
}

// --- Functions below this ship commented out. Uncomment as required. ---

/**
 * Implements hook_civicrm_preProcess().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_preProcess
 */
//function cartable_civicrm_preProcess($formName, &$form) {
//
//}

153
154
155
156
157
158
function cartable_civicrm_pageRun(  &$page ) {
  if($page instanceof CRM_Cartable_Page_GenerateMembershipTypes){
    CRM_Core_Resources::singleton()->addScriptFile('cartable', 'assets/built/membership-types.js', 110, 'html-header', FALSE);
  }
}

Bastien Ho's avatar
Bastien Ho committed
159
160
161
162
163
164
/**
 * Implements hook_civicrm_navigationMenu().
 *
 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_navigationMenu
 */
function cartable_civicrm_navigationMenu(&$menu) {
165
166
167
168
169
170
171
172
 _cartable_civix_insert_navigation_menu($menu, 'Administer/CiviMember', [
   'label' => 'Générer des types d’adhésion',
   'name' => 'GenerateMembershipTypes',
   'url' => 'civicrm/cartable/generate-membership-types',
   'permission' => 'access CiviCRM',
   'operator' => 'OR',
   'separator' => 1,
 ]);
Bastien Ho's avatar
Bastien Ho committed
173
174
 _cartable_civix_navigationMenu($menu);
}
175

Bastien Ho's avatar
Bastien Ho committed
176
177
178
179
180
181
182
183
184
function cartable_include($path){
    $complete_path = __DIR__.'/'.$path;
    if(file_exists($complete_path)){
        include($complete_path);
        return $complete_path;
    }
    return false;
}

Bastien Ho's avatar
Bastien Ho committed
185
186
187
188
189
190
function cartable_civicrm_buildForm($formName, &$form) {
  header('FormName:'.$formName);
  $templatePath = realpath(dirname(__FILE__)."/templates");

  if ($formName == 'CRM_Contribute_Form_Contribution_Main') {
    CRM_Core_Resources::singleton()->addScriptFile('cartable', 'vendor/bastienho/jquery-tabs/jquery-tabs.js');
191
    CRM_Core_Resources::singleton()->addScriptFile('cartable', 'assets/built/academies.js');
Bastien Ho's avatar
Bastien Ho committed
192
    CRM_Core_Resources::singleton()->addStyleFile('cartable', 'vendor/bastienho/jquery-tabs/jquery-tabs.css');
193
    CRM_Core_Resources::singleton()->addStyleFile('cartable', 'assets/built/academies.css');
Bastien Ho's avatar
Bastien Ho committed
194
  }
195
196
197
  if ($formName == 'CRM_Contribute_Form_Contribution_Main' || $formName == 'CRM_Contact_Form_Contact') {
    CRM_Core_Resources::singleton()->addScriptFile('cartable', 'assets/built/donnees-particulier.js');
  }
Bastien Ho's avatar
Bastien Ho committed
198
}
199

Bastien Ho's avatar
Bastien Ho committed
200
function cartable_get_values($dataset, $sort=true){
201
202
    $maybe_file = __DIR__."/inc/values/{$dataset}.php";
    if(file_exists($maybe_file)){
Bastien Ho's avatar
Bastien Ho committed
203
204
205
206
207
        $array = include $maybe_file;
        if($sort){
            asort($array,  SORT_NATURAL);
        }
        return $array;
208
209
210
211
    }
    return [];
}

Bastien Ho's avatar
Bastien Ho committed
212
213
214
215
216
217
218
function cartable_get_custom_field_id($fieldname, $groupname=null){
    $FieldId = civicrm_api3('CustomField', 'getvalue', ['return'=>"id", 'name'=>$fieldname, 'custom_group_id'=>$groupname]);
    if(!$FieldId){
        return false;
    }
    return "custom_{$FieldId}";
}
219

220
221
222
function cartable_get_donnees_particuliers_vars(){
    $CorpsField = cartable_get_custom_field_id('Corps', 'DonneesParticulier');
    $DisciplineField = cartable_get_custom_field_id('Discipline', 'DonneesParticulier');
Bastien Ho's avatar
Bastien Ho committed
223
    $AutreDisciplineField = cartable_get_custom_field_id('AutreDiscipline', 'DonneesParticulier');
224
225
    $StatutField = cartable_get_custom_field_id('Statut', 'DonneesParticulier');
    $TypeDePosteField = cartable_get_custom_field_id('TypeDePoste', 'DonneesParticulier');
Bastien Ho's avatar
Bastien Ho committed
226
    if(!$CorpsField || !$DisciplineField || !$AutreDisciplineField || !$StatutField || !$TypeDePosteField){
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
        return false;
    }
    return [
        'hide_if_not' => [
            $CorpsField => [
                $StatutField => [
                    'titulaire',
                    'stagiaire',
                ],
            ],
            $TypeDePosteField => [
                $StatutField => [
                    'cdd',
                    'cdi',
                ],
            ],
            $DisciplineField => [
                $CorpsField => [
                    'Certifié·e',
                    'Agrégé·e',
                    'PLP',
                ],
                $TypeDePosteField => [
                    'Enseignant·e',
                ],
            ],
Bastien Ho's avatar
Bastien Ho committed
253
254
255
256
257
258
259
260
            $AutreDisciplineField => [
                $CorpsField => [
                    'Autre',
                ],
                $TypeDePosteField => [
                    'Autre',
                ],
            ],
261
262
263
264
265
        ],
        'filter_options' => [
            $DisciplineField => [
                // Corps
                $CorpsField => [
266
267
                    'Certifié·e' => cartable_get_values('disciplines-fonctionnaires'),
                    'Agrégé·e' => cartable_get_values('disciplines-fonctionnaires'),
Bastien Ho's avatar
Bastien Ho committed
268
                    'PLP' => cartable_get_values('disciplines-non-titulaires'),
269
270
271
                ],
                // Type de post
                $TypeDePosteField => [
272
                    'Enseignant·e' => cartable_get_values('disciplines-non-titulaires'),
273
274
275
276
277
                ],
            ],
        ],
    ];
}
Bastien Ho's avatar
Comment    
Bastien Ho committed
278

279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
function cartable_get_membership_vars(){
    $membershipRatio = civicrm_api3('Setting', 'getvalue', [
        'return' => "value",
        'name' => "cartable_membership_ratio",
    ]);
    $membershipRatesSettings = civicrm_api3('Setting', 'getvalue', [
        'return' => "value",
        'name' => "cartable_membership_rates",
    ]);
    $membershipRates = [];
    if($membershipRatesSettings){
        $membershipRatesRows = explode("\n", $membershipRatesSettings);
        foreach($membershipRatesRows as $membershipRatesRow){
            $membershipRate = explode('|', $membershipRatesRow);
            $membershipRates[(int) $membershipRate[0]] = (int) $membershipRate[1];
        }
    }
    $vars = [
        'membershipRatio' => $membershipRatio,
        'membershipRates' => $membershipRates,
    ];
    if(civicrm_api3('Setting', 'getvalue', [
        'return' => "value",
        'name' => "debug_enabled",
    ])){
        $vars['debug'] = [
            '_RatesSettings' => $membershipRatesSettings,
            '_membershipRatesRows' => $membershipRatesRows,
        ];
    }

    return $vars;
}

Bastien Ho's avatar
Bastien Ho committed
313
function cartable_get_indexed_syndicates_and_provinces($vars){
Bastien Ho's avatar
Bastien Ho committed
314
    $ProvinceField = cartable_get_custom_field_id('Departements', 'DonneesSyndicat');
315
316
317
    if(!$ProvinceField){
        return false;
    }
Bastien Ho's avatar
Bastien Ho committed
318
    // First get all needed values
319
320
321
    $CountryId = civicrm_api3('Country', 'getvalue', ['return'=>"id", 'name'=>"france"]);
    $Provinces = civicrm_api3('StateProvince', 'get', ['country_id'=>$CountryId, 'options'=>['limit'=>0], 'sequential'=>1]);
    $Syndicates = civicrm_api3('Contact', 'get', ['return'=>"id,display_name,{$ProvinceField}", 'contact_sub_type'=>"SyndicatSUD", 'options'=>['limit'=>-1], 'sequential'=>1 ]);
322
323
324
    $MembershipTypes = civicrm_api3('MembershipType', 'get', []);

    $membershipFieldID = $vars['membershipFieldID'];
325
326
327

    $IndexedSyndicates = [];
    $IndexedProvinces = [];
328
329
    $IndexedMembershipTypes = [];
    $IndexedMembershipFields = [];
330

Bastien Ho's avatar
Bastien Ho committed
331
    // Index values per relation
332
333
334
335
    foreach($Provinces['values'] as $province){
        $IndexedSyndicates[$province['abbreviation']] = [];
        $IndexedProvinces[$province['id']] = $province['abbreviation'];
    }
336
337
338
339
340
341
342
    foreach($MembershipTypes['values'] as $membershipType){
        $IndexedMembershipTypes[$membershipType['member_of_contact_id']] = $membershipType;
    }
    foreach($vars['priceSet']['fields'][$membershipFieldID]['options'] as $membershipField){
        $IndexedMembershipFields[$membershipField['membership_type_id']] = $membershipField;
    }

Bastien Ho's avatar
Bastien Ho committed
343
    // Aggregate syndicates per department, membership types per syndicate
344
345
346
347
    foreach($Syndicates['values'] as $syndicate){
        if(!isset($syndicate[$ProvinceField]) || empty($syndicate[$ProvinceField])){
            continue;
        }
348
349
350
351
352
353
        //CIVICRM_QFID_15_price_12
        //CIVICRM_QFID_{$membershipFieldOpeionId}_price_{$membershipFieldID}
        $syndicate['membership_field'] = isset($IndexedMembershipTypes[$syndicate['id']])
            ? 'CIVICRM_QFID_'.$IndexedMembershipFields[$IndexedMembershipTypes[$syndicate['id']]['id']]['id'].'_price_'.$IndexedMembershipFields[$IndexedMembershipTypes[$syndicate['id']]['id']]['price_field_id']
            : null;

354
355
356
357
358
359
360
        $syndicate_provinces = $syndicate[$ProvinceField];

        foreach($syndicate[$ProvinceField] as $province_id){
            $IndexedSyndicates[$IndexedProvinces[$province_id]][] = $syndicate;
        }
    }

Bastien Ho's avatar
typo    
Bastien Ho committed
361
    $variables = [
362
363
        'IndexedSyndicates' => $IndexedSyndicates,
        'IndexedProvinces' => $IndexedProvinces,
Bastien Ho's avatar
Bastien Ho committed
364
365
366
367
368
369
    ];

    if(civicrm_api3('Setting', 'getvalue', [
        'return' => "value",
        'name' => "debug_enabled",
    ])){
Bastien Ho's avatar
typo    
Bastien Ho committed
370
        $variables['debug'] = [
Bastien Ho's avatar
Bastien Ho committed
371
372
373
            '_Provinces' => $Provinces['values'],
            '_Syndicates' => $Syndicates['values'],
            '_MembershipTypes' => $MembershipTypes['values'],
374
            '_PriceSet' => $vars['priceSet'],
Bastien Ho's avatar
Bastien Ho committed
375
            '_MembershipFields' => $vars['priceSet']['fields'][$membershipFieldID],
376
            '_Vars' => $vars,
Bastien Ho's avatar
Bastien Ho committed
377
378
379
        ];
    }

Bastien Ho's avatar
typo    
Bastien Ho committed
380
    return $variables;
381
}