Last active
December 11, 2015 22:09
-
-
Save kraigh/4668041 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Implements hook_block_info(). | |
| */ | |
| function jnet_community_map_block_info() { | |
| $blocks['jnet_community_map_block'] = array( | |
| 'info' => t('Community Map Block'), | |
| 'cache' => DRUPAL_NO_CACHE | |
| ); | |
| return $blocks; | |
| } | |
| /** | |
| * Implements hook_cron(). | |
| */ | |
| function jnet_community_map_cron() { | |
| // Get the community map data. | |
| // Include the City API library. | |
| require_once(DRUPAL_ROOT . '/sites/all/libraries/city-api/lib/ca-main.php'); | |
| // Store our data. | |
| $group_data_array = array(); | |
| $group_data_map_json = array(); | |
| $group_emails = array(); | |
| $ca = new CityApi(); | |
| // $ca->debug = true; | |
| $ca->json = true; | |
| $groups_index_results = $ca->groups_index(array('group_types' => "CG")); | |
| $groups_object = json_decode($groups_index_results); | |
| // DEBUG | |
| $debug_folder = date("Y_m_d_H_i_s"); | |
| if (!is_dir(DRUPAL_ROOT . '/files/city_api_test')) { | |
| mkdir(DRUPAL_ROOT . '/files/city_api_test'); | |
| } | |
| if (!is_dir(DRUPAL_ROOT . '/files/city_api_test/' . $debug_folder)) { | |
| mkdir(DRUPAL_ROOT . '/files/city_api_test/' . $debug_folder); | |
| file_put_contents(DRUPAL_ROOT . '/files/city_api_test/' . $debug_folder . '/groups_index_1', $groups_index_results); | |
| } | |
| if (isset($groups_object->total_pages)) { | |
| for ($i=1; $i<=$groups_object->total_pages; $i++) { | |
| // Now that we've called it once to know total pages -- let's walk through all of them, unless we have already done so. | |
| if ($i != 1) { | |
| $groups_index_results = $ca->groups_index(array('group_types' => "CG", 'page' => $i)); | |
| $groups_object = json_decode($groups_index_results); | |
| // DEBUG | |
| file_put_contents(DRUPAL_ROOT . '/files/city_api_test/' . $debug_folder . '/groups_index_' . $i, $groups_index_results); | |
| } | |
| foreach ($groups_object->groups as $group) { | |
| $groups_addresses_results = $ca->groups_addresses_index($group->id); | |
| $addresses_object = json_decode($groups_addresses_results); | |
| // DEBUG | |
| file_put_contents(DRUPAL_ROOT . '/files/city_api_test/' . $debug_folder . '/groups_addresses_index_' . $group->id, $groups_addresses_results); | |
| // TAGS | |
| // Get day, type of group | |
| $groups_tags_results = $ca->groups_tags_index($group->id); | |
| $tags_object = json_decode($groups_tags_results); | |
| $groups_roles_results = $ca->groups_roles_index($group->id, array('title' => 'Leaders')); | |
| $roles_object = json_decode($groups_roles_results); | |
| // Compile information ... | |
| $group_id = $group->id; | |
| $group_name = $group->name; | |
| $group_nickname = $group->nickname; | |
| $group_campus = $group->campus_name; | |
| $group_unlisted_status = $group->unlisted; | |
| $group_address_count = $addresses_object->total_entries; | |
| $group_street = $addresses_object->addresses[0]->street; | |
| $group_zip_code = $addresses_object->addresses[0]->zipcode; | |
| $group_latitude = $addresses_object->addresses[0]->latitude; | |
| $group_longitude = $addresses_object->addresses[0]->longitude; | |
| $group_tags = array(); | |
| $group_leaders = array(); | |
| foreach($tags_object->tags as $tag) { | |
| array_push($group_tags, $tag->name); | |
| } | |
| foreach($roles_object->roles as $leader) { | |
| $group_leader_results = $ca->users_show($leader->user_id); | |
| $leader_object = json_decode($group_leader_results); | |
| // DEBUG | |
| file_put_contents(DRUPAL_ROOT . '/files/city_api_test/' . $debug_folder . '/users_show_' . $leader->user_id, $group_leader_results); | |
| array_push($group_leaders, $leader_object->email); | |
| // Only place if we have name, campus, 1 address, latitude, longitude, tags, and leaders | |
| if (strlen($group_name) && strlen($group_campus) && ($group_address_count > 0) && strlen($group_latitude) && strlen($group_longitude) && (sizeof($group_tags) > 0) && (sizeof($group_leaders) > 0)) { | |
| $group_data_array[$group_id] = array( | |
| 'id' => $group_id, | |
| 'name' => $group_name, | |
| 'nickname' => $group_nickname, | |
| 'campus' => $group_campus, | |
| 'unlisted' => $group_unlisted_status, | |
| 'address_count' => $group_address_count, | |
| 'street' => $group_street, | |
| 'zip_code' => $group_zip_code, | |
| 'latitude' => $group_latitude, | |
| 'longitude' => $group_longitude, | |
| 'tags' => $group_tags, | |
| 'leaders' => $group_leaders, | |
| ); | |
| } | |
| } | |
| } | |
| $marker_counter = 1; | |
| foreach ($group_data_array as $group) { | |
| $group_data_map_json[$group['id']] = array( | |
| 'marker_id' => $marker_counter, | |
| 'latitude' => $group['latitude'], | |
| 'longitude' => $group['longitude'], | |
| 'draggable' => FALSE, | |
| 'title' => $group['name'], | |
| 'campus' => $group['campus'], | |
| 'tags' => $group['tags'], | |
| ); | |
| // Store the group email separately. | |
| $group_emails[$group['id']] = $group['leaders']; | |
| $marker_counter++; | |
| } | |
| } | |
| } | |
| param_set('city_api_map_json', $group_data_map_json); | |
| param_set('city_api_map_emails', $group_emails); | |
| } | |
| /** | |
| * Implements hook_block_view(). | |
| */ | |
| function jnet_community_map_block_view($delta = '') { | |
| $block = array(); | |
| switch ($delta) { | |
| case 'jnet_community_map_block': | |
| $group_data_map_json = param_get('city_api_map_json'); | |
| // Add the map JSON data as a js setting. | |
| drupal_add_js(array('jnet_community_map' => array('data_map_json' => $group_data_map_json)), 'setting'); | |
| // Add all JS files. | |
| drupal_add_js('https://www.google.com/jsapi'); | |
| drupal_add_js('http://maps.google.com/maps/api/js?sensor=true'); | |
| drupal_add_js(drupal_get_path('module', 'jnet_community_map') . '/js/infobox.js'); | |
| drupal_add_js(drupal_get_path('module', 'jnet_community_map') . '/js/email-popups.js'); | |
| drupal_add_js(drupal_get_path('module', 'jnet_community_map') . '/js/markers.js'); | |
| drupal_add_js(drupal_get_path('module', 'jnet_community_map') . '/js/jnet_community_map.js'); | |
| drupal_add_css(drupal_get_path('module', 'jnet_community_map') . '/jnet_community_map.css'); | |
| // Add the markup. | |
| $block['subject'] = ''; | |
| $block['content'] = ' | |
| <div id="joinRequestPopup" class="emailPopup reveal-modal"> | |
| <a href="javascript:;" class="close-reveal-modal">×</a> | |
| <div id="action-form"> | |
| ' . render(drupal_get_form('jnet_community_map_action_form','join')) . ' | |
| </div> | |
| </div> | |
| <div id="reportErrorPopup" class="emailPopup reveal-modal"> | |
| <a href="javascript:;" class="close-reveal-modal">×</a> | |
| <div id="action-form"> | |
| ' . render(drupal_get_form('jnet_community_map_action_form','report')) . ' | |
| </div> | |
| </div> | |
| <form class="custom" id="map-filters"> | |
| <a href="javascript:;" id="centerMap" class="button small secondary radius"><i class="g-foundicon-location" style="margin-right:5px;"></i>Center on My Location</a> | |
| <select id="select-church" class="church-map"> | |
| </select> | |
| <select id="select-day" class="church-map"> | |
| </select> | |
| <input type="checkbox" id = "kidFriendly" name="kid-friendly" value="1">Kid-Friendly</input> | |
| </form> | |
| <select id="select-type" class="church-map" style="display:none;"> | |
| </select> | |
| <div id="map_canvas" class="twelve columns"></div> | |
| '; | |
| break; | |
| // NOTE: #select-type (above) should go inside the form (below #select-day) if you want it to appear. | |
| } | |
| return $block; | |
| } | |
| /** | |
| * Implements hook_form(). | |
| */ | |
| function jnet_community_map_action_form($form, &$form_state, $type) { | |
| $form['#jnet_form_type'] = $type; | |
| $form['email'] = array( | |
| '#type' => 'textfield', | |
| '#title' => t('Your Email:'), | |
| '#required' => TRUE, | |
| ); | |
| $form['message'] = array( | |
| '#type' => 'textarea', | |
| '#title' => t('Your Message:'), | |
| '#required' => TRUE, | |
| ); | |
| $form['group-id'] = array( | |
| '#type' => 'textfield', | |
| '#title' => '', | |
| ); | |
| $form['group-name'] = array( | |
| '#type' => 'textfield', | |
| '#title' => '', | |
| ); | |
| $form['group-kids'] = array( | |
| '#type' => 'textfield', | |
| '#title' => '', | |
| ); | |
| $form['group-days'] = array( | |
| '#type' => 'textfield', | |
| '#title' => '', | |
| ); | |
| $form['submit'] = array( | |
| '#type' => 'button', | |
| '#value' => 'Send Message', | |
| '#ajax' => array( | |
| 'callback' => 'jnet_community_map_action_ajax_email', | |
| 'wrapper' => 'action-form', | |
| 'method' => 'replace', | |
| 'effect' => 'fade', | |
| ), | |
| ); | |
| return $form; | |
| } | |
| /** | |
| * Email Validator | |
| */ | |
| function jnet_validate_email($element, &$form_state, $form) { | |
| if (!valid_email_address($element['#value'])) { | |
| form_error($element, t('The email address you entered is invalid.')); | |
| } | |
| } | |
| function jnet_community_map_action_ajax_email($form, &$form_state) { | |
| switch ($form['#jnet_form_type']) { | |
| case 'request': | |
| $message_header = 'A user at thejourney.org has requested more information about joining your community group. Their message is below.'; | |
| break; | |
| case 'report': | |
| $message_header = 'A user at thejourney.org has reported incorrect community group info. Their message is below.'; | |
| break; | |
| } | |
| $group_emails = param_get('city_api_map_emails'); | |
| //$send_form_to = implode(', ', $group_emails[$form_state['values']['group-id']]); | |
| $send_form_to = '[email protected]'; | |
| $params = array( | |
| 'context' => array( | |
| 'subject' => 'Request to join your community group - ' . $form_state['values']['email'], | |
| 'body' => '<p>' . $message_header . '</p><p><b>Email:</b> ' . $form_state['values']['email'] . '</p><p><b>Message:</b> ' . $form_state['values']['message'] . '</p><p></p><p><u>Group Info</u></p><p>Group Name: ' . $form_state['values']['group-name'] . '</p><p>Group ID: ' . $form_state['values']['group-id'] . '</p><p>Leader Email: ' . $form_state['values']['leader-email'] . '</p><p>Group Meeting Day: ' . $form_state['values']['group-days'] . '</p><p>Kid Friendly: ' . $form_state['values']['group-kids'] . '</p>', | |
| ), | |
| 'plaintext' => $message_header . ' Email: ' . $form_state['values']['email'] . '. Message: ' . $form_state['values']['message'] . '. Group Name: ' . $form_state['values']['group-name'] . '. Group ID: ' . $form_state['values']['group-id'] . '. Leader Email: ' . $form_state['values']['leader-email'] . '. Group Meeting Day: ' . $form_state['values']['group-days'] . '. Kid Friendly: ' . $form_state['values']['group-kids'] . '', | |
| ); | |
| if (valid_email_address($form_state['values']['email'])) { | |
| drupal_mail('mimemail', 'key', $send_form_to, language_default(), $params); | |
| return '<div>Email Sent</div>'; | |
| } else { | |
| form_set_error('email','This e-mail address is invalid.'); | |
| return $form; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment