Merge pull request #10717 from JMAConsulting/CRM-20934
[civicrm-core.git] / Civi / Core / SqlTrigger / StaticTriggers.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.7 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2017 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 namespace Civi\Core\SqlTrigger;
30
31 /**
32 * Build a set of simple, literal SQL triggers.
33 *
34 * @package CRM
35 * @copyright CiviCRM LLC (c) 2004-2017
36 */
37 class StaticTriggers {
38
39 /**
40 * @var array
41 * A list of triggers, in the same format as hook_civicrm_triggerInfo.
42 * Additionally, you may specify `upgrade_check` to ensure that the trigger
43 * is *not* installed during early upgrade steps (before key dependencies are met).
44 *
45 * Ex: $triggers[0]['upgrade_check'] = array('table' => 'civicrm_case', 'column'=> 'modified_date');
46 *
47 * @see \CRM_Utils_Hook::triggerInfo
48 */
49 private $triggers;
50
51 /**
52 * StaticTriggers constructor.
53 * @param $triggers
54 */
55 public function __construct($triggers) {
56 $this->triggers = $triggers;
57 }
58
59
60 /**
61 * Add our list of triggers to the global list.
62 *
63 * @param \Civi\Core\Event\GenericHookEvent $e
64 * @see \CRM_Utils_Hook::triggerInfo
65 */
66 public function onTriggerInfo($e) {
67 $this->alterTriggerInfo($e->info, $e->tableName);
68 }
69
70 /**
71 * Add our list of triggers to the global list.
72 *
73 * @see \CRM_Utils_Hook::triggerInfo
74 * @see \CRM_Core_DAO::triggerRebuild
75 *
76 * @param array $info
77 * See hook_civicrm_triggerInfo.
78 * @param string|NULL $tableFilter
79 * See hook_civicrm_triggerInfo.
80 */
81 public function alterTriggerInfo(&$info, $tableFilter = NULL) {
82 foreach ($this->getTriggers() as $trigger) {
83 if ($tableFilter !== NULL) {
84 // Because sadism.
85 if (in_array($tableFilter, (array) $trigger['table'])) {
86 $trigger['table'] = $tableFilter;
87 }
88 }
89
90 if (\CRM_Core_Config::isUpgradeMode() && isset($trigger['upgrade_check'])) {
91 $uc = $trigger['upgrade_check'];
92 if (!\CRM_Core_DAO::checkFieldExists($uc['table'], $uc['column'])
93 ) {
94 continue;
95 }
96 }
97 unset($trigger['upgrade_check']);
98 $info[] = $trigger;
99 }
100 }
101
102 /**
103 * @return mixed
104 */
105 public function getTriggers() {
106 return $this->triggers;
107 }
108
109 /**
110 * @param mixed $triggers
111 * @return StaticTriggers
112 */
113 public function setTriggers($triggers) {
114 $this->triggers = $triggers;
115 return $this;
116 }
117
118 /**
119 * @param $trigger
120 * @return StaticTriggers
121 */
122 public function addTrigger($trigger) {
123 $this->triggers[] = $trigger;
124 return $this;
125 }
126
127 }