Completed
Branch develop (6390f5)
by
unknown
27:55
created

DataPoliciesCron::exec()   C

Complexity

Conditions 13
Paths 11

Size

Total Lines 467

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 11
nop 0
dl 0
loc 467
rs 5.2933
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* Copyright (C) 2018 SuperAdmin
3
 * Copyright (C) 2018       Frédéric France     <[email protected]>
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
/**
20
 * \file    datapolicies/class/datapoliciescron.class.php
21
 * \ingroup datapolicies
22
 * \brief   Example hook overload.
23
 */
24
25
/**
26
 * Class DataPoliciesCron
27
 */
28
class DataPoliciesCron
29
{
30
	/**
31
	 * Function exec
32
	 *
33
	 * @return boolean
34
	 */
35
    public function exec()
36
    {
37
        global $conf, $db, $langs, $user;
38
39
        $langs->load('datapolicies@datapolicies');
40
41
        // FIXME Removed hardcoded values of id
42
        $arrayofparameters=array(
43
            'DATAPOLICIES_TIERS_CLIENT' => array(
44
                'sql' => "
45
                    SELECT s.rowid FROM ".MAIN_DB_PREFIX."societe as s
46
                    WHERE (s.fk_forme_juridique IN (11, 12, 13, 15, 17, 18, 19, 35, 60, 312, 316, 401, 600, 700, 1005) OR s.fk_typent = 8)
47
                    AND s.entity = %d
48
                    AND s.client = 1
49
                    AND s.fournisseur = 0
50
                    AND s.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
51
                    AND s.rowid NOT IN (
52
                        SELECT DISTINCT a.fk_soc
53
                        FROM ".MAIN_DB_PREFIX."actioncomm as a
54
                        WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
55
                        AND a.fk_soc IS NOT NULL
56
                    )
57
                ",
58
                "class" => "Societe",
59
                "file" => DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php',
60
                'fields_anonym' => array(
61
                    'name' => $langs->trans('ANONYME'),
62
                    'name_bis' => '',
63
                    'name_alias' => '',
64
                    'address' => '',
65
                    'town' => '',
66
                    'zip' => '',
67
                    'phone' => '',
68
                    'email' => '',
69
                    'url' => '',
70
                    'fax' => '',
71
                    'state' => '',
72
                    'country' => '',
73
                    'state_id' => '',
74
                    'skype' => '',
75
                    'country_id' => '',
76
                )
77
            ),
78
            'DATAPOLICIES_TIERS_PROSPECT' => array(
79
                'sql' => "
80
                    SELECT s.rowid FROM ".MAIN_DB_PREFIX."societe as s
81
                    WHERE (s.fk_forme_juridique IN (11, 12, 13, 15, 17, 18, 19, 35, 60, 312, 316, 401, 600, 700, 1005) OR s.fk_typent = 8)
82
                    AND s.entity = %d
83
                    AND s.client = 2
84
                    AND s.fournisseur = 0
85
                    AND s.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
86
                    AND s.rowid NOT IN (
87
                        SELECT DISTINCT a.fk_soc
88
                        FROM ".MAIN_DB_PREFIX."actioncomm as a
89
                        WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
90
                        AND a.fk_soc IS NOT NULL
91
                    )
92
                ",
93
                "class" => "Societe",
94
                "file" => DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php',
95
                'fields_anonym' => array(
96
                    'name' => $langs->trans('ANONYME'),
97
                    'name_bis' => '',
98
                    'name_alias' => '',
99
                    'address' => '',
100
                    'town' => '',
101
                    'zip' => '',
102
                    'phone' => '',
103
                    'email' => '',
104
                    'url' => '',
105
                    'fax' => '',
106
                    'state' => '',
107
                    'country' => '',
108
                    'state_id' => '',
109
                    'skype' => '',
110
                    'country_id' => '',
111
                )
112
            ),
113
            'DATAPOLICIES_TIERS_PROSPECT_CLIENT' => array(
114
                'sql' => "
115
                    SELECT s.rowid FROM ".MAIN_DB_PREFIX."societe as s
116
                    WHERE (s.fk_forme_juridique  IN (11, 12, 13, 15, 17, 18, 19, 35, 60, 312, 316, 401, 600, 700, 1005) OR s.fk_typent = 8)
117
                    AND s.entity = %d
118
                    AND s.client = 3
119
                    AND s.fournisseur = 0
120
                    AND s.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
121
                    AND s.rowid NOT IN (
122
                        SELECT DISTINCT a.fk_soc
123
                        FROM ".MAIN_DB_PREFIX."actioncomm as a
124
                        WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
125
                        AND a.fk_soc IS NOT NULL
126
                    )
127
                ",
128
                "class" => "Societe",
129
                "file" => DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php',
130
                'fields_anonym' => array(
131
                    'name' => $langs->trans('ANONYME'),
132
                    'name_bis' => '',
133
                    'name_alias' => '',
134
                    'address' => '',
135
                    'town' => '',
136
                    'zip' => '',
137
                    'phone' => '',
138
                    'email' => '',
139
                    'url' => '',
140
                    'fax' => '',
141
                    'state' => '',
142
                    'country' => '',
143
                    'state_id' => '',
144
                    'skype' => '',
145
                    'country_id' => '',
146
                )
147
            ),
148
            'DATAPOLICIES_TIERS_NIPROSPECT_NICLIENT' => array(
149
                'sql' => "
150
                    SELECT s.rowid FROM ".MAIN_DB_PREFIX."societe as s
151
                    WHERE (s.fk_forme_juridique  IN (11, 12, 13, 15, 17, 18, 19, 35, 60, 312, 316, 401, 600, 700, 1005) OR s.fk_typent = 8)
152
                    AND s.entity = %d
153
                    AND s.client = 0
154
                    AND s.fournisseur = 0
155
                    AND s.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
156
                    AND s.rowid NOT IN (
157
                        SELECT DISTINCT a.fk_soc
158
                        FROM ".MAIN_DB_PREFIX."actioncomm as a
159
                        WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
160
                        AND a.fk_soc IS NOT NULL
161
                    )
162
                ",
163
                "class" => "Societe",
164
                "file" => DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php',
165
                'fields_anonym' => array(
166
                    'name' => $langs->trans('ANONYME'),
167
                    'name_bis' => '',
168
                    'name_alias' => '',
169
                    'address' => '',
170
                    'town' => '',
171
                    'zip' => '',
172
                    'phone' => '',
173
                    'email' => '',
174
                    'url' => '',
175
                    'fax' => '',
176
                    'state' => '',
177
                    'country' => '',
178
                    'state_id' => '',
179
                    'skype' => '',
180
                    'country_id' => '',
181
                )
182
            ),
183
            'DATAPOLICIES_TIERS_FOURNISSEUR' => array(
184
                'sql' => "
185
                    SELECT s.rowid FROM ".MAIN_DB_PREFIX."societe as s
186
                    WHERE (s.fk_forme_juridique  IN (11, 12, 13, 15, 17, 18, 19, 35, 60, 312, 316, 401, 600, 700, 1005) OR s.fk_typent = 8)
187
                    AND s.entity = %d
188
                    AND s.fournisseur = 1
189
                    AND s.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
190
                    AND s.rowid NOT IN (
191
                        SELECT DISTINCT a.fk_soc
192
                        FROM ".MAIN_DB_PREFIX."actioncomm as a
193
                        WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
194
                        AND a.fk_contact IS NOT NULL
195
                    )
196
                ",
197
                "class" => "Societe",
198
                "file" => DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php',
199
                'fields_anonym' => array(
200
                    'name' => $langs->trans('ANONYME'),
201
                    'name_bis' => '',
202
                    'name_alias' => '',
203
                    'address' => '',
204
                    'town' => '',
205
                    'zip' => '',
206
                    'phone' => '',
207
                    'email' => '',
208
                    'url' => '',
209
                    'fax' => '',
210
                    'state' => '',
211
                    'country' => '',
212
                    'state_id' => '',
213
                    'skype' => '',
214
                    'country_id' => '',
215
                )
216
            ),
217
            'DATAPOLICIES_CONTACT_CLIENT' => array(
218
                'sql' => "
219
                    SELECT c.rowid FROM ".MAIN_DB_PREFIX."socpeople as c
220
                    INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc
221
                    WHERE c.entity = %d
222
                    AND c.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
223
                    AND s.client = 1
224
                    AND s.fournisseur = 0
225
                    AND c.rowid NOT IN (
226
                        SELECT DISTINCT a.fk_contact
227
                        FROM ".MAIN_DB_PREFIX."actioncomm as a
228
                        WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
229
                        AND a.fk_contact IS NOT NULL
230
                    )
231
                ",
232
                "class" => "Contact",
233
                "file" => DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php',
234
                'fields_anonym' => array(
235
                    'lastname' => $langs->trans('ANONYME'),
236
                    'firstname' => '',
237
                    'civility_id' => '',
238
                    'poste' => '',
239
                    'address' => '',
240
                    'town' => '',
241
                    'zip' => '',
242
                    'phone_pro' => '',
243
                    'phone_perso' => '',
244
                    'phone_mobile' => '',
245
                    'email' => '',
246
                    'url' => '',
247
                    'fax' => '',
248
                    'state' => '',
249
                    'country' => '',
250
                    'state_id' => '',
251
                    'skype' => '',
252
                    'jabberid' => '',
253
                    'country_id' => '',
254
                )
255
            ),
256
            'DATAPOLICIES_CONTACT_PROSPECT' => array(
257
                'sql' => "
258
                    SELECT c.rowid FROM ".MAIN_DB_PREFIX."socpeople as c
259
                    INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc
260
                    WHERE c.entity = %d
261
                    AND c.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
262
                    AND s.client = 2
263
                    AND s.fournisseur = 0
264
                    AND c.rowid NOT IN (
265
                        SELECT DISTINCT a.fk_contact
266
                        FROM ".MAIN_DB_PREFIX."actioncomm as a
267
                        WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
268
                        AND a.fk_contact IS NOT NULL
269
                    )
270
                ",
271
                "class" => "Contact",
272
                "file" => DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php',
273
                'fields_anonym' => array(
274
                    'lastname' => $langs->trans('ANONYME'),
275
                    'firstname' => '',
276
                    'civility_id' => '',
277
                    'poste' => '',
278
                    'address' => '',
279
                    'town' => '',
280
                    'zip' => '',
281
                    'phone_pro' => '',
282
                    'phone_perso' => '',
283
                    'phone_mobile' => '',
284
                    'email' => '',
285
                    'url' => '',
286
                    'fax' => '',
287
                    'state' => '',
288
                    'country' => '',
289
                    'state_id' => '',
290
                    'skype' => '',
291
                    'jabberid' => '',
292
                    'country_id' => '',
293
                )
294
            ),
295
            'DATAPOLICIES_CONTACT_PROSPECT_CLIENT' => array(
296
                'sql' => "
297
                    SELECT c.rowid FROM ".MAIN_DB_PREFIX."socpeople as c
298
                    INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc
299
                    WHERE c.entity = %d
300
                    AND c.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
301
                    AND s.client = 3
302
                    AND s.fournisseur = 0
303
                    AND c.rowid NOT IN (
304
                        SELECT DISTINCT a.fk_contact
305
                        FROM ".MAIN_DB_PREFIX."actioncomm as a
306
                        WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
307
                        AND a.fk_contact IS NOT NULL
308
                    )
309
                ",
310
                "class" => "Contact",
311
                "file" => DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php',
312
                'fields_anonym' => array(
313
                    'lastname' => $langs->trans('ANONYME'),
314
                    'firstname' => '',
315
                    'civility_id' => '',
316
                    'poste' => '',
317
                    'address' => '',
318
                    'town' => '',
319
                    'zip' => '',
320
                    'phone_pro' => '',
321
                    'phone_perso' => '',
322
                    'phone_mobile' => '',
323
                    'email' => '',
324
                    'url' => '',
325
                    'fax' => '',
326
                    'state' => '',
327
                    'country' => '',
328
                    'state_id' => '',
329
                    'skype' => '',
330
                    'jabberid' => '',
331
                    'country_id' => '',
332
                )
333
            ),
334
            'DATAPOLICIES_CONTACT_NIPROSPECT_NICLIENT' => array(
335
                'sql' => "
336
                    SELECT c.rowid FROM ".MAIN_DB_PREFIX."socpeople as c
337
                    INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc
338
                    WHERE c.entity = %d
339
                    AND c.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
340
                    AND s.client = 0
341
                    AND s.fournisseur = 0
342
                    AND c.rowid NOT IN (
343
                        SELECT DISTINCT a.fk_contact
344
                        FROM ".MAIN_DB_PREFIX."actioncomm as a
345
                        WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
346
                        AND a.fk_contact IS NOT NULL
347
                    )
348
                ",
349
                "class" => "Contact",
350
                "file" => DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php',
351
                'fields_anonym' => array(
352
                    'lastname' => $langs->trans('ANONYME'),
353
                    'firstname' => '',
354
                    'civility_id' => '',
355
                    'poste' => '',
356
                    'address' => '',
357
                    'town' => '',
358
                    'zip' => '',
359
                    'phone_pro' => '',
360
                    'phone_perso' => '',
361
                    'phone_mobile' => '',
362
                    'email' => '',
363
                    'url' => '',
364
                    'fax' => '',
365
                    'state' => '',
366
                    'country' => '',
367
                    'state_id' => '',
368
                    'skype' => '',
369
                    'jabberid' => '',
370
                    'country_id' => '',
371
                )
372
            ),
373
            'DATAPOLICIES_CONTACT_FOURNISSEUR' => array(
374
                'sql' => "
375
                    SELECT c.rowid FROM ".MAIN_DB_PREFIX."socpeople as c
376
                    INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc
377
                    WHERE c.entity = %d
378
                    AND c.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
379
                    AND s.fournisseur = 1
380
                    AND c.rowid NOT IN (
381
                        SELECT DISTINCT a.fk_contact
382
                        FROM ".MAIN_DB_PREFIX."actioncomm as a
383
                        WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
384
                        AND a.fk_contact IS NOT NULL
385
                    )
386
                ",
387
                "class" => "Contact",
388
                "file" => DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php',
389
                'fields_anonym' => array(
390
                    'lastname' => $langs->trans('ANONYME'),
391
                    'firstname' => '',
392
                    'civility_id' => '',
393
                    'poste' => '',
394
                    'address' => '',
395
                    'town' => '',
396
                    'zip' => '',
397
                    'phone_pro' => '',
398
                    'phone_perso' => '',
399
                    'phone_mobile' => '',
400
                    'email' => '',
401
                    'url' => '',
402
                    'fax' => '',
403
                    'state' => '',
404
                    'country' => '',
405
                    'state_id' => '',
406
                    'skype' => '',
407
                    'jabberid' => '',
408
                    'country_id' => '',
409
                )
410
            ),
411
            'DATAPOLICIES_ADHERENT' => array(
412
                'sql' => "
413
                    SELECT a.rowid FROM ".MAIN_DB_PREFIX."adherent as a
414
                    WHERE a.entity = %d
415
                    AND a.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
416
                    AND a.rowid NOT IN (
417
                        SELECT DISTINCT a.fk_element
418
                        FROM ".MAIN_DB_PREFIX."actioncomm as a
419
                        WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
420
                        AND a.elementtype LIKE 'member'
421
                        AND a.fk_element IS NOT NULL
422
                    )
423
                ",
424
                "class" => "Adherent",
425
                "file" => DOL_DOCUMENT_ROOT . '/adherents/class/adherent.class.php',
426
                'fields_anonym' => array(
427
                    'lastname' => $langs->trans('ANONYME'),
428
                    'firstname' => $langs->trans('ANONYME'),
429
                    'civility_id' => '',
430
                    'societe' => '',
431
                    'address' => '',
432
                    'town' => '',
433
                    'zip' => '',
434
                    'phone' => '',
435
                    'phone_perso' => '',
436
                    'phone_mobile' => '',
437
                    'email' => '',
438
                    'url' => '',
439
                    'fax' => '',
440
                    'state' => '',
441
                    'country' => '',
442
                    'state_id' => '',
443
                    'skype' => '',
444
                    'country_id' => '',
445
                )
446
            ),
447
        );
448
449
        foreach ($arrayofparameters as $key => $params) {
450
            if ($conf->global->$key != '' && is_numeric($conf->global->$key) && (int) $conf->global->$key > 0) {
451
452
                $sql = sprintf($params['sql'], (int) $conf->entity, (int) $conf->global->$key, (int) $conf->global->$key);
453
454
                $resql = $db->query($sql);
455
456
                if ($resql && $db->num_rows($resql) > 0) {
457
458
                    $num = $db->num_rows($resql);
459
                    $i = 0;
460
461
                    require_once $params['file'];
462
                    $object = new $params['class']($db);
463
464
                    while ($i < $num)
465
                    {
466
                        $obj = $db->fetch_object($resql);
467
468
                        $object->fetch($obj->rowid);
469
                        $object->id = $obj->rowid;
470
471
                        if ($object->isObjectUsed($obj->rowid) > 0) {
472
                            foreach ($params['fields_anonym'] as $fields => $val) {
473
                                $object->$fields = $val;
474
                            }
475
                            $object->update($obj->rowid, $user);
476
                            if ($params['class'] == 'Societe') {
477
                                // On supprime les contacts associé
478
                                $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople WHERE fk_soc = " . $obj->rowid;
479
                                $db->query($sql);
480
                            }
481
                        } else {
482
                            if (DOL_VERSION < 8) {
483
                                $ret = $object->delete($obj->rowid, $user);
484
                            } else {
485
                                if ($object->element == 'adherent') {
486
                                    $ret = $object->delete($obj->rowid);
487
                                } else {
488
                                    $ret = $object->delete();
489
                                }
490
                            }
491
492
                        }
493
494
                        $i++;
495
                    }
496
                }
497
            }
498
499
        }
500
        return true;
501
    }
502
503
504
    /**
505
     * sendMailing
506
     *
507
     * @return boolean
508
     */
509
    public function sendMailing()
510
    {
511
        global $conf, $db, $langs, $user;
512
513
        $langs->load('datapolicies@datapolicies');
514
515
        require_once DOL_DOCUMENT_ROOT . '/datapolicies/class/datapolicies.class.php';
516
517
        $contacts = new DataPolicies($db);
518
        $contacts->getAllContactNotInformed();
519
        $contacts->getAllCompaniesNotInformed();
520
        $contacts->getAllAdherentsNotInformed();
521
        return true;
522
    }
523
}