Rename RelationshipVortex to RelationshipCache
[civicrm-core.git] / CRM / Contact / BAO / RelationshipCache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class CRM_Contact_BAO_RelationshipCache.
14 */
15 class CRM_Contact_BAO_RelationshipCache extends CRM_Contact_DAO_RelationshipCache {
16
17 /**
18 * The "mappings" array defines the values to put into `civicrm_relationship_cache`
19 * using data from `civicrm_relationship rel` and `civicrm_relationship_type reltype`.
20 *
21 * @var array
22 * Array(string $intoColumn => string $selectValue)
23 */
24 private static $mappings = [
25 'a_b' => [
26 'relationship_id' => 'rel.id',
27 'relationship_type_id' => 'rel.relationship_type_id',
28 'orientation' => '"a_b"',
29 'near_contact_id' => 'rel.contact_id_a',
30 'near_relation' => 'reltype.name_a_b',
31 'far_contact_id' => 'rel.contact_id_b',
32 'far_relation' => 'reltype.name_b_a',
33 'start_date' => 'rel.start_date',
34 'end_date' => 'rel.end_date',
35 'is_active' => 'rel.is_active',
36 'case_id' => 'rel.case_id',
37 ],
38 'b_a' => [
39 'relationship_id' => 'rel.id',
40 'relationship_type_id' => 'rel.relationship_type_id',
41 'orientation' => '"b_a"',
42 'near_contact_id' => 'rel.contact_id_b',
43 'near_relation' => 'reltype.name_b_a',
44 'far_contact_id' => 'rel.contact_id_a',
45 'far_relation' => 'reltype.name_a_b',
46 'start_date' => 'rel.start_date',
47 'end_date' => 'rel.end_date',
48 'is_active' => 'rel.is_active',
49 'case_id' => 'rel.case_id',
50 ],
51 ];
52
53 /**
54 * A list of fields which uniquely identify a row.
55 *
56 * @var array
57 */
58 private static $keyFields = ['relationship_id', 'orientation'];
59
60 /**
61 * A list of of fields in `civicrm_relationship_type` which (if changed)
62 * will necessitate an update to the cache.
63 *
64 * @var array
65 */
66 private static $relTypeWatchFields = ['name_a_b', 'name_b_a'];
67
68 /**
69 * Add our list of triggers to the global list.
70 *
71 * @param \Civi\Core\Event\GenericHookEvent $e
72 * @see \CRM_Utils_Hook::triggerInfo
73 */
74 public static function onHookTriggerInfo($e) {
75 $relUpdates = self::createInsertUpdateQueries();
76 foreach ($relUpdates as $relUpdate) {
77 /**
78 * This trigger runs whenever a "civicrm_relationship" record is inserted or updated.
79 *
80 * Goal: Ensure that every relationship record has two corresponding entries in the
81 * cache, the forward relationship (A=>B) and reverse relationship (B=>A).
82 */
83 $triggers[] = [
84 'table' => 'civicrm_relationship',
85 'when' => 'AFTER',
86 'event' => ['INSERT', 'UPDATE'],
87 'sql' => $relUpdate->copy()->where('rel.id = NEW.id')->toSQL() . ";\n",
88 ];
89
90 $triggers[] = [
91 /**
92 * This trigger runs whenever a "civicrm_relationship_type" record is updated.
93 *
94 * Goal: Ensure that the denormalized fields ("name_b_a"/"name_a_b" <=> "relation") remain current.
95 */
96 'table' => 'civicrm_relationship_type',
97 'when' => 'AFTER',
98 'event' => ['UPDATE'],
99 'sql' => sprintf("\nIF (%s) THEN\n %s;\n END IF;\n",
100
101 // Condition
102 implode(' OR ', array_map(function ($col) {
103 return "(OLD.$col != NEW.$col COLLATE utf8_bin)";
104 }, self::$relTypeWatchFields)),
105
106 // Action
107 $relUpdate->copy()->where('rel.relationship_type_id = NEW.id')->toSQL()
108 ),
109 ];
110 }
111
112 // Note: We do not need a DELETE trigger to maintain `civicrm_relationship_cache` because it uses `<onDelete>CASCADE</onDelete>`.
113
114 $st = new \Civi\Core\SqlTrigger\StaticTriggers($triggers);
115 $st->onTriggerInfo($e);
116 }
117
118 /**
119 * Read all records from civicrm_relationship and populate the cache.
120 * Each ordinary relationship in `civicrm_relationship` becomes two
121 * distinct records in the cache (one for A=>B relations; and one for B=>A).
122 *
123 * This method is primarily written (a) for manual testing and (b) in case
124 * a broken DBMS, screwy import, buggy code, etc causes a corruption.
125 *
126 * NOTE: This is closely related to FiveTwentyNine::populateRelationshipCache(),
127 * except that the upgrader users pagination.
128 */
129 public static function rebuild() {
130 $relUpdates = self::createInsertUpdateQueries();
131
132 CRM_Core_DAO::executeQuery('TRUNCATE civicrm_relationship_cache');
133 foreach ($relUpdates as $relUpdate) {
134 $relUpdate->execute();
135 }
136 }
137
138 /**
139 * Prepare a list of SQL queries that map data from civicrm_relationship
140 * to civicrm_relationship_cache.
141 *
142 * @return CRM_Utils_SQL_Select[]
143 * A list of SQL queries - one for each mapping.
144 */
145 public static function createInsertUpdateQueries() {
146 $queries = [];
147 foreach (self::$mappings as $name => $mapping) {
148 $queries[$name] = CRM_Utils_SQL_Select::from('civicrm_relationship rel')
149 ->join('reltype', 'INNER JOIN civicrm_relationship_type reltype ON rel.relationship_type_id = reltype.id')
150 ->syncInto('civicrm_relationship_cache', self::$keyFields, $mapping);
151 }
152 return $queries;
153 }
154
155 }