plugin-pages.php 60.8 KB
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 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
<?php
// Exit if accessed directly
if( !defined( 'ABSPATH' ) ) exit;


if( !class_exists( 'vxcf_mailchimp_pages' ) ) {

/**
* Main class
*
* @since       1.0.0
*/
class vxcf_mailchimp_pages   extends vxcf_mailchimp{
    public $ajax=false;
    
/**
* initialize plugin hooks
*  
*/
  public function __construct() {
  
  $this->data=$this->get_data_object();
global $pagenow; 
  if(in_array($pagenow, array("admin-ajax.php"))){
  add_action('wp_ajax_update_feed_'.$this->id, array($this, 'update_feed'));
  add_action('wp_ajax_update_feed_sort_'.$this->id, array($this, 'update_feed_sort'));
  add_action('wp_ajax_get_field_map_'.$this->id, array($this, 'get_field_map_ajax'));
  add_action('wp_ajax_get_field_map_object_'.$this->id, array($this, 'get_field_map_object_ajax'));
  add_action('wp_ajax_get_objects_'.$this->id, array($this, 'get_objects_ajax'));
  add_action('wp_ajax_log_detail_'.$this->id, array($this, 'log_detail'));
   add_action('wp_ajax_refresh_data_'.$this->id, array($this, 'refresh_data')); 
  add_action('wp_ajax_send_to_crm_'.$this->id, array($this, 'send_to_crm')); 
  }
  //crmperks forms
  add_action( 'cfx_add_meta_box', array($this,'add_meta_box_crmperks_form'),10,2 );
  add_action('cfx_form_entry_updated', array($this, 'update_entry_crm_perks_forms'),10,3);
  add_action('cfx_form_post_note_added', array($this, 'create_note_crm_perks_forms'),10,3);
  add_action('cfx_form_pre_note_deleted', array($this, 'delete_note_crm_perks_forms'),10,2);
  add_action('cfx_form_pre_trash_leads', array($this, 'trash_leads_crm_perks_forms'),10,2);
  add_action('cfx_form_pre_restore_leads', array($this, 'restore_leads_crm_perks_forms'),10,2); 
  if($this->is_crm_page()){
  $base_url=$this->get_base_url();
  wp_register_script( 'vxc-tooltip',$base_url. 'js/jquery.tipTip.js', array( 'jquery' ), $this->version, true );
  wp_register_style('vxc-tooltip', $base_url. 'css/tooltip.css');
  wp_register_style('vx-fonts', $base_url. 'css/font-awesome.min.css');
  wp_register_style('vx-datepicker', $base_url. 'css/jquery-ui.min.css');
  wp_register_script( 'vxg-select2',$base_url. 'js/select2.min.js', array( 'jquery' ), $this->version, true );
  wp_register_style('vxg-select2', $base_url. 'css/select2.min.css',array(),array('ver'=>'1.0'));
  wp_register_script( 'vx-sorter',$base_url. 'js/jquery.tablesorter.min.js', array( 'jquery' ), $this->version, true );
  }  
  //creates the subnav left menu
 add_filter("admin_menu", array($this, 'create_menu'), 50);
add_filter( 'vx_cf_meta_boxes_right', array($this,'add_meta_box'),10,3 );
 add_action( 'admin_notices', array( $this, 'admin_notices' ) );  
   add_filter('plugin_action_links', array($this, 'plugin_action_links'), 10, 2); 
    add_action('vxcf_entry_submit_btn', array($this, 'entry_checkbox'));  

  add_action('vx_cf7_post_note_added', array($this, 'create_note_e'),10,3);
  add_action('vx_cf7_pre_note_deleted', array($this, 'delete_note_e'),10,2);
  add_action('vx_cf7_pre_trash_leads', array($this, 'trash_leads_e'));
  add_action('vx_cf7_pre_restore_leads', array($this, 'restore_leads_e'));
  add_action('vx_cf7_entry_updated', array($this, 'update_entry_e'),10,3);
//
  add_action('vx_contact_post_note_added', array($this, 'create_note_c'),10,3);
  add_action('vx_contact_pre_note_deleted', array($this, 'delete_note_c'),10,2);
  add_action('vx_contact_pre_trash_leads', array($this, 'trash_leads_c'));
  add_action('vx_contact_pre_restore_leads', array($this, 'restore_leads_c'));
  add_action('vx_contact_entry_updated', array($this, 'update_entry_c'),10,3);
  
   add_filter('vx_callcenter_entries_action',array($this,'bulk_send_crm_callcenter'),10,4);
 add_filter('vx_callcenter_bulk_actions',array($this,'add_bulk_send_crm_callcenter'));
  $this->setup_plugin();
 }

  public function update_entry_crm_perks_forms($entry_id,$lead,$form){ 
    $option=get_option($this->type.'_settings',array());

if(   !empty($option['update'])  ){ 
    $event= 'update';
  $lead['__vx_id']=$entry_id; 
  $form['id']='vf_'.$form['id'];
  $form['cfx_type']='vf'; 
  $push=$this->push($lead,$form,$event);
        if(!empty($push['msg'])){
  $this->screen_msg($push['msg'],$push['class']);  
  }
  }
}
public function create_note_crm_perks_forms($id, $entry, $note){
$option=get_option($this->type.'_settings',array());
if(!empty($option['notes']) ){
  if(!empty($entry['form_id'])){
  $form=array('id'=>'vf_'.$entry['form_id']);
  $entry['__vx_id']=$entry['id']; 
  $title=substr($note,0,100);
self::$note=array('id'=>$id,'body'=>$note,'title'=>$title);
$push=$this->push($entry,$form,'add_note');

  }
}
}

public function delete_note_crm_perks_forms($note_id,$entry){
$option=get_option($this->type.'_settings',array());      
if(!empty($option['notes'])){
if(!empty($entry['form_id'])){
$form=array('id'=>'vf_'.$entry['form_id']);
self::$note=array('id'=>$note_id);
$entry['__vx_id']=$entry['id']; 
$push=$this->push($entry,$form,'delete_note');
} 
      }
}
public function trash_leads_crm_perks_forms($leads,$form_id){
$option=get_option($this->type.'_settings',array());

      if(!empty($option['delete'])){
       if(is_array($leads)){   
       $updated=$error='';
        foreach($leads as $lead){
        if(!empty($form_id)){
$entry=array('__vx_id'=>$lead);
$push=$this->push($entry,array('id'=>$form_id),'delete');
              if(!empty($push['msg'])){
                  if($push['class'] == 'updated'){
                   $updated.=$push['msg'].'<br>';   
                  }else{
                    $error.=$push['msg'].'<br>';    
                  }
  }
        } 
        }
     if(!empty($updated)){
        $this->add_msg($updated,'updated');  
     }   
     
     if(!empty($error)){
        $this->add_msg($error,'error');  
     }
       }
      }

}

public function restore_leads_crm_perks_forms($leads,$form_id){
     $option=get_option($this->type.'_settings',array());

      if(!empty($option['restore'])){
           
       if(is_array($leads)){   
       $updated=$error=''; 
        foreach($leads as $lead){
   if( !empty($lead['id'])){
$lead['__vx_id']=$lead['id']; 
$push=$this->push($lead,array('id'=>$form_id),'restore');

              if(!empty($push['msg'])){
                  if($push['class'] == 'updated'){
                   $updated.=$push['msg'].'<br>';   
                  }else{
                    $error.=$push['msg'].'<br>';    
                  }
  }
        } 
        }
     if(!empty($updated)){
        $this->add_msg($updated,'updated');  
     }   
     
     if(!empty($error)){
        $this->add_msg($error,'error');  
     }
       }
      }
    //  var_dump($leads); die();  
}

 
public function add_bulk_send_crm_callcenter($list){ 
 $list['send_mailchimp']=__('Send to Mailchimp','contact-form-mailchimp-crm');
  return $list;  
}
public function bulk_send_crm_callcenter($msg,$action,$ids,$type){ 
    if($action == 'send_mailchimp'){
        $entry=array(); $notice=''; $class='updated';

        if(class_exists('vx_dialpad')){
            $pad=new vx_dialpad();
            $form=$pad->get_form($type);
        if(is_array($ids)){
            foreach($ids as $id){
               $entry=$pad->get_entry($type,$id);       
         if(!empty($entry['id'])){
$entry['__vx_id']=$entry['id'];
          $temp=$this->push($entry,$form,'',false);

          if(!empty($temp['msg'])){
         if(!empty($notice)){
          $notice.='<br/>';   
         }
          $notice.=$temp['msg'];
        if($temp['class'] !='updated'){
         $class=$temp['class'];   
        }
          }    
         }   }
        }
        }
        if(empty($notice)){
            $notice='Nothing Sent to Mailchimp';
        }
    $msg=array('msg'=>$notice,'class'=>$class);   
    }

return $msg;
}

public function update_entry_e($entry,$entry_id,$lead){ 
 $this->update_entry($entry,$entry_id,$lead);   
}
public function update_entry_c($entry,$entry_id,$lead){ 
  $this->update_entry($entry,$entry_id,$lead,'addon');     
}
    /**
    * Send entry to crm on update
    * 
    * @param mixed $form
    * @param mixed $lead_id
    */
public function update_entry($entry,$entry_id,$lead,$type=''){ 
  $option=get_option($this->type.'_settings',array());

if( !empty($_POST[$this->id.'_send']) || (  !empty($option['update']) ) ){ 
    // only update , if already sent to crm
    //will in case of auto update option or send to crm checkbox
    $event= 'update';
  $entry['__vx_id']=$entry_id; 
  $entry['__vx_type']=$type; 

  $form=$this->get_form($lead['form_id']);
    $push=$this->push($entry,$form,$event);
        if(!empty($push['msg'])){
  $this->screen_msg($push['msg'],$push['class']);  
  }
  }

}
public function trash_leads_e($leads){
 $this->trash_leads($leads);   
}
public function trash_leads_c($leads){
 $this->trash_leads($leads,'addon');   
}
/**
* Delete entry from crm on deletion
* 
* @param mixed $lead_id
*/
public function trash_leads($leads,$type=''){
     $option=get_option($this->type.'_settings',array());

      if(!empty($option['delete'])){
       if(is_array($leads)){   
       $updated=$error='';
        foreach($leads as $lead){
           $entry=$this->get_cf_entry($lead,$type);
        if(!empty($entry['form_id'])){
$form=$this->get_form($entry['form_id']);
$entry['__vx_id']=$lead;
$push=$this->push($entry,$form,'delete');
              if(!empty($push['msg'])){
                  if($push['class'] == 'updated'){
                   $updated.=$push['msg'].'<br>';   
                  }else{
                    $error.=$push['msg'].'<br>';    
                  }
  }
        } 
        }
     if(!empty($updated)){
        $this->add_msg($updated,'updated');  
     }   
     
     if(!empty($error)){
        $this->add_msg($error,'error');  
     }
       }
      }
     //  var_dump($leads); die();  
  }
  
public function restore_leads_e($leads){
    $this->restore_leads($leads);
}  
public function restore_leads_c($leads){
    $this->restore_leads($leads,'addon');
} 
  /**
* Send entry to crm on restore
* 
* @param mixed $lead_id
*/
public function restore_leads($leads,$type=''){
     $option=get_option($this->type.'_settings',array());

      if(!empty($option['restore'])){
           
       if(is_array($leads)){   
       $updated=$error='';
        foreach($leads as $lead){
           $entry=$this->get_cf_entry($lead,$type);
          
        if(!empty($entry['form_id'])){
$form=$this->get_form($entry['form_id']);
if($type == 'addon'){
  $detail=$entry;  
}else{
 $detail=$this->get_cf_entry_detail($lead);
}
 $detail['__vx_id']=$lead;
$push=$this->push($detail,$form,'restore');
              if(!empty($push['msg'])){
                  if($push['class'] == 'updated'){
                   $updated.=$push['msg'].'<br>';   
                  }else{
                    $error.=$push['msg'].'<br>';    
                  }
  }
        } 
        }
     if(!empty($updated)){
        $this->add_msg($updated,'updated');  
     }   
     
     if(!empty($error)){
        $this->add_msg($error,'error');  
     }
       }
      }
    //  var_dump($leads); die();  
  }
public function delete_note_e($note_id,$lead_id){
$this->delete_note($note_id,$lead_id);   
}
public function delete_note_c($note_id,$lead_id){
$this->delete_note($note_id,$lead_id,'addon');   
}  
  /**
  * delete note from crm when deleted from GF entry
  * 
  * @param mixed $note_id
  * @param mixed $lead_id
  */
public function delete_note($note_id,$lead_id,$type=''){
$option=get_option($this->type.'_settings',array());
       
      if(!empty($option['notes'])){
          
$entry=$this->get_cf_entry($lead_id,$type);
if(!empty($entry['form_id'])){
$form=$this->get_form($entry['form_id']);
    self::$note=array('id'=>$note_id);
    $entry['__vx_id']=$entry['id']; 
$push=$this->push($entry,$form,'delete_note');

} 
      }
}
public function create_note_e($id, $lead_id, $note){
$this->create_note($id, $lead_id, $note);
}
public function create_note_c($id, $lead_id, $note){

$this->create_note($id, $lead_id, $note,'addon');
}
    /**
* send entry note to crm
*   
* @param mixed $id
* @param mixed $lead_id
* @param mixed $user_id
* @param mixed $user_name
* @param mixed $note
* @param mixed $note_type
*/
public function create_note($id, $lead_id, $note,$type=''){

        $option=get_option($this->type.'_settings',array());

      if(!empty($option['notes'])){
  $entry=$this->get_cf_entry($lead_id,$type);
  if(!empty($entry['form_id'])){
  $form=$this->get_form($entry['form_id']);
  if($type == 'addon'){
   $entry_detail=$entry;   
  }else{
  $entry_detail=$this->get_cf_entry_detail($lead_id);
  }
  $entry_detail['__vx_id']=$lead_id; 

  $title=substr($note,0,100);
self::$note=array('id'=>$id,'body'=>$note,'title'=>$title);

$push=$this->push($entry_detail,$form,'add_note');

  }
}

  }
  public function entry_checkbox($lead){
  ?>
  <div class="vx_row">
  <label><input type="checkbox" name="<?php echo esc_attr($this->id) ?>_send" value="yes"> <?php esc_html_e('Send to Mailchimp','contact-form-mailchimp-crm') ?></label>
  </div>
  <?php  
}
    /**
  * Display custom notices
  * show mailchimp response
  * 
  */
  public function admin_notices(){

  $debug = !empty(self::$debug_html) && current_user_can($this->id.'_edit_settings');
  if($debug){ 
  echo "<div class='error'><p>".self::$debug_html."</p></div>"; 
  self::$debug_html='';
  }
  if(!empty($_POST[$this->id.'_send_btn']) && !empty($_REQUEST['id'])){
$tab=$this->post('tab');
$id=$this->post('id');
$form=array('title'=>'Contact Form');
$lead=array();
if($tab == 'contacts'){
  global $vxcf_crm;
  if(method_exists($vxcf_crm,'get_entry')){
    $lead=$vxcf_crm->get_entry($id);
$form['id']='vx_contacts';
  }  
}else{
    $info=$this->get_cf_entry($id);
    $lead=$this->get_cf_entry_detail($id);
 if(!empty($info['form_id'])){
  $form['id']=$info['form_id'];   
 }
 $lead['__vx_entry']=$info;   
}
$lead['__vx_id']=$id;
$push=$this->push($lead,$form);
    if(!empty($push['msg'])){
  $this->screen_msg($push['msg'],$push['class']);  
  }
  }
  //send to crm in order page message
  $msgs=get_option($this->id.'_msgs');

  if(is_array($msgs)){
    foreach($msgs as $msg){
     $this->screen_msg($msg['msg'],$msg['class']);    
    }  
  update_option($this->id.'_msgs','');
  }
  }
    /**
  * Add settings and support link
  * 
  * @param mixed $links
  * @param mixed $file
  */
  public function plugin_action_links( $links, $file ) {
   $slug=$this->get_slug();
      if ( $file == $slug ) {
          $settings_link=$this->link_to_settings();
            array_unshift( $links, '<a href="' .$settings_link. '">' . esc_html__('Settings', 'contact-form-mailchimp-crm') . '</a>' );
        }
        return $links;
   }
  /**
  * Creates left nav menu under Forms
  * 
  * @param mixed $menus
  */
  public  function create_menu(){
  // Adding submenu if user has access
        $page_title =__('Mailchimp for Contact Form 7','contact-form-mailchimp-crm');
        $menu_title =__('Mailchimp','contact-form-mailchimp-crm');
        $capability = $this->id."_read_feeds"; 
            $menu_id='vxcf_leads';  
if(empty($GLOBALS['admin_page_hooks'][$menu_id])){
add_menu_page($page_title,$menu_title,$capability,$this->id,array( $this,'mapping_page'));
}else{
add_submenu_page('vxcf_leads',$page_title,$menu_title,$capability,$this->id,array( $this,'mapping_page'));
}
  } 
  /**
  * plugin admin features
  * 
  */
  public function setup_plugin(){
        global $wpdb;
     
      if(isset($_REQUEST[$this->id.'_tab_action']) && $_REQUEST[$this->id.'_tab_action']=="del_account"){
 check_admin_referer('vx_nonce','vx_nonce');
 if( current_user_can($this->id."_edit_settings")){ 
$id=$this->post('id');
$data=$this->get_data_object();
$res=$data->del_account($id);
$class='';
 if($res){
       $msg=__('Account Deleted Successfully','contact-form-mailchimp-crm');
  $class='updated';   
 }else{
       $msg=__('Error While Removing Account','contact-form-mailchimp-crm');
  $class='error';      
 }
  $this->add_msg($msg,$class);
 }
  $redir=$this->link_to_settings('accounts');
wp_redirect($redir.'&'.$this->id.'_msg=1');
die();
  }

  
  if($this->post('vx_tab_action_'.$this->id)=="export_log"){
  check_admin_referer('vx_nonce','vx_nonce');
  if(!current_user_can($this->id."_export_logs")){ 
  $msg=__('You do not have permissions to export logs','contact-form-mailchimp-crm');
  $this->display_msg('admin',$msg);
  return;   
  }
  header('Content-disposition: attachment; filename='.date("Y-m-d",current_time('timestamp')).'.csv');
  header('Content-Type: application/excel');
  $data=$this->get_data_object();
  $sql_end=$data->get_log_query();
  $forms=array();
  $sql="select * $sql_end limit 3000";
  $results = $wpdb->get_results($sql , ARRAY_A );
  $fields=array(); $field_titles=array("#",__('Status','contact-form-mailchimp-crm'),__('Mailchimp ID','contact-form-mailchimp-crm') ,__('Entry ID','contact-form-mailchimp-crm'),__('Description','contact-form-mailchimp-crm'),__('Time','contact-form-mailchimp-crm'));
  $fp = fopen('php://output', 'w');
  fputcsv($fp, $field_titles);
  $sno=0;
  foreach($results as $row){
  $sno++;
  $row=$this->verify_log($row);
  fputcsv($fp, array($sno,$row['title'],$row['_crm_id'],$row['entry_id'],$row['desc'],$row['time']));    
  }
  fclose($fp);
  die();
  }
  
  if($this->post('vx_tab_action_'.$this->id)=="clear_logs" ){
  check_admin_referer('vx_nonce','vx_nonce');
  if(!current_user_can($this->id."_edit_settings")){ 
  $msg=__('You do not have permissions to clear logs','contact-form-mailchimp-crm');
  $this->display_msg('admin',$msg);
  return;   
  }
  $data=$this->get_data_object();
  $clear=$data->clear_logs();

  
       $msg=__('Error While Clearing Mailchimp Logs','contact-form-mailchimp-crm');
      $level="error";
      if(!empty($clear)){
      $msg=__('Mailchimp Logs Cleared Successfully','contact-form-mailchimp-crm');   
      $level="updated";
      }
      $this->add_msg($msg,$level);
      $link=$this->link_to_settings('logs').$this->id.'_msg=1';
  wp_redirect($link);
  die();
  }  
  //
  self::$tooltips = array(
 'vx_feed_name' =>  esc_html__('Enter feed name of your choice.', 'contact-form-mailchimp-crm'),
  'vx_sel_object' => esc_html__('Select the Object to Create when a Form is Submitted.', 'contact-form-mailchimp-crm'),
   'vx_sel_account' =>__('Select the Mailchimp account you would like to export entries to.', 'contact-form-mailchimp-crm'),
  'vx_sel_form' => esc_html__('Select the Contact Form you would like to integrate with Mailchimp. Contacts generated by this form will be automatically added to your Mailchimp account.', 'contact-form-mailchimp-crm'),
  
  'vx_map_fields' => esc_html__('Associate your Mailchimp fields to the appropriate Contact Form fields.', 'contact-form-mailchimp-crm'),
  
  'vx_optin_condition' =>__('When the opt-in condition is enabled, form submissions will only be exported to Mailchimp when the condition is met. When disabled all form submissions will be exported.', 'contact-form-mailchimp-crm'),
  
  'vx_manual_export' => esc_html__('If you do not want all entries sent to Mailchimp, but only specific, approved entries, check this box. To manually send an entry to Mailchimp, go to Entries, choose the entry you would like to send to Mailchimp, and then click the "Send to Mailchimp" button.', 'contact-form-mailchimp-crm'),
  
    'vx_entry_notes' => esc_html__('Enable this option if you want to synchronize Contact Form entry notes to Mailchimp Object notes. For example , when you add a note to a Contact Form entry, it will be added to the Mailchimp Object selected in the feed.', 'contact-form-mailchimp-crm'),
    
      'vx_primary_key' => esc_html__('Which field should be used to update existing objects?', 'contact-form-mailchimp-crm'),
      
  
  'vx_disable_logs'=>__('When an order is sent to Mailchimp we store that order information in the database and show it in the Mailchimp Log. Check this box if you do not want to save the exported order information in the logs.','contact-form-mailchimp-crm'),
  
'vx_sources'=>__('Get Lead Sources list from mailchimp.','contact-form-mailchimp-crm'),
'vx_statuses'=>__('Get Lead Status list from mailchimp.','contact-form-mailchimp-crm'),
'vx_cats'=>__('Get Categories list from mailchimp.','contact-form-mailchimp-crm'),

'vx_source'=>__('Which Lead Source should be assigned to this object.','contact-form-mailchimp-crm'),
'vx_status'=>__('Which Lead Status should be assigned to this object.','contact-form-mailchimp-crm'),
'vx_cat'=>__('Which Category should be assigned to this object.','contact-form-mailchimp-crm'),

'vx_source_check'=>__('If enabled, a Lead Source will be assigned to the Object','contact-form-mailchimp-crm'),
'vx_status_check'=>__('If enabled, a Lead Status will be assigned to the Object','contact-form-mailchimp-crm'),
'vx_cat_check'=>__('If enabled, a Category will be assigned to the Object','contact-form-mailchimp-crm'),

'vx_assign_opportunity'=>__('Enable this option if you want to assign an opportunity this object.','contact-form-mailchimp-crm'),
'vx_sel_opportunity'=>__('Select Opportunity feed. Opportunity created by this feed will be assigned to this object.','contact-form-mailchimp-crm'),

'vx_assign_contact'=>__('Enable this option , if you want to assign a Contact to this object','contact-form-mailchimp-crm'),
'vx_sel_contact'=>__('Select Contact feed. Contact created by this feed will be assigned to this object','contact-form-mailchimp-crm'),

'vx_assign_organization'=>__('Enable this option , if you want to assign a Organization to this object','contact-form-mailchimp-crm'),
'vx_sel_organization'=>__('Select Organization feed. Organization created by this feed will be assigned to this object','contact-form-mailchimp-crm'),

   'vx_owner_check'=>__('Enable this option if you want to assign another object owner.','contact-form-mailchimp-crm'),
   'vx_owners'=>__('Get Users list from Mailchimp','contact-form-mailchimp-crm'),
   'vx_order_notes'=>__('Enable this option if you want to synchronize WooCommerce Order notes to Mailchimp Object notes. For example, when you add a note to a WooCommerce Order, it will be added to the Mailchimp Object selected in the feed.','contact-form-mailchimp-crm'),
   'vx_sel_owner'=>__('Select a user as a owner of this object','contact-form-mailchimp-crm'),
   
       'vx_entry_note'=>__('Check this option if you want to send more data as CRM entry note.', 'contact-form-mailchimp-crm'),
   'vx_note_fields'=>__('Select fields which you want to send as a note', 'contact-form-mailchimp-crm'),
   'vx_disable_note'=>__('Enable this option if you want to add note only for new CRM entry', 'contact-form-mailchimp-crm')
   
  );
  
  }
public function add_meta_box_crmperks_form($lead,$form){
 $lead_id=isset($lead['id']) ? $lead['id'] : ""; 
$form_id=isset($lead['form_id']) ? 'vf_'.$lead['form_id'] : ""; 

if(! $this->has_feed($form_id)) { return ''; }
$data=$this->get_data_object();
$log_entry=$data->get_log_by_lead($lead['id'],$form_id);
$log_url=$this->link_to_settings('logs').'&entry_id='.$lead['id'];
?>
<div class="vx_div" style="margin-top: 20px;">
<div class="table_head_i"><?php esc_html_e('Capsule', 'contact-form-mailchimp-crm'); ?></div>  
<div class="vx_group">
<div class="vx_send_crm_msg">
<?php
$comments=false;
if( !empty($log_entry) ){
    $comments=true;
$log=$this->verify_log($log_entry);
echo $this->format_log_msg($log);
}
?></div>
<p style="margin-top: 12px;">
<button class="button vx_send_crm_btn" data-crm="<?php echo esc_attr($this->crm_name); ?>" type="button" data-action="send_to_crm_<?php echo esc_attr($this->id) ?>" value="yes">
<span class="reg_ok"><i class="fa fa-send"></i> Send to <?php echo esc_attr($this->crm_name); ?></span> 
<span class="reg_proc" style="display: none;"><i class="fa fa-circle-o-notch fa-spin"></i> Sending ...</span>
</button>
  <?php
      if($comments ){
  ?>
  <a href="<?php echo esc_url($log_url) ?>" target="_blank" class="button"><i class="fa fa-external-link"></i> Go to Logs</a>
  <?php
      }
  ?>
  </p>
</div>
</div>
<?php
   
} 
public function send_to_crm(){

check_ajax_referer('vx_nonce','vx_nonce');  
if(current_user_can($this->id."_send_to_crm")){   
$id=(int)$this->post('id'); 
 $log=array('meta'=>'Unknow Error');
if(class_exists('cfx_form')){
$entry=cfx_form::get_entry($id);  
if(!empty($entry)){
$form=cfx_form::get_form($entry['form_id']);
if(!empty($form['fields'])){    
$detail=cfx_form::get_entries_detail($id,$form['fields']); 
if(!empty($detail[0])){
$detail=$detail[0];
$lead=array();
foreach($detail as $k=>$v){
    $field_id=substr($k,0,strpos($k,'_'));
    if(is_numeric($field_id)){
$lead[$field_id]=$v;    
    }
}
$lead['__vx_id']=$entry['id']; 
$form['id']='vf_'.$form['id']; 
$form['cfx_type']='vf';
$push=$this->push($lead,$form);
$data=$this->get_data_object();
$log_entry=$data->get_log_by_lead($entry['id'],$form['id']);
$log=$this->verify_log($log_entry);
} } } 
echo $this->format_log_msg($log);
} }else{
 $msg=__('You do not have permissions for this action','contact-form-mailchimp-crm');
$this->screen_msg($msg,'error');    
}
die();
}    
public function format_log_msg($log){
    $msg=!empty($log['meta']) ? $log['meta'] : $log['desc'];
if(!empty($log['status']) && !empty($log['a_link']) && !empty($log['crm_id'])){
    $msg.=' '.$log['a_link'];
}
$st=empty($log['status']) ? '0' : $log['status'];
//$this->screen_msg($msg,$class);
$icons=array('0'=>array('color'=>'#DC513B','icon'=>'fa-warning'),'4'=>array('color'=>'#3897C3','icon'=>'fa-filter'),
'2'=>array('color'=>'#d5962c','icon'=>'fa-edit'),'5'=>array('color'=>'#DC513B','icon'=>'fa-times'));

$bg='#83B131'; $icon='fa-check';
if(isset($icons[$st])){
  $bg=$icons[$st]['color'];  
  $icon=$icons[$st]['icon'];  
}
return '<div style="background-color: '.$bg.';" class="vx_msg_div"><i class="fa '.$icon.'"></i> '.$msg.'</div>';
} 
public function add_meta_box($boxes,$lead,$detail){
$form_id=isset($lead['form_id']) ? $lead['form_id'] : ""; 

if( $this->has_feed($form_id)) { 
 $boxes['mailchimp_crm']=array('title'=>'<i class="fa fa-plug"></i> '.__('MailChimp', 'contact-form-mailchimp-crm'),'callback'=>array($this,'meta_box_html'));  
} 
return $boxes; 
}
public function meta_box_html($lead,$detail){
     $lead_id=isset($lead['id']) ? $lead['id'] : ""; 
    $form_id=isset($lead['form_id']) ? $lead['form_id'] : "";
     $data=$this->get_data_object();
$data=$this->get_data_object();
$log_entry=$data->get_log_by_lead($lead['id'],$form_id);
$log_url=$this->link_to_settings('logs').'&entry_id='.$lead['id'];
include_once(self::$path."templates/crm-entry-box.php");
 
} 
  /**
  * Whether to show the Entry "Send to CRM" button or not
  *
  * If the entry's form has been mapped to CRM feed, show the Send to CRM button. Otherwise, don't.
  *
  * @return boolean True: Show the button; False: don't show the button.
  */
  public  function show_send_to_crm_button() {
  
  $form_id = rgget('id');
  
  return $this->has_feed($form_id);
  }
public function has_feed($form_id) {
  $data=$this->get_data_object();
  $feeds = $data->get_feed_by_form( $form_id , true);
  
  return !empty($feeds);
  }
    /**
  * refresh data , ajax method
  * 
  */
  public function refresh_data(){
      check_ajax_referer("vx_crm_ajax","vx_crm_ajax"); 
  if(!current_user_can($this->id."_read_settings")){ 
   die();  
 }   
  $res=array();
  $action=$this->post('vx_action');

  
  $camp_id_sel=$this->post('camp_id');

  $account=$this->post('account');
  $object=$this->post('object');
  $status_sel=$this->post('status');
  $owner_sel=$this->post('owner');

 $info=array(); $meta=array();
  if(!empty($account)){
 $info=$this->get_info($account);
 if(!empty($info['meta']) ){
   $meta=$info['meta'];  
 }
  }

    $api=$this->get_api($info);
     $data=array();
  switch($action){
      case"refresh_groups":
    $camps=$api->get_groups($object); 
    if(is_array($camps)){
    $res['status']="ok";
  
    }else{
     $res['error']=$camps;   
    }
  $meta['groups']=$camps; 
  ob_start();
  $this->groups(array(),$meta);  
  $data['vx_groups_data']=ob_get_clean();
  $res['data']=$data;   
      break;    
  case"refresh_users":
    $users=$api->get_users($object); 
    
    if(is_array($users)){
    $res['status']="ok";

   
    }else{
     $res['error']=$users;   
    }
        $data['crm_sel_user']=$this->gen_select($users,$owner_sel,__('Select User','contact-form-mailchimp-crm'));
  $meta['users']=$users;  
  $res['data']=$data;   
      break;

  }
     
  if(isset($info['id'])){
    $this->update_info( array("meta"=>$meta) , $info['id'] );
}
if(isset($res['error'])){
    $res['status']='error';
    if(empty($res['error'])){
    $res['error']=__('Unknown Error','contact-form-mailchimp-crm');
    }
}
  die(json_encode($res));    
  }
  /**
  * CRM menu page
  * 
  */
  public  function mapping_page(){
       wp_enqueue_style('vx-fonts');
      wp_enqueue_script('vxc-tooltip');
      wp_enqueue_style('vxc-tooltip');
         $tabs=array('feeds'=>__('Mailchimp Feeds','contact-form-mailchimp-crm') , 'logs'=>__('Mailchimp Log','contact-form-mailchimp-crm') , 'accounts'=>__('Mailchimp Accounts','contact-form-mailchimp-crm'), 'settings'=>__('Settings','contact-form-mailchimp-crm'));
         
         $tabs=apply_filters('vx_plugin_tabs_'.$this->id,$tabs);
                  
      $view = isset($_GET["tab"]) ? $this->post("tab") : 'feeds';
      $tab=$view;
      if(!isset($tabs[$view])){
       $tab='feeds';   
      }
          ?>
                <style type="text/css">
        .vx_img_head{
            line-height: 44px;
            margin-bottom: 12px;
        }
        .vx_img_head img{
        height: 44px;
        margin-right: 10px;
        vertical-align: middle;
        }    
            </style>
    <div class="wrap">      
    <h2 class="nav-tab-wrapper">
    <?php
    $link=$this->link_to_settings();
        foreach($tabs as  $k=>$v){
  $v=is_array($v) && isset($v['label']) ? $v['label'] : $v;           
    ?>
         <a href="<?php echo esc_url($link.'&tab='.$k); ?>" class="nav-tab <?php if($k == $tab){echo 'nav-tab-active';} ?>"><?php echo esc_html($v); ?></a>
            
    <?php
        }
        ?>
        </h2>
  
    <div style="padding-top: 10px;">    
        <?php
  
  if($view == 'edit') {
    $this->edit_page($this->post('id'));
  }else if($view == "logs") {
  $this->log_page();
  }else if($view == "accounts") {
  $this->accounts_page();
  } else if($view == "settings") {
  $this->settings_page();
  }else if(isset($tabs[$tab]) && is_array($tabs[$tab])) {
  call_user_func($tabs[$tab]['function']);    
  }   else {
  $this->list_page();
  }
  ?>
  </div>
  </div>
              <script type="text/javascript">
  jQuery(document).ready(function($){

                        var unsaved=false;

      $('#mainform :input').change(function(){ 
        unsaved=true;
      });
       $('#mainform').submit(function(){ 
        unsaved=false;
      });
      
      $(window).bind("beforeunload",function(event) { 
    if(unsaved) return 'Changes you made may not be saved';
});

    $(document).on('click','.vx_toggle_key',function(e){
  e.preventDefault();  
  var key=$(this).parents(".vx_tr").find(".crm_text"); 

  if($(this).hasClass('vx_hidden')){ 
  $(this).text('<?php esc_html_e('Show Key','contact-form-mailchimp-crm') ?>');  
  $(this).removeClass('vx_hidden');
  key.attr('type','password');  
  }else{
  $(this).text('<?php esc_html_e('Hide Key','contact-form-mailchimp-crm') ?>');  
  $(this).addClass('vx_hidden');
  key.attr('type','text');  
  }
  });
start_tooltip();  
  });
    function start_tooltip(){
      // Tooltips
  var tiptip_args = {
  'attribute' : 'data-tip',
  'fadeIn' : 50,
  'fadeOut' : 50,
  'defaultPosition': 'top',
  'delay' : 200
  };
  jQuery(".vxc_tips").tipTip( tiptip_args );

  }
  
  </script>
  <?php
  }

  
  /**
  * Displays the crm feeds list page
  * 
  */
  private  function list_page(){
  if(!current_user_can($this->id.'_read_feeds')){
  esc_html_e('You do not have permissions to access this page','contact-form-mailchimp-crm');    
  return;
  }
  $is_section=apply_filters('add_page_html_'.$this->id,false);

  if($is_section === true){
    return;
} 
  $config = $this->data->get_feed('new_form');
  $offset=$this->time_offset();
  if(isset($_POST["action"]) && $_POST["action"] == "delete"){
  check_admin_referer("vx_crm_ajax");
  
  $id = absint($this->post("action_argument"));
  $this->data->delete_feed($id);
  ?>
  <div class="updated fade" style="margin:10px 0;">
  <p>
  <?php esc_html_e("Feed deleted.", 'contact-form-mailchimp-crm') ?>
  </p>
  </div>
  <?php
  }
  else if (!empty($_POST["bulk_action"])){
  check_admin_referer("vx_crm_ajax");
  $selected_feeds = $this->post("feed");
  if(is_array($selected_feeds)){
  foreach($selected_feeds as $feed_id)
  $this->data->delete_feed($feed_id);
  }
  ?>
  <div class="updated fade" style="margin:10px 0;">
  <p>
  <?php esc_html_e("Feeds deleted.", 'contact-form-mailchimp-crm') ?>
  </p>
  </div>
  <?php
  }
  $feeds = $this->data->get_feeds(); 

  wp_enqueue_script('jquery-ui-sortable');
$page_link=$this->link_to_settings('accounts');

  $data=$this->get_data_object();
  $accounts=$data->get_accounts(true);
  //
$new_feed_link=$this->get_feed_link($config['id']);  
$valid_accounts= is_array($accounts) && count($accounts) > 0 ? true : false;
  
include_once(self::$path . "templates/feeds.php");
  }
  /**
  * Displays the crm feeds list page
  * 
  */
  public  function log_page(){
  
  if(!current_user_can($this->id.'_read_logs')){
  esc_html_e('You do not have permissions to access this page','contact-form-mailchimp-crm');    
  return;
  }
  $is_section=apply_filters('add_page_html_'.$this->id,false);

  if($is_section === true){
    return;
} 
  $log_ids=array();
   $bulk_action=$this->post('bulk_action');
      $offset=$this->time_offset();
  if($bulk_action!=""){
   $log_id=$this->post('log_id');  
   if(is_array($log_id) && count($log_id)>0){
    foreach($log_id as $id){
     if(is_numeric($id)){
    $log_ids[]=(int)$id;     
     }   
    }
    if($bulk_action == "delete"){
$count=$this->data->delete_log($log_ids);
  $this->screen_msg(sprintf(__('Successfully Deleted %d Item(s)','contact-form-mailchimp-crm'),$count));  
    }
    else if(in_array($bulk_action,array("send_to_crm_bulk","send_to_crm_bulk_force"))){
         self::$api_timeout='1000';
         
       foreach($log_ids  as $id){
  $log = $this->data->get_log_by_id($id); 
  
  $form_id=$this->post('form_id',$log);
  $entry_id=$this->post('entry_id',$log);
    $log['__vx_id']=$entry_id;
  $form=$this->get_form($form_id);
  if(!empty($entry_id) && class_exists('vxcf_form')){
       $entry=$this->get_cf_entry($entry_id);
  }else{
      //
  $entry['__vx_data']=json_decode($log['data'],true);    
  }
  if(!empty($entry) && is_array($entry)){ 
    $push=$this->push($entry,$form,$log['event'],true,$log);
  }else{
    $push=array('class'=>'error','msg'=>__('Entry Not Found','contact-form-mailchimp-crm'));  
  }
    if(is_array($push) && isset($push['class'])){
    $this->screen_msg($push['msg'],$push['class']); 
    }
   ///var_dump($log_ids,$log); die();  
    }
   
   }
   }
    unset($_GET['bulk_action']);
    unset($_GET['vx_nonce']);
    //$logs_link=admin_url('admin.php?'.http_build_query($_GET));
    //wp_redirect($logs_link);
    // die();
  }
  wp_enqueue_script('jquery-ui-datepicker' );
     wp_enqueue_style('vx-datepicker');
  $times=array("today"=>"Today","yesterday"=>"Yesterday","this_week"=>"This Week","last_7"=>"Last 7 Days","last_30"=>"Last 30 Days","this_month"=>"This Month","last_month"=>"Last Month","custom"=>"Select Range"); 
  $data= $this->data->get_log(); $items=count($data['feeds']);
  $crm_order=$entry_order=$desc_order=$time_order="up"; 
  $crm_class=$entry_class=$desc_class=$time_class="vx_hide_sort";
  $order=$this->post('order');
    $order_icon= $order == "desc" ? "down" : "up"; 
  if(isset($_REQUEST['orderby'])){
  switch($_REQUEST['orderby']){
  case"crm_id": $crm_order=$order_icon;  $crm_class="";   break;    
  case"entry_id": $entry_order=$order_icon; $entry_class="";    break;    
  case"object": $desc_order=$order_icon; $desc_class="";   break;    
  case"time": $time_order=$order_icon; $time_class="";   break;    
  }          
  }
$bulk_actions=array(""=>__('Bulk Action','contact-form-mailchimp-crm'),"delete"=>__('Delete','contact-form-mailchimp-crm'),
//  'send_to_crm_bulk'=>__('Send to Mailchimp','contact-form-mailchimp-crm'),'send_to_crm_bulk_force'=>__('Force Send to Mailchimp - Ignore filters','contact-form-mailchimp-crm')
);
$base_url=$this->get_base_url();
$objects=$this->get_objects(); 
$statuses=array("1"=>__("Created",'contact-form-mailchimp-crm'),"2"=>__("Updated",'contact-form-mailchimp-crm'),"error"=>__("Failed",'contact-form-mailchimp-crm'),"4"=>__("Filtered",'contact-form-mailchimp-crm'),"5"=>__("Deleted",'contact-form-mailchimp-crm')); 

include_once(self::$path . "templates/logs.php");
  }



/**
* feed link
* 
* @param mixed $id
*/
public function get_feed_link($id=""){
    $tab='feeds';
    if(!empty($id)){
        $tab='edit';
    }
    $str="admin.php?page={$this->id}&tab={$tab}&id={$id}" ;
  return  admin_url( $str );
}  

  /**
  * Field mapping HTML
  * 
  * @param mixed $feed
  * @param mixed $settings
  * @param mixed $refresh
  * @return mixed
  */
  private  function get_field_mapping($feed,$info="",$refresh=false){
  $fields=array(); 
   if($info == ""){
       $account=$this->post('account',$feed);
  $info=$this->get_info($account);
  }

  if(empty($feed['form_id']) || empty($feed['object']))
  return ''; 
  $module=''; $form_id=0;
  if(isset($feed['object']))
  $module=$feed['object'];
  if(isset($feed['form_id']))
  $form_id=$feed['form_id'];
  //
$api_type=isset($info['data']['api']) ? $info['data']['api'] : '';
$info_meta= isset($info['meta']) && is_array($info['meta']) ? $info['meta'] : array(); 
$feed_meta= isset($feed['meta']) && is_array($feed['meta']) ? $feed['meta'] : array(); 
$info_data= isset($info['data']) && is_array($info['data']) ? $info['data'] : array(); 
$id= isset($feed['id']) ? $feed['id'] : ''; 
//
  $meta=isset($feed['data']) && is_array($feed['data']) ? $feed['data'] : array();

  
  $map=isset($meta['map']) && is_array($meta['map']) ? $meta['map'] : array(); 

  $optin_field=isset($meta['optin_field']) ?$meta['optin_field'] : ''; 
  //
    $api_type=$this->post('api',$info_data);   

  if($this->ajax){

  $api=$this->get_api($info);
  $fields=$api->get_crm_fields($module); 

  if(is_array($fields)){ 

  if(!vxcf_mailchimp::$is_pr){
          $temp_fields=array();
       $phone=array('tags','interests');
$n=0;
foreach($fields as $k=>$v){
    $add=true;
if(in_array($k,$phone)){ $add=false; } //remove tags
if($v['type'] == 'phone'){ $add=false; } //remove phones
if( !empty($v['is_custom'])  ){  if( $n>10 ){ $add=false; } $n++; }  //allow first 7 custom fields
if($add){    
$temp_fields[$k]=$v;     
      }
}
$fields=$temp_fields;
}

  $info_meta['fields']=$fields;     
  $info_meta['object']=$module;     
  $info_meta['feed_id']=$this->post('id');   
  $this->update_info( array('meta'=>$info_meta),$info['id']);        
  }   
  }else{
 $fields=$this->post('fields',$feed_meta); 
  }

  if(!is_array($fields)){
  $fields= $fields == "" ? "Error while getting fields" : $fields;   
  ?>
  <div class="error below-h2">
  <p><?php echo wp_kses_post($fields)?></p>
  </div>
  <?php
  return;
  }
  $meta=isset($feed['data']) && is_array($feed['data']) ? $feed['data'] : array();
  
  $map=isset($meta['map']) && is_array($meta['map']) ? $meta['map'] : array(); 

  $optin_field=isset($meta['optin_field']) ?$meta['optin_field'] : ''; 
  
  $vx_op=$this->get_filter_ops(); 
  if(isset($meta['filters']) && is_array($meta['filters'])&& count($meta['filters'])>0){
  $filters=$meta['filters'];    
  }else{
  $filters=array("1"=>array("1"=>array("field"=>"")));   
  }

  $map_fields=array();
  $skip_fields=array();

  foreach($fields as $k=>$v){

      $req=$this->post('req',$v);
      if($req == 'true'){
   $map_fields[$k]=$v;       
      }  

  }
//mapping fields
foreach($map as $field_k=>$field_v){
  if(isset($fields[$field_k])){
  $map_fields[$field_k]=$fields[$field_k];    
  }  
}




  $sel_fields=array(""=>__("Standard Field",'contact-form-mailchimp-crm'),"value"=>__("Custom Value",'contact-form-mailchimp-crm'));
include_once(self::$path . "templates/fields-mapping.php"); 
  }
public function groups($meta,$info){

 if(!empty($info['groups']) && is_array($info['groups'])){ 
 foreach($info['groups'] as $id=>$v){   
    ?>
  <div class="vx_row">
   <div class="vx_col1">
  <label for="crm_sel_camp"><?php echo $v['title'] ?></label>
</div> <div class="vx_col2">
<?php
    if(!empty($v['items'])){
foreach($v['items'] as $k=>$l){
$check= !empty($meta['groups'][$k]) ? 'checked="checked"' : '';    
?><div class="vx_check_item">
<input type="checkbox" <?php echo $check ?> id="meta_item_<?php echo esc_attr($k) ?>" name="meta[groups][<?php echo esc_attr($k) ?>]" value="1">
<label for="meta_item_<?php echo esc_attr($k) ?>"><?php echo $l; ?></label>
</div><?php
}        
    }
?>
</div>
<div class="clear"></div>
</div> <?php }
 }
}
 
  /**
  * Updates feed
  * 
  */
  public  function update_feed(){
  check_ajax_referer('vx_crm_ajax','vx_crm_ajax');
  if(!current_user_can($this->id."_edit_feeds")){ 
  return;   
  }
  $id = $this->post("feed_id");
  $feed = $this->data->get_feed($id);
  $this->data->update_feed(array("is_active"=>$this->post("is_active")),$id);
  } 
  
  /**
  * Update the feed sort order
  *
  * @since  3.1
  * @return void
  */
  public  function update_feed_sort(){
  check_ajax_referer('vx_crm_ajax','vx_crm_ajax');
    if(!current_user_can($this->id."_edit_feeds")){ 
  return;   
  }
  if( empty( $_POST['sort'] ))
  {
  exit(false);
  }
  
  $this->data->update_feed_order($this->post('sort'));
  }
  public function set_logging_supported($plugins) {
      $slug=$this->plugin_dir_name(); 
        $plugins[$slug] = esc_html__('Mailchimp','contact-form-mailchimp-crm');
        return $plugins;
    }
  /**
  * Field map ajax method
  * 
  */
  public  function get_field_map_ajax(){
        check_ajax_referer('vx_crm_ajax','vx_crm_ajax');
  if(!current_user_can($this->id."_read_feeds")){ 
  return;   
  }
$this->ajax=true;
  $msg="";
  if(empty($_REQUEST['module'])){
  $msg=__("Please Choose Object",'contact-form-mailchimp-crm');
  }else  if(empty($_REQUEST['form_id'])){
  $msg=__("Please Choose Form",'contact-form-mailchimp-crm');
  }
  if($msg !=""){
  ?>
  <div class="error below-h2" style="background: #f3f3f3">
  <p><?php echo esc_html($msg); ?></p>
  </div>
  <?php
  die();
  }     
  $module=$this->post('module');
   $form_id=$this->post('form_id');
  $refresh=$_REQUEST['refresh'] == "true" ? true: false;
    $id=$this->post('id');
  $feed=$this->data->get_feed($id);
    $this->account=$account=$this->post('account');

  $info=$this->get_info($account); 
/*  $object=$this->post('object',$feed);
  if(!$refresh && $object != $module){
   $refresh=true;   
  } */
  $feed['form_id']=$form_id;  
  $feed['object']=$module;  
  $this->get_field_mapping($feed,$info,true); 
  die();
  } 
  public  function get_field_map_object_ajax(){
        check_ajax_referer('vx_crm_ajax','vx_crm_ajax');
  if(!current_user_can($this->id."_read_feeds")){ 
  return;   
  }
   $this->ajax=true;
  $msg="";
  if(empty($_REQUEST['account'])){
  $msg=__("Please Choose Account",'contact-form-mailchimp-crm');
  }else  if(empty($_REQUEST['form_id'])){
  $msg=__("Please Choose Form",'contact-form-mailchimp-crm');
  }
  if($msg !=""){
  ?>
  <div class="error below-h2" style="margin-top: 20px;">
  <p><?php echo esc_html($msg); ?></p>
  </div>
  <?php
  die();
  }     
  $this->account=$account=$this->post('account');
  $form_id=$this->post('form_id');
    $id=$this->post('id');
    $feed= $this->data->get_feed($id);
      $feed['form_id']=$form_id;
  $info=$this->get_info($account); 
/*  $object=$this->post('object',$feed);
  if(!$refresh && $object != $module){
   $refresh=true;   
  } */
$this->field_map_object($account,$form_id,$feed,$info);
  die();
  }
    /**
  * available operators for custom filters
  * 
  */
  public function get_filter_ops(){
           return array("is"=>"Exactly Matches","is_not"=>"Does Not Exactly Match","contains"=>"(Text) Contains","not_contains"=>"(Text) Does Not Contain","is_in"=>"(Text) Is In","not_in"=>"(Text) Is Not In","starts"=>"(Text) Starts With","not_starts"=>"(Text) Does Not Start With","ends"=>"(Text) Ends With","not_ends"=>"(Text) Does Not End With","less"=>"(Number) Less Than","greater"=>"(Number) Greater Than","less_date"=>"(Date/Time) Less Than","greater_date"=>"(Date/Time) Greater Than","equal_date"=>"(Date/Time) Equals","empty"=>"Is Empty","not_empty"=>"Is Not Empty"); 
  }
  /**
  * crm fields select options
  * 
  * @param mixed $fields
  * @param mixed $selected
  */
  public function crm_select($fields,$selected,$first_empty=true){
  $field_options=""; 
    if($first_empty){ 
  $field_options="<option value=''></option>";
  }
    if(is_array($fields)){
        foreach($fields as $k=>$v){
              if(isset($v['label'])){
  $sel=$selected == $k ? 'selected="selected"' : "";
  $field_options.="<option value='".esc_attr($k)."' ".$sel.">".esc_html($v['label'])."</option>";       
  }
        }
    }
  return $field_options;    
  }
        /**
  * general(key/val) select options
  * 
  * @param mixed $fields
  * @param mixed $selected
  */
  public function gen_select($fields,$selected,$placeholder=""){
  $field_options="<option value=''>".esc_html($placeholder)."</option>";  
    if(is_array($fields)){
        foreach($fields as $k=>$v){
  $sel=$selected == $k ? 'selected="selected"' : "";
  $field_options.="<option value='".esc_attr($k)."' ".$sel.">".esc_html($v)."</option>";      
        }
    }
  return $field_options;    
  }
  public function get_object_feeds($form_id,$account,$object,$skip=''){ 
      
$feeds=$this->data->get_object_feeds($form_id,$account,$object,$skip);
$arr=array();
if(is_array($feeds) && count($feeds)>0){
    foreach($feeds as $k=>$feed){
      if(isset($feed['id'])){
      $arr[$feed['id']]=$feed['name'];    
      }  
    }
}
return $arr;
}
  /**
  * Log detail
  * 
  */
  public function log_detail(){
$log_id=$this->post('id');
$log=$this->data->get_log_by_id($log_id); 
  $data=json_decode($log['data'],true); 
  $response=json_decode($log['response'],true);
    $triggers=array('manual'=>'Submitted Manually','submit'=>'Form Submission','update'=>'Entry Update'
  ,'delete'=>'Entry Deletion','add_note'=>'Entry Note Created','delete_note'=>'Entry Note Deleted');
  $event= empty($log['event']) ? 'manual' : $log['event'];
  $extra=array('Object'=>$log['object']);
  if(isset($triggers[$event])){
    $extra['Trigger']=$triggers[$event];  
  }
  $extra_log=json_decode($log['extra'],true);
  if(is_array($extra_log)){
      $extra=array_merge($extra,$extra_log);
  }
  $error=true; 
  $vx_ops=$this->get_filter_ops();
  $form_id=$this->post('form_id',$log);
  $labels=array("url"=>"URL","body"=>"Search Body","response"=>"Search Response","filter"=>"Filter",'note_object_link'=>'Note Object ID');
  $log_link=$this->link_to_settings('logs').'&log_id='.$log['id']; 
include_once(self::$path . "templates/log.php");
      die();
  }

        /**
     * Get Objects , AJAX method
     * @return null
     */
public function get_objects_ajax(){
    check_ajax_referer('vx_crm_ajax','vx_crm_ajax');
    

    $object=$this->post('object');
    $account=$this->post('account');
      $crm=$this->get_info($account); 

  $objects=$this->get_objects($crm,true); 

$field_options="<option>".esc_html__("Select Object",'contact-form-mailchimp-crm')."</option>"; 
  if(is_array($objects)){
  foreach($objects as $k=>$v){
      $sel="";
      if($k == $object){
          $sel='selected="selected"';
      }
  $field_options.="<option value='".esc_attr($k)."' ".$sel.">".esc_attr($v)."</option>";      
  }  
  }
echo   $field_options;

die();
}
  /**
  * Settings page
  * 
  */
  public  function settings_page(){ 
  if(!current_user_can($this->id.'_read_settings')){
  $msg_text=__('You do not have permissions to access this page','contact-form-mailchimp-crm');   
  $this->display_msg('admin',$msg_text); 
  return;
  }
  

  $is_section=apply_filters('add_page_html_'.$this->id,false);

  if($is_section === true){
    return;
}  
  $msgs=array(); $lic_key=false;
  $message=$force_check= false;
   $id=$this->post('id');
   $tooltips=self::$tooltips;
  
   
  if(!empty($_POST[$this->id."_uninstall"])){
  check_admin_referer("vx_nonce");
  if(!current_user_can($this->id."_uninstall")){
  return;
  }    
  $this->uninstall();
  $uninstall_msg=sprintf(__("Contact Form Mailchimp Plugin has been successfully uninstalled. It can be re-activated from the %s plugins page %s.", 'contact-form-mailchimp-crm'),"<a href='plugins.php'>","</a>");
$this->screen_msg($uninstall_msg);
  return;
  }
                


          $meta=get_option($this->type.'_settings',array());

       if(!empty($_POST['save'])){ 
             if(current_user_can($this->id."_edit_settings")){ 

  $meta=isset($_POST['meta']) ? $this->post('meta') : array();
  
   $msgs['submit']=array('class'=>'updated','msg'=>__('Settings Changed Successfully','contact-form-mailchimp-crm'));
  update_option($this->type.'_settings',$meta);
  }      
      } 
     $this->show_msgs($msgs);
    $nonce=wp_create_nonce("vx_nonce"); 
include_once(self::$path . "templates/settings.php");
  } 
  /**
  * Accounts page
  * 
  */
  public  function accounts_page(){ 
  if(!current_user_can($this->id.'_read_settings')){
  $msg_text=__('You do not have permissions to access this page','contact-form-mailchimp-crm');   
  $this->display_msg('admin',$msg_text); 
  return;
  }
  $is_section=apply_filters('add_page_html_'.$this->id,false);

  if($is_section === true){
    return;
}  

  $msgs=array(); $lic_key=false;
  $message=$force_check= false;
   $id=$this->post('id');
   $tooltips=self::$tooltips;
 $offset=$this->time_offset();
  if(!empty($_POST["save"])){ //var_dump($_REQUEST); die(); 
  check_admin_referer("vx_nonce");
  if(!current_user_can($this->id."_edit_settings")){ 
  esc_html_e('You do not have permissions to save settings','contact-form-mailchimp-crm');
  return;   
  }
  $msgs['submit']=array('class'=>'updated','msg'=>__('Settings Changed Successfully','contact-form-mailchimp-crm'));
  $valid_email=true; $crm=$this->post('crm');
  if($this->post('error_email',$crm) !=""){
   $emails=explode(",",$this->post('error_email',$crm));
  foreach($emails as $email){
      $email=trim($email);
    if($email !="" && !$this->is_valid_email($email)){
  $valid_email=false; 
    }  
  }   
  }
  if(!$valid_email){
      $msgs['submit']=array("class"=>"error","msg"=>__('Invalid Email(s)','contact-form-mailchimp-crm'));
  }
   $info=$this->get_info($id); 
   
   if(isset($info['data']) && is_array($info['data']) && is_array($crm)){
       $crm= array_merge($info['data'],$crm);
   }
  /////////////                          
  $this->update_info(array('data'=> $crm),$id);
  $force_check=true;
  ////////////////////
  }                

  $data=$this->get_data_object();
  $new_account_id=$data->get_new_account();
 $page_link=$this->link_to_settings('accounts');
 $new_account=$page_link."&id=".$new_account_id;
  if(!empty($id)){
  $info=$this->get_info($id);  

  if(empty($info)){
   $id="";   
  } }
  if(!empty($id)){   
  $valid_user=false;
  
  
  $api=$this->get_api($info);
 
  if(empty($_POST)){
   $api->timeout="5";   
  }

  $link=$page_link.'&id='.$id;
  if(!$force_check && isset($_POST['vx_test_connection'])){
    $force_check=true;  
  }
  if(!empty($info['data'])){
  $info=$info['data'];  
}
  //
//  $force_check=true;
  $info=$this->validate_api($info,$force_check);
  //var_dump($info); die('----------');
     if($force_check){
       $this->update_info( array("data"=> $info),$id);
  }
    $con_class=$this->post('class',$info);
  if(!empty($con_class)){
      
   $msgs['connection']=array('class'=>$con_class,'msg'=>$info['msg']);
   }
                if(isset($_POST['vx_test_connection'])){  
  if($con_class != "updated" ){
      $msg=__('Connection to Mailchimp is NOT Working','contact-form-mailchimp-crm');  
  }else{
     $msg=__('Connection to Mailchimp is Working','contact-form-mailchimp-crm');   
  }
  $title=__('Test Connection: ','contact-form-mailchimp-crm');
  $msgs['test']=array('class'=>$con_class,'msg'=>'<b>'.$title.'</b>'.$msg);
  }

  }else{
      $accounts=$data->get_accounts();

  }

        $nonce=wp_create_nonce("vx_nonce"); 
include_once(self::$path . "templates/accounts.php");
  } 

    /**
  * Create or edit crm feed page
  * 
  */
  private  function edit_page($fid=""){ 
  if(!current_user_can($this->id.'_read_feeds')){
  esc_html_e('You do not have permissions to access this page','contact-form-mailchimp-crm');    
  return;
  }
  wp_enqueue_style('vx-fonts');
    wp_enqueue_script('vxg-select2' );
  wp_enqueue_style('vxg-select2');
  wp_enqueue_script( 'jquery-ui-sortable');
  $is_section=apply_filters('add_page_html_'.$this->id,false);

  if($is_section === true){
    return;
} 
$msgs=array();
$feed= $this->data->get_feed($fid); 
           //updating meta information
  if(isset($_POST[$this->id."_submit"])){ 
  check_admin_referer("vx_nonce");
  if(!current_user_can($this->id.'_edit_feeds')){
  esc_html_e('You do not have permissions to edit/save feed','contact-form-mailchimp-crm'); 
  return;
  }
  //
  $time = current_time( 'mysql' ,1);
   $feed_update=array("data"=>$this->post("meta"),"name"=>$this->post('name'),"account"=>$this->post('account'),"object"=>$this->post('object'),"form_id"=>$this->post('form_id'),"time"=>$time);
if(!empty($_POST['account'])){
  $info=$this->get_info($this->post('account'));

  if(isset($info['meta']['feed_id']) && isset($info['meta']['fields']) && !empty($info['meta']['feed_id']) && $info['meta']['feed_id'] == $fid ){
 $meta=isset($feed['meta']) && is_array($feed['meta']) ? $feed['meta'] : array();
 $info_meta=$info['meta'];
 $meta['fields']=$info_meta['fields'];
 $feed_update['meta']=$meta;
 unset($info_meta['feed_id']);

 $this->update_info(array('meta'=>$info_meta),$info['id']);
} }
if(is_array($feed_update) && is_array($feed)){
    $feed=array_merge($feed,$feed_update);
} 
  $is_valid=$this->data->update_feed($feed_update,$fid);

  if($is_valid){
      $feed_link=$this->link_to_settings('feeds');
      $msgs['save']=array('class'=>'updated','msg'=>sprintf(__("Feed Updated. %sback to list%s", 'contact-form-mailchimp-crm'), '<a href="'.$feed_link.'">', "</a>"));
  }
  else{
  $msgs['save']=array('class'=>'error','msg'=>__("Feed could not be updated. Please enter all required information below.", 'contact-form-mailchimp-crm'));

  }
  } 
    //getting  API
  $_data=$this->get_data_object();
  $accounts=$_data->get_accounts(true); 
  $forms=$this->get_forms(); 

   
  $account=$this->post('account',$feed);
  $form_id=$this->post('form_id',$feed);
  $info=$this->get_info($account); 
  $config = $this->data->get_feed('new_form');
  $feeds_link=$this->link_to_settings('feeds');  
  $feed_link=$this->link_to_settings('edit');
  $new_feed_link=$feed_link.'&id='.$config['id'];  
// $form_id=$this->post('id');


include_once(self::$path . "templates/feed-account.php");
  
  }
    /**
  * all form fields + addon fields
  * 
  * @param mixed $form_id
  */
  public  function get_all_fields($form_id){
      if($this->fields ){
     return $this->fields;     
      }

$tags=$this->get_form_fields($form_id); 

if(is_array($tags)){
  foreach($tags as $id=>$tag){
   $fields[$id]=array('id'=>$id,'label'=>$tag['label']);    
   }   
    
}  
  $fields['__vx_id']=array('id'=>'__vx_id','label'=>__('Entry ID','contact-form-mailchimp-crm')); 
$fields['_vx_form_id']=array('id'=>'_vx_form_id','label'=>__('Form ID','contact-form-mailchimp-crm')); 
$fields['_vx_form_name']=array('id'=>'_vx_form_title','label'=>__('Form Title','contact-form-mailchimp-crm')); 

$fields['_vx_url']=array('id'=>'_vx_url','label'=>__('Page URL','contact-form-mailchimp-crm')); 
$fields['_vx_created']=array('id'=>'_vx_created','label'=>__('Entry Created','contact-form-mailchimp-crm')); 
$fields['_vx_updated']=array('id'=>'_vx_updated','label'=>__('Entry Updated','contact-form-mailchimp-crm'));

  $this->fields=$fields=array('cf'=>array("title"=>__('Contact Form Fields','contact-form-mailchimp-crm'),"fields"=>$fields));
  
  if($this->do_actions()){ 

  $this->fields=$fields=apply_filters('vx_mapping_standard_fields',$this->fields);
  }
  return $fields;
  }
    /**
  * contact form fields label
  * 
  * @param mixed $form_id
  * @param mixed $key
  */
  public function get_gf_field_label($form_id,$key){
  $fields=$this->get_all_fields($form_id);  
  $label=$key;
  if(is_array($fields)){
  foreach($fields as $ke=>$field){
      if(isset($field['fields']) && is_array($field['fields']) ){
          foreach($field['fields'] as $k=>$v){     
                if($ke == "gf"){
   $k=$v[0];   
  }
  if($k == $key && isset($field['fields'][$k])){
    if($ke == "gf"){
   $label=$v[1];     
    }else if(isset($field['fields'][$k]['label'])){
   $label= $field['fields'][$k]['label'];     
    }  
  if(!empty($label)){
      return $label;
  }
  }
  
          }
      }
      
  }}
  return $label;
  }
  /**
  * contact form field select options
  * 
  * @param mixed $form_id
  * @param mixed $selected_val
  */
  public  function  form_fields_options($form_id,$sel_val=""){
  if($this->fields == null){
  $this->fields=$this->get_all_fields($form_id);
  }  
        
      if(!is_array($sel_val)){
$sel_val=array($sel_val);
      } 
  $sel="<option value=''></option>";
  $fields=$this->fields; 
  if(is_array($fields)){
  foreach($fields as $key=>$fields_arr){
if(is_array($fields_arr['fields'])){
    $sel.="<optgroup label='".$fields_arr['title']."'>";
      foreach($fields_arr['fields'] as $k=>$v){
          $option_k=$k;
          $option_name=$v;

    $option_name=$v['label'];  

          $select="";
              if( in_array($option_k,$sel_val)){

  $select='selected="selected"';

  }
  $sel.='<option value="'.esc_attr($option_k).'" '.$select.'>'.esc_html($option_name).'</option>';    
  }    }
  }}  
  return $sel;    
  }  
  /**
  * field mapping box's Contents
  * 
  */
  public function field_map_object($account,$form_id,$feed,$info) { 

  //get objects from crm
  $objects=$this->get_objects($info); 
  
  if(empty($feed['object'])){
      $feed['object']="";
  }
  if(!empty($feed['object']) && is_array($objects) && !isset($objects[$feed['object']])){
  $feed['object']="";     
  }  
  $modules=array(""=>__("Select Object",'contact-form-mailchimp-crm'));
  if(isset($objects) && is_array($objects)){
  foreach($objects as $k=>$v){
  $modules[$k]=$v;     
  }   
  } 
  $meta=$this->post('meta',$info);
  $object=$this->post('object',$feed); 
 include_once(self::$path."templates/feed-object.php");  
  }
  /**
  * validate API
  * 
  * @param mixed $info
  * @param mixed $force_check
  */
  public function validate_api($info,$check=false){
  $time=current_time('timestamp'); 

  if($check){ 
  $api=$this->get_api(array('data'=>$info));
  $info=$api->get_token(); 
  } 

  if(isset($info['valid_token'])  && $info['valid_token']!="") { 
  $msg=__( 'Successfully Connected to Mailchimp','contact-form-mailchimp-crm' );
     if(isset($info['_time'])){
       $msg.=" - ".date('F d, Y h:i:s A',$info['_time']);
   }
      $info['msg']=$msg; 
  $info['class']="updated";     
  
  }else{
  $info['class']="";  
  if(!empty($info['api_key'])){
  $info['msg']= !empty($info['error']) ? $info['error'] : 'API Token is Not Valid'; 
  $info['class']="error"; 
  }
        }
    if($check){ 
  $info['_time']=$time;     
  }
  return $info;
  }
  public function get_forms(){

      $forms=array();
      global $vxcf_form;
if(is_object($vxcf_form) && method_exists($vxcf_form,'get_forms')){
    $forms=$vxcf_form->get_forms();  
}else{
      if(class_exists('WPCF7_ContactForm')){
    if( !function_exists('wpcf7_contact_forms') ) {
        $cf_forms = get_posts( array(
            'numberposts' => -1,
            'orderby' => 'ID',
            'order' => 'ASC',
            'post_type' => 'wpcf7_contact_form' ) );
    }
    else {
        $forms_cf = wpcf7_contact_forms();
        $cf_forms=array();
        if(count($forms_cf)>0){
            foreach($forms_cf as $k=>$f){
             $v=new stdClass();
               if( isset( $f->id ) ) {
                    $v->ID = $f->id;    // as serialized option data
                } 
                 if( isset( $f->title ) ) {
                    $v->post_title = $f->title;    // as serialized option data
                }   
            $cf_forms[]=$v;
            }
        }
    }

    if(is_array($cf_forms) && count($cf_forms)>0){
 foreach($cf_forms as $form){
     if(!empty($form->post_title)){
  $forms_arr[$form->ID]=$form->post_title;       
     }
 } 
        $forms['cf']=array('label'=>'Contact Form 7','forms'=>$forms_arr); 
    } 
 ///////   
    }
}
$forms=apply_filters('vx_add_crm_form',$forms);

   return $forms;
  }
   public function add_msg($msg,$level='updated'){
   $option=get_option($this->id.'_msgs',array());
   if(!is_array($option)){
   $option=array();    
   }
   $option[]=array('msg'=>$msg,'class'=>$level);
 update_option($this->id.'_msgs',$option);  
 }
    public function show_msgs($msgs=""){ 
/* $option=get_option($this->id.'_msgs',array());
 if(is_array($option) && count($msgs)>0){
//     $msgs=array_merge($msgs,$option);
 } */
   if(is_array($msgs) && count($msgs)>0){
   foreach($msgs as $msg){
     $this->screen_msg($msg['msg'],$msg['class']);  
   }
 /* if(empty($option)){ 
//  update_option($this->id.'_msgs',array());
  } */ 
   }  
 } 
    /**
  * Tooltip image
  * 
  * @param mixed $str
  */
  public function tooltip($str){
   
  if(!isset(self::$tooltips[$str])){return;}
  ?>
  <i class="vx_icons vxc_tips fa fa-question-circle" data-tip="<?php echo esc_attr(self::$tooltips[$str]) ?>"></i> 
  <?php  
  }
}
}
new vxcf_mailchimp_pages();