Conditions | 74 |
Paths | > 20000 |
Total Lines | 350 |
Code Lines | 215 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
618 | function action_map_markers() { |
||
619 | |||
620 | header_remove('X-Frame-Options'); |
||
621 | $this->view = 'map_markers'; |
||
622 | |||
623 | // Define globals for use in the view. |
||
624 | $this->bean->map_center = array(); |
||
625 | $this->bean->map_markers = array(); |
||
626 | $this->bean->map_markers_groups = array(); |
||
627 | $this->bean->custom_markers = array(); |
||
628 | $this->bean->custom_areas = array(); |
||
629 | |||
630 | // Create New Sugar_Smarty Object |
||
631 | $this->sugarSmarty = new Sugar_Smarty(); |
||
632 | $this->sugarSmarty->assign("mod_strings", $GLOBALS['mod_strings']); |
||
633 | $this->sugarSmarty->assign("app_strings", $GLOBALS['app_strings']); |
||
634 | $this->sugarSmarty->assign('app_list_strings', $GLOBALS['app_list_strings']); |
||
635 | $this->sugarSmarty->assign('moduleListSingular', $GLOBALS['app_list_strings']['moduleListSingular']); |
||
636 | $this->sugarSmarty->assign('moduleList', $GLOBALS['app_list_strings']['moduleList']); |
||
637 | //echo '<pre>'; |
||
638 | //var_dump($_REQUEST); |
||
639 | |||
640 | // Related Map Record Defines the Map |
||
641 | if (!empty($_REQUEST['record']) || |
||
642 | (!empty($_REQUEST['relate_id']) && !empty($_REQUEST['relate_module'])) || |
||
643 | (!empty($_REQUEST['quick_address']) && !empty($_REQUEST['display_module']))) { |
||
644 | |||
645 | // If map 'record' then define map details from current module. |
||
646 | if (@is_guid($_REQUEST['record'])) { |
||
647 | // Get the map object |
||
648 | $map = get_module_info($GLOBALS['currentModule']); |
||
649 | $map->retrieve($_REQUEST['record']); |
||
650 | // Define map variables |
||
651 | $map_parent_type = $map->parent_type; |
||
652 | $map_parent_id = $map->parent_id; |
||
653 | $map_module_type = $map->module_type; |
||
654 | $map_unit_type = $map->unit_type; |
||
655 | $map_distance = $map->distance; |
||
656 | } |
||
657 | // Else if a 'relate_id' use it as the Relate Center Point (Lng/Lat) |
||
658 | else if (@(is_guid($_REQUEST['relate_id']) && !empty($_REQUEST['relate_module']))) { |
||
659 | // Define map variables |
||
660 | $map_parent_type = $_REQUEST['relate_module']; |
||
661 | $map_parent_id = $_REQUEST['relate_id']; |
||
662 | $map_module_type = (!empty($_REQUEST['display_module'])) ? $_REQUEST['display_module'] : $_REQUEST['relate_module']; |
||
663 | $map_distance = (!empty($_REQUEST['distance'])) ? $_REQUEST['distance'] : $this->settings['map_default_distance']; |
||
664 | $map_unit_type = (!empty($_REQUEST['unit_type'])) ? $_REQUEST['unit_type'] : $this->settings['map_default_unit_type']; |
||
665 | } |
||
666 | // Else if a 'quick_address' use it as the Center Point (Lng/Lat) |
||
667 | else if (!empty($_REQUEST['quick_address']) && !empty($_REQUEST['display_module'])) { |
||
668 | // Define map variables / No Parent |
||
669 | $map_parent_type = null; |
||
670 | $map_parent_id = null; |
||
671 | $map_module_type = (!empty($_REQUEST['display_module'])) ? $_REQUEST['display_module'] : $_REQUEST['relate_module']; |
||
672 | $map_distance = (!empty($_REQUEST['distance'])) ? $_REQUEST['distance'] : $this->settings['map_default_distance']; |
||
673 | $map_unit_type = (!empty($_REQUEST['unit_type'])) ? $_REQUEST['unit_type'] : $this->settings['map_default_unit_type']; |
||
674 | } |
||
675 | |||
676 | // Define display object, note - 'Accounts_Members' is a special display type |
||
677 | $this->display_object = ($map_module_type == 'Accounts_Members') ? get_module_info('Accounts') : get_module_info($map_module_type); |
||
678 | $mod_strings_display = return_module_language($GLOBALS['current_language'], $this->display_object->module_name); |
||
679 | $mod_strings_display = array_merge($mod_strings_display, $GLOBALS['mod_strings']); |
||
680 | |||
681 | // If relate module/id object |
||
682 | if (!empty($map_parent_type) && !empty($map_parent_id)) { |
||
683 | |||
684 | // Define relate objects |
||
685 | $this->relate_object = get_module_info($map_parent_type); |
||
686 | $this->relate_object->retrieve($map_parent_id); |
||
687 | $mod_strings_related = return_module_language($GLOBALS['current_language'], $this->relate_object->module_name); |
||
688 | $mod_strings_related = array_merge($mod_strings_related, $GLOBALS['mod_strings']); |
||
689 | |||
690 | // Get the Relate object Assoc Data |
||
691 | $where_conds = $this->relate_object->table_name . ".id = '" . $map_parent_id . "'"; |
||
692 | $query = $this->relate_object->create_new_list_query("" . $this->relate_object->table_name . ".assigned_user_id", $where_conds, array(), array(), 0, '', false, $this->relate_object, false); |
||
693 | //var_dump($query); |
||
694 | $relate_result = $this->bean->db->query($query); |
||
695 | $relate = $this->bean->db->fetchByAssoc($relate_result); |
||
696 | // Add Relate (Center Point) Marker |
||
697 | $this->bean->map_center = $this->getMarkerData($map_parent_type, $relate, true, $mod_strings_related); |
||
698 | // Define Center Point |
||
699 | $center_lat = $this->relate_object->jjwg_maps_lat_c; |
||
700 | $center_lng = $this->relate_object->jjwg_maps_lng_c; |
||
701 | } |
||
702 | // Use Quick Address as Center Point |
||
703 | else { |
||
704 | // Geocode 'quick_address' |
||
705 | $aInfo = $this->bean->getGoogleMapsGeocode($_REQUEST['quick_address'], false, true); |
||
706 | // If not status 'OK', then fail here and exit. Note: Inside of iFrame |
||
707 | if (!empty($aInfo['status']) && $aInfo['status'] != 'OK' && preg_match('/[A-Z\_]/', $aInfo['status'])) { |
||
708 | echo '<br /><br /><div><b>'.$GLOBALS['mod_strings']['LBL_MAP_LAST_STATUS'].': '.$aInfo['status'].'</b></div><br /><br />'; |
||
709 | exit; |
||
710 | } |
||
711 | //var_dump($aInfo); |
||
712 | // Define Marker Data |
||
713 | $aInfo['name'] = $_REQUEST['quick_address']; |
||
714 | $aInfo['id'] = 0; |
||
715 | $aInfo['module'] = ($map_module_type == 'Accounts_Members') ? 'Accounts' : $map_module_type; |
||
716 | $aInfo['address'] = $_REQUEST['quick_address']; |
||
717 | $aInfo['jjwg_maps_address_c'] = $_REQUEST['quick_address']; |
||
718 | $aInfo['jjwg_maps_lat_c'] = $aInfo['lat']; |
||
719 | $aInfo['jjwg_maps_lng_c'] = $aInfo['lng']; |
||
720 | $this->bean->map_center = $this->getMarkerData($map_parent_type, $aInfo, true); |
||
721 | // Define Center Point |
||
722 | $center_lat = $aInfo['lat']; |
||
723 | $center_lng = $aInfo['lng']; |
||
724 | } |
||
725 | //var_dump($aInfo); |
||
726 | // Define $x and $y expressions |
||
727 | $x = '(69.1*((' . $this->display_object->table_name . '_cstm.jjwg_maps_lat_c)-(' . $center_lat . ')))'; |
||
728 | $y = '(53.0*((' . $this->display_object->table_name . '_cstm.jjwg_maps_lng_c)-(' . $center_lng . ')) * COS((' . $center_lat . ')/57.1))'; |
||
729 | $calc_distance_expression = 'SQRT(' . $x . '*' . $x . '+' . $y . '*' . $y . ')'; |
||
730 | if (strtolower($map_unit_type) == 'km' || strtolower($map_unit_type) == 'kilometer') { |
||
731 | $calc_distance_expression .= '*1.609'; // 1 mile = 1.609 km |
||
732 | } |
||
733 | |||
734 | // Find the Items to Display |
||
735 | // Assume there is no address at 0,0; it's in the Atlantic Ocean! |
||
736 | $where_conds = "(" . $this->display_object->table_name . "_cstm.jjwg_maps_lat_c != 0 OR " . |
||
737 | "" . $this->display_object->table_name . "_cstm.jjwg_maps_lng_c != 0) " . |
||
738 | " AND " . |
||
739 | "(" . $this->display_object->table_name . "_cstm.jjwg_maps_geocode_status_c = 'OK')" . |
||
740 | " AND " . |
||
741 | "(" . $calc_distance_expression . " < " . $map_distance . ")"; |
||
742 | $query = $this->display_object->create_new_list_query('display_object_distance', $where_conds, array(), array(), 0, '', false, $this->display_object, false); |
||
743 | // Add the disply_object_distance into SELECT list |
||
744 | $query = str_replace('SELECT ', 'SELECT (' . $calc_distance_expression . ') AS display_object_distance, ', $query); |
||
745 | if ($map_module_type == 'Contacts') { // Contacts - Account Name |
||
746 | $query = str_replace(' FROM contacts ', ' ,accounts.name AS account_name, accounts.id AS account_id FROM contacts ', $query); |
||
747 | $query = str_replace(' FROM contacts ', ' FROM contacts LEFT JOIN accounts_contacts ON contacts.id=accounts_contacts.contact_id and accounts_contacts.deleted = 0 LEFT JOIN accounts ON accounts_contacts.account_id=accounts.id AND accounts.deleted=0 ', $query); |
||
748 | } elseif ($map_module_type == 'Opportunities') { // Opps - Account Name |
||
749 | $query = str_replace(' FROM opportunities ', ' ,accounts.name AS account_name, accounts.id AS account_id FROM opportunities ', $query); |
||
750 | $query = str_replace(' FROM opportunities ', ' FROM opportunities LEFT JOIN accounts_opportunities ON opportunities.id=accounts_opportunities.opportunity_id and accounts_opportunities.deleted = 0 LEFT JOIN accounts ON accounts_opportunities.account_id=accounts.id AND accounts.deleted=0 ', $query); |
||
751 | } elseif ($map_module_type == 'Accounts_Members') { // 'Accounts_Members' is a special display type |
||
752 | $query = str_replace(' AND accounts.deleted=0', ' AND accounts.deleted=0 AND accounts.parent_id = \''.$this->bean->db->quote($map_parent_id).'\'', $query); |
||
753 | } |
||
754 | //var_dump($query); |
||
755 | $display_result = $this->bean->db->limitQuery($query, 0, $this->settings['map_markers_limit']); |
||
756 | while ($display = $this->bean->db->fetchByAssoc($display_result)) { |
||
757 | if (!empty($map_distance) && !empty($display['id'])) { |
||
758 | $marker_data_module_type = ($map_module_type == 'Accounts_Members') ? 'Accounts' : $map_module_type; |
||
759 | $marker_data = $this->getMarkerData($marker_data_module_type, $display, false, $mod_strings_display); |
||
760 | if (!empty($marker_data)) { |
||
761 | $this->bean->map_markers[] = $marker_data; |
||
762 | } |
||
763 | } |
||
764 | } |
||
765 | //var_dump($this->bean->map_markers); |
||
766 | // Next define the Custom Markers and Areas related to this Map |
||
767 | // Define relate and display objects from the necessary classes (utils.php) |
||
768 | @$markers_object = get_module_info('jjwg_Markers'); |
||
|
|||
769 | @$areas_object = get_module_info('jjwg_Areas'); |
||
770 | |||
771 | // Relationship Names: jjwg_maps_jjwg_areas and jjwg_maps_jjwg_markers |
||
772 | // Find the Related Beans: Maps to Markers |
||
773 | if (@(is_object($markers_object) && is_object($map))) { |
||
774 | $related_custom_markers = $map->get_linked_beans('jjwg_maps_jjwg_markers', 'jjwg_Markers'); |
||
775 | if ($related_custom_markers) { |
||
776 | foreach ($related_custom_markers as $marker_bean) { |
||
777 | $marker_data = $this->getMarkerDataCustom($marker_bean); |
||
778 | if (!empty($marker_data)) { |
||
779 | $this->bean->custom_markers[] = $marker_data; |
||
780 | } |
||
781 | } |
||
782 | } |
||
783 | } |
||
784 | |||
785 | // Find the Related Beans: Maps to Areas |
||
786 | if (@(is_object($areas_object) && is_object($map))) { |
||
787 | $related_custom_areas = $map->get_linked_beans('jjwg_maps_jjwg_areas', 'jjwg_Areas'); |
||
788 | if ($related_custom_areas) { |
||
789 | foreach ($related_custom_areas as $area_bean) { |
||
790 | $area_data = $this->getAreaDataCustom($area_bean); |
||
791 | if (!empty($area_data)) { |
||
792 | $this->bean->custom_areas[] = $area_data; |
||
793 | } |
||
794 | } |
||
795 | } |
||
796 | } |
||
797 | |||
798 | |||
799 | // Map Target List (ProspectLists) |
||
800 | } elseif (!empty($_REQUEST['list_id'])) { |
||
801 | |||
802 | $this->bean->map_markers = array(); |
||
803 | $this->display_object = get_module_info('ProspectLists'); |
||
804 | // Use the Export Query |
||
805 | if (!empty($_REQUEST['list_id'])) { |
||
806 | $this->display_object->retrieve($_REQUEST['list_id']); |
||
807 | if ($this->display_object->id == $_REQUEST['list_id']) { |
||
808 | $prospect_list_object = $this->display_object; |
||
809 | $list_id = $this->display_object->id; |
||
810 | } |
||
811 | } |
||
812 | |||
813 | if (!empty($list_id)) { |
||
814 | |||
815 | $list_modules = array('Accounts', 'Contacts', 'Leads', 'Users', 'Prospects'); |
||
816 | $temp_marker_groups = array(); |
||
817 | |||
818 | foreach ($list_modules as $display_module) { |
||
819 | |||
820 | $this->display_object = get_module_info($display_module); |
||
821 | $mod_strings_display = return_module_language($GLOBALS['current_language'], $this->display_object->module_name); |
||
822 | $mod_strings_display = array_merge($mod_strings_display, $GLOBALS['mod_strings']); |
||
823 | |||
824 | // Find the Items to Display |
||
825 | // Assume there is no address at 0,0; it's in the Atlantic Ocean! |
||
826 | $where_conds = "(" . $this->display_object->table_name . "_cstm.jjwg_maps_lat_c != 0 OR " . |
||
827 | "" . $this->display_object->table_name . "_cstm.jjwg_maps_lng_c != 0) " . |
||
828 | " AND " . |
||
829 | "(" . $this->display_object->table_name . "_cstm.jjwg_maps_geocode_status_c = 'OK')"; |
||
830 | $query = $this->display_object->create_new_list_query('', $where_conds, array(), array(), 0, '', false, $this->display_object, false); |
||
831 | if ($display_module == 'Contacts') { // Contacts - Account Name |
||
832 | $query = str_replace(' FROM contacts ', ' ,accounts.name AS account_name, accounts.id AS account_id FROM contacts ', $query); |
||
833 | $query = str_replace(' FROM contacts ', ' FROM contacts LEFT JOIN accounts_contacts ON contacts.id=accounts_contacts.contact_id and accounts_contacts.deleted = 0 LEFT JOIN accounts ON accounts_contacts.account_id=accounts.id AND accounts.deleted=0 ', $query); |
||
834 | } |
||
835 | // Add List JOIN |
||
836 | $query = str_replace(' FROM '.$this->display_object->table_name.' ', ' FROM '.$this->display_object->table_name.' '. |
||
837 | 'LEFT JOIN prospect_lists_prospects ON prospect_lists_prospects.related_id = '.$this->display_object->table_name.'.id AND prospect_lists_prospects.deleted=0 '. |
||
838 | 'LEFT JOIN prospect_lists ON prospect_lists_prospects.prospect_list_id = prospect_lists.id AND prospect_lists.deleted=0 ', |
||
839 | $query); |
||
840 | // Restrict WHERE to related type and $list_id |
||
841 | $query .= ' AND prospect_lists_prospects.related_type = \''.$this->display_object->module_name.'\' AND '. |
||
842 | 'prospect_lists.id = \''.$this->bean->db->quote($list_id).'\''; |
||
843 | //var_dump($query); |
||
844 | $display_result = $this->bean->db->limitQuery($query, 0, $this->settings['map_markers_limit']); |
||
845 | $display_type_found = false; |
||
846 | while ($display = $this->bean->db->fetchByAssoc($display_result)) { |
||
847 | if (!empty($display['id'])) { |
||
848 | $marker_data = $this->getMarkerData($display_module, $display, false, $mod_strings_display); |
||
849 | $marker_data['group'] = $GLOBALS['app_list_strings']['moduleList'][$display_module]; |
||
850 | if (!empty($marker_data)) { |
||
851 | $this->bean->map_markers[] = $marker_data; |
||
852 | } |
||
853 | $display_type_found = true; |
||
854 | } |
||
855 | } |
||
856 | if ($display_type_found) { |
||
857 | $temp_marker_groups[] = $GLOBALS['app_list_strings']['moduleList'][$display_module]; |
||
858 | } |
||
859 | |||
860 | } |
||
861 | |||
862 | $this->bean->map_markers_groups = $temp_marker_groups; |
||
863 | } |
||
864 | |||
865 | |||
866 | // Map Records |
||
867 | } elseif (!empty($_REQUEST['uid']) || !empty($_REQUEST['current_post'])) { |
||
868 | |||
869 | if (in_array($_REQUEST['display_module'], $this->settings['valid_geocode_modules'])) { |
||
870 | $display_module = $_REQUEST['display_module']; |
||
871 | } else { |
||
872 | $display_module = 'Accounts'; |
||
873 | } |
||
874 | if ($_REQUEST['current_post'] == 'session') { |
||
875 | $current_post = $_SESSION['jjwg_Maps']['current_post']; |
||
876 | } else { |
||
877 | $current_post = $_REQUEST['current_post']; |
||
878 | } |
||
879 | $query = ''; |
||
880 | $selected_query = ''; |
||
881 | $records = array(); |
||
882 | $order_by = ''; |
||
883 | |||
884 | $this->display_object = get_module_info($display_module); |
||
885 | $mod_strings_display = return_module_language($GLOBALS['current_language'], $this->display_object->module_name); |
||
886 | $mod_strings_display = array_merge($mod_strings_display, $GLOBALS['mod_strings']); |
||
887 | |||
888 | if (!empty($_REQUEST['uid'])) { |
||
889 | // Several records selected or this page |
||
890 | $records = explode(',', $_REQUEST['uid']); |
||
891 | } elseif (!empty($current_post)) { |
||
892 | // Select all records (advanced search) |
||
893 | $search_array = generateSearchWhere($display_module, $current_post); |
||
894 | //var_dump($search_array); |
||
895 | if (!empty($search_array['where'])) { |
||
896 | // Related Field Bug: Get relate/link patched 'where' and 'join' |
||
897 | @$ret_array = create_export_query_relate_link_patch($display_module, $search_array['searchFields'], $search_array['where']); |
||
898 | if(!empty($ret_array['join'])) { |
||
899 | @$selected_query = $this->display_object->create_export_query($order_by, $ret_array['where'], $ret_array['join']); |
||
900 | } else { |
||
901 | @$selected_query = $this->display_object->create_export_query($order_by, $ret_array['where']); |
||
902 | } |
||
903 | // SugarOnDemand JOIN Bug: If $ret_array['join'] is not included in query, force it in! |
||
904 | if (strpos($ret_array['join'], $selected_query) === false) { |
||
905 | $selected_query = str_replace(' where ', $ret_array['join'].' where ', $selected_query); |
||
906 | } |
||
907 | // Avoiding subquery. Let's just record the record ID's for later |
||
908 | $selected_result = $this->bean->db->limitQuery($selected_query, 0, $this->settings['map_markers_limit']); |
||
909 | while ($display = $this->bean->db->fetchByAssoc($selected_result)) { |
||
910 | $records[] = $display['id']; |
||
911 | } |
||
912 | } |
||
913 | } |
||
914 | //var_dump($records); |
||
915 | |||
916 | // Find the Items to Display |
||
917 | // Assume there is no address at 0,0; it's in the Atlantic Ocean! |
||
918 | $where_conds = "(" . $this->display_object->table_name . "_cstm.jjwg_maps_lat_c != 0 OR " . |
||
919 | "" . $this->display_object->table_name . "_cstm.jjwg_maps_lng_c != 0) " . |
||
920 | " AND " . |
||
921 | "(" . $this->display_object->table_name . "_cstm.jjwg_maps_geocode_status_c = 'OK')"; |
||
922 | $query = $this->display_object->create_new_list_query('', $where_conds, array(), array(), 0, '', false, $this->display_object, false); |
||
923 | if ($display_module == 'Contacts') { // Contacts - Account Name |
||
924 | $query = str_replace(' FROM contacts ', ' ,accounts.name AS account_name, accounts.id AS account_id FROM contacts ', $query); |
||
925 | $query = str_replace(' FROM contacts ', ' FROM contacts LEFT JOIN accounts_contacts ON contacts.id=accounts_contacts.contact_id and accounts_contacts.deleted = 0 LEFT JOIN accounts ON accounts_contacts.account_id=accounts.id AND accounts.deleted=0 ', $query); |
||
926 | } elseif ($display_module == 'Opportunities') { // Opps - Account Name |
||
927 | $query = str_replace(' FROM opportunities ', ' ,accounts.name AS account_name, accounts.id AS account_id FROM opportunities ', $query); |
||
928 | $query = str_replace(' FROM opportunities ', ' FROM opportunities LEFT JOIN accounts_opportunities ON opportunities.id=accounts_opportunities.opportunity_id and accounts_opportunities.deleted = 0 LEFT JOIN accounts ON accounts_opportunities.account_id=accounts.id AND accounts.deleted=0 ', $query); |
||
929 | } |
||
930 | //var_dump($query); |
||
931 | |||
932 | $display_result = $this->bean->db->limitQuery($query, 0, $this->settings['map_markers_limit']); |
||
933 | $this->bean->map_markers = array(); |
||
934 | while ($display = $this->bean->db->fetchByAssoc($display_result)) { |
||
935 | if (!empty($search_array['where'])) { // Select all records (advanced search) with where clause |
||
936 | if (in_array($display['id'], $records)) { |
||
937 | $this->bean->map_markers[] = $this->getMarkerData($display_module, $display, false, $mod_strings_display); |
||
938 | } |
||
939 | } elseif (!empty($_REQUEST['uid'])) { // Several records selected or this page selected |
||
940 | if (in_array($display['id'], $records)) { |
||
941 | $this->bean->map_markers[] = $this->getMarkerData($display_module, $display, false, $mod_strings_display); |
||
942 | } |
||
943 | } else { // All |
||
944 | $this->bean->map_markers[] = $this->getMarkerData($display_module, $display, false, $mod_strings_display); |
||
945 | } |
||
946 | } |
||
947 | } |
||
948 | |||
949 | // Sort marker groups for the view |
||
950 | sort($this->bean->map_markers_groups); |
||
951 | |||
952 | // Set display object for later use |
||
953 | $this->bean->display_object = $this->display_object; |
||
954 | |||
955 | // Get Prospect List Array Dropdown |
||
956 | $list = get_module_info('ProspectLists'); |
||
957 | $list_query = $list->create_list_query('prospect_lists.name', '1=1', 0); |
||
958 | $list_result = $list->db->query($list_query); |
||
959 | $list_array = array(); |
||
960 | while ($alist = $list->db->fetchByAssoc($list_result)) { |
||
961 | if (!empty($alist['name']) && !empty($alist['id'])) { |
||
962 | $list_array[$alist['id']] = $alist['name']; |
||
963 | } |
||
964 | } |
||
965 | $this->bean->list_array = $list_array; |
||
966 | |||
967 | } |
||
968 | |||
1200 |
If you suppress an error, we recommend checking for the error condition explicitly: