Commit | Line | Data |
---|---|---|
44212747 TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
41498ac5 | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
44212747 | 6 | | | |
41498ac5 TO |
7 | | This work is published under the GNU AGPLv3 license with some | |
8 | | permitted exceptions and without any warranty. For full license | | |
9 | | and copyright information, see https://civicrm.org/licensing | | |
44212747 TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | namespace Civi\Core\SqlTrigger; | |
14 | ||
15 | /** | |
16 | * Build a set of simple, literal SQL triggers. | |
17 | * | |
18 | * @package CRM | |
ca5cec67 | 19 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
44212747 TO |
20 | */ |
21 | class StaticTriggers { | |
22 | ||
23 | /** | |
24 | * @var array | |
25 | * A list of triggers, in the same format as hook_civicrm_triggerInfo. | |
26 | * Additionally, you may specify `upgrade_check` to ensure that the trigger | |
27 | * is *not* installed during early upgrade steps (before key dependencies are met). | |
28 | * | |
29 | * Ex: $triggers[0]['upgrade_check'] = array('table' => 'civicrm_case', 'column'=> 'modified_date'); | |
30 | * | |
31 | * @see \CRM_Utils_Hook::triggerInfo | |
32 | */ | |
33 | private $triggers; | |
34 | ||
35 | /** | |
36 | * StaticTriggers constructor. | |
37 | * @param $triggers | |
38 | */ | |
39 | public function __construct($triggers) { | |
40 | $this->triggers = $triggers; | |
41 | } | |
42 | ||
44212747 TO |
43 | /** |
44 | * Add our list of triggers to the global list. | |
45 | * | |
46 | * @param \Civi\Core\Event\GenericHookEvent $e | |
47 | * @see \CRM_Utils_Hook::triggerInfo | |
48 | */ | |
49 | public function onTriggerInfo($e) { | |
50 | $this->alterTriggerInfo($e->info, $e->tableName); | |
51 | } | |
52 | ||
53 | /** | |
54 | * Add our list of triggers to the global list. | |
55 | * | |
56 | * @see \CRM_Utils_Hook::triggerInfo | |
57 | * @see \CRM_Core_DAO::triggerRebuild | |
58 | * | |
59 | * @param array $info | |
60 | * See hook_civicrm_triggerInfo. | |
4dbdebf0 | 61 | * @param string|null $tableFilter |
44212747 TO |
62 | * See hook_civicrm_triggerInfo. |
63 | */ | |
64 | public function alterTriggerInfo(&$info, $tableFilter = NULL) { | |
65 | foreach ($this->getTriggers() as $trigger) { | |
66 | if ($tableFilter !== NULL) { | |
67 | // Because sadism. | |
68 | if (in_array($tableFilter, (array) $trigger['table'])) { | |
69 | $trigger['table'] = $tableFilter; | |
70 | } | |
71 | } | |
72 | ||
73 | if (\CRM_Core_Config::isUpgradeMode() && isset($trigger['upgrade_check'])) { | |
74 | $uc = $trigger['upgrade_check']; | |
eed7e803 | 75 | if (!\CRM_Core_BAO_SchemaHandler::checkIfFieldExists($uc['table'], $uc['column']) |
44212747 TO |
76 | ) { |
77 | continue; | |
78 | } | |
79 | } | |
80 | unset($trigger['upgrade_check']); | |
81 | $info[] = $trigger; | |
82 | } | |
83 | } | |
84 | ||
85 | /** | |
86 | * @return mixed | |
87 | */ | |
88 | public function getTriggers() { | |
89 | return $this->triggers; | |
90 | } | |
91 | ||
92 | /** | |
93 | * @param mixed $triggers | |
94 | * @return StaticTriggers | |
95 | */ | |
96 | public function setTriggers($triggers) { | |
97 | $this->triggers = $triggers; | |
98 | return $this; | |
99 | } | |
100 | ||
101 | /** | |
102 | * @param $trigger | |
103 | * @return StaticTriggers | |
104 | */ | |
105 | public function addTrigger($trigger) { | |
106 | $this->triggers[] = $trigger; | |
107 | return $this; | |
108 | } | |
109 | ||
110 | } |