-
-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathOrganization.php
More file actions
904 lines (811 loc) · 42.9 KB
/
Organization.php
File metadata and controls
904 lines (811 loc) · 42.9 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
<?php
namespace Admidio\Organizations\Entity;
use Admidio\Categories\Entity\Category;
use Admidio\Documents\Entity\Folder;
use Admidio\Events\Entity\Event;
use Admidio\Forum\Service\ForumService;
use Admidio\Infrastructure\Database;
use Admidio\Photos\Entity\Album;
use Admidio\Preferences\ValueObject\SettingsManager;
use Admidio\ProfileFields\ValueObjects\ProfileFields;
use Admidio\Roles\Entity\ListConfiguration;
use Admidio\Roles\Entity\Membership;
use Admidio\Roles\Entity\Role;
use Admidio\Infrastructure\Entity\Entity;
use Admidio\Infrastructure\Entity\Text;
use Admidio\Roles\Entity\RolesRights;
use Ramsey\Uuid\Uuid;
use Admidio\Infrastructure\Exception;
use Admidio\Changelog\Entity\LogChanges;
/**
* @brief Handle organization data of Admidio and is connected to database table adm_organizations
*
* This class creates the organization object and manages the access to the
* organization specific preferences of the table adm_preferences. There
* are also some method to read the relationship of organizations if the
* database contains more as one organization.
*
* **Code example**
* ```
* // create object and read the value of the language preference
* $organization = new Organization($gDb, $organizationId);
* $settingsManager =& $organization->getSettingsManager();
* $language = $settingsManager->get('system_language');
* // language = 'de'
* ```
* @copyright The Admidio Team
* @see https://www.admidio.org/
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
*/
class Organization extends Entity
{
/**
* @var bool Flag will be set if the class had already searched for child organizations
*/
protected bool $bCheckChildOrganizations = false;
/**
* @var array<int,string> Array with all child organizations of this organization
*/
protected array $childOrganizations = array();
/**
* @var SettingsManager Manager for organization preferences
*/
protected SettingsManager $settingsManager;
/**
* @var int Number of all organizations in database
*/
protected int $countOrganizations = 0;
/**
* Constructor that will create an object of a recordset of the table adm_organizations.
* If the id is set than the specific organization will be loaded.
* @param Database $database Object of the class Database. This should be the default global object **$gDb**.
* @param int|string $organization The recordset of the organization with this id will be loaded.
* The organization can be the table id or the organization shortname.
* If id isn't set than an empty object of the table is created.
* @throws Exception
*/
public function __construct(Database $database, $organization = '')
{
parent::__construct($database, TBL_ORGANIZATIONS, 'org');
if (is_numeric($organization)) {
$this->readDataById($organization);
} else {
$this->readDataByColumns(array('org_shortname' => $organization));
}
if ((int)$this->getValue('org_id') > 0) {
$this->settingsManager = new SettingsManager($database, (int)$this->getValue('org_id'));
$this->settingsManager->resetAll();
}
}
/**
* Initialize all necessary data of this object.
* @return void
* @throws Exception
*/
public function clear()
{
parent::clear();
$this->bCheckChildOrganizations = false;
$this->childOrganizations = array();
$this->countOrganizations = 0;
if (isset($this->settingsManager)) {
$this->settingsManager->clearAll();
}
}
/**
* Reads the number of all records of this table. In addition to the parent method
* this method will cache the value and will return the cached value on multiple calls.
* @return int Number of all organizations in database.
* @throws Exception
*/
public function countAllRecords(): int
{
if ($this->countOrganizations === 0) {
$this->countOrganizations = parent::countAllRecords();
}
return $this->countOrganizations;
}
/**
* Creates all necessary data for a new organization. This method can only be called once for an organization.
* It will create the basic categories, lists, roles, systemmails etc.
* @param int $userId The id of the administrator who creates the new organization.
* This will be the first valid user of the new organization.
* @throws Exception
*/
public function createBasicData(int $userId)
{
global $gL10n, $gProfileFields;
// read id of system user from database
$sql = 'SELECT usr_id
FROM ' . TBL_USERS . '
WHERE usr_login_name = ? -- $gL10n->get(\'SYS_SYSTEM\')';
$systemUserStatement = $this->db->queryPrepared($sql, array($gL10n->get('SYS_SYSTEM')));
$systemUserId = (int)$systemUserStatement->fetchColumn();
// create all systemmail texts and write them into table adm_texts
$systemmailsTexts = array(
'SYSMAIL_REGISTRATION_CONFIRMATION' => $gL10n->get('SYS_SYSMAIL_REGISTRATION_CONFIRMATION'),
'SYSMAIL_REGISTRATION_NEW' => $gL10n->get('SYS_SYSMAIL_REGISTRATION_ADMINISTRATOR'),
'SYSMAIL_REGISTRATION_APPROVED' => $gL10n->get('SYS_SYSMAIL_REGISTRATION_USER'),
'SYSMAIL_REGISTRATION_REFUSED' => $gL10n->get('SYS_SYSMAIL_REFUSE_REGISTRATION'),
'SYSMAIL_LOGIN_INFORMATION' => $gL10n->get('SYS_SYSMAIL_LOGIN_INFORMATION'),
'SYSMAIL_PASSWORD_RESET' => $gL10n->get('SYS_SYSMAIL_PASSWORD_RESET')
);
$text = new Text($this->db);
$orgId = (int)$this->getValue('org_id');
foreach ($systemmailsTexts as $key => $value) {
$text->clear();
$text->setValue('txt_org_id', $orgId);
$text->setValue('txt_name', $key);
$text->setValue('txt_text', $value);
$text->save();
}
// create default category for roles, events and weblinks
$sql = 'INSERT INTO ' . TBL_CATEGORIES . '
(cat_org_id, cat_uuid, cat_type, cat_name_intern, cat_name, cat_default, cat_sequence, cat_usr_id_create, cat_timestamp_create)
VALUES (?, ?, \'ROL\', \'COMMON\', \'SYS_COMMON\', true, 1, ?, ?)';
$queryParams = array($orgId, Uuid::uuid4(), $systemUserId, DATETIME_NOW);
$this->db->queryPrepared($sql, $queryParams);
$categoryCommon = $this->db->lastInsertId();
$sql = 'INSERT INTO ' . TBL_CATEGORIES . '
(cat_org_id, cat_uuid, cat_type, cat_name_intern, cat_name, cat_default, cat_system, cat_sequence, cat_usr_id_create, cat_timestamp_create)
VALUES (?, ?, \'ROL\', \'GROUPS\', \'INS_GROUPS\', false, false, 2, ?, ?)
, (?, ?, \'ROL\', \'COURSES\', \'INS_COURSES\', false, false, 3, ?, ?)
, (?, ?, \'ROL\', \'TEAMS\', \'INS_TEAMS\', false, false, 4, ?, ?)
, (?, ?, \'ROL\', \'EVENTS\', \'SYS_EVENTS_CONFIRMATION_OF_PARTICIPATION\', false, true, 5, ?, ?)
, (?, ?, \'LNK\', \'COMMON\', \'SYS_COMMON\', true, false, 1, ?, ?)
, (?, ?, \'LNK\', \'INTERN\', \'INS_INTERN\', false, false, 2, ?, ?)
, (?, ?, \'ANN\', \'COMMON\', \'SYS_COMMON\', true, false, 1, ?, ?)
, (?, ?, \'ANN\', \'IMPORTANT\', \'SYS_IMPORTANT\', false, false, 2, ?, ?)
, (?, ?, \'FOT\', \'COMMON\', \'SYS_COMMON\', true, false, 1, ?, ?)
, (?, ?, \'EVT\', \'COMMON\', \'SYS_COMMON\', true, false, 1, ?, ?)
, (?, ?, \'EVT\', \'TRAINING\', \'INS_TRAINING\', false, false, 2, ?, ?)
, (?, ?, \'EVT\', \'COURSES\', \'INS_COURSES\', false, false, 3, ?, ?)
, (?, ?, \'IVT\', \'COMMON\', \'SYS_COMMON\', true, false, 1, ?, ?)';
$queryParams = array(
$orgId, Uuid::uuid4(), $systemUserId, DATETIME_NOW,
$orgId, Uuid::uuid4(), $systemUserId, DATETIME_NOW,
$orgId, Uuid::uuid4(), $systemUserId, DATETIME_NOW,
$orgId, Uuid::uuid4(), $systemUserId, DATETIME_NOW,
$orgId, Uuid::uuid4(), $systemUserId, DATETIME_NOW,
$orgId, Uuid::uuid4(), $systemUserId, DATETIME_NOW,
$orgId, Uuid::uuid4(), $systemUserId, DATETIME_NOW,
$orgId, Uuid::uuid4(), $systemUserId, DATETIME_NOW,
$orgId, Uuid::uuid4(), $systemUserId, DATETIME_NOW,
$orgId, Uuid::uuid4(), $systemUserId, DATETIME_NOW,
$orgId, Uuid::uuid4(), $systemUserId, DATETIME_NOW,
$orgId, Uuid::uuid4(), $systemUserId, DATETIME_NOW,
$orgId, Uuid::uuid4(), $systemUserId, DATETIME_NOW,
);
$this->db->queryPrepared($sql, $queryParams);
// if the second organization is added than also create global categories
if ($this->countAllRecords() === 2) {
$categoryAnnouncement = new Category($this->db);
$categoryAnnouncement->setValue('cat_type', 'ANN');
$categoryAnnouncement->setValue('cat_name_intern', 'ANN_ALL_ORGANIZATIONS');
$categoryAnnouncement->setValue('cat_name', 'SYS_ALL_ORGANIZATIONS');
$categoryAnnouncement->save();
$categoryEvents = new Category($this->db);
$categoryEvents->setValue('cat_type', 'EVT');
$categoryEvents->setValue('cat_name_intern', 'EVT_ALL_ORGANIZATIONS');
$categoryEvents->setValue('cat_name', 'SYS_ALL_ORGANIZATIONS');
$categoryEvents->save();
$categoryWeblinks = new Category($this->db);
$categoryWeblinks->setValue('cat_type', 'LNK');
$categoryWeblinks->setValue('cat_name_intern', 'LNK_ALL_ORGANIZATIONS');
$categoryWeblinks->setValue('cat_name', 'SYS_ALL_ORGANIZATIONS');
$categoryWeblinks->save();
}
// insert root folder name for documents & files module
$sql = 'INSERT INTO ' . TBL_FOLDERS . '
(fol_org_id, fol_uuid, fol_type, fol_name, fol_path, fol_locked, fol_public, fol_usr_id, fol_timestamp)
VALUES (?, ?, \'DOCUMENTS\', ?, ?, false, true, ?, ?)';
$queryParams = array($orgId, Uuid::uuid4(), Folder::getRootFolderName('documents', $this->getValue('org_shortname')), FOLDER_DATA, $systemUserId, DATETIME_NOW);
$this->db->queryPrepared($sql, $queryParams);
// insert inventory fields
$sql = 'INSERT INTO ' . TBL_INVENTORY_FIELDS . '
(inf_uuid, inf_org_id, inf_type, inf_name_intern, inf_name, inf_description, inf_system, inf_required_input, inf_sequence, inf_usr_id_create, inf_timestamp_create, inf_usr_id_change, inf_timestamp_change)
VALUES (?, ?, \'TEXT\', \'ITEMNAME\', \'SYS_INVENTORY_ITEMNAME\', \'SYS_INVENTORY_ITEMNAME_DESC\', 1, 1, 0, ?, ?, NULL, NULL),
(?, ?, \'CATEGORY\', \'CATEGORY\', \'SYS_CATEGORY\', \'SYS_INVENTORY_CATEGORY_DESC\', 1, 1, 1, ?, ?, NULL, NULL),
(?, ?, \'TEXT\', \'KEEPER\', \'SYS_INVENTORY_KEEPER\', \'SYS_INVENTORY_KEEPER_DESC\', 1, 0, 2, ?, ?, NULL, NULL),
(?, ?, \'CHECKBOX\', \'IN_INVENTORY\', \'SYS_INVENTORY_IN_INVENTORY\', \'SYS_INVENTORY_IN_INVENTORY_DESC\', 1, 0, 3, ?, ?, NULL, NULL),
(?, ?, \'TEXT\', \'LAST_RECEIVER\', \'SYS_INVENTORY_LAST_RECEIVER\', \'SYS_INVENTORY_LAST_RECEIVER_DESC\', 1, 0, 4, ?, ?, NULL, NULL),
(?, ?, \'DATE\', \'RECEIVED_ON\', \'SYS_INVENTORY_RECEIVED_ON\', \'SYS_INVENTORY_RECEIVED_ON_DESC\', 1, 0, 5, ?, ?, NULL, NULL),
(?, ?, \'DATE\', \'RECEIVED_BACK_ON\', \'SYS_INVENTORY_RECEIVED_BACK_ON\', \'SYS_INVENTORY_RECEIVED_BACK_ON_DESC\', 1, 0, 6, ?, ?, NULL, NULL);
';
$queryParams = array(
Uuid::uuid4(), $orgId, $systemUserId, DATETIME_NOW,
Uuid::uuid4(), $orgId, $systemUserId, DATETIME_NOW,
Uuid::uuid4(), $orgId, $systemUserId, DATETIME_NOW,
Uuid::uuid4(), $orgId, $systemUserId, DATETIME_NOW,
Uuid::uuid4(), $orgId, $systemUserId, DATETIME_NOW,
Uuid::uuid4(), $orgId, $systemUserId, DATETIME_NOW,
Uuid::uuid4(), $orgId, $systemUserId, DATETIME_NOW
);
$this->db->queryPrepared($sql, $queryParams);
// now create default roles
// Create role administrator
$roleAdministrator = new Role($this->db);
$roleAdministrator->saveChangesWithoutRights();
$roleAdministrator->setValue('rol_cat_id', $categoryCommon, false);
$roleAdministrator->setValue('rol_name', $gL10n->get('SYS_ADMINISTRATOR'));
$roleAdministrator->setValue('rol_description', $gL10n->get('INS_DESCRIPTION_ADMINISTRATOR'));
$roleAdministrator->setValue('rol_assign_roles', 1);
$roleAdministrator->setValue('rol_approve_users', 1);
$roleAdministrator->setValue('rol_announcements', 1);
$roleAdministrator->setValue('rol_events', 1);
$roleAdministrator->setValue('rol_documents_files', 1);
$roleAdministrator->setValue('rol_forum_admin', 1);
$roleAdministrator->setValue('rol_photo', 1);
$roleAdministrator->setValue('rol_weblinks', 1);
$roleAdministrator->setValue('rol_inventory_admin', 1);
$roleAdministrator->setValue('rol_edit_user', 1);
$roleAdministrator->setValue('rol_mail_to_all', 1);
$roleAdministrator->setValue('rol_mail_this_role', 3);
$roleAdministrator->setValue('rol_profile', 1);
$roleAdministrator->setValue('rol_all_lists_view', 1);
$roleAdministrator->setValue('rol_administrator', 1);
$roleAdministrator->setValue('rol_view_memberships', Role::VIEW_LOGIN_USERS);
$roleAdministrator->save();
// Create role member
$roleMember = new Role($this->db);
$roleMember->saveChangesWithoutRights();
$roleMember->setValue('rol_cat_id', $categoryCommon, false);
$roleMember->setValue('rol_name', $gL10n->get('SYS_MEMBER'));
$roleMember->setValue('rol_description', $gL10n->get('INS_DESCRIPTION_MEMBER'));
$roleMember->setValue('rol_mail_this_role', 2);
$roleMember->setValue('rol_profile', 1);
$roleMember->setValue('rol_default_registration', 1);
$roleMember->setValue('rol_view_memberships', Role::VIEW_NOBODY);
$roleMember->save();
// Create role board
$roleManagement = new Role($this->db);
$roleManagement->saveChangesWithoutRights();
$roleManagement->setValue('rol_cat_id', $categoryCommon, false);
$roleManagement->setValue('rol_name', $gL10n->get('INS_BOARD'));
$roleManagement->setValue('rol_description', $gL10n->get('INS_DESCRIPTION_BOARD'));
$roleManagement->setValue('rol_announcements', 1);
$roleManagement->setValue('rol_events', 1);
$roleManagement->setValue('rol_weblinks', 1);
$roleManagement->setValue('rol_edit_user', 1);
$roleManagement->setValue('rol_mail_to_all', 1);
$roleManagement->setValue('rol_mail_this_role', 2);
$roleManagement->setValue('rol_profile', 1);
$roleManagement->setValue('rol_all_lists_view', 1);
$roleManagement->setValue('rol_view_memberships', Role::VIEW_LOGIN_USERS);
$roleManagement->save();
// set edit role rights to forum categories for role member
$sql = 'SELECT cat_id
FROM ' . TBL_CATEGORIES . '
WHERE cat_type = \'FOT\'
AND cat_org_id = ? -- $orgId';
$pdoStatement = $this->db->queryPrepared($sql, array($orgId));
$row = $pdoStatement->fetch();
$rightCategoryView = new RolesRights($this->db, 'category_edit', (int)$row['cat_id']);
$rightCategoryView->saveRoles(array($roleMember->getValue('rol_id')));
// Create membership for user in role 'Administrator' and 'Members'
$membershipAdministrator = new Membership($this->db);
$membershipAdministrator->startMembership($roleAdministrator->getValue('rol_id'), $userId, false);
$membershipMember = new Membership($this->db);
$membershipMember->startMembership($roleMember->getValue('rol_id'), $userId, false);
// create object with current user field structure
$gProfileFields = new ProfileFields($this->db, $orgId);
// create default list configurations
$addressList = new ListConfiguration($this->db);
$addressList->setValue('lst_name', $gL10n->get('INS_ADDRESS_LIST'));
$addressList->setValue('lst_org_id', $orgId);
$addressList->setValue('lst_global', 1);
$addressList->addColumn((int)$gProfileFields->getProperty('LAST_NAME', 'usf_id'), 0, 'ASC');
$addressList->addColumn((int)$gProfileFields->getProperty('FIRST_NAME', 'usf_id'), 0, 'ASC');
$addressList->addColumn((int)$gProfileFields->getProperty('BIRTHDAY', 'usf_id'));
$addressList->addColumn((int)$gProfileFields->getProperty('STREET', 'usf_id'));
$addressList->addColumn((int)$gProfileFields->getProperty('POSTCODE', 'usf_id'));
$addressList->addColumn((int)$gProfileFields->getProperty('CITY', 'usf_id'));
$addressList->save();
$phoneList = new ListConfiguration($this->db);
$phoneList->setValue('lst_name', $gL10n->get('INS_PHONE_LIST'));
$phoneList->setValue('lst_org_id', $orgId);
$phoneList->setValue('lst_global', 1);
$phoneList->addColumn((int)$gProfileFields->getProperty('LAST_NAME', 'usf_id'), 0, 'ASC');
$phoneList->addColumn((int)$gProfileFields->getProperty('FIRST_NAME', 'usf_id'), 0, 'ASC');
$phoneList->addColumn((int)$gProfileFields->getProperty('PHONE', 'usf_id'));
$phoneList->addColumn((int)$gProfileFields->getProperty('MOBILE', 'usf_id'));
$phoneList->addColumn((int)$gProfileFields->getProperty('EMAIL', 'usf_id'));
$phoneList->save();
$contactDetailsList = new ListConfiguration($this->db);
$contactDetailsList->setValue('lst_name', $gL10n->get('SYS_CONTACT_DETAILS'));
$contactDetailsList->setValue('lst_org_id', $orgId);
$contactDetailsList->setValue('lst_global', 1);
$contactDetailsList->addColumn((int)$gProfileFields->getProperty('LAST_NAME', 'usf_id'), 0, 'ASC');
$contactDetailsList->addColumn((int)$gProfileFields->getProperty('FIRST_NAME', 'usf_id'), 0, 'ASC');
$contactDetailsList->addColumn((int)$gProfileFields->getProperty('BIRTHDAY', 'usf_id'));
$contactDetailsList->addColumn((int)$gProfileFields->getProperty('STREET', 'usf_id'));
$contactDetailsList->addColumn((int)$gProfileFields->getProperty('POSTCODE', 'usf_id'));
$contactDetailsList->addColumn((int)$gProfileFields->getProperty('CITY', 'usf_id'));
$contactDetailsList->addColumn((int)$gProfileFields->getProperty('PHONE', 'usf_id'));
$contactDetailsList->addColumn((int)$gProfileFields->getProperty('MOBILE', 'usf_id'));
$contactDetailsList->addColumn((int)$gProfileFields->getProperty('EMAIL', 'usf_id'));
$contactDetailsList->save();
$formerList = new ListConfiguration($this->db);
$formerList->setValue('lst_name', $gL10n->get('INS_MEMBERSHIP'));
$formerList->setValue('lst_org_id', $orgId);
$formerList->setValue('lst_global', 1);
$formerList->addColumn((int)$gProfileFields->getProperty('LAST_NAME', 'usf_id'), 0, 'ASC');
$formerList->addColumn((int)$gProfileFields->getProperty('FIRST_NAME', 'usf_id'), 0, 'ASC');
$formerList->addColumn((int)$gProfileFields->getProperty('BIRTHDAY', 'usf_id'));
$formerList->addColumn('mem_begin');
$formerList->addColumn('mem_end');
$formerList->save();
$participantList = new ListConfiguration($this->db);
$participantList->setValue('lst_name', $gL10n->get('SYS_PARTICIPANTS'));
$participantList->setValue('lst_org_id', $orgId);
$participantList->setValue('lst_global', 1);
$participantList->addColumn((int)$gProfileFields->getProperty('LAST_NAME', 'usf_id'), 0, 'ASC');
$participantList->addColumn((int)$gProfileFields->getProperty('FIRST_NAME', 'usf_id'), 0, 'ASC');
$participantList->addColumn('mem_approved');
$participantList->addColumn('mem_comment');
$participantList->addColumn('mem_count_guests');
$participantList->save();
$contactsList = new ListConfiguration($this->db);
$contactsList->setValue('lst_name', $gL10n->get('SYS_CONTACTS'));
$contactsList->setValue('lst_org_id', $orgId);
$contactsList->setValue('lst_global', 1);
$contactsList->addColumn((int)$gProfileFields->getProperty('LAST_NAME', 'usf_id'), 0, 'ASC');
$contactsList->addColumn((int)$gProfileFields->getProperty('FIRST_NAME', 'usf_id'), 0, 'ASC');
$contactsList->addColumn('usr_login_name');
$contactsList->addColumn((int)$gProfileFields->getProperty('GENDER', 'usf_id'));
$contactsList->addColumn((int)$gProfileFields->getProperty('BIRTHDAY', 'usf_id'));
$contactsList->addColumn((int)$gProfileFields->getProperty('CITY', 'usf_id'));
$contactsList->addColumn('usr_timestamp_change');
$contactsList->save();
// create default category report configuration
$categoryReportColumns = 'p' . $gProfileFields->getProperty('FIRST_NAME', 'usf_id') . ',' .
'p' . $gProfileFields->getProperty('LAST_NAME', 'usf_id') . ',' .
'p' . $gProfileFields->getProperty('STREET', 'usf_id') . ',' .
'p' . $gProfileFields->getProperty('CITY', 'usf_id') . ',' .
'r' . $roleAdministrator->getValue('rol_id') . ',' .
'r' . $roleManagement->getValue('rol_id') . ',' .
'r' . $roleMember->getValue('rol_id');
$categoryReport = new Entity($this->db, TBL_CATEGORY_REPORT, 'crt');
$categoryReport->setValue('crt_org_id', $orgId);
$categoryReport->setValue('crt_name', $gL10n->get('SYS_GENERAL_ROLE_ASSIGNMENT'));
$categoryReport->setValue('crt_col_fields', $categoryReportColumns);
$categoryReport->setValue('crt_number_col', 0);
$categoryReport->save();
// set new default configuration to the module settings
$organizationSettings = new SettingsManager($this->db, $orgId);
$organizationSettings->getAll();
$organizationSettings->set('system_notifications_role', $roleAdministrator->getValue('rol_uuid'));
$organizationSettings->set('groups_roles_default_configuration', $addressList->getValue('lst_id'));
$organizationSettings->set('events_list_configuration', $participantList->getValue('lst_id'));
$organizationSettings->set('contacts_list_configuration', $contactsList->getValue('lst_id'));
$organizationSettings->set('category_report_default_configuration', $categoryReport->getValue('crt_id'));
}
/**
* Create an organization object depending on an optional organization shortname string. If an
* organization shortname is set than this organization will be read otherwise the organization
* with the minimum ID will be read.
* @param Database $db Object of the class Database. This should be the default global object **$gDb**.
* @param string $organization The organization shortname. If this is set than this organization
* will be read otherwise the organization with the minimum ID.
* @return Organization Returns an organization object.
* @throws Exception
*/
public static function createDefaultOrganizationObject(Database $db, string $organization = ''): Organization
{
if ($organization !== '') {
$organizationObject = new Organization($db, $organization);
} else {
$sql = 'SELECT MIN(org_id) as organization_id FROM ' . TBL_ORGANIZATIONS;
$pdoStatement = $db->queryPrepared($sql, array(), false);
$row = $pdoStatement->fetch();
$organizationObject = new Organization($db, (int)$row['organization_id']);
}
return $organizationObject;
}
/**
* Deletes the selected record of the table and all references in other tables.
* After that the class will be initialized.
* @return bool **true** if no error occurred
* @throws Exception
*/
public function delete(): bool
{
$this->db->startTransaction();
// delete all category reports
$sql = 'DELETE FROM ' . TBL_CATEGORY_REPORT . '
WHERE crt_org_id = ? -- $this->getValue(\'org_id\')
';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all announcements
$sql = 'DELETE FROM ' . TBL_ANNOUNCEMENTS . '
WHERE ann_cat_id IN (
SELECT cat.cat_id
FROM (SELECT cat_id
FROM ' . TBL_CATEGORIES . '
WHERE cat_org_id = ? -- $this->getValue(\'org_id\')
) cat
)';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all events
$sql = 'SELECT evt.*
FROM ' . TBL_EVENTS . ' evt
INNER JOIN ' . TBL_CATEGORIES . ' ON cat_id = dat_cat_id
WHERE cat_org_id = ? -- $this->getValue(\'org_id\') ';
$eventsStatement = $this->db->queryPrepared($sql, array($this->getValue('org_id')));
while ($eventRow = $eventsStatement->fetch()) {
$event = new Event($this->db);
$event->setArray($eventRow);
$event->delete();
}
// delete all forum posts
$sql = 'DELETE FROM ' . TBL_FORUM_POSTS . '
WHERE fop_fot_id IN (
SELECT fot.fot_id
FROM (SELECT fot_id
FROM ' . TBL_FORUM_TOPICS . '
INNER JOIN ' . TBL_CATEGORIES . ' ON cat_id = fot_cat_id
WHERE cat_org_id = ? -- $this->getValue(\'org_id\')
) fot
)';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all forum topics
$sql = 'DELETE FROM ' . TBL_FORUM_TOPICS . '
WHERE fot_cat_id IN (
SELECT cat.cat_id
FROM (SELECT cat_id
FROM ' . TBL_CATEGORIES . '
WHERE cat_org_id = ? -- $this->getValue(\'org_id\')
) cat
)';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all photos
$sql = 'SELECT pho.*
FROM ' . TBL_PHOTOS . ' pho
WHERE pho_pho_id_parent IS NULL
AND pho_org_id = ? -- $this->getValue(\'org_id\') ';
$albumStatement = $this->db->queryPrepared($sql, array($this->getValue('org_id')));
while ($photoAlbumRow = $albumStatement->fetch()) {
$photoAlbum = new Album($this->db);
$photoAlbum->setArray($photoAlbumRow);
$photoAlbum->delete();
}
// delete all weblinks
$sql = 'DELETE FROM ' . TBL_LINKS . '
WHERE lnk_cat_id IN (
SELECT cat.cat_id
FROM (SELECT cat_id
FROM ' . TBL_CATEGORIES . '
WHERE cat_org_id = ? -- $this->getValue(\'org_id\')
) cat
)';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all roles rights of categories
$sql = 'DELETE FROM ' . TBL_ROLES_RIGHTS_DATA . '
WHERE rrd_ror_id IN (
SELECT ror.ror_id
FROM (SELECT ror_id
FROM ' . TBL_ROLES_RIGHTS . '
INNER JOIN ' . TBL_CATEGORIES . ' ON cat_id = ror_ror_id_parent
WHERE cat_org_id = ? -- $this->getValue(\'org_id\')
AND ror_name_intern = \'category_view\'
) ror
)';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all lists
$sql = 'DELETE FROM ' . TBL_LIST_COLUMNS . '
WHERE lsc_lst_id IN (
SELECT lst.lst_id
FROM (SELECT lst_id
FROM ' . TBL_LISTS . '
WHERE lst_org_id = ? -- $this->getValue(\'org_id\')
) lst
)';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
$sql = 'DELETE FROM ' . TBL_LISTS . '
WHERE lst_org_id = ? -- $this->getValue(\'org_id\')
';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all auto logins
$sql = 'DELETE FROM ' . TBL_AUTO_LOGIN . '
WHERE atl_org_id = ? -- $this->getValue(\'org_id\') ';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all preferences
$sql = 'DELETE FROM ' . TBL_PREFERENCES . '
WHERE prf_org_id = ? -- $this->getValue(\'org_id\') ';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all registrations
$sql = 'DELETE FROM ' . TBL_REGISTRATIONS . '
WHERE reg_org_id = ? -- $this->getValue(\'org_id\') ';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all sessions
$sql = 'DELETE FROM ' . TBL_SESSIONS . '
WHERE ses_org_id = ? -- $this->getValue(\'org_id\') ';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all texts
$sql = 'DELETE FROM ' . TBL_TEXTS . '
WHERE txt_org_id = ? -- $this->getValue(\'org_id\') ';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all folders
$sqlAdminRoles = 'SELECT fol_uuid
FROM ' . TBL_FOLDERS . '
WHERE fol_type = \'DOCUMENTS\'
AND fol_name = \'documents_' . $this->getValue('org_shortname') . '\' ';
$statementFolder = $this->db->queryPrepared($sqlAdminRoles);
$folder_uuid = $statementFolder->fetchColumn();
$documentsFilesRootFolder = new Folder($this->db);
$documentsFilesRootFolder->getFolderForDownload($folder_uuid);
$documentsFilesRootFolder->delete();
// delete all memberships
$sql = 'DELETE FROM ' . TBL_MEMBERS . '
WHERE mem_rol_id IN (
SELECT rol.rol_id
FROM (SELECT rol_id
FROM ' . TBL_ROLES . '
INNER JOIN ' . TBL_CATEGORIES . ' ON cat_id = rol_cat_id
WHERE cat_org_id = ? -- $this->getValue(\'org_id\')
) rol
)';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all roles rights of roles
$sql = 'DELETE FROM ' . TBL_ROLES_RIGHTS_DATA . '
WHERE rrd_rol_id IN (
SELECT rol.rol_id
FROM (SELECT rol_id
FROM ' . TBL_ROLES . '
INNER JOIN ' . TBL_CATEGORIES . ' ON cat_id = rol_cat_id
WHERE cat_org_id = ? -- $this->getValue(\'org_id\')
) rol
)';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all role dependencies
$sql = 'DELETE FROM ' . TBL_ROLE_DEPENDENCIES . '
WHERE rld_rol_id_parent IN (
SELECT rol.rol_id
FROM (SELECT rol_id
FROM ' . TBL_ROLES . '
INNER JOIN ' . TBL_CATEGORIES . ' ON cat_id = rol_cat_id
WHERE cat_org_id = ? -- $this->getValue(\'org_id\')
) rol
)';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
$sql = 'DELETE FROM ' . TBL_ROLE_DEPENDENCIES . '
WHERE rld_rol_id_child IN (
SELECT rol.rol_id
FROM (SELECT rol_id
FROM ' . TBL_ROLES . '
INNER JOIN ' . TBL_CATEGORIES . ' ON cat_id = rol_cat_id
WHERE cat_org_id = ? -- $this->getValue(\'org_id\')
) rol
)';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all roles
$sql = 'DELETE FROM ' . TBL_ROLES . '
WHERE rol_cat_id IN (
SELECT cat.cat_id
FROM (SELECT cat_id
FROM ' . TBL_CATEGORIES . '
WHERE cat_org_id = ? -- $this->getValue(\'org_id\')
) cat
)';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all categories
$sql = 'DELETE FROM ' . TBL_CATEGORIES . '
WHERE cat_org_id = ? -- $this->getValue(\'org_id\')
';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all inventory item data
$sql = 'DELETE FROM ' . TBL_INVENTORY_ITEM_DATA . '
WHERE ind_ini_id IN (
SELECT ivt.ini_id
FROM (SELECT ini_id
FROM ' . TBL_INVENTORY_ITEMS . '
WHERE ini_org_id = ? -- $this->getValue(\'org_id\')
) ivt
)';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all inventory item lend data
$sql = 'DELETE FROM ' . TBL_INVENTORY_ITEM_LEND_DATA . '
WHERE inl_ini_id IN (
SELECT ini.ini_id
FROM (SELECT ini_id
FROM ' . TBL_INVENTORY_ITEMS . '
WHERE ini_org_id = ? -- $this->getValue(\'org_id\')
) ini
)';
// delete all inventory items
$sql = 'DELETE FROM ' . TBL_INVENTORY_ITEMS . '
WHERE ini_org_id = ? -- $this->getValue(\'org_id\')
';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// delete all inventory field options
$sql = 'DELETE FROM ' . TBL_INVENTORY_FIELD_OPTIONS . '
WHERE ifo_inf_id IN (
SELECT inf.inf_id
FROM (SELECT inf_id
FROM ' . TBL_INVENTORY_FIELDS . '
WHERE inf_org_id = ? -- $this->getValue(\'org_id\')
) inf
)';
// delete all inventory fields
$sql = 'DELETE FROM ' . TBL_INVENTORY_FIELDS . '
WHERE inf_org_id = ? -- $this->getValue(\'org_id\')
';
$this->db->queryPrepared($sql, array($this->getValue('org_id')));
// now delete the organization
parent::delete();
return $this->db->endTransaction();
}
/**
* @return array<int,string> Returns an array with all child organizations
* @throws Exception
*/
protected function getChildOrganizations(): array
{
if (!$this->bCheckChildOrganizations) {
// Daten erst einmal aus DB einlesen
$this->childOrganizations = $this->getOrganizationsInRelationship(true, false);
$this->bCheckChildOrganizations = true;
}
return $this->childOrganizations;
}
/**
* Create a comma separated list with all organization ids of children,
* parent and this organization that is prepared for use in SQL
* @param bool $shortname If set to true then a list of all shortnames will be returned
* @return string Returns a string with a comma separated list of all organization
* ids that are parents or children and the own id
* @throws Exception
*/
public function getFamilySQL(bool $shortname = false): string
{
$organizations = $this->getOrganizationsInRelationship();
if ($shortname) {
/**
* @param string $value
* @return string
*/
function addQuotationMarks(string $value): string
{
return '\'' . $value . '\'';
}
$organizationShortnames = array_values($organizations);
$organizationShortnames[] = $this->getValue('org_shortname');
$organizationShortnames = array_map('addQuotationMarks', $organizationShortnames);
return implode(',', $organizationShortnames);
}
$organizationIds = array_keys($organizations);
$organizationIds[] = (int)$this->getValue('org_id');
return implode(',', $organizationIds);
}
/**
* Read all child and parent organizations of this organization and returns an array with them.
* @param bool $child If set to **true** (default) then all child organizations will be in the array
* @param bool $parent If set to **true** (default) then the parent organization will be in the array
* @param bool $longname If set to **true** then the value of the array will be the **org_longname**
* otherwise it will be **org_shortname**
* @return array<int,string> Returns an array with all child and parent organizations e.g. array('org_id' => 'org_shortname')
* @throws Exception
*/
public function getOrganizationsInRelationship(bool $child = true, bool $parent = true, bool $longname = false): array
{
$sqlWhere = array();
$queryParams = array();
if ($child) {
$sqlWhere[] = 'org_org_id_parent = ?';
$queryParams[] = (int)$this->getValue('org_id');
}
$orgParentId = (int)$this->getValue('org_org_id_parent');
if ($parent && $orgParentId > 0) {
$sqlWhere[] = 'org_id = ?';
$queryParams[] = $orgParentId;
}
$sql = 'SELECT org_id, org_longname, org_shortname
FROM ' . TBL_ORGANIZATIONS . '
WHERE ' . implode(' OR ', $sqlWhere);
$pdoStatement = $this->db->queryPrepared($sql, $queryParams);
$childOrganizations = array();
while ($row = $pdoStatement->fetch()) {
$orgId = (int)$row['org_id'];
if ($longname) {
$childOrganizations[$orgId] = $row['org_longname'];
} else {
$childOrganizations[$orgId] = $row['org_shortname'];
}
}
return $childOrganizations;
}
/**
* @return SettingsManager
* @throws Exception
*/
public function &getSettingsManager(): SettingsManager
{
if (!isset($this->settingsManager)) {
$this->settingsManager = new SettingsManager($this->db, (int)$this->getValue('org_id'));
$this->settingsManager->resetAll();
}
return $this->settingsManager;
}
/**
* Method checks if the organization is configured as a child organization in the recordset.
* @return bool Return **true** if the organization is a child of another organization
* @throws Exception
*/
public function isChildOrganization(): bool
{
return $this->getValue('org_org_id_parent') > 0;
}
/**
* Method checks if the organization is configured as a parent organization in the recordset.
* @return bool Return **true** if the organization is the parent of at least one other organization
* @throws Exception
*/
public function isParentOrganization(): bool
{
return count($this->getChildOrganizations()) > 0;
}
/**
* Set a new value for a column of the database table.
* The value is only saved in the object. You must call the method **save** to store the new value to the database
* @param string $columnName The name of the database column whose value should get a new value
* @param mixed $newValue The new value that should be stored in the database field
* @param bool $checkValue The value will be checked if it's valid. If set to **false** than the value will not be checked.
* @return bool Returns **true** if the value is stored in the current object and **false** if a check failed
* @throws Exception
*/
public function setValue(string $columnName, $newValue, bool $checkValue = true): bool
{
if ($checkValue) {
// org_shortname shouldn't be edited
if ($columnName === 'org_shortname' && !$this->newRecord) {
return false;
} elseif ($columnName === 'org_homepage' && $newValue !== '') {
$newValue = admFuncCheckUrl($newValue);
if ($newValue === false) {
return false;
}
}
}
return parent::setValue($columnName, $newValue, $checkValue);
}
/**
* @return array<string,mixed>
*/
public function getDbColumns(): array
{
return $this->dbColumns;
}
/**
* Adjust the changelog entry for this db record: Add the parent fold as a related object
*
* @param LogChanges $logEntry The log entry to adjust
*
* @return void
*/
protected function adjustLogEntry(LogChanges $logEntry) {
$orgParentId = (int) $this->getValue('org_org_id_parent');
if ($orgParentId > 0) {
$sql = 'SELECT org_id, org_longname, org_shortname
FROM '.TBL_ORGANIZATIONS.'
WHERE org_id = ?';
$pdoStatement = $this->db->queryPrepared($sql, [$orgParentId]);
while ($row = $pdoStatement->fetch()) {
$logEntry->setLogRelated($row['org_id'], $row['org_longname']);
}
}
}
/**
* Return a human-readable representation of this record.
* For organizations, simply use the longname
*
* @return string The readable representation of the record (can also be a translatable identifier)
*/
public function readableName(): string
{
if (array_key_exists($this->columnPrefix.'_longname', $this->dbColumns)) {
return $this->dbColumns[$this->columnPrefix.'_longname'];
} else {
return $this->dbColumns[$this->keyColumnName];
}
}
}