commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / drupal / modules / civicrm_rules / civicrm_rules.module
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 * $Id$
33 *
34 */
35
36 /*
37 * Implemenation of hook_enable()
38 */
39 function civicrm_rules_enable() {
40 db_query("UPDATE {system} SET weight = 110 WHERE name = 'civicrm_rules'");
41 }
42
43 /**
44 * Implement the post hook and fire the corresponding rules event
45 */
46 function civicrm_rules_civicrm_post($op, $objectName, $objectId, &$objectRef) {
47 if (!module_exists('rules')) {
48 return;
49 }
50
51 if (in_array($objectName,
52 array('Individual', 'Household', 'Organization')
53 )) {
54 $objectName = 'Contact';
55 }
56
57 // process only contacts for now
58 $validObjects = array_merge(
59 variable_get('civicrm_rules_post_entities', array()),
60 array('contact' => 'contact')
61 );
62
63 if (!in_array(strtolower($objectName), $validObjects, true)) {
64 return;
65 }
66
67 $eventObject = strtolower($objectName);
68 if (!in_array($eventObject, $validObjects) &&
69 !in_array($eventObject, array('contact'))
70 ) {
71 return;
72 }
73
74 if ($eventObject != 'contact') {
75 // without the civicrm prefix too much chance of another drupal module being enabled
76 $eventObject = 'civicrm_' . $eventObject;
77 }
78
79 $eventName = NULL;
80 switch ($op) {
81 case 'create':
82 case 'edit':
83 case 'delete':
84 $eventName = "{$eventObject}_{$op}";
85 break;
86
87 default:
88 break;
89 }
90
91 if ($eventName) {
92 rules_invoke_event($eventName, $objectRef);
93 }
94 }
95
96 /**
97 * Implement civicrm pageRun hook and fire the corresponding rules event
98 */
99 function civicrm_rules_civicrm_pageRun(&$page) {
100 if (!module_exists('rules')) {
101 return;
102 }
103
104 // process only contacts for now
105 $validObjects = array('CRM_Contact_Page_View_Summary');
106
107 $className = get_class($page);
108 if (!in_array($className, $validObjects)) {
109 return;
110 }
111
112 $eventName = NULL;
113 switch ($className) {
114 case 'CRM_Contact_Page_View_Summary':
115 $eventName = 'contact_view';
116 break;
117
118 default:
119 break;
120 }
121
122 if ($eventName) {
123 rules_invoke_event($eventName, $page);
124 }
125 }
126
127 /**
128 * Implement the postProcess hook and fire the corresponding rules event
129 * We'll do this for specific forms only (for now)
130 */
131 function civicrm_rules_civicrm_postProcess($formName, &$form) {
132 require_once 'CRM/Mailing/Info.php';
133 if (CRM_Mailing_Info::workflowEnabled()) {
134 $eventName = $eventParams = NULL;
135
136 switch ($formName) {
137 case 'CRM_Mailing_Form_Group':
138 if ($form->get('continue') == TRUE) {
139 $eventName = 'mailing_edit';
140 $mailingId = $form->get('mid');
141 }
142 else {
143 $query = "
144 SELECT max(id)
145 FROM civicrm_mailing
146 ";
147 $mailingId = CRM_Core_DAO::singleValueQuery($query);
148 $eventName = 'mailing_create';
149 }
150 break;
151
152 case 'CRM_Mailing_Form_Schedule':
153 if ($form->get('mid')) {
154 $mailingId = $form->get('mid');
155 }
156 else {
157 $mailingId = $form->_mailingID;
158 }
159 $eventName = 'mailing_scheduled';
160 break;
161
162 case 'CRM_Mailing_Form_Approve':
163 if ($form->_mailingID) {
164 $mailingId = $form->_mailingID;
165 }
166 else {
167 $mailingId = $form->get('mid');
168 }
169 $eventName = 'mailing_approved';
170 break;
171
172 case 'CRM_Mailing_Form_Upload':
173 $eventName = 'mailing_uploaded';
174 $mailingId = $form->_mailingID;
175 break;
176
177 case 'CRM_Mailing_Form_Test':
178 $eventName = 'mailing_inform';
179 $mailingId = $form->get('mailing_id') ? $form->get('mailing_id') : $form->get('mid');
180 break;
181
182 default:
183 break;
184 }
185
186 if ($eventName) {
187 require_once 'CRM/Mailing/DAO/Mailing.php';
188 $eventParams = new CRM_Mailing_DAO_Mailing();
189 $eventParams->id = $mailingId;
190 $eventParams->find(TRUE);
191 rules_invoke_event($eventName, $eventParams);
192 }
193 }
194 }
195 /**
196 * Implementation of hook_permission().
197 */
198 function civicrm_rules_permission() {
199 return array(
200 'access civicrm rules settings' =>
201 array('title' => t('Access CiviCRM Rules Settings'),
202 'description' => t('Access CiviCRM Rules Settings.'),
203 ),
204 );
205 }
206
207 function civicrm_rules_menu() {
208
209 $items['admin/config/civicrm/rules'] = array(
210 'title' => t('CiviCRM Rules settings'),
211 'description' => t('CiviCRM Rules Configuration'),
212 'page callback' => 'drupal_get_form',
213 'page arguments' => array('civicrm_rules_admin_settings'),
214 'access arguments' => array('access civicrm rules settings'),
215 'file' => 'civicrm_rules_admin_form.inc',
216 'type' => MENU_NORMAL_ITEM,
217 );
218 return $items;
219 }
220