commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / drupal / modules / civicrm_engage / civicrm_engage.admin.inc
1 <?php
2 #http://pastebin.com/U32jjBVu - D7 has renamed admin settings
3 function civicrm_engage_admin_settings($form_state = NULL) {
4 $form['last_donate'] = array(
5 '#type' => 'fieldset',
6 '#title' => t('CiviEngage Walklist Last Donate presentation'),
7 '#weight' => 0,
8 '#collapsible' => TRUE,
9 '#collapsed' => FALSE,
10 );
11 $form['last_donate']['civicrm_engage_no_canvas_period'] = array(
12 '#title' => t("Exclusion Period"),
13 '#type' => 'textfield',
14 '#description' => t('If you wish to leave someone alone for a period after their last donation please enter it here (in months). 0 means disabled'),
15 '#default_value' => variable_get('civicrm_engage_no_canvas_period', "0"),
16 );
17 $form['last_donate']['civicrm_engage_no_canvass_text'] = array(
18 '#title' => t("Text when within exclusion period"),
19 '#type' => 'textfield',
20 '#description' => t('Text to put in if contact not to be canvassed'),
21 '#default_value' => variable_get('civicrm_engage_no_canvass_text', "Do Not Canvass"),
22 );
23 $form['break_settings'] = array(
24 '#type' => 'fieldset',
25 '#title' => t('CiviEngage Walklist data grouping'),
26 '#weight' => 0,
27 '#collapsible' => TRUE,
28 '#collapsed' => FALSE,
29 );
30 $form['break_settings']['civicrm_engage_groupbreak_odd_even'] = array(
31 '#title' => t("Group break on odds / evens"),
32 '#type' => 'checkbox',
33 '#description' => t('Do you want a group-break between each side of the street'),
34 '#default_value' => variable_get('civicrm_engage_groupbreak_odd_even', "1"),
35 );
36 $form['break_settings']['civicrm_engage_groupbreak_street'] = array(
37 '#title' => t("Group break on street "),
38 '#type' => 'checkbox',
39 '#description' => t('Do you want a group-break between each street'),
40 '#default_value' => variable_get('civicrm_engage_groupbreak_street', "1"),
41 );
42 $form['break_settings']['civicrm_engage_groupbreak_city'] = array(
43 '#title' => t("Group break on city"),
44 '#type' => 'checkbox',
45 '#description' => t('Do you want a group-break between each city'),
46 '#default_value' => variable_get('civicrm_engage_groupbreak_city', "1"),
47 );
48 $form['break_settings']['civicrm_engage_groupbreak_zip'] = array(
49 '#title' => t("Group break on zip code"),
50 '#type' => 'checkbox',
51 '#description' => t('Do you want a group-break between each side of the street'),
52 '#default_value' => variable_get('civicrm_engage_groupbreak_zip', "1"),
53 );
54 $form['break_settings']['civicrm_engage_lines_per_group'] = array(
55 '#title' => t("Row limit per group"),
56 '#type' => 'textfield',
57 '#description' => t('Limit rows per group - ie. if you want to print out in blocks of 6'),
58 '#default_value' => variable_get('civicrm_engage_lines_per_group', "6"),
59 );
60 $form['break_settings']['civicrm_engage_group_per_page'] = array(
61 '#title' => t("New page for each group?"),
62 '#type' => 'checkbox',
63 '#description' => t('Do you want each group to have new page? (default is yes)'),
64 '#default_value' => variable_get('civicrm_engage_group_per_page', "1"),
65 );
66 return system_settings_form($form);
67 }
68
69 function civicrm_engage_admin_load_data($form_state = NULL) {
70 $subtypes_added = NULL;
71 if(civicrm_engage_subtypes_added()) {
72 $subtypes_added = ' <em>' . t("It appears that the contact sub types are present.") . '</em>';
73 }
74 $custom_data_added = NULL;
75 if(civicrm_engage_custom_data_added()) {
76 $custom_data_added = ' <em>' . t("It appears that the custom data is present.") . '</em>';
77 }
78 $custom_voter_us_data_added = NULL;
79 if(civicrm_engage_custom_voter_us_data_added()) {
80 $custom_voter_us_data_added = ' <em>' . t("It appears that the US custom voter data is present.") . '</em>';
81 }
82 $address_parsing_enabled = NULL;
83 if(civicrm_engage_address_parsing_enabled()) {
84 $address_parsing_enabled = ' <em>' . t("It appears that address parsing is enabled.") . '</em>';
85 }
86 $autocomplete_options_set = NULL;
87 if(civicrm_engage_autocomplete_options_set()) {
88 $autocomplete_options_set = ' <em>' . t("It appears that the autocomplete options are properly set.") . '</em>';
89 }
90 $form['load_data'] = array(
91 '#type' => 'fieldset',
92 '#title' => t('CiviEngage Load Data and Set Configuration Options'),
93 '#weight' => 0,
94 '#collapsible' => TRUE,
95 '#collapsed' => FALSE,
96 );
97
98 $form['load_data']['contact_subtypes'] = array(
99 '#type' => 'checkbox',
100 '#title' => t('Add contact subtypes'),
101 '#description' => t("Civicrm Engage depends on the presence of a Media, Funder, Elected Official, Media Outlet and Foundation contact subtypes. Check this box to add them. This step is not reversible.") . $subtypes_added
102 );
103 $form['load_data']['address_parsing'] = array(
104 '#type' => 'checkbox',
105 '#title' => t('Enable address parsing'),
106 '#description' => t("Street parsing is required for walk lists because it needs to sort by even/odd address numbers.") . $address_parsing_enabled
107 );
108 $form['load_data']['autocomplete_options'] = array(
109 '#type' => 'checkbox',
110 '#title' => t('Set phone to autocomplete'),
111 '#description' => t("Autocomplete options double as indicator of whether a field should show up in batch update. For civicrm engage to work properly, phone should be set to autocomplete so it shows up in batch update.") . $autocomplete_options_set
112 );
113 $form['load_data']['custom_data'] = array(
114 '#type' => 'checkbox',
115 '#title' => t('Add general custom groups, fields and other data'),
116 '#description' => t("Civicrm Engage depends on a number of custom data groups and fields and profiles. Check this box to add them. This step is not reversible and will take several minutes to complete.") . $custom_data_added
117 );
118 $form['load_data']['custom_voter_us_data'] = array(
119 '#type' => 'checkbox',
120 '#title' => t('Add US voter custom groups, fields and other data'),
121 '#description' => t("Civicrm Engage depends on voter-related fields being imported (to track congressional district, city district, etc.). Current only US-specific voter fields are available.") . $custom_voter_us_data_added
122 );
123 $form['load_data']['submit'] = array(
124 '#type' => 'submit',
125 '#value' => 'Submit'
126 );
127 return $form;
128 }
129
130
131 function civicrm_engage_admin_load_data_submit($form, &$form_state) {
132 if($form_state['values']['contact_subtypes'] == 1) {
133 civicrm_engage_load_contact_subtypes();
134 }
135 if($form_state['values']['custom_data'] == 1) {
136 civicrm_engage_load_custom_data();
137 }
138 if($form_state['values']['custom_voter_us_data'] == 1) {
139 civicrm_engage_load_custom_voter_us_data();
140 }
141 if($form_state['values']['address_parsing'] == 1) {
142 civicrm_engage_enable_address_parsing();
143 }
144 if($form_state['values']['autocomplete_options'] == 1) {
145 civicrm_engage_set_autocomplete_options();
146 }
147 }
148
149 /**
150 * Check to see if the subtypes have already been added.
151 **/
152 function civicrm_engage_subtypes_added() {
153 civicrm_initialize();
154 require_once ("CRM/Contact/BAO/ContactType.php");
155 $existing_types = CRM_Contact_BAO_ContactType::subTypeInfo();
156 if(array_key_exists('Media_Outlet', $existing_types)) {
157 return true;
158 }
159 return false;
160 }
161
162 /**
163 * Check to see if the custom data has already been added.
164 **/
165 function civicrm_engage_custom_data_added() {
166 civicrm_initialize();
167 $sql = "SELECT is_active FROM civicrm_uf_group WHERE name = 'update_constituent_info'";
168 $dao = CRM_Core_DAO::executeQuery($sql);
169 $dao->fetch();
170 if(!property_exists($dao, 'is_active')) return false;
171 if($dao->is_active == 1) return true;
172 return false;
173 }
174
175 /**
176 * Check to see if the custom voter us data has already been added.
177 **/
178 function civicrm_engage_custom_voter_us_data_added() {
179 civicrm_initialize();
180 $sql = "SELECT is_active FROM civicrm_uf_group WHERE name = 'update_voter_info'";
181 $dao = CRM_Core_DAO::executeQuery($sql);
182 $dao->fetch();
183 if(!property_exists($dao, 'is_active')) return false;
184 if($dao->is_active == 1) return true;
185 return false;
186 }
187
188 /**
189 * Create contact subtypes which are referenced in the
190 * xml file that is imported to boostrap the CiviCRM
191 * Campaign component. This function is indempotent, so
192 * it's run on enable.
193 */
194 function civicrm_engage_load_contact_subtypes() {
195 // parent_id : 1 is Individual, 3 is Organization
196 $create = array(
197 'Media_Contact' => array(
198 'label' => 'Media Contact',
199 'parent_id' => 1,
200 'is_active' => 1,
201 'name' => 'Media_Contact',
202 ),
203 'Funder_Contact' => array(
204 'label' => 'Funder Contact',
205 'parent_id' => 1,
206 'is_active' => 1,
207 'name' => 'Funder_Contact',
208 ),
209 'Elected_Official' => array(
210 'label' => 'Elected Official',
211 'parent_id' => 1,
212 'is_active' => 1,
213 'name' => 'Elected_Official',
214 ),
215 'Media_Outlet' => array(
216 'label' => 'Media Outlet',
217 'parent_id' => 3,
218 'is_active' => 1,
219 'name' => 'Media_Outlet',
220 ),
221 'Foundation' => array(
222 'label' => 'Foundation',
223 'parent_id' => 3,
224 'is_active' => 1,
225 'name' => 'Foundation',
226 ),
227 );
228 require_once ("CRM/Contact/BAO/ContactType.php");
229 $existing_types = CRM_Contact_BAO_ContactType::subTypeInfo();
230 while (list($k) = each($existing_types)) {
231 if (array_key_exists($k, $create)) {
232 drupal_set_message(t("Not creating @type, already exists.", array("@type" => $k)));
233 unset($create[$k]);
234 }
235 }
236 $contact_type = new CRM_Contact_BAO_ContactType();
237 while (list($k, $v) = each($create)) {
238 drupal_set_message(t("Creating %c", array('%c' => $k)));
239 $contact_type->add($v);
240 }
241 }
242
243 function civicrm_engage_load_custom_data() {
244 drupal_set_message("Loading default civicrm_engage custom data.");
245 $xml_file = civicrm_engage_get_absolute_module_path() . '/CustomGroupData.xml';
246 require_once 'CRM/Utils/Migrate/Import.php';
247 $import = new CRM_Utils_Migrate_Import();
248 $import->run($xml_file);
249
250 // The xml file sets up a field with a contact reference limited to the staff
251 // group (staff responsible). So, we have to make sure the staff group exists
252 // and if not create it and update the id number in the staff responsible
253 // field definition.
254 drupal_set_message("Creating staff group.");
255 return civicrm_engage_load_staff_group();
256 }
257
258 /**
259 * Create group that will be used to populate the responsible
260 * staff custom data field.
261 **/
262 function civicrm_engage_load_staff_group() {
263 // Record the id of the group we create so we can ensure that the
264 // staff responsible contact reference field is restricted to the
265 // right group.
266 $id = NULL;
267 try {
268 // Check if it exists already
269 $params = array('name' => 'staff');
270 $result = civicrm_api3('Group', 'Get', $params);
271 if($result['count'] > 0) {
272 // We already have the group in place.
273 drupal_set_message(t("Staff group already exists."));
274 $row = array_pop($result['values']);
275 $id = $row['id'];
276 }
277 else {
278 // Otherwise, we have to add it.
279 $params = array(
280 'name' => 'staff',
281 'title' => 'Staff',
282 'description' => 'Contacts in this group are used to populate the staff responsible field.',
283 'is_active' => 1,
284 'group_type' => '2',
285 'is_hidden' => '0',
286 'is_reserved' => '0'
287 );
288 $result = civicrm_api3('Group', 'create', $params);
289 drupal_set_message(t("Staff group created."));
290 $row = array_pop($result['values']);
291 $id = $row['id'];
292 }
293 if($id) {
294 $sql = "UPDATE civicrm_custom_field SET filter = 'action=lookup&group=%0' WHERE
295 name = 'staff_responsible' OR name = 'event_contact_person'";
296 $params = array(
297 0 => array($id, 'Integer')
298 );
299 CRM_Core_DAO::executeQuery($sql, $params);
300 }
301 }
302 catch (CiviCRM_API3_Exception $e) {
303 drupal_set_message(t("Failed to load staff group."));
304 drupal_set_message($e->getMessage());
305 return FALSE;
306 }
307 return TRUE;
308 }
309
310 function civicrm_engage_load_custom_voter_us_data() {
311 drupal_set_message("Loading default civicrm_engage US custom voter data.");
312 $xml_file = civicrm_engage_get_absolute_module_path() . '/Voter.us.xml';
313 require_once 'CRM/Utils/Migrate/Import.php';
314 $import = new CRM_Utils_Migrate_Import();
315 $import->run($xml_file);
316 }
317
318 function civicrm_engage_address_parsing_enabled() {
319 civicrm_initialize();
320 include_once 'CRM/Core/BAO/Setting.php';
321 $address_options = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
322 'address_options',
323 TRUE, NULL, TRUE
324 );
325 if($address_options['street_address_parsing'] == 1) {
326 return TRUE;
327 }
328 return FALSE;
329 }
330
331 /**
332 * Helper file to get path to civicrm_engage directory
333 **/
334 function civicrm_engage_get_absolute_module_path() {
335 // we need to build the path to the Voter.us.xml file
336 // shipped in the civicrm_engage directory. civicrm_engage could
337 // be installed outside of the civicrm root, so we can't rely on
338 // that contstant.
339
340 // instead... start with the drupal root
341 $root = getcwd();
342 // and then added the relative path to the module
343 return $root . '/' . drupal_get_path('module', 'civicrm_engage');
344 }
345
346 /**
347 * Street parsing is required for walk lists because it needs to
348 * sort by even/odd address numbers so, when canvassing a street
349 * in which even addresses are on one side and odd on the other, you
350 * can divide the task between two people with two different lists.
351 */
352 function civicrm_engage_enable_address_parsing() {
353 civicrm_initialize();
354 include_once 'CRM/Core/BAO/Setting.php';
355 $address_options = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
356 'address_options',
357 TRUE, NULL, TRUE
358 );
359 $address_options['street_address_parsing'] = 1;
360 CRM_Core_BAO_Setting::setValueOption(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
361 'address_options',
362 $address_options
363 );
364 drupal_set_message("Addressing parsing options saved.");
365 }
366
367 function civicrm_engage_autocomplete_options_set() {
368 civicrm_initialize();
369 include_once 'CRM/Core/BAO/Setting.php';
370 $contact_autocomplete_options = CRM_Core_BAO_Setting::valueOptions(
371 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
372 'contact_autocomplete_options',
373 TRUE, NULL, TRUE
374 );
375 if($contact_autocomplete_options['phone'] == 1) {
376 return TRUE;
377 }
378 return FALSE;
379 }
380
381 /**
382 * Check Phone Number for auto complete. This setting doubles for batch
383 * update, and when making a phone list in CiviCampaign you really need
384 * to have the phone number included.
385 */
386 function civicrm_engage_set_autocomplete_options() {
387 civicrm_initialize();
388 include_once 'CRM/Core/BAO/Setting.php';
389 $contact_autocomplete_options = CRM_Core_BAO_Setting::valueOptions(
390 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
391 'contact_autocomplete_options',
392 TRUE, NULL, TRUE
393 );
394 $contact_autocomplete_options['phone'] = 1;
395 CRM_Core_BAO_Setting::setValueOption(
396 CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
397 'contact_autocomplete_options',
398 $contact_autocomplete_options
399 );
400 drupal_set_message(t("Autocomplete options have been saved."));
401 }