hooks_reaction_add("views_query_alter", function (&$view, &$query) {
    // Only do this for the state_resource_view View
    if ($view->name === 'state_resource_view') {
        // Only do this for the state_resource_wizardresults_program Display in this View
        if ($view->current_display === 'state_resource_wizardresults_program') {
            // dsm($view);
            //  dsm($query);
            // Only do this is an argument was passed
            if (!empty($view->args[0])) {
                //  dsm(“%” . $view->args[0] . “%”);
                //  dsm($view);
                $tax = ucfirst($view->args[1]);
                $tax1 = ucfirst($view->args[2]);
                $vocabulary = taxonomy_vocabulary_machine_name_load('user_submittted_tags');
                $tree = taxonomy_get_tree($vocabulary->vid);
                $termid = array();
                foreach ($tree as $term) {
                    if ($term->name == $tax) {
                        array_push($termid, $term->tid);
                    } else {
                        if ($term->name == $tax1) {
                            //$termid = $term->tid;
                            array_push($termid, $term->tid);
                        }
                    }
                }
                $query->where[2]['conditions'][0]['value'] = $view->args[0];
                $query->where[2]['conditions'][1]['value'] = array($view->args[1]);
                $query->where[2]['conditions'][2]['value'] = array($termid);
                $query->add_field('field_data_field_program_owner_share', 'field_program_owner_share_value');
                $query->table_queue['field_data_field_program_owner_share']['join']->type = 'LEFT';
                $query->add_field('field_data_field_tagged_terms', 'field_tagged_terms_tid');
                $query->table_queue['field_data_field_tagged_terms']['join']->type = 'LEFT';
                /* code here */
            }
        } else {
            if ($view->current_display === 'state_resource_service') {
                // dsm($view);
                //  dsm($query);
                // Only do this is an argument was passed
                if (!empty($view->args[0])) {
                    //  dsm(“%” . $view->args[0] . “%”);
                    $tax = ucfirst($view->args[3]);
                    $tax1 = ucfirst($view->args[4]);
                    $tax2 = ucfirst($view->args[5]);
                    $vocabulary = taxonomy_vocabulary_machine_name_load('user_submittted_tags');
                    $tree = taxonomy_get_tree($vocabulary->vid);
                    $termid = array();
                    foreach ($tree as $term) {
                        if ($term->name == $tax) {
                            //$termid = $term->tid;
                            array_push($termid, $term->tid);
                        } else {
                            if ($term->name == $tax1) {
                                //$termid = $term->tid;
                                array_push($termid, $term->tid);
                            } else {
                                if ($term->name == $tax2) {
                                    //$termid = $term->tid;
                                    array_push($termid, $term->tid);
                                }
                            }
                        }
                    }
                    $query->where[2]['conditions'][0]['value'] = $view->args[0];
                    $query->where[2]['conditions'][1]['value'] = array($view->args[1]);
                    $query->where[2]['conditions'][2]['value'] = array($view->args[2]);
                    $query->where[2]['conditions'][3]['value'] = array($termid);
                    $query->add_field('field_data_field_program_owner_share', 'field_program_owner_share_value');
                    $query->table_queue['field_data_field_program_owner_share']['join']->type = 'LEFT';
                    $query->add_field('field_data_field_tagged_terms', 'field_tagged_terms_tid');
                    $query->table_queue['field_data_field_tagged_terms']['join']->type = 'LEFT';
                    /* code here */
                }
            } else {
                if ($view->current_display === 'state_resource_ownership_tools') {
                    // dsm($view);
                    //  dsm($query);
                    // Only do this is an argument was passed
                    if (!empty($view->args[0])) {
                        //  dsm(“%” . $view->args[0] . “%”);
                        $tax = ucfirst($view->args[2]);
                        $tax1 = ucfirst($view->args[3]);
                        $tax2 = ucfirst($view->args[4]);
                        $vocabulary = taxonomy_vocabulary_machine_name_load('user_submittted_tags');
                        $tree = taxonomy_get_tree($vocabulary->vid);
                        $termid = array();
                        if (!empty($view->args[4])) {
                            foreach ($tree as $term) {
                                if ($term->name == $tax) {
                                    //$termid = $term->tid;
                                    array_push($termid, $term->tid);
                                } else {
                                    if ($term->name == $tax1) {
                                        //$termid = $term->tid;
                                        array_push($termid, $term->tid);
                                    } else {
                                        if ($term->name == $tax2) {
                                            //$termid = $term->tid;
                                            array_push($termid, $term->tid);
                                        }
                                    }
                                }
                            }
                        } else {
                            foreach ($tree as $term) {
                                if ($term->name == $tax) {
                                    // $termid = $term->tid;
                                    array_push($termid, $term->tid);
                                } else {
                                    if ($term->name == $tax1) {
                                        //$termid = $term->tid;
                                        array_push($termid, $term->tid);
                                    }
                                }
                            }
                        }
                        $query->where[2]['conditions'][0]['value'] = $view->args[0];
                        $query->where[2]['conditions'][1]['value'] = array($view->args[1]);
                        $query->where[2]['conditions'][2]['value'] = array($termid);
                        $query->add_field('field_data_field_program_owner_share', 'field_program_owner_share_value');
                        $query->table_queue['field_data_field_program_owner_share']['join']->type = 'LEFT';
                        $query->add_field('field_data_field_tagged_terms', 'field_tagged_terms_tid');
                        $query->table_queue['field_data_field_tagged_terms']['join']->type = 'LEFT';
                        dsm($query);
                        /* code here */
                    }
                } else {
                    if ($view->current_display === 'state_resource_wizardresults_service') {
                        // dsm('hello');
                        if (!empty($view->args[0])) {
                            //dsm(“%” . $view->args[0] . “%”);
                            $tax = ucfirst($view->args[1]);
                            $tax1 = ucfirst($view->args[2]);
                            $vocabulary = taxonomy_vocabulary_machine_name_load('user_submittted_tags');
                            $tree = taxonomy_get_tree($vocabulary->vid);
                            $termid = array();
                            foreach ($tree as $term) {
                                if ($term->name == $tax) {
                                    // $termid = $term->tid;
                                    array_push($termid, $term->tid);
                                } else {
                                    if ($term->name == $tax1) {
                                        //$termid = $term->tid;
                                        array_push($termid, $term->tid);
                                    }
                                }
                            }
                            $query->where[2]['conditions'][0]['value'] = $view->args[0];
                            $query->where[2]['conditions'][1]['value'] = array($view->args[1]);
                            $query->where[2]['conditions'][2]['value'] = array($termid);
                            $query->add_field('field_data_field_program_owner_share', 'field_program_owner_share_value');
                            $query->table_queue['field_data_field_program_owner_share']['join']->type = 'LEFT';
                            $query->add_field('field_data_field_tagged_terms', 'field_tagged_terms_tid');
                            $query->table_queue['field_data_field_tagged_terms']['join']->type = 'LEFT';
                            /* code here */
                        }
                    } else {
                        if ($view->current_display === 'state_resource_program') {
                            if (!empty($view->args[0])) {
                                // dsm(“%” . $view->args[0] . “%”);
                                //dsm($view);
                                //dsm($query);
                                $tax = ucfirst($view->args[3]);
                                $tax1 = ucfirst($view->args[4]);
                                $tax2 = ucfirst($view->args[5]);
                                $tax3 = ucfirst($view->args[6]);
                                $vocabulary = taxonomy_vocabulary_machine_name_load('user_submittted_tags');
                                $tree = taxonomy_get_tree($vocabulary->vid);
                                $termid = array();
                                foreach ($tree as $term) {
                                    if ($term->name == $tax) {
                                        //$termid = $term->tid;
                                        array_push($termid, $term->tid);
                                    } else {
                                        if ($term->name == $tax1) {
                                            //$termid = $term->tid;
                                            array_push($termid, $term->tid);
                                        } else {
                                            if ($term->name == $tax2) {
                                                //$termid = $term->tid;
                                                array_push($termid, $term->tid);
                                            } else {
                                                if ($term->name == $tax3) {
                                                    //$termid = $term->tid;
                                                    array_push($termid, $term->tid);
                                                }
                                            }
                                        }
                                    }
                                }
                                $query->where[2]['conditions'][0]['value'] = $view->args[0];
                                $query->where[2]['conditions'][1]['value'] = $view->args[1];
                                $query->where[2]['conditions'][2]['value'] = array($view->args[2]);
                                $query->where[2]['conditions'][3]['value'] = array($termid);
                                $query->add_field('field_data_field_program_owner_share', 'field_program_owner_share_value');
                                $query->table_queue['field_data_field_program_owner_share']['join']->type = 'LEFT';
                                $query->add_field('field_data_field_tagged_terms', 'field_tagged_terms_tid');
                                $query->table_queue['field_data_field_tagged_terms']['join']->type = 'LEFT';
                                /* code here */
                            }
                        } else {
                            if ($view->current_display === 'ownership_resource_center') {
                                if (!empty($view->args[0])) {
                                    // dsm(“%” . $view->args[0] . “%”);
                                    //dsm($view);
                                    $query->where[1]['conditions'][2]['value'] = array($view->args[0]);
                                    $query->where[1]['conditions'][3]['value'] = array($view->args[1]);
                                    if (!empty($view->args[3])) {
                                        $query->where[1]['conditions'][2]['value'] = array($view->args[0], $view->args[1], $view->args[2]);
                                        $query->where[1]['conditions'][3]['value'] = array($view->args[3]);
                                    } else {
                                        if (!empty($view->args[2])) {
                                            $query->where[1]['conditions'][2]['value'] = array($view->args[0], $view->args[1]);
                                            $query->where[1]['conditions'][3]['value'] = array($view->args[2]);
                                        }
                                    }
                                    /* code here */
                                }
                            } else {
                                if ($view->current_display === 'state_resource_all_content') {
                                    if (!empty($view->args[0])) {
                                        $tax = ucfirst($view->args[1]);
                                        $vocabulary = taxonomy_vocabulary_machine_name_load('user_submittted_tags');
                                        $tree = taxonomy_get_tree($vocabulary->vid);
                                        $termid = '';
                                        foreach ($tree as $term) {
                                            if ($term->name == $tax) {
                                                $termid = $term->tid;
                                            }
                                        }
                                        $query->where[2]['conditions'][0]['value'] = $view->args[0];
                                        //$query-> where[2]['conditions'][1]['value'] = array( $view->args[1] );
                                        $query->where[2]['conditions'][1]['value'] = array($termid);
                                        $query->where[2]['conditions'][2]['value'] = array($view->args[2]);
                                        $query->where[2]['conditions'][3]['value'] = array($view->args[2]);
                                        $query->where[2]['conditions'][4]['value'] = array($view->args[1]);
                                        //$query-> where[1]['conditions'][2]['value'] = array($view->args[0]);
                                        //$query-> where[1]['conditions'][3]['value'] = array($view->args[1]);
                                        $query->add_field('field_data_field_tagged_terms', 'field_tagged_terms_tid');
                                        $query->table_queue['field_data_field_tagged_terms']['join']->type = 'LEFT';
                                        $query->add_field('field_data_field_program_loc', 'field_program_loc_value');
                                        $query->table_queue['field_data_field_program_loc']['join']->type = 'LEFT';
                                        $query->add_field('field_data_field_tools_state', 'field_tools_state_value');
                                        $query->table_queue['field_data_field_tools_state']['join']->type = 'LEFT';
                                        $query->add_field('field_data_field_state_resource_state', 'field_state_resource_state_value');
                                        $query->table_queue['field_data_field_state_resource_state']['join']->type = 'LEFT';
                                        /* code here */
                                    }
                                } else {
                                    if ($view->current_display === 'state_resource_program_tag') {
                                        //  dsm($view);
                                        //   dsm($query);
                                        // Only do this is an argument was passed
                                        if (!empty($view->args[0])) {
                                            //  dsm(“%” . $view->args[0] . “%”);
                                            $tax = ucfirst($view->args[0]);
                                            $vocabulary = taxonomy_vocabulary_machine_name_load('user_submittted_tags');
                                            $tree = taxonomy_get_tree($vocabulary->vid);
                                            $termid = '';
                                            foreach ($tree as $term) {
                                                if ($term->name == $tax) {
                                                    $termid = $term->tid;
                                                }
                                            }
                                            $query->where[2]['conditions'][0]['value'] = $termid;
                                            $query->where[2]['conditions'][1]['value'] = array($view->args[1]);
                                        }
                                    } else {
                                        if ($view->current_display === 'state_resource_service_tag') {
                                            //  dsm($view);
                                            //   dsm($query);
                                            // Only do this is an argument was passed
                                            if (!empty($view->args[0])) {
                                                //  dsm(“%” . $view->args[0] . “%”);
                                                $tax = ucfirst($view->args[0]);
                                                $vocabulary = taxonomy_vocabulary_machine_name_load('user_submittted_tags');
                                                $tree = taxonomy_get_tree($vocabulary->vid);
                                                $termid = '';
                                                foreach ($tree as $term) {
                                                    if ($term->name == $tax) {
                                                        $termid = $term->tid;
                                                    }
                                                }
                                                $query->where[2]['conditions'][0]['value'] = $termid;
                                                $query->where[2]['conditions'][1]['value'] = array($view->args[1]);
                                            }
                                        } else {
                                            if ($view->current_display === 'state_resource_tools_tag') {
                                                //  dsm($view);
                                                //   dsm($query);
                                                // Only do this is an argument was passed
                                                if (!empty($view->args[0])) {
                                                    //  dsm(“%” . $view->args[0] . “%”);
                                                    $tax = ucfirst($view->args[0]);
                                                    $vocabulary = taxonomy_vocabulary_machine_name_load('user_submittted_tags');
                                                    $tree = taxonomy_get_tree($vocabulary->vid);
                                                    $termid = '';
                                                    foreach ($tree as $term) {
                                                        if ($term->name == $tax) {
                                                            $termid = $term->tid;
                                                        }
                                                    }
                                                    $query->where[2]['conditions'][0]['value'] = $termid;
                                                    $query->where[2]['conditions'][1]['value'] = array($view->args[1]);
                                                }
                                            } else {
                                                if ($view->current_display === 'ownership_resource_all_content') {
                                                    if (!empty($view->args[0])) {
                                                        $tax = ucfirst($view->args[3]);
                                                        $tax1 = ucfirst($view->args[4]);
                                                        $vocabulary = taxonomy_vocabulary_machine_name_load('user_submittted_tags');
                                                        $tree = taxonomy_get_tree($vocabulary->vid);
                                                        $termid = array();
                                                        if (!empty($view->args[5])) {
                                                            $tax2 = ucfirst($view->args[5]);
                                                            $tax3 = ucfirst($view->args[6]);
                                                            foreach ($tree as $term) {
                                                                if ($term->name == $tax) {
                                                                    //$termid = $term->tid;
                                                                    array_push($termid, $term->tid);
                                                                } else {
                                                                    if ($term->name == $tax1) {
                                                                        //$termid = $term->tid;
                                                                        array_push($termid, $term->tid);
                                                                    } else {
                                                                        if ($term->name == $tax2) {
                                                                            //$termid = $term->tid;
                                                                            array_push($termid, $term->tid);
                                                                        } else {
                                                                            if ($term->name == $tax3) {
                                                                                //$termid = $term->tid;
                                                                                array_push($termid, $term->tid);
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        } else {
                                                            foreach ($tree as $term) {
                                                                if ($term->name == $tax) {
                                                                    //$termid = $term->tid;
                                                                    array_push($termid, $term->tid);
                                                                } else {
                                                                    if ($term->name == $tax1) {
                                                                        //$termid = $term->tid;
                                                                        array_push($termid, $term->tid);
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        $query->where[2]['conditions'][0]['value'] = $view->args[0];
                                                        $query->where[2]['conditions'][1]['value'] = $view->args[1];
                                                        $query->where[2]['conditions'][2]['value'] = array($view->args[2]);
                                                        $query->where[2]['conditions'][3]['value'] = array($termid);
                                                        dsm($query);
                                                        dsm($view);
                                                        $query->add_field('field_data_field_tagged_terms', 'field_tagged_terms_tid');
                                                        $query->table_queue['field_data_field_tagged_terms']['join']->type = 'LEFT';
                                                        $query->add_field('field_data_field_program_owner_share', 'field_program_owner_share_value');
                                                        $query->table_queue['field_data_field_program_owner_share']['join']->type = 'LEFT';
                                                        /* code here */
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
});
/*
    [--] BACKGROUND [--]
    The results shown in wizards in BusinessUSA currently* are stored in the Wizard's source-spreadsheet. The 
    wizard-tags which are associated with the results are also ONLY held in the Wizard's source-spreadsheet.
    There may or may not be content (nodes) in the Drupal database that is referenced by these spreadsheets.
    Due to this, it is impossible for Solr to index ALL of the content (results) within wizards. This script is meant to solve 
    this problem.
    * = this comment was written on 2013/08/22
*/
/**
 * Implements hook_cron()
 * 
 * Hook to periodically trigger associateWizardTagsWithNodesInDrupalDatabase()
 */
hooks_reaction_add("cron", function () {
    associateWizardTagsWithNodesInDrupalDatabaseForAllWizards(true);
});
/** array associateWizardTagsWithNodesInDrupalDatabase()
 *
 * Triggers associateWizardTagsWithNodesInDrupalDatabase() for each wizard in BusinessUSA
 *
 * Returns an associative array of wizard-excelsheet-paths (parameter given to associateWizardTagsWithNodesInDrupalDatabase()) as the key, and 
 * the return of associateWizardTagsWithNodesInDrupalDatabase() as the values.
 */
function associateWizardTagsWithNodesInDrupalDatabaseForAllWizards($returnSummaryOnly = false)
{
    // Dont let this process run if it has already ran within the past 12 hours
    $secondsIn12Hours = 43200;
    if (time() < variable_get('associateWizardTagsWithNodes_lastRun', 0) + $secondsIn12Hours) {
        error_log(__FUNCTION__ . " will not execute its functionality, it has ran too recently, Coder Bookmark: CB-NOGJX2V-BC.");
        return;
<?php

/**
 * Implements hook_cron().
 * Hook to periodically trigger cleanup function(s)
 */
hooks_reaction_add("cron", function () {
    cleanup();
});
/**
 * void cleanup()
 * Triggers functions to be fired on "clean up" cycle.
 */
function cleanup()
{
    error_log("Cleanup cycle has startted (CleanUp.php)");
    cleanup_ExcelExportDir();
    cleanup_DeleteDuplicateEvents();
    cleanup_UnpublishPastEvents();
    cleanup_FixMissingLatLongForResourceCenters();
    export_gov_micro_page_cleanup();
    cleanup_FixBadTextFormatUsage();
}
/**
 * void cleanup_FixBadTextFormatUsage()
 * 
 * This function will search for fields that are using the php_code  
 * text-format that shouldn't. And set them to full_html
 *
 * Some fields should not be allowed to use the php_code text-
 * format because users under the Editor role are denied from 
<?php

/**
 * Created by PhpStorm.
 * User: sanjay.gupta
 * Date: 7/16/14
 * Time: 2:54 PM
 */
hooks_reaction_add("apachesolr_query_alter", function ($query) {
    $param = $query->getParams();
    $query->replaceParam(q, strtolower($param['q']));
    //$query->replaceParam(q, strtolower($query->getParams()['q']));
});
 *  This (sub)module controles what Drupal-fields are indexed into Solr. By default, ALL fields
 *  will be indexed, and certain select fields will not (this decision is made in getDrupalNodeFieldsToAddToSolrDocument).
 */
hooks_reaction_add("apachesolr_index_documents_alter", function (array &$documents, $entity, $entity_type, $env_id) {
    // Get an array of all the fields (and values) we should add to Solr-index for this document (based on Drupal fields)
    $fieldsToAdd = getDrupalNodeFieldsToAddToSolrDocument($entity);
    // Add this data to the Solr-Document
    foreach ($fieldsToAdd as $fieldNameToAdd => $fieldValueToAdd) {
        foreach ($documents as &$document) {
            $document->addField($fieldNameToAdd, $fieldValueToAdd, false);
        }
    }
    /* We shall not index the following fields in Solr (for the given content-types) */
    $noIndexMsg = 'ThisFiledIsNotToBeIndexedForThisContentType';
    if ($entity->type === 'swim_lane_page') {
        $document->setField('content', $noIndexMsg);
        $document->setField('teaser', $noIndexMsg);
        $document->setField('spell', $noIndexMsg);
        $document->setField('ts_custom_content', $noIndexMsg);
        $document->setField('ts_field_search_snippet_override', $noIndexMsg);
        $document->setField('ts_field_search_icon_override', $noIndexMsg);
        $document->setField('sm_field_program_wizard_type', $noIndexMsg);
        $document->setField('ts_field_program_wizard_type', $noIndexMsg);
        $document->setField('ts_wizard_indexable_text', $entity->body['und'][0]['value']);
    }
    if (strpos(request_uri(), '-DEBUG-HOOK-APACHESOLR-INDEX-DOCUMENTS-ALTER-') !== false) {
        $debugMsg = array('datetime' => date('U'), 'Function' => 'hook_apachesolr_index_documents_alter', 'Arguments' => array('documents' => $documents, 'entity' => $entity, 'env_id' => $env_id));
    }
});
function getDrupalNodeFieldsToAddToSolrDocument($entity)
{
    $ret = array('ts_custom_content' => '');
/**
 * Implements hook_views_query_alter().
 * Alters the view query to return only results within range of latitude/longitude.
 * Latitude, Longitude, and Range, will be pulled from the View's arguments.
 * This change will not be applied when argument are not given to the view
 */
hooks_reaction_add("views_query_alter", function (&$view, &$query) {
    // Debug verbosity
    if (strpos(request_uri(), '-DEBUG-VIEWSQUERYALTER-USEAC-VERBOSE-') !== false) {
        print '<div class="debug-info debug-viewqueryalter-info" style="display: none;">';
        print '<!--';
        var_dump($query);
        print '-->';
        print '</div>';
    }
    // Only do this for the useac_location_exporting_wizards View
    if ($view->name === 'useac_location_exporting_wizards') {
        // Only do this if all 3 arguments were past to this view
        if (!empty($view->args) && !empty($view->args[0]) && !empty($view->args[1]) && !empty($view->args[2])) {
            list($lat, $ln, $range) = $view->args;
            ensureMySqlDistanceFunctionsExist();
            $distLatLongFunct = "distLatLong({$lat}, {$ln}, field_data_field_appoffice_lat.field_appoffice_lat_value + 0.0, field_data_field_appoffice_long.field_appoffice_long_value + 0.0)";
            $query->add_where_expression(0, $distLatLongFunct . " < {$range}");
            $query->add_field('field_data_field_appoffice_lat', 'field_appoffice_lat_value');
            $query->add_field('field_data_field_appoffice_long', 'field_appoffice_long_value');
            $query->fields['node_title'] = array('field' => "CONCAT( node.title, ' ', convert({$distLatLongFunct}, DECIMAL) ) ", 'table' => '', 'alias' => 'node_title');
            $query->orderby = array(array('field' => $distLatLongFunct, 'direction' => 'ASC'));
        }
    }
});
<?php

hooks_reaction_add("node_insert", function ($node) {
    if ($node->type == "event") {
        apachesolr_mark_node($node->nid);
        apachesolr_cron();
    }
});
//end hooks reaction add
hooks_reaction_add("node_update", function ($node) {
    if ($node->type == "event") {
        apachesolr_mark_node($node->nid);
        apachesolr_cron();
    }
});
//end hooks reaction add
hooks_reaction_add("views_query_alter", function (&$view, &$query) {
    // Only do this for the vendors_near_you View
    if ($view->name === 'vendors_near_you' && ($view->current_display === 'block_1' || $view->current_display === 'block_5')) {
        // Only do this if atleast 2 arguments were past to this view
        if (!empty($view->args) && !empty($view->args[0]) && !empty($view->args[1])) {
            $lat = $view->args[0];
            $ln = $view->args[1];
            $query->add_field('field_data_field_vend_address_zip_lat', 'field_vend_address_zip_lat_value');
            $query->add_field('field_data_field_vend_address_zip_lng', 'field_vend_address_zip_lng_value');
            $distLatLongFunct = "distLatLong({$lat}, {$ln}, (\n                            SELECT lat.field_vend_address_zip_lat_value\n                            FROM field_data_field_vend_address_zip_lat lat\n                            WHERE lat.entity_id = node.nid\n                            LIMIT 1\n                        ), \n                        (\n                            SELECT lng.field_vend_address_zip_lng_value\n                            FROM field_data_field_vend_address_zip_lng lng\n                            WHERE lng.entity_id = node.nid\n                            LIMIT 1\n                        )\n                    )\n                ";
            $query->add_where_expression(0, $distLatLongFunct . " < 100");
            $query->orderby = array(array('field' => $distLatLongFunct, 'direction' => 'ASC'));
            //$query->add_where_expression(0, 'field_vend_address_zip_value = 90210');
        }
        if (strpos(request_uri(), '-DEBUG-VIEWSQUERYALTER-VENDORSNEARYOU-VERBOSE-') !== false) {
            dsm(array('view' => $view, 'view-args' => $view->args, 'query' => $query));
        }
    }
    // Only do this for the vendors_near_you View
    if ($view->name === 'vendors_near_you' && ($view->current_display === 'block_2' || $view->current_display === 'block_3')) {
        // Only do this if atleast 2 arguments were past to this view
        if (!empty($view->args) && !empty($view->args[0]) && !empty($view->args[1])) {
            $lat = $view->args[0];
            $ln = $view->args[1];
            $query->add_field('field_data_field_appoffice_lat', 'field_appoffice_lat_value');
            $query->add_field('field_data_field_appoffice_long', 'field_appoffice_long_value');
            $distLatLongFunct = "distLatLong({$lat}, {$ln}, (\n                            SELECT lat.field_appoffice_lat_value\n                            FROM field_data_field_appoffice_lat lat\n                            WHERE lat.entity_id = node.nid\n                            LIMIT 1\n                        ), \n                        (\n                            SELECT lng.field_appoffice_long_value\n                            FROM field_data_field_appoffice_long lng\n                            WHERE lng.entity_id = node.nid\n                            LIMIT 1\n                        )\n                    )\n                ";
            $query->add_where_expression(0, $distLatLongFunct . " < 100");
            $query->orderby = array(array('field' => $distLatLongFunct, 'direction' => 'ASC'));
        }
    }
});
<?php

/**
 * Implements hook_views_query_alter().
 * Alters the view query to return only results within range of latitude/longitude.
 * Latitude, Longitude, and Range, will be pulled from the View's arguments.
 * This change will not be applied when argument are not given to the view
 */
hooks_reaction_add("views_query_alter", function (&$view, &$query) {
    // Only do this for the sbdcs View
    if ($view->name === 'sbdcs') {
        // Only do this if all 3 arguments were past to this view
        if (!empty($view->args) && !empty($view->args[0]) && !empty($view->args[1]) && !empty($view->args[2])) {
            list($lat, $ln, $range) = $view->args;
            ensureMySqlDistanceFunctionsExist();
            $distLatLongFunct = "distLatLong({$lat}, {$ln}, field_data_field_appoffice_lat.field_appoffice_lat_value + 0.0, field_data_field_appoffice_long.field_appoffice_long_value + 0.0)";
            $query->add_where_expression(0, $distLatLongFunct . " < {$range}");
            $query->add_field('field_data_field_appoffice_lat', 'field_appoffice_lat_value');
            $query->add_field('field_data_field_appoffice_long', 'field_appoffice_long_value');
            $query->fields['node_title'] = array('field' => "CONCAT( node.title, ' ', convert({$distLatLongFunct}, DECIMAL) ) ", 'table' => '', 'alias' => 'node_title');
            $query->orderby = array(array('field' => $distLatLongFunct, 'direction' => 'ASC'));
        }
        if (strpos(request_uri(), '-DEBUG-VIEWSQUERYALTER-SBDCS-VERBOSE-') !== false) {
            dsm($query);
        }
    }
});
<?php

//this was showing up twice for some reason, so I had to hack it to only display once
//forgive me
global $count;
$count = 0;
hooks_reaction_add("block_view_alter", function (&$data, $block) {
    if ($block->title == 'Events Filters') {
        global $count;
        $count++;
        if ($count != 2) {
            $data['content'] = ' ';
            $block->title = '';
            return '';
        }
        drupal_add_js("sites/all/modules/contrib/jquery_update/replace/ui/ui/minified/jquery-ui.min.js", 'file');
        //the following is a nasty hack to get in the calendar theming, forgive me, I ran out of time figuring this out
        drupal_add_css("sites/all/modules/contrib/jquery_update/replace/ui/themes/base/jquery.ui.theme.css", array('type' => 'external', 'group' => CSS_THEME));
        //end hack - womp womp
        $data['content'] = create_events_search_filters($_REQUEST);
    }
});
//end hooks reaction add
function create_events_search_filters($searchVariables)
{
    $keyword = htmlspecialchars($searchVariables['keyword']);
    if (htmlspecialchars($searchVariables['applySubmit'])) {
        $keyword = 'Search Events';
    } else {
        $searchVariables = array();
    }
    }
    // Only do this for the business_sunday_events View
    if ($view->name === 'business_sunday_events') {
        // Only do this for the front_page_resource_centers Display in this View
        if ($view->current_display === 'business_sunday') {
            // Only do this if all 2 arguments were past to this view
            if (!empty($view->args) && !empty($view->args[0]) && !empty($view->args[1])) {
                list($lat, $ln) = $view->args;
                ensureMySqlDistanceFunctionsExist();
                $distLatLongFunct = "distLatLong({$lat}, {$ln}, field_data_field_event_latitude.field_event_latitude_value + 0.0, field_data_field_event_longitude.field_event_longitude_value + 0.0)";
                $query->orderby = array(array('field' => $distLatLongFunct, 'direction' => 'ASC'));
            }
        }
    }
    // Apply pager functionality to API views.
    if (in_array($view->name, array('article_api', 'data_api', 'event_api', 'event_api', 'keyword_search_api', 'program_api', 'service_api', 'success_story_api', 'tool_api'))) {
        if (!empty($_GET['page']) && is_numeric($_GET['page'])) {
            $pager = $view->display_handler->get_plugin('pager');
            // We have to do this because these views do use views_plugin_pager_some which doesn't actually support pages.
            $offset = $pager->options['items_per_page'] * ($_GET['page'] - 1);
            $view->set_offset($offset);
        }
    }
});
hooks_reaction_add("views_pre_build", function (&$view) {
    if (in_array($view->name, array('article_api', 'data_api', 'event_api', 'event_api', 'keyword_search_api', 'program_api', 'service_api', 'success_story_api', 'tool_api'))) {
        $view->init_style();
        $page = !isset($_GET['page']) ? 1 : $_GET['page'];
        $view->style_plugin->options['filename'] = preg_replace('/(.*)(\\.[^.]*)/', '\\1_page_' . intval($page) . '\\2', $view->style_plugin->options['filename']);
    }
});
/*

    [--] NOTICE [--]
    
    The purpose of this script is to detect when Events are created/updated with no latitude/longitude
    information, but have a [valid] address associated. In this scenario, the lat/long values should be 
    automatically populated.
*/
/** 
 * Implements hook_node_presave() 
 * For mode documentation on this hook, refer to: 
 *     https://api.drupal.org/api/drupal/modules%21node%21node.api.php/function/hook_node_presave/7
 */
hooks_reaction_add("node_presave", function ($node) {
    if ($node->type === 'event') {
        event_check_latlong($node, false);
        // $node is passed byref here and updated as needed
    }
});
/**
 * object event_check_latlong(object $node);
 * object event_check_latlong(int $nid, bool $doNodeSave);
 *
 * Given a node or node-id, will check to see if the given node has latitude/longitude information, if 
 * it does not, and has a [valid] address associated, the lat/long values will be populated.
 */
function event_check_latlong($node, $doNodeSave = false)
{
    $thisFunct = __FUNCTION__;
    // If we are given a node-id instead of a node object, node the node
    if (!is_object($node) && is_numeric($node)) {
        $node = node_load($node);
hooks_reaction_add("views_query_alter", function (&$view, &$query) {
    // Only do this for the closes_resource_center_retheme View
    if ($view->name === 'closes_resource_center_retheme') {
        // Only do this for the front_page_events Display in this View
        if ($view->current_display === 'events') {
            // Only do this if all 2 arguments were past to this view
            if (!empty($view->args) && !empty($view->args[0]) && !empty($view->args[1])) {
                $zip = $view->args[0];
                $range = $view->args[1];
                $locInfo = getLatLongFromZipCode($zip);
                $lat = $locInfo['lat'];
                $lng = $locInfo['lng'];
                ensureMySqlDistanceFunctionsExist();
                $distLatLongFunct = "distLatLong({$lat}, {$lng}, field_data_field_event_latitude.field_event_latitude_value + 0.0, field_data_field_event_longitude.field_event_longitude_value + 0.0)";
                $query->add_where_expression(0, $distLatLongFunct . " < {$range}");
            }
        }
    }
    // Only do this for the closes_resource_center_retheme View
    if ($view->name === 'closes_resource_center_retheme') {
        // Only do this for the front_page_resource_centers Display in this View
        if ($view->current_display === 'resource_centers') {
            // Only do this if all 2 arguments were past to this view
            if (!empty($view->args) && !empty($view->args[0]) && !empty($view->args[1])) {
                $zip = $view->args[0];
                $range = $view->args[1];
                $locInfo = getLatLongFromZipCode($zip);
                $lat = $locInfo['lat'];
                $lng = $locInfo['lng'];
                $range = $view->args[1];
                ensureMySqlDistanceFunctionsExist();
                $distLatLongFunct = "distLatLong({$lat}, {$lng}, field_data_field_appoffice_lat.field_appoffice_lat_value + 0.0, field_data_field_appoffice_long.field_appoffice_long_value + 0.0)";
                $query->add_where_expression(0, $distLatLongFunct . " < {$range}");
                $query->orderby = array(array('field' => "IF( field_data_field_appoffice_type.field_appoffice_type_value = 'SBA District Office', 0, 1 )", 'direction' => 'ASC'), array('field' => $distLatLongFunct, 'direction' => 'ASC'));
                dsm($query);
            }
        }
    }
});