       1:    2:    3:    4:    5:    6:    7:    8:    9:   10:   11:   12:   13:   14:   15:   16:   17:   18:   19:   20:   21:   22:   23:   24:   25:   26:   27:   28:   29:   30:   31:   32:   33:   34:   35:   36:   37:   38:   39:   40:   41:   42:   43:   44:   45:   46:   47:   48:   49:   50:   51:   52:   53:   54:   55:   56:   57:   58:   59:   60:   61:   62:   63:   64:   65:   66:   67:   68:   69:   70:   71:   72:   73:   74:   75:   76:   77:   78:   79:   80:   81:   82:   83:   84:   85:   86:   87:   88:   89:   90:   91:   92:   93:   94:   95:   96:   97:   98:   99:  100:  101:  102:  103:  104:  105:  106:  107:  108:  109:  110:  111:  112:  113:  114:  115:  116:  117:  118:  119:  120:  121:  122:  123:  124:  125:  126:  127:  128:  129:  130:  131:  132:  133:  134:  135:  136:  137:  138:  139:  140:  141:  142:  143:  144:  145:  146:  147:  148:  149:  150:  151:  152:  153:  154:  155:  156:  157:  158:  159:  160:  161:  162:  163:  164:  165:  166:  167:  168:  169:  170:  171:  172:  173:  174:  175:  176:  177:  178:  179:  180:  181:  182:  183:  184:  185:  186:  187:  188:  189:  190:  191:  192:  193:  194:  195:  196:  197:  198:  199:  200:  201:  202:  203:  204:  205:  206:  207:  208:  209:  210:  211:  212:  213:  214:  215:  216:  217:  218:  219:  220:  221:  222:  223:  224:  225:  226:  227:  228:  229:  230:  231:  232:  233:  234:  235:  236:  237:  238:  239:  240:  241:  242:  243:  244:  245:  246:  247:  248:  249:  250:  251:  252:  253:  254:  255:  256:  257:  258:  259:  260:  261:  262:  263:  264:  265:  266:  267:  268:  269:  270:  271:  272:  273:  274:  275:  276:  277:  278:  279:  280:  281:  282:  283:  284:  285:  286:  287:  288:  289:  290:  291:  292:  293:  294:  295:  296:  297:  298:  299:  300:  301:  302:  303:  304:  305:  306:  307:  308:  309:  310:  311:  312:  313:  314:  315:  316:  317:  318:  319:  320:  321:  322:  323:  324:  325:  326:  327:  328:  329:  330:  331:  332:  333:  334:  335:  336:  337:  338:  339:  340:  341:  342:  343:  344:  345:  346:  347:  348:  349:  350:  351:  352:  353:  354:  355:  356:  357:  358:  359:  360:  361:  362:  363:  364:  365:  366:  367:  368:  369:  370:  371:  372:  373:  374:  375:  376:  377:  378:  379:  380:  381:  382:  383:  384:  385:  386:  387:  388:  389:  390:  391:  392:  393:  394:  395:  396:  397:  398:  399:  400:  401:  402:  403:  404:  405:  406:  407:  408:  409:  410:  411:  412:  413:  414:  415:  416:  417:  418:  419:  420:  421:  422:  423:  424:  425:  426:  427:  428:  429:  430:  431:  432:  433:  434:  435:  436:  437:  438:  439:  440:  441:  442:  443:  444:  445:  446:  447:  448:  449:  450:  451:  452:  453:  454:  455:  456:  457:  458:  459:  460:  461:  462:  463:  464:  465:  466:  467:  468:  469:  470:  471:  472:  473:  474:  475:  476:  477:  478:  479:  480:  481:  482:  483:  484:  485:  486:  487:  488:  489:  490:  491:  492:  493:  494:  495:  496:  497:  498:  499:  500:  501:  502:  503:  504:  505:  506:  507:  508:  509:  510:  511:  512:  513:  514:  515:  516:  517:  518:  519:  520:  521:  522:  523:  524:  525:  526:  527:  528:  529:  530:  531:  532:  533:  534:  535:  536:  537:  538:  539:  540:  541:  542:  543:  544:  545:  546:  547:  548:  549:  550:  551:  552:  553:  554:  555:  556:  557:  558:  559:  560:  561:  562:  563:  564:  565:  566:  567:  568:  569:  570:  571:  572:  573:  574:  575:  576:  577:  578:  579:  580:  581:  582:  583:  584:  585:  586:  587:  588:  589:  590:  591:  592:  593:  594:  595:  596:  597:  598:  599:  600:  601:  602:  603:  604:  605:  606:  607:  608:  609:  610:  611:  612:  613:  614:  615:  616:  617:  618:  619:  620:  621:  622:  623:  624:  625:  626:  627:  628:  629:  630:  631:  632:  633:  634:  635:  636:  637:  638:  639:  640:  641:  642:  643:  644:  645:  646:  647:  648:  649:  650:  651:  652:  653:  654:  655:  656:  657:  658:  659:  660:  661:  662:  663:  664:  665:  666:  667:  668:  669:  670:  671:  672:  673:  674:  675:  676:  677:  678:  679:  680:  681:  682:  683:  684:  685:  686:  687:  688:  689:  690:  691:  692:  693:  694:  695:  696:  697:  698:  699:  700:  701:  702:  703:  704:  705:  706:  707:  708:  709:  710:  711:  712:  713:  714:  715:  716:  717:  718:  719:  720:  721:  722:  723:  724:  725:  726:  727:  728:  729:  730:  731:  732:  733:  734:  735:  736:  737:  738:  739:  740:  741:  742:  743:  744:  745:  746:  747:  748:  749:  750:  751:  752:  753:  754:  755:  756:  757:  758:  759:  760:  761:  762:  763:  764:  765:  766:  767:  768:  769:  770:  771:  772:  773:  774:  775:  776:  777:  778:  779:  780:  781:  782:  783:  784:  785:  786:  787:  788:  789:  790:  791:  792:  793:  794:  795:  796:  797:  798:  799:  800:  801:  802:  803:  804:  805:  806:  807:  808:  809:  810:  811:  812:  813:  814:  815:  816:  817:  818:  819:  820:  821:  822:  823:  824:  825:  826:  827:  828:  829:  830:  831:  832:  833:  834:  835:  836:  837:  838:  839:  840:  841:  842:  843:  844:  845:  846:  847:  848:  849:  850:  851:  852:  853:  854:  855:  856:  857:  858:  859:  860:  861:  862:  863:  864:  865:  866:  867:  868:  869:  870:  871:  872:  873:  874:  875:  876:  877:  878:  879:  880:  881:  882:  883:  884:  885:  886:  887:  888:  889:  890:  891:  892:  893:  894:  895:  896:  897:  898:  899:  900:  901:  902:  903:  904:  905:  906:  907:  908:  909:  910:  911:  912:  913:  914:  915:  916:  917:  918:  919:  920:  921:  922:  923:  924:  925:  926:  927:  928:  929:  930:  931:  932:  933:  934:  935:  936:  937:  938:  939:  940:  941:  942:  943:  944:  945:  946:  947:  948:  949:  950:  951:  952:  953:  954:  955:  956:  957:  958:  959:  960:  961:  962:  963:  964:  965:  966:  967:  968:  969:  970:  971:  972:  973:  974:  975:  976:  977:  978:  979:  980:  981:  982:  983:  984:  985:  986:  987:  988:  989:  990:  991:  992:  993:  994:  995:  996:  997:  998:  999: 1000: 1001: 1002: 1003: 1004: 1005: 1006: 1007: 1008: 1009: 1010: 1011: 1012: 1013: 1014: 1015: 1016: 1017: 1018: 1019: 1020: 1021: 1022: 1023: 1024: 1025: 1026: 1027: 1028: 1029: 1030: 1031: 1032: 1033: 1034: 1035: 1036: 1037: 1038: 1039: 1040: 1041: 1042: 1043: 1044: 1045: 1046: 1047: 1048: 1049: 1050: 1051: 1052: 1053: 1054: 1055: 1056: 1057: 1058: 1059: 1060: 1061: 1062: 1063: 1064: 1065: 1066: 1067: 1068: 1069: 1070: 1071: 1072: 1073: 1074: 1075: 1076: 1077: 1078: 1079: 1080: 1081: 1082: 1083: 1084: 1085: 1086: 1087: 1088: 1089: 1090: 1091: 1092: 1093: 1094: 1095: 1096: 1097: 1098: 1099: 1100: 1101: 1102: 1103: 1104: 1105: 1106: 1107: 1108: 1109: 1110: 1111: 1112: 1113: 1114: 1115: 1116: 1117: 1118: 1119: 1120: 1121: 1122: 1123: 1124: 1125: 1126: 1127: 1128: 1129: 1130: 1131: 1132: 1133: 1134: 1135: 1136: 1137: 1138: 1139: 1140: 1141: 1142: 1143: 1144: 1145: 1146: 1147: 1148: 1149: 1150: 1151: 1152: 1153: 1154: 1155: 1156: 1157: 1158: 1159: 1160: 1161: 1162: 1163: 1164: 1165: 1166: 1167: 1168: 1169: 1170: 1171: 1172: 1173: 1174: 1175: 1176: 1177: 1178: 1179: 1180: 1181: 1182: 1183: 1184: 1185: 1186: 1187: 1188: 1189: 1190: 1191: 1192: 1193: 1194: 1195: 1196: 1197: 1198: 1199: 1200: 1201: 1202: 1203: 1204: 1205: 1206: 1207: 1208: 1209: 1210: 1211: 1212: 1213: 1214: 1215: 1216: 1217: 1218: 1219: 1220: 1221: 1222: 1223: 1224: 1225: 1226: 1227: 1228: 1229: 1230: 1231: 1232: 1233: 1234: 1235: 1236: 1237: 1238: 1239: 1240: 1241: 1242: 1243: 1244: 1245: 1246: 1247: 1248: 1249: 1250: 1251: 1252: 1253: 1254: 1255: 1256: 1257: 1258: 1259: 1260: 1261: 1262: 1263: 1264: 1265: 1266: 1267: 1268: 1269: 1270: 1271: 1272: 1273: 1274: 1275: 1276: 1277: 1278: 1279: 1280: 1281: 1282: 1283: 1284: 1285: 1286: 1287: 1288: 1289: 1290: 1291: 1292: 1293: 1294: 1295: 1296: 1297: 1298: 1299: 1300: 1301: 1302: 1303: 1304: 1305: 1306: 1307: 1308: 1309: 1310: 1311: 1312: 1313: 1314: 1315: 1316: 1317: 1318: 1319: 1320: 1321: 1322: 1323: 1324: 1325: 1326: 1327: 1328: 1329: 1330: 1331: 1332: 1333: 1334: 1335: 1336: 1337: 1338: 1339: 1340: 1341: 1342: 1343: 1344: 1345: 1346: 1347: 1348: 1349: 1350: 1351: 1352: 1353: 1354: 1355: 1356: 1357: 1358: 1359: 1360: 1361: 1362: 1363: 1364: 1365: 1366: 1367: 1368: 1369: 1370: 1371: 1372: 1373: 1374: 1375: 1376: 1377: 1378: 1379: 1380: 1381: 1382: 1383: 1384: 1385: 1386: 1387: 1388:

    <?php

    require_once(plugin_dir_path(__FILE__) . 'class.marker-library-dialog.php');

    /*
    Marker Category functionality for WP Google Maps Pro

    */

    function wpgmaps_menu_category_layout() {

        if (!isset($_GET['action'])) {

            if (function_exists('wpgmza_register_pro_version')) {
                echo"<div class=\"wrap\"><div id=\"icon-edit\" class=\"icon32 icon32-posts-post\"><br></div><h2>".__("Marker Categories","wp-google-maps")." <a href=\"admin.php?page=wp-google-maps-menu-categories&action=new\" class=\"add-new-h2\">".__("Add New Category","wp-google-maps")."</a></h2>";
                wpgmaps_list_categories();
            } else {
                echo"<div class=\"wrap\"><div id=\"icon-edit\" class=\"icon32 icon32-posts-post\"><br></div><h2>".__("Marker Categories","wp-google-maps")."</h2>";
                echo"<p><i><a href='http://www.wpgmaps.com/purchase-professional-version/?utm_source=plugin&utm_medium=link&utm_campaign=category' title='".__("Pro Version","wp-google-maps")."'>".__("Create marker categories","wp-google-maps")."</a> ".__("with the","wp-google-maps")." <a href='http://www.wpgmaps.com/purchase-professional-version/?utm_source=plugin&utm_medium=link&utm_campaign=category' title='Pro Version'>".__("Pro Version","wp-google-maps")."</a> ".__("of WP Google Maps for only","wp-google-maps")." <strong>$14.99!</strong></i></p>";

            }
            echo "</div>";
            echo"<br /><div style='float:right;'><a href='http://www.wpgmaps.com/documentation/troubleshooting/' title='WP Google Maps Troubleshooting Section'>".__("Problems with the plugin? See the troubleshooting manual.","wp-google-maps")."</a></div>";
        } else {

            if ($_GET['action'] == "trash" && isset($_GET['cat_id'])) {
                if (isset($_GET['s']) && $_GET['s'] == "1") {
                    if (wpgmaps_trash_cat($_GET['cat_id'])) {
                        echo "<script>window.location = \"".get_option('siteurl')."/wp-admin/admin.php?page=wp-google-maps-menu-categories\"</script>";
                    } else {
                        _e("There was a problem deleting the category.");;
                    }
                } else {
                    echo "<h2>".__("Delete your Category","wp-google-maps")."</h2><p>".__("Are you sure you want to delete the category","wp-google-maps")."? <br /><a href='?page=wp-google-maps-menu-categories&action=trash&cat_id=".$_GET['cat_id']."&s=1'>".__("Yes","wp-google-maps")."</a> | <a href='?page=wp-google-maps-menu-categories'>".__("No","wp-google-maps")."</a></p>";
                }

            }

            if ($_GET['action'] == "new") {
                wpgmza_pro_category_new_layout();
            }
            if ($_GET['action'] == "edit") {
                wpgmza_pro_category_edit_layout($_GET['cat_id']);
            }

        }

    }

    if (isset($_GET['page']) && $_GET['page'] == 'wp-google-maps-menu-categories') {
        add_action('admin_print_scripts', 'wpgmaps_admin_category_scripts');
        add_action('admin_print_styles', 'wpgmaps_admin_category_styles');
    }
    function wpgmaps_admin_category_scripts() {
        wp_enqueue_script( 'jquery' );
        wp_enqueue_script('jquery-ui-core');

        if (function_exists('wp_enqueue_media')) {
            wp_enqueue_media();
            //wp_register_script('my-wpgmaps-upload', plugins_url('js/category_media.js', __FILE__), array('jquery'), '1.0', true);
            wp_register_script('my-wpgmaps-upload', plugin_dir_url(__DIR__) . 'js/category_media.js');
            wp_enqueue_script('my-wpgmaps-upload');
        } else {
            wp_enqueue_script('media-upload');
            wp_enqueue_script('thickbox');
            wp_register_script('my-wpgmaps-upload', WP_PLUGIN_URL.'/'.plugin_basename(dirname(__FILE__)).'/js/admin_category.js', array('jquery','media-upload','thickbox'));
            wp_enqueue_script('my-wpgmaps-upload');
        }

    }
    function wpgmaps_admin_category_styles() {

    }

    function wpgmza_pro_category_new_layout() {

        // DB Fix
        global $wpdb;
        if(!$wpdb->get_results("SHOW COLUMNS FROM {$wpdb->prefix}wpgmza_categories LIKE 'parent'"))
            $wpdb->query("ALTER TABLE {$wpdb->prefix}wpgmza_categories ADD COLUMN parent int(11)");

        $markerLibraryDialog = new WPGMZA\MarkerLibraryDialog();
        $markerLibraryDialog->html();

        $display_marker = "<img src=\"".wpgmaps_get_plugin_url()."/images/marker.png\" />";
        $map_ids = wpgmza_return_all_map_ids();

        echo "<div class='wrap'>";
        echo "  <h1>WP Google Maps</h1>";
        echo "  <div class='wide'>";
        echo "      <h2>".__("Add a Marker Category","wp-google-maps")."</h2>";
        echo "      <form action='admin.php?page=wp-google-maps-menu-categories' method='post' id='wpgmaps_add_marker_category' name='wpgmaps_add_marker_category_form'>";
        echo "      <table class='wpgmza-listing-comp'>";
        echo "          <tr>";
        echo "              <td><strong>".__("Category Name","wp-google-maps")."</strong>:</td>";
        echo "              <td><input type='text' name='wpgmaps_marker_category_name' id='wpgmaps_marker_category_name' value='' /></td>";
        echo "              <td></td>";
        echo "          </tr>";
        echo "          <tr style='height:20px;'>";
        echo "              <td></td>";
        echo "              <td></td>";
        echo "              <td></td>";
        echo "          </tr>";
        echo "          <tr valign='top'>";
        echo "              <td valign='middle'><strong>".__("Category Marker","wp-google-maps")."</strong>:</td>";
        echo "              <td align='left'><span id=\"wpgmza_mm\">$display_marker</span>";
        echo "              Enter URL <input id=\"upload_default_category_marker\" name=\"upload_default_category_marker\" type='text' size='35' class='regular-text' maxlength='700' value='' /> or <input class='wpgmza_general_btn' id=\"upload_default_category_marker_btn\" type=\"button\" value=\"".__("Upload Image","wp-google-maps")."\" /> ";
        ?>
        <input
            class="wpgmza_general_btn wpgmza-marker-library"
            data-target-name="upload_default_category_marker"
            value="<?php _e('Marker Library', 'wp-google-maps'); ?>"/>
        <?php
        echo "<a href=\"javascript:void(0);\" onClick=\"document.forms['wpgmaps_add_marker_category_form'].upload_default_category_marker.value = ''; var span = document.getElementById('wpgmza_mm'); while( span.firstChild ) { span.removeChild( span.firstChild ); } span.appendChild( document.createTextNode('')); return false;\" title=\"Reset to default\">-reset-</a> <small><i>

        " . __("WP Google Maps encourages you to make use of the amazing icons at ", "wp-google-maps")."<a href='https://mappity.org'>https://mappity.org</a>

        </i></small></td>";

        echo "          </tr>";

        ?>

        <tr>
            <td>
                <strong>
                    <?php _e('Category Image:', 'wp-google-maps'); ?>
                </strong>
            </td>
            <td>
                <?php _e('Enter URL', 'wp-google-maps'); ?>
                <input name="category_image"/>

                <?php _e('or', 'wp-google-maps'); ?>

                <button class="wpgmza_general_btn" type="button" data-media-dialog-target="input[name='category_image']">
                    <?php _e('Upload Image', 'wp-google-maps'); ?>
                </button>
            </td>
        </tr>

        <?php
        echo "          <tr>";
        echo "              <td><strong>".__("Retina Ready","wp-google-maps")."</strong>:</td>";
        echo "              <td><div class='switch'><input type='checkbox' class='cmn-toggle cmn-toggle-round-flat' id='wpgmaps_marker_category_retina' name='wpgmaps_marker_category_retina' value='1'><label for='wpgmaps_marker_category_retina'></label></div> ".__("This marker is a retina-ready marker","wp-google-maps")."</td>";
        echo "              <td></td>";
        echo "          </tr>";
        echo "          <tr style='height:20px;'>";
        echo "              <td></td>";
        echo "              <td></td>";
        echo "              <td></td>";
        echo "          </tr>";
        echo "          <tr>";
        echo "              <td valign='top'><strong>".__("Parent Category:","wp-google-maps")."</strong></td>";
        echo "              <td>";
        echo "                  <select name='parent_category' id='parent_category'>";
        echo "                      <option value='0'>".__( "None", "wp-google-maps" )."</option>";

        $cats = wpgmza_return_all_categories();
        if ($cats) {
            foreach ($cats as $cat) {

                $cat_id = $cat->id;
                if (isset($cat->category_name)) { $cat_name = $cat->category_name; } else { $cat_name = ""; }

                $display_cat_name = $cat_name . " (#" . $cat_id . ")";
                echo "                      <option value='".$cat_id."'>". $display_cat_name ."</option>";
            }
        }
        echo "                  </select>";
        echo "                  <p class='description'>" . __( "Optional", "wp-google-maps" ) . "</p>";
        echo "              </td>";
        echo "          </tr>";
        echo "          <tr style='height:20px;'>";
        echo "              <td></td>";
        echo "              <td></td>";
        echo "              <td></td>";
        echo "          </tr>";
        echo "          <tr>";
        echo "              <td><strong>".__("Priority","wp-google-maps")."</strong>:</td>";
        echo "              <td><input type='number' name='wpgmaps_marker_category_priority' id='wpgmaps_marker_category_priority' value='0'  step='1' /></td>";
        echo "              <td></td>";
        echo "          </tr>";
        echo "          <tr style='height:20px;'>";
        echo "              <td></td>";
        echo "              <td></td>";
        echo "              <td></td>";
        echo "          </tr>";

        echo "          <tr>";
        echo "              <td valign='top'><strong>".__("Assigned to ","wp-google-maps")."</strong>:</td>";
        echo "              <td>";
        echo "                  <div class='switch'><input class='cmn-toggle cmn-toggle-round-flat' id='map-cat-all' type='checkbox' name='assigned_to_map[]' value='ALL'><label for='map-cat-all'></label></div> All Maps <br /><br />";

        foreach ($map_ids as $map_id) {
            $map_data = wpgmza_get_map_data($map_id);
            echo "                  <div class='switch'><input class='cmn-toggle cmn-toggle-round-flat' type='checkbox' id='map-cat-".$map_id."' name='assigned_to_map[]' value='".$map_id."'> <label for='map-cat-".$map_id."'></label></div>".$map_data->map_title."  (#".$map_id.")<br />";
        }
        echo "              </td>";
        echo "          </tr>";

        echo "      </table>";

        echo "          <p class='submit'><input type='submit' name='wpgmza_save_marker_category' class='button-primary' value='".__("Save Category","wp-google-maps")." &raquo;' /></p>";
        echo "      </form>";
        echo "  </div>";
        echo "</div>";

    }
    function wpgmza_pro_category_edit_layout($cat_id) {

        global $wpdb;
        global $wpgmza_tblname_categories;

        $markerLibraryDialog = new WPGMZA\MarkerLibraryDialog();
        $markerLibraryDialog->html();

        $map_ids = wpgmza_return_all_map_ids();

        $cat_id = (int)$cat_id;

        $results = $wpdb->get_results("
          SELECT *
          FROM $wpgmza_tblname_categories
          WHERE `id` = '$cat_id' LIMIT 1
        ");

        $category = $results[0];

        if (isset($results[0]->category_icon) && $results[0]->category_icon != '') {
            $display_marker = "<img src='".$results[0]->category_icon."' />";
            $display_url = $results[0]->category_icon;
        } else {
            $display_marker = "<img src=\"".wpgmaps_get_plugin_url()."/images/marker.png\" />";
            $display_url = "";

        }

        if (isset($results[0]->retina) && intval($results[0]->retina) == 1) {
            $retina_checked = "checked='checked'";
        } else {
            $retina_checked = "";
        }

        if (isset($results[0]->parent) && $results[0]->parent > 0) {
            $cat_parent_selected = $results[0]->parent;
        } else {
            $cat_parent_selected = 0;
        }

        echo "<div class='wrap'>";
        echo "  <h1>WP Google Maps</h1>";
        echo "  <div class='wide'>";
        echo "      <h2>".__("Edit a Marker Category","wp-google-maps")."</h2>";
        echo "      <form action='admin.php?page=wp-google-maps-menu-categories' method='post' id='wpgmaps_add_marker_category' name='wpgmaps_edit_marker_category_form'>";

        echo "      <table class='wpgmza-listing-comp'>";
        echo "          <tr>";
        echo "              <td><strong>".__("Category Name","wp-google-maps")."</strong>:</td>";
        echo "              <td><input type='hidden' name='wpgmaps_marker_category_id' id='wpgmaps_marker_category_id' value='".$results[0]->id."' /><input type='text' name='wpgmaps_marker_category_name' id='wpgmaps_marker_category_name' value='".$results[0]->category_name."' /></td>";
        echo "              <td></td>";
        echo "          </tr>";
        echo "          <tr style='height:20px;'>";
        echo "              <td></td>";
        echo "              <td></td>";
        echo "              <td></td>";
        echo "          </tr>";
        echo "          <tr valign='top'>";
        echo "              <td valign='middle'><strong>".__("Category Marker","wp-google-maps")."</strong>:</td>";
        echo "              <td align='left'><span id=\"wpgmza_mm\">$display_marker</span> </td>";
        echo "              <td valign='middle'>Enter URL <input id=\"upload_default_category_marker\" name=\"upload_default_category_marker\" type='text' size='35' class='regular-text' maxlength='700' value='$display_url' /> or <input class='wpgmza_general_btn' id=\"upload_default_category_marker_btn\" type=\"button\" value=\"".__("Upload Image","wp-google-maps")."\" /> ";

        ?>
        <input
            class="wpgmza_general_btn wpgmza-marker-library"
            data-target-name="upload_default_category_marker"
            value="<?php _e('Marker Library', 'wp-google-maps'); ?>"/>
        <?php

        echo "<a href=\"javascript:void(0);\" onClick=\"document.forms['wpgmaps_edit_marker_category_form'].upload_default_category_marker.value = ''; var span = document.getElementById('wpgmza_mm'); while( span.firstChild ) { span.removeChild( span.firstChild ); } span.appendChild( document.createTextNode('')); return false;\" title=\"Reset to default\">-reset-</a> <small><i>".__("Get great map markers <a href='http://mapicons.nicolasmollet.com/' target='_BLANK' title='Great Google Map Markers'>here</a>","wp-google-maps")."</i></small></td>";
        echo "          </tr>";

        ?>
        <tr>
            <td>
                <strong>
                    <?php _e('Category Image:', 'wp-google-maps'); ?>
                </strong>
            </td>
            <td>
                <?php _e('Enter URL', 'wp-google-maps'); ?>
                <input name="category_image" value="<?php echo htmlspecialchars($category->image); ?>"/>

                <?php _e('or', 'wp-google-maps'); ?>

                <button class="wpgmza_general_btn" type="button" data-media-dialog-target="input[name='category_image']">
                    <?php _e('Upload Image', 'wp-google-maps'); ?>
                </button>
            </td>
        </tr>
        <?php

        echo "          <tr>";
        echo "              <td><strong>".__("Retina Ready","wp-google-maps")."</strong>:</td>";
        echo "              <td><div class='switch'><input type='checkbox' class='cmn-toggle cmn-toggle-round-flat' id='wpgmaps_marker_category_retina' name='wpgmaps_marker_category_retina' value='1' $retina_checked><label for='wpgmaps_marker_category_retina'></label></div> ".__("This marker is a retina-ready marker","wp-google-maps")."</td>";
        echo "              <td></td>";
        echo "          </tr>";
        echo "          <tr style='height:20px;'>";
        echo "              <td></td>";
        echo "              <td></td>";
        echo "              <td></td>";
        echo "          </tr>";

        echo "          <tr>";
        echo "              <td valign='top'><strong>".__("Parent Category:","wp-google-maps")."</strong></td>";
        echo "              <td>";
        echo "                  <select name='parent_category' id='parent_category'>";
        echo "                      <option value='0'>".__( "None", "wp-google-maps" )."</option>";

        $cats = wpgmza_return_all_categories();
        if ($cats) {
            foreach ($cats as $cat) {

                $current_cat_id = $cat->id;
                if (isset($cat->category_name)) { $cat_name = $cat->category_name; } else { $cat_name = ""; }

                /* do not show if this is the same category id as the one we are editing - cannot be a parent of iteself... */
                if ($cat_id !== $current_cat_id) {
                    $display_cat_name = $cat_name . " (#" . $current_cat_id . ")";
                    if ($cat_parent_selected == $current_cat_id) {
                        echo "                      <option value='".$current_cat_id."' selected>". $display_cat_name ."</option>";
                    } else {
                        echo "                      <option value='".$current_cat_id."'>". $display_cat_name ."</option>";

                    }
                }
            }
        }
        echo "                  </select>";
        echo "                  <p class='description'>" . __( "Optional", "wp-google-maps" ) . "</p>";
        echo "              </td>";
        echo "          </tr>";
        echo "          <tr style='height:20px;'>";
        echo "              <td></td>";
        echo "              <td></td>";
        echo "              <td></td>";
        echo "          </tr>";
        echo "          <tr>";
        echo "              <td><strong>".__("Priority","wp-google-maps")."</strong>:</td>";
        echo "              <td><input type='number' name='wpgmaps_marker_category_priority' id='wpgmaps_marker_category_priority' value='".$results[0]->priority."'  step='1' /></td>";
        echo "              <td></td>";
        echo "          </tr>";
        echo "          <tr style='height:20px;'>";
        echo "              <td></td>";
        echo "              <td></td>";
        echo "              <td></td>";
        echo "          </tr>";

        echo "          <tr>";
        echo "              <td valign='top'><strong>".__("Assigned to ","wp-google-maps")."</strong>:</td>";
        echo "              <td>";
        echo "                  <div class='switch'><input class='cmn-toggle cmn-toggle-round-flat' id='map-cat-all' type='checkbox' name='assigned_to_map[]' value='ALL' ".wpgmza_check_cat_map('ALL',$cat_id)."><label for='map-cat-all'></label></div> All Maps <br /><br />";

        foreach ($map_ids as $map_id) {
            $map_data = wpgmza_get_map_data($map_id);
            echo "                   <div class='switch'><input class='cmn-toggle cmn-toggle-round-flat' type='checkbox' id='map-cat-".$map_id."' name='assigned_to_map[]' value='".$map_id."' ".wpgmza_check_cat_map($map_id,$cat_id)."> <label for='map-cat-".$map_id."'></label></div>".$map_data->map_title."  (id ".$map_id.")<br />";
        }
        echo "              </td>";
        echo "          </tr>";

        echo "      </table>";

        echo "          <p class='submit'><input type='submit' name='wpgmza_edit_marker_category' class='button-primary' value='".__("Save Category","wp-google-maps")." &raquo;' /></p>";
        echo "      </form>";
        echo "  </div>";
        echo "</div>";

    }

    function wpgmza_check_cat_map($map_id,$cat_id) {
        global $wpdb;
        global $wpgmza_tblname_category_maps;

        $map_id = (int)$map_id;
        $cat_id = (int)$cat_id;

        if ($map_id == "ALL") {
            $sql = "SELECT COUNT(*) FROM `".$wpgmza_tblname_category_maps."` WHERE `cat_id` = '$cat_id' AND `map_id` = '0' LIMIT 1";
        } else {
            $sql = "SELECT COUNT(*) FROM `".$wpgmza_tblname_category_maps."` WHERE `cat_id` = '$cat_id' AND `map_id` = '$map_id' LIMIT 1";
        }
        $results = $wpdb->get_var($sql);
        if ($results>0) { return "checked"; } else { return ""; }
    }

    add_action('admin_head', 'wpgmaps_category_head');
    function wpgmaps_category_head() {

        global $wpdb;
        global $wpgmza_tblname_categories;
        global $wpgmza_tblname_category_maps;

        if(empty($wpgmza_tblname_categories))
            return;

        $columns = $wpdb->get_col("SHOW COLUMNS FROM $wpgmza_tblname_categories");

        if(array_search('image', $columns) === false)
            $wpdb->query("ALTER TABLE $wpgmza_tblname_categories ADD COLUMN image VARCHAR(512)");

        if(array_search('parent', $columns) === false)
            $wpdb->query("ALTER TABLE $wpgmza_tblname_categories ADD COLUMN parent INT(11) NOT NULL DEFAULT 0");

        if(array_search('priority', $columns) === false)
            $wpdb->query("ALTER TABLE $wpgmza_tblname_categories ADD COLUMN priority INT(11) NOT NULL DEFAULT 0");

        if (isset($_GET['page']) && $_GET['page'] == "wp-google-maps-menu-categories" && isset($_POST['wpgmza_save_marker_category'])) {
            if (isset($_POST['wpgmza_save_marker_category'])){

                $wpgmaps_category_name = esc_attr($_POST['wpgmaps_marker_category_name']);
                $wpgmaps_category_marker = esc_attr($_POST['upload_default_category_marker']);
                $wpgmza_category_image = esc_attr($_POST['category_image']);

                if (isset($_POST['wpgmaps_marker_category_retina'])) { $wpgmaps_category_retina = sanitize_text_field( $_POST['wpgmaps_marker_category_retina'] ); } else { $wpgmaps_category_retina = 0; }

                if ( !isset( $_POST['assigned_to_map'] ) ) { $_POST['assigned_to_map'][0] = __( "All", "wp-google-maps" ); }

                if ( !isset( $_POST['parent_category'] ) ) { $cat_parent = 0; } else { $cat_parent = intval( sanitize_text_field( $_POST['parent_category'] ) );}

                $cat_priority = isset( $_POST['wpgmaps_marker_category_priority'] ) ? intval( $_POST['wpgmaps_marker_category_priority'] ) : 0;

                $rows_affected = $wpdb->query( $wpdb->prepare(
                        "INSERT INTO $wpgmza_tblname_categories SET
                            category_name = %s,
                            active = %d,
                            category_icon = %s,
                            image = %s,
                            retina = %d,
                            parent = %d,
                            priority = %d
                        ",
                        $wpgmaps_category_name,
                        0,
                        $wpgmaps_category_marker,
                        $wpgmza_category_image,
                        intval($wpgmaps_category_retina),
                        $cat_parent,
                        $cat_priority
                    )
                );

                $cat_id = $wpdb->insert_id;

                if ($_POST['assigned_to_map'][0] == "ALL") {
                        $rows_affected = $wpdb->query( $wpdb->prepare(
                            "INSERT INTO $wpgmza_tblname_category_maps SET
                                cat_id = %d,
                                map_id = %d
                            ",
                            $cat_id,
                            0
                        )
                        );
                } else {
                    if( isset( $_POST['assigned_to_map'] ) ){
                        foreach ($_POST['assigned_to_map'] as $assigned_map) {

                            $rows_affected = $wpdb->query( $wpdb->prepare(
                                "INSERT INTO $wpgmza_tblname_category_maps SET
                                    cat_id = %d,
                                    map_id = %d
                                ",
                                $cat_id,
                                $assigned_map
                            )
                            );
                        }
                    }
                }
                echo "<div class='updated'>";
                _e("Your category has been created.","wp-google-maps");
                echo "</div>";

                do_action('wpgmza_categories_saved');

            }

        }
        if (isset($_GET['page']) && $_GET['page'] == "wp-google-maps-menu-categories" && isset($_POST['wpgmza_edit_marker_category'])) {

                global $wpdb;
                global $wpgmza_tblname_categories;
                global $wpgmza_tblname_category_maps;
                $wpgmaps_cid = esc_attr($_POST['wpgmaps_marker_category_id']);
                if ( !isset($_POST['wpgmaps_marker_category_name'] ) ) { $wpgmaps_category_name = "Unnamed category"; } else { $wpgmaps_category_name = esc_attr($_POST['wpgmaps_marker_category_name']); }
                if ( isset($_POST['wpgmaps_marker_category_retina'] ) ) { $wpgmaps_category_retina = esc_attr($_POST['wpgmaps_marker_category_retina']); } else { $wpgmaps_category_retina = 0; }

                if ( !isset($_POST['assigned_to_map'] ) ) { $_POST['assigned_to_map'][0] = __( "All", "wp-google-maps" ); }

                if ( !isset( $_POST['parent_category'] ) ) { $cat_parent = 0; } else { $cat_parent = intval( sanitize_text_field( $_POST['parent_category'] ) ); }

                $cat_priority = isset( $_POST['wpgmaps_marker_category_priority'] ) ? intval( $_POST['wpgmaps_marker_category_priority'] ) : 0;

                $wpgmaps_category_marker = esc_attr($_POST['upload_default_category_marker']);
                $wpgmza_category_image = esc_attr($_POST['upload_default_category_marker']);

                $rows_affected = $wpdb->query( $wpdb->prepare(
                    "DELETE FROM $wpgmza_tblname_category_maps WHERE
                    cat_id = %d"
                    ,
                    $wpgmaps_cid)
                ); // remove all instances of this category in the category_maps table

                $rows_affected = $wpdb->query( $wpdb->prepare(
                    "UPDATE $wpgmza_tblname_categories SET

                    category_name = %s,
                    active = %d,
                    category_icon = %s,
                    image = %s,
                    retina = %d,
                    parent = %d,
                    priority = %d

                    WHERE
                    id = %d",
                    $wpgmaps_category_name,
                    0,
                    $wpgmaps_category_marker,
                    $_POST['category_image'],
                    intval($wpgmaps_category_retina),
                    $cat_parent,
                    $cat_priority,
                    $wpgmaps_cid)
                );

                if ($_POST['assigned_to_map'][0] == "ALL") {
                        $rows_affected = $wpdb->query( $wpdb->prepare(
                            "INSERT INTO $wpgmza_tblname_category_maps SET
                                cat_id = %d,
                                map_id = %d
                            ",
                            $wpgmaps_cid,
                            0
                        )
                        );
                } else {

                    foreach ($_POST['assigned_to_map'] as $assigned_map) {

                        $rows_affected = $wpdb->query( $wpdb->prepare(
                            "INSERT INTO $wpgmza_tblname_category_maps SET
                                cat_id = %d,
                                map_id = %d
                            ",
                            $wpgmaps_cid,
                            $assigned_map
                        )
                        );
                    }
                }

                echo "<div class='updated'>";
                _e("Your category has been saved.","wp-google-maps");
                echo "</div>";

                do_action('wpgmza_categories_saved');
        }
    }

    function old_wpgmza_pro_return_category_select_list($map_id) {
        global $wpdb;
        global $wpgmza_tblname_categories;
        global $wpgmza_tblname_category_maps;
        $ret_msg = "";
        $ret_msg .= "<option value=\"0\">".__("All","wp-google-maps")."</option>";

        $map_id = (int)$map_id;

        $sql = "SELECT * FROM `$wpgmza_tblname_category_maps` LEFT JOIN `$wpgmza_tblname_categories` ON $wpgmza_tblname_category_maps.cat_id = $wpgmza_tblname_categories.id WHERE (`map_id` = '$map_id' OR `map_id` = '0') AND $wpgmza_tblname_categories.active = '0' ORDER BY `category_name` ASC";

        $results = $wpdb->get_results($sql);
        foreach ( $results as $result ) {
                $ret_msg .= "<option value=\"".$result->id."\">".stripslashes($result->category_name)."</option>";
        }

        return $ret_msg;

    }
    function wpgmza_pro_return_category_select_list($map_id) {
        global $wpdb;
        global $wpgmza_tblname_categories;
        global $wpgmza_tblname_category_maps;
        $ret_msg = "";
        $ret_msg .= "<option value=\"0\">".__("All","wp-google-maps")."</option>";

        $map_id = (int)$map_id;

        $sql = "SELECT * FROM `$wpgmza_tblname_category_maps` LEFT JOIN `$wpgmza_tblname_categories` ON $wpgmza_tblname_category_maps.cat_id = $wpgmza_tblname_categories.id WHERE ( `map_id` = '$map_id' OR `map_id` = '0' ) AND `$wpgmza_tblname_categories`.`active` = 0 AND `parent` = '0' ORDER BY priority DESC, category_name ASC";

        $results = $wpdb->get_results($sql);

        /**
         * Builds the final category array that we will use to construct HTML
         * @var array
         */
        $category_array = array();

        foreach ( $results as $result ) {
            $category_array[$result->id] = array();

            $category_array[$result->id]['title'] = stripslashes($result->category_name);
            $category_array[$result->id]['id'] = $result->id;
            $category_array[$result->id]['retina'] = $result->retina;
            $category_array[$result->id]['active'] = $result->active;
            $category_array[$result->id]['parent_id'] = 0;
            $category_array[$result->id]['total_markers'] = wpgmza_return_marker_count_by_category( $result->id, $map_id );
            $category_array[$result->id]['children'] = false; /* set this to false by default */

            /* lets look for children */
            $continue = true;
            $parents_to_check = array();
            $parents_to_check[$result->id] = false;

            $safety_counter = 0;
            while ($continue) {
                $safety_counter++;
                if ($safety_counter > 1000) {
                    break;
                }

                foreach ($parents_to_check as $current_parent_id => $has_been_checked) {
                    if (!$has_been_checked) {
                        $current_parent_id = (int)$current_parent_id;
                        $sql = "SELECT * FROM `$wpgmza_tblname_categories` WHERE `active` = 0 AND `parent` = '".$current_parent_id."' ORDER BY `category_name` ASC";

                        $child_results = $wpdb->get_results($sql);
                        if ($child_results) {

                            /* there are children */
                            foreach ( $child_results as $child_result ) {

                                $category_array[$child_result->id]['title'] = stripslashes($child_result->category_name);
                                $category_array[$child_result->id]['id'] = $child_result->id;
                                $category_array[$child_result->id]['retina'] = $child_result->retina;
                                $category_array[$child_result->id]['active'] = $child_result->active;
                                $category_array[$child_result->id]['parent_id'] = $current_parent_id;
                                $category_array[$child_result->id]['total_markers'] = wpgmza_return_marker_count_by_category( $child_result->id, $map_id );
                                $category_array[$child_result->id]['children'] = false; /* set this to false by default */

                                /* mark this ID as 'to be checked' within the loop */
                                $category_array[$current_parent_id]['children'][] = $child_result->id;
                                $parents_to_check[$child_result->id] = false;

                            }

                            /* mark this parent as 'checked' */

                        } else {
                            /* no children left.. */
                        }
                        $parents_to_check[$current_parent_id] = true;

                    }
                }

                /* lets identify if we've gone through the enture checker array and marked everything as true. i.e. check if we are complete */
                $still_continue = false;
                foreach ($parents_to_check as $current_parent_id => $has_been_checked) {
                    if (!$has_been_checked) { $still_continue = true; /* theres still categories to look at*/ }
                }
                if (!$still_continue) {
                    $continue = false;
                }

            }

            //$ret_msg .= "<div class='wpgmza_cat_checkbox_item_holder wpgmza_cat_checkbox_item_holder_".$result->id."' ".$div_style."><input type='checkbox' class='wpgmza_checkbox' id='wpgmza_cat_checkbox_".$result->id."' name='wpgmza_cat_checkbox$array_suffix' mid=\"".$map_id."\" value=\"".$result->id."\" /><label for='wpgmza_cat_checkbox_".$result->id."'>".stripslashes($result->category_name)."</label></div>";
        }

         /* build the tree */
        $tree = wpgmza_build_tree($category_array);

        global $wpmgza_cat_tree_html;
        $wpmgza_cat_tree_html = '';

        wpgmza_consume_tree_dropdown($tree, "", $map_id, '');

        $ret_msg.= $wpmgza_cat_tree_html;

        return $ret_msg;

    }
    function wpgmza_pro_return_category_checkbox_list($map_id,$show_all = true,$array = false) {
        global $wpdb;
        global $wpgmza_tblname_categories;
        global $wpgmza_tblname_category_maps;

        if ($array) { $array_suffix = "[]"; } else { $array_suffix = ""; }

        $sql = "SELECT * FROM `$wpgmza_tblname_category_maps` LEFT JOIN `$wpgmza_tblname_categories` ON $wpgmza_tblname_category_maps.cat_id = $wpgmza_tblname_categories.id WHERE ( `map_id` = '$map_id' OR `map_id` = '0' ) AND `$wpgmza_tblname_categories`.`active` = 0 AND `parent` = '0' ORDER BY priority DESC, `category_name` ASC";

        $ret_msg = "<div class='wpgmza_cat_checkbox_holder wpgmza_cat_checkbox_".$map_id."'>";
        //$ret_msg .= "<div class='wpgmza_cat_checkbox_item_holder_first'>";
        //if ($show_all) { $ret_msg .= "<input type='checkbox' class='wpgmza_checkbox' id='wpgmza_cat_checkbox_0' name='wpgmza_cat_checkbox$array_suffix' mid=\"".$map_id."\" value=\"0\" /><label for='wpgmza_cat_checkbox_0'>".__("All","wp-google-maps")."</label>"; }
        //$ret_msg .= "</div>";

        $results = $wpdb->get_results($sql);
        $cat_link_text = sprintf( __( '<a href="%1$s">Create a category.</a>', 'wp-google-maps' ),
            admin_url("admin.php?page=wp-google-maps-menu-categories")
        );

        if (!$results && is_admin()) { return __("<em><small>No categories found</small></em>","wp-google-maps"). " <em><small>".$cat_link_text."</em></small>"; }
        if (!$results && !is_admin()) { return __("<em><small>No categories found</small></em>","wp-google-maps"); }

        /**
         * Builds the final category array that we will use to construct HTML
         * @var array
         */
        $category_array = array();

        foreach ( $results as $result ) {
            $category_array[$result->id] = array();

            $category_array[$result->id]['title'] = stripslashes($result->category_name);
            $category_array[$result->id]['id'] = $result->id;
            $category_array[$result->id]['retina'] = $result->retina;
            $category_array[$result->id]['active'] = $result->active;
            $category_array[$result->id]['parent_id'] = 0;
            $category_array[$result->id]['total_markers'] = wpgmza_return_marker_count_by_category( $result->id, $map_id );
            $category_array[$result->id]['children'] = false; /* set this to false by default */

            /* lets look for children */
            $continue = true;
            $parents_to_check = array();
            $parents_to_check[$result->id] = false;

            $safety_counter = 0;
            while ($continue) {
                $safety_counter++;
                if ($safety_counter > 1000) {
                    break;
                }

                foreach ($parents_to_check as $current_parent_id => $has_been_checked) {
                    if (!$has_been_checked) {
                        $current_parent_id = (int)$current_parent_id;
                        $sql = "SELECT * FROM `$wpgmza_tblname_categories` WHERE `$wpgmza_tblname_categories`.`active` = 0 AND `parent` = '".$current_parent_id."' ORDER BY `category_name` ASC";

                        $child_results = $wpdb->get_results($sql);
                        if ($child_results) {

                            /* there are children */
                            foreach ( $child_results as $child_result ) {

                                $category_array[$child_result->id]['title'] = stripslashes($child_result->category_name);
                                $category_array[$child_result->id]['id'] = $child_result->id;
                                $category_array[$child_result->id]['retina'] = $child_result->retina;
                                $category_array[$child_result->id]['active'] = $child_result->active;
                                $category_array[$child_result->id]['parent_id'] = $current_parent_id;
                                $category_array[$child_result->id]['total_markers'] = wpgmza_return_marker_count_by_category( $child_result->id, $map_id );
                                $category_array[$child_result->id]['children'] = false; /* set this to false by default */

                                /* mark this ID as 'to be checked' within the loop */
                                $category_array[$current_parent_id]['children'][] = $child_result->id;
                                $parents_to_check[$child_result->id] = false;

                            }

                            /* mark this parent as 'checked' */

                        } else {
                            /* no children left.. */
                        }
                        $parents_to_check[$current_parent_id] = true;

                    }
                }

                /* lets identify if we've gone through the enture checker array and marked everything as true. i.e. check if we are complete */
                $still_continue = false;
                foreach ($parents_to_check as $current_parent_id => $has_been_checked) {
                    if (!$has_been_checked) { $still_continue = true; /* theres still categories to look at*/ }
                }
                if (!$still_continue) {
                    $continue = false;
                }

            }

            //$ret_msg .= "<div class='wpgmza_cat_checkbox_item_holder wpgmza_cat_checkbox_item_holder_".$result->id."' ".$div_style."><input type='checkbox' class='wpgmza_checkbox' id='wpgmza_cat_checkbox_".$result->id."' name='wpgmza_cat_checkbox$array_suffix' mid=\"".$map_id."\" value=\"".$result->id."\" /><label for='wpgmza_cat_checkbox_".$result->id."'>".stripslashes($result->category_name)."</label></div>";
        }

         /* build the tree */
        $tree = wpgmza_build_tree($category_array);

        global $wpmgza_cat_tree_html;
        $wpmgza_cat_tree_html = '';

        wpgmza_consume_tree($tree, $array_suffix, $map_id);

        $ret_msg.= $wpmgza_cat_tree_html;

        $ret_msg .= "</div>";

        return $ret_msg;

    }

    function wpgmza_return_marker_count_by_category( $cat_id = false, $map_id = false ) {

        if (!$cat_id || !$map_id)
            return 0;

        global $wpdb;
        global $wpgmza_tblname;
        $counter = 0;

        $map_id = (int)$map_id;
        $cat_id = (int)$cat_id;

        $wpgmza_sql1 = "SELECT `id`,`category` FROM $wpgmza_tblname WHERE `map_id` = '$map_id' AND `category` != '0'";

        $results = $wpdb->get_results($wpgmza_sql1);

        foreach ($results as $result) {
            $cat_check = explode(",", $result->category);

            foreach ($cat_check as $key => $cat_c) {
                if ($cat_c == $cat_id) {
                    $counter++;
                }
            }

        }

        return intval($counter);

    }

    global $wpmgza_cat_tree_marker_counter;
    $wpmgza_cat_tree_marker_counter = 0;

    function wpgmza_return_marker_count_category_via_elements( $elements, $wpgmza_settings ) {
        if (isset($wpgmza_settings['wpgmza_settings_cat_display_qty']) && $wpgmza_settings['wpgmza_settings_cat_display_qty'] == 'yes' && !is_admin()) {
            global $wpmgza_cat_tree_marker_counter;
            $wpmgza_cat_tree_marker_counter = 0;
            wpgmza_tree_marker_counter( $elements, $wpgmza_settings );
            return "<span class='wpgmza_cat_count'>(".$wpmgza_cat_tree_marker_counter.")</span>";
        } else {
            return '';
        }
    }

    function wpgmza_tree_marker_counter( $elements, $wpgmza_settings ) {
        global $wpmgza_cat_tree_marker_counter;
        foreach($elements as $element){
            $marker_count = $element['total_markers'];
            $wpmgza_cat_tree_marker_counter += $marker_count;

            if (!isset($wpgmza_settings['wpgmza_settings_cat_logic']) || $wpgmza_settings['wpgmza_settings_cat_logic'] === "0") {
                /* only get the sub category count if the user has selected "OR" category logic in the settings */
                if(is_array($element['children'])){
                    wpgmza_tree_marker_counter($element['children'], $wpgmza_settings);
                }
            }

        }
    }

    global $wpmgza_cat_tree_html;

    function wpgmza_consume_tree(array $array,$array_suffix,$map_id, $is_child = false) {

        $wpgmza_settings = get_option("WPGMZA_OTHER_SETTINGS");
        $is_child_class = $is_child ? "wpgmza_cat_ul_child" : "";

        global $wpmgza_cat_tree_html;
        $wpmgza_cat_tree_html .= "<ul class='wpgmza_cat_ul wpgmza_cat_checkbox_item_holder ".$is_child_class."'>";

        foreach($array as $key => $value){
            //If $value is an array.

            $wpmgza_cat_tree_html .= "<li class='wpgmza_cat_checkbox_item_holder wpgmza_cat_checkbox_item_holder_".$value['id']."'>";
            $wpmgza_cat_tree_html .= "<input type='checkbox' class='wpgmza_checkbox' id='wpgmza_cat_checkbox_".$value['id']."' name='wpgmza_cat_checkbox".$array_suffix."' mid=\"".$map_id."\" value=\"".$value['id']."\" />";
            $wpmgza_cat_tree_html .= "<label for='wpgmza_cat_checkbox_".$value['id']."'>".stripslashes($value['title'])." ".wpgmza_return_marker_count_category_via_elements(array(0=>$value), $wpgmza_settings)."</label>";

            if(is_array($value['children'])){
                wpgmza_consume_tree($value['children'], $array_suffix, $map_id , true);
            }
            $wpmgza_cat_tree_html .= "</li>";
        }

        $wpmgza_cat_tree_html .= "</ul>";

    }

    function wpgmza_consume_tree_dropdown(array $array,$array_suffix,$map_id, $ext_string = '') {

        $wpgmza_settings = get_option("WPGMZA_OTHER_SETTINGS");

        global $wpmgza_cat_tree_html;

        foreach($array as $key => $value){
            //If $value is an array.

            if ($value['parent_id'] == 0) {
                $ext_string = '';
            }

            $wpmgza_cat_tree_html .= "<option value=\"".$value['id']."\">".$ext_string.stripslashes($value['title'])." ".wpgmza_return_marker_count_category_via_elements(array(0=>$value), $wpgmza_settings)."</option>";

            if(is_array($value['children'])){
                $ext_string .= '&nbsp; ';
                wpgmza_consume_tree_dropdown($value['children'], $array_suffix, $map_id, $ext_string);
            }
        }
        $ext_string = '-';

    }

    function wpgmza_build_tree(array $elements, $parentId = 0) {
        $branch = array();

        foreach ($elements as $element) {
            if ($element['parent_id'] == $parentId) {
                $children = wpgmza_build_tree($elements, $element['id']);
                if ($children) {
                    $element['children'] = $children;
                }
                $branch[] = $element;
            }
        }
        return $branch;
    }

    function wpgmza_pro_return_category_dropdown_list($map_id, $show_all = true, $array = false) {
        global $wpdb;
        global $wpgmza_tblname_categories;
        global $wpgmza_tblname_category_maps;

        if ($array) {
            $array_suffix = "[]";
        } else {
            $array_suffix = "";
        }

        $map_id = (int)$map_id;

        $sql = "SELECT * FROM `$wpgmza_tblname_category_maps` LEFT JOIN `$wpgmza_tblname_categories` ON $wpgmza_tblname_category_maps.cat_id = $wpgmza_tblname_categories.id WHERE ( `map_id` = '$map_id' OR `map_id` = '0' ) AND `$wpgmza_tblname_categories`.`active` = 0 ORDER BY `category_name` ASC";

        $results = $wpdb->get_results($sql);

        $ret_msg .= "<select name='wpgmza_cat_checkbox'>";

        if ($show_all) {
            $ret_msg .= "<option id='wpgmza_cat_checkbox_0' name='wpgmza_cat_checkbox$array_suffix' mid=\"" . $map_id . "\" value=\"0\" />" . __("All", "wp-google-maps") . "</option>";
        }

        foreach ($results as $result) {
            $ret_msg .= "<option class='wpgmza_checkbox' id='wpgmza_cat_checkbox_" . $result->id . "' name='wpgmza_cat_checkbox' mid=\"" . $map_id . "\" value=\"" . $result->id . "\">" . stripslashes($result->category_name) . "</option>";
        }

        $ret_msg .= "</select>";

        $ret_msg .= "</div>";

        return $ret_msg;
    }

    function wpgmza_pro_return_category_blocks($map_id,$show_all = true,$array = false) {
        global $wpdb;
        global $wpgmza_tblname_categories;
        global $wpgmza_tblname_category_maps;

        $map_id = (int)$map_id;

        if ($array) { $array_suffix = "[]"; } else { $array_suffix = ""; }
        $sql = "SELECT * FROM `$wpgmza_tblname_category_maps` WHERE `map_id` = '$map_id' OR `map_id` = '0'";
            $ret_msg = "<div style='display:block; overflow:hidden;'>";

        $results = $wpdb->get_results($sql);
        if (!$results) { return __("<em><small>No categories found</small></em>","wp-google-maps"); }
        foreach ( $results as $result ) {

            $cat_id = $result->cat_id;

            $results = $wpdb->get_results("
                SELECT *
                FROM `$wpgmza_tblname_categories`
                WHERE `active` = 0
                AND `id` = '$cat_id'
                ORDER BY `id` DESC
                ");
            foreach ( $results as $result ) {
                $ret_msg .= "<div class='wpgmza_cat_block' style='border: 1px solid #fff; box-shadow: 0 0 1px #a8c6c4; text-align: left; margin-bottom: 8px; font-family: 'museo_sans300'; font-size: 12px;  width: 159px; padding: 5px; color: #FFF;'><a href='javascript:void(0);' id='wpgmza_cat_block_".$result->id."' name='wpgmza_cat_block$array_suffix' mid=\"".$map_id."\" markerid=\"".$result->id."\" />".stripslashes( $result->category_name )."</a></div>";
            }

        }
            $ret_msg .= "</div>";

        return $ret_msg;

    }
    function wpgmza_pro_return_maps_linked_to_cat($cat_id) {
        global $wpdb;
        global $wpgmza_tblname_category_maps;
        $ret_msg = "";

        $sql = "SELECT * FROM `$wpgmza_tblname_category_maps` WHERE `cat_id` = '$cat_id'";
        $results = $wpdb->get_results($sql);
        $cnt = count($results);
        $cnt_i = 1;
        foreach ( $results as $result ) {

            $map_id = $result->map_id;
            if ($map_id == 0) {
                $ret_msg .= "<a href=\"?page=wp-google-maps-menu\">".__("All maps","wp-google-maps")."</option>";
                return $ret_msg;
            } else {
                $map_data = wpgmza_get_map_data($map_id);
                if ($cnt_i == $cnt) { $wpgmza_com = ""; } else { $wpgmza_com = ","; }
                $ret_msg .= "<a href=\"?page=wp-google-maps-menu&action=edit&map_id=".$map_id."\">".$map_data->map_title."</a>$wpgmza_com ";
            }
            $cnt_i++;

        }

        return $ret_msg;

    }

    function wpgmaps_list_categories() {

        global $wpdb;
        global $wpgmza_tblname_categories;

        $results = $wpdb->get_results("SELECT * FROM `$wpgmza_tblname_categories` WHERE `active` = 0 AND `parent` = '0' ORDER BY `category_name` ASC");
        $ret_msg = "<table class=\"wp-list-table widefat fixed striped pages\">";
        $ret_msg .= "  <thead>";
        $ret_msg .= "      <tr>";
        $ret_msg .= "          <th scope='col' width='100px' id='id' class='manage-column column-id' style=''>".__("ID","wp-google-maps")."</th>";
        $ret_msg .= "          <th scope='col' id='cat_cat' class='manage-column column-map_title'  style=''>".__("Category","wp-google-maps")."</th>";
        $ret_msg .= "          <th scope='col' id='cat_icon' class='manage-column column-map_width' style=\"\">".__("Icon","wp-google-maps")."</th>";
        $ret_msg .= "          <th scope='col' id='cat_linked' class='manage-column column-map_width' style=\"\">".__("Linked maps","wp-google-maps")."</th>";
        $ret_msg .= "          <th scope='col' id='cat_priority' class='manage-column column-map_width' style=\"\">".__("Priority","wp-google-maps")."</th>";
        $ret_msg .= "      </tr>";
        $ret_msg .= "  </thead>";
        $ret_msg .= "<tbody id=\"the-list\" class='list:wp_list_text_link'>";

        /**
         * Builds the final category array that we will use to construct HTML
         * @var array
         */
        $category_array = array();

        foreach ( $results as $result ) {
            $category_array[$result->id] = array();

            $category_array[$result->id]['title'] = stripslashes($result->category_name);
            $category_array[$result->id]['id'] = $result->id;
            $category_array[$result->id]['retina'] = $result->retina;
            $category_array[$result->id]['active'] = $result->active;
            $category_array[$result->id]['category_icon'] = $result->category_icon;
            $category_array[$result->id]['parent_id'] = 0;
            $category_array[$result->id]['total_markers'] = wpgmza_return_marker_count_by_category( $result->id, false );
            $category_array[$result->id]['children'] = false; /* set this to false by default */
            $category_array[$result->id]['priority'] = $result->priority;

            /* lets look for children */
            $continue = true;
            $parents_to_check = array();
            $parents_to_check[$result->id] = false;

            $safety_counter = 0;
            while ($continue) {
                $safety_counter++;
                if ($safety_counter > 1000) {
                    break;
                }

                foreach ($parents_to_check as $current_parent_id => $has_been_checked) {
                    if (!$has_been_checked) {
                        $current_parent_id = (int)$current_parent_id;

                        $sql = "SELECT * FROM `$wpgmza_tblname_categories` WHERE `active` = 0 AND `parent` = '".$current_parent_id."' ORDER BY `category_name` ASC";

                        $child_results = $wpdb->get_results($sql);
                        if ($child_results) {

                            /* there are children */
                            foreach ( $child_results as $child_result ) {

                                $category_array[$child_result->id]['title'] = stripslashes($child_result->category_name);
                                $category_array[$child_result->id]['id'] = $child_result->id;
                                $category_array[$child_result->id]['retina'] = $child_result->retina;
                                $category_array[$child_result->id]['active'] = $child_result->active;
                                $category_array[$child_result->id]['category_icon'] = $child_result->category_icon;
                                $category_array[$child_result->id]['parent_id'] = $current_parent_id;
                                $category_array[$child_result->id]['total_markers'] = wpgmza_return_marker_count_by_category( $child_result->id, false );
                                $category_array[$child_result->id]['children'] = false; /* set this to false by default */
                                $category_array[$child_result->id]['priority'] = $child_result->priority;

                                /* mark this ID as 'to be checked' within the loop */
                                $category_array[$current_parent_id]['children'][] = $child_result->id;
                                $parents_to_check[$child_result->id] = false;

                            }

                            /* mark this parent as 'checked' */

                        } else {
                            /* no children left.. */
                        }
                        $parents_to_check[$current_parent_id] = true;

                    }
                }

                /* lets identify if we've gone through the enture checker array and marked everything as true. i.e. check if we are complete */
                $still_continue = false;
                foreach ($parents_to_check as $current_parent_id => $has_been_checked) {
                    if (!$has_been_checked) { $still_continue = true; /* theres still categories to look at*/ }
                }
                if (!$still_continue) {
                    $continue = false;
                }

            }

            //$ret_msg .= "<div class='wpgmza_cat_checkbox_item_holder wpgmza_cat_checkbox_item_holder_".$result->id."' ".$div_style."><input type='checkbox' class='wpgmza_checkbox' id='wpgmza_cat_checkbox_".$result->id."' name='wpgmza_cat_checkbox$array_suffix' mid=\"".$map_id."\" value=\"".$result->id."\" /><label for='wpgmza_cat_checkbox_".$result->id."'>".stripslashes($result->category_name)."</label></div>";
        }

         /* build the tree */

        $tree = array();
        $tree = wpgmza_build_tree($category_array);

        global $wpmgza_cat_tree_html;
        $wpmgza_cat_tree_html = '';

        wpgmza_consume_tree_main_list($tree, "", false, '');

        $ret_msg.= $wpmgza_cat_tree_html;
        $ret_msg .= "</tbody>";
        $ret_msg .= "</table>";

        echo $ret_msg;

    }

    function wpgmza_consume_tree_main_list(array $array,$array_suffix,$map_id, $ext_string = '') {

        $wpgmza_settings = get_option("WPGMZA_OTHER_SETTINGS");

        global $wpmgza_cat_tree_html;

        foreach($array as $key => $value) {

            if ($value['parent_id'] == 0) {
                $ext_string = '';
            }

            $trashlink = "| <a href=\"?page=wp-google-maps-menu-categories&action=trash&cat_id=".$value['id']."\" title=\"Trash\">".__("Trash","wp-google-maps")."</a>";

            $wpmgza_cat_tree_html .=  "<tr id=\"record_".$value['id']."\">";
            $wpmgza_cat_tree_html .=  "  <td class='id column-id'>".$value['id']."</td>";
            $wpmgza_cat_tree_html .=  "  <td class='column-map_title'><strong><big><a href=\"?page=wp-google-maps-menu-categories&action=edit&cat_id=".$value['id']."\" title=\"".__("Edit","wp-google-maps")."\">".$ext_string.stripslashes($value['title'])."</a></big></strong><br /><a href=\"?page=wp-google-maps-menu-categories&action=edit&cat_id=".$value['id']."\" title=\"".__("Edit","wp-google-maps")."\">".__("Edit","wp-google-maps")."</a> $trashlink</td>";
            $wpmgza_cat_tree_html .=  "  <td class='column-map_width'><img src=\"".$value['category_icon']."\" style=\"max-width:100px; max-height:100px;\" /></td>";
            $wpmgza_cat_tree_html .=  "  <td class='column-map_width'>".wpgmza_pro_return_maps_linked_to_cat($value['id'])."</td>";
            $wpmgza_cat_tree_html .=  "  <td class='column-map_width'>" . (isset($value['priority']) ? $value['priority'] : "" ). "</td>";
            $wpmgza_cat_tree_html .=  "</tr>";

            //If $value is an array.

            if(is_array($value['children'])){
                $ext_string .= '— ';
                wpgmza_consume_tree_main_list($value['children'], $array_suffix, $map_id, $ext_string);
            }
        }
        $ext_string = '-';

    }

    function old_wpgmaps_list_categories() {
        global $wpdb;
        global $wpgmza_tblname_categories;

        $results = $wpdb->get_results("SELECT * FROM `$wpgmza_tblname_categories` WHERE `active` = 0 ORDER BY `category_name` ASC");
        echo "<table class=\"wp-list-table widefat fixed striped pages\">";
        echo "  <thead>";
        echo "      <tr>";
        echo "          <th scope='col' width='100px' id='id' class='manage-column column-id' style=''>".__("ID","wp-google-maps")."</th>";
        echo "          <th scope='col' id='cat_cat' class='manage-column column-map_title'  style=''>".__("Category","wp-google-maps")."</th>";
        echo "          <th scope='col' id='cat_icon' class='manage-column column-map_width' style=\"\">".__("Icon","wp-google-maps")."</th>";
        echo "          <th scope='col' id='cat_parent' class='manage-column column-map_width' style=\"\">".__("Parent","wp-google-maps")."</th>";
        echo "          <th scope='col' id='cat_linked' class='manage-column column-map_width' style=\"\">".__("Linked maps","wp-google-maps")."</th>";
        echo "      </tr>";
        echo "  </thead>";
        echo "<tbody id=\"the-list\" class='list:wp_list_text_link'>";

        foreach ( $results as $result ) {
            $trashlink = "| <a href=\"?page=wp-google-maps-menu-categories&action=trash&cat_id=".$result->id."\" title=\"Trash\">".__("Trash","wp-google-maps")."</a>";

            if ($result->parent > 0) {
                $parent_data = wpgmza_return_category_data( $result->parent );
                $parent_link = admin_url('admin.php?page=wp-google-maps-menu-categories&action=edit&cat_id='.$result->parent);
                $parent_title = "<a href='".$parent_link."'>".stripslashes( $parent_data->category_name )."</a>";
            } else {
                $parent_title = '';
            }

            echo "<tr id=\"record_".$result->id."\">";
            echo "  <td class='id column-id'>".$result->id."</td>";
            echo "  <td class='column-map_title'><strong><big><a href=\"?page=wp-google-maps-menu-categories&action=edit&cat_id=".$result->id."\" title=\"".__("Edit","wp-google-maps")."\">".stripslashes( $result->category_name )."</a></big></strong><br /><a href=\"?page=wp-google-maps-menu-categories&action=edit&cat_id=".$result->id."\" title=\"".__("Edit","wp-google-maps")."\">".__("Edit","wp-google-maps")."</a> $trashlink</td>";
            echo "  <td class='column-map_width'><img src=\"".$result->category_icon."\" style=\"max-width:100px; max-height:100px;\" /></td>";
            echo "  <td class='column-map_width'>".$parent_title."</td>";
            echo "  <td class='column-map_width'>".wpgmza_pro_return_maps_linked_to_cat($result->id)."</td>";
            echo "</tr>";
        }
        echo "</table>";
    }

    function wpgmaps_trash_cat( $cat_id ) {
        global $wpdb;
        global $wpgmza_tblname_categories;
        global $wpgmza_tblname_category_maps;

        $cat_id = (int)$cat_id;

        if ( isset( $cat_id ) ) {
            $rows_affected = $wpdb->query( $wpdb->prepare( "UPDATE $wpgmza_tblname_categories SET active = %d WHERE id = %d", 1, $cat_id) );
            $rows_affected = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpgmza_tblname_category_maps WHERE cat_id = %d", $cat_id) );

            do_action('wpgmza_category_deleted');

            return true;
        } else {
            return false;
        }
    }

    /**
     * Return all category data from the table row that matches
     *
     * @param  intval $cat_id   Category ID
     * @return array|boolean    Array if there is data or FALSE if not
     */
    function wpgmza_return_category_data( $cat_id ) {
        global $wpgmza_tblname_categories;
        global $wpdb;
        $result = $wpdb->get_row( "SELECT * FROM `".$wpgmza_tblname_categories."` WHERE `id` = '".intval( $cat_id )."' AND `active` = 0 LIMIT 1" );
        return $result;
    }

    /**
     * Return all active categories
     *
     * @param  intval           $map_id    Map ID (optional)
     * @param  intval           $active    0 = active, 1 = deleted
     * @return array|boolean               Array if there is data or FALSE if not
     */
    function wpgmza_return_all_categories( $map_id = false, $active = 0 ) {
        global $wpdb;
        global $wpgmza_tblname_categories;
        global $wpgmza_tblname_category_maps;

        $active = (int)$active;

        if ( !$map_id ) {
            /* get all category data for all maps */
            $results = $wpdb->get_results("SELECT * FROM `".$wpgmza_tblname_categories."` WHERE `active` = ".$active." ORDER BY `id` DESC");
        } else {
            /* get all category data for a specific map */

            $sql = "

            SELECT $wpgmza_tblname_category_maps.* , $wpgmza_tblname_categories.*
            FROM $wpgmza_tblname_category_maps LEFT JOIN $wpgmza_tblname_categories

            ON $wpgmza_tblname_category_maps.cat_id = $wpgmza_tblname_categories.id
            WHERE  ($wpgmza_tblname_category_maps.map_id = 0 OR $wpgmza_tblname_category_maps.map_id = $map_id)
            AND `active` = $active
           ";

            $results = $wpdb->get_results($sql);
        }
        return $results;
    }

    /**
     * Returns an array of category data for the specific map (parents and children)
     * @param  intval $map_id Map ID
     * @return array
     */
    function wpgmza_get_category_localized_data( $map_id ) {
        if ( !$map_id )
            return;

        $cat_data = wpgmza_return_all_categories( $map_id, 0 );
        return $cat_data;

    }