/**
  * Process bulk actions for deleting
  */
 function process_bulk_action()
 {
     global $wpdb;
     $ids_for_action = '';
     $hashes_for_action = '';
     // @TODO Fix the delete logic
     if (strstr($this->current_action(), 'delete')) {
         if ('delete_selected' === $this->current_action()) {
             $contact = $_GET['contact'];
             for ($i = 0; $i < count($contact); $i++) {
                 $ids_for_action .= esc_url($contact[$i]);
                 if ($i != count($_GET['contact']) - 1) {
                     $ids_for_action .= ',';
                 }
             }
         } else {
             if ('delete_all' === $this->current_action()) {
                 $contacts = $this->get_contacts();
                 foreach ($contacts as $contact) {
                     $ids_for_action .= $contact['ID'] . ',';
                 }
                 $ids_for_action = rtrim($ids_for_action, ',');
             }
         }
         $q = $wpdb->prepare("SELECT hashkey FROM {$wpdb->ir_leads} WHERE lead_id IN ( " . $ids_for_action . " ) ", "");
         $hashes = $wpdb->get_results($q);
         if (count($hashes)) {
             foreach ($hashes as $hash) {
                 $hashes_for_action .= "'" . $hash->hashkey . "',";
             }
             $hashes_for_action = rtrim($hashes_for_action, ',');
             $q = $wpdb->prepare("UPDATE {$wpdb->ir_pageviews} SET pageview_deleted  = 1 WHERE lead_hashkey IN (" . $hashes_for_action . ") ", "");
             $delete_pageviews = $wpdb->query($q);
             $q = $wpdb->prepare("UPDATE {$wpdb->ir_submissions} SET form_deleted  = 1 WHERE lead_hashkey IN (" . $hashes_for_action . ") ", "");
             $delete_submissions = $wpdb->query($q);
             $q = $wpdb->prepare("UPDATE {$wpdb->ir_leads} SET lead_deleted  = 1 WHERE lead_id IN (" . $ids_for_action . ") ", "");
             $delete_leads = $wpdb->query($q);
             $q = $wpdb->prepare("UPDATE {$wpdb->ir_tag_relationships} SET tag_relationship_deleted = 1 WHERE contact_hashkey IN (" . $hashes_for_action . ") ", "");
             $delete_tags = $wpdb->query($q);
         }
     }
     if (isset($_POST['bulk_edit_lists'])) {
         $q = $wpdb->prepare("SELECT tag_id FROM {$wpdb->ir_tags} WHERE tag_slug = %s", $_POST['bulk_selected_tag']);
         $list_id = $wpdb->get_var($q);
         if (empty($_POST['inboundrocket_selected_contacts'])) {
             $contacts = $this->get_contacts();
             foreach ($contacts as $contact) {
                 $ids_for_action .= $contact['ID'] . ',';
             }
             $ids_for_action = rtrim($ids_for_action, ',');
         } else {
             $ids_for_action = $_POST['inboundrocket_selected_contacts'];
         }
         $q = $wpdb->prepare("\n                SELECT \n                    l.hashkey, l.lead_email,\n                    ( SELECT ltr.tag_id FROM {$wpdb->ir_tag_relationships} ltr WHERE ltr.tag_id = %d AND ltr.contact_hashkey = l.hashkey GROUP BY ltr.contact_hashkey ) AS tag_set \n                FROM \n                    {$wpdb->ir_leads} l\n                WHERE \n                    l.lead_id IN ( " . $ids_for_action . " ) AND l.lead_deleted = 0 GROUP BY l.lead_id", $list_id);
         $contacts = $wpdb->get_results($q);
         $insert_values = '';
         $contacts_to_update = '';
         if (count($contacts)) {
             foreach ($contacts as $contact) {
                 if ($contact->tag_set === NULL) {
                     $insert_values .= '(' . $list_id . ', "' . $contact->hashkey . '"),';
                 } else {
                     $contacts_to_update .= "'" . $contact->hashkey . "',";
                 }
             }
         }
         if ($_POST['bulk_edit_list_action'] === 'add_list') {
             if ($insert_values) {
                 $q = "INSERT INTO {$wpdb->ir_tag_relationships} ( tag_id, contact_hashkey ) VALUES " . rtrim($insert_values, ',');
                 $wpdb->query($q);
             }
             if ($contacts_to_update) {
                 // update the relationships for the contacts that exist already making sure to set all the tag_relationship_deleted = 0
                 $q = $wpdb->prepare("UPDATE {$wpdb->ir_tag_relationships} SET tag_relationship_deleted = 0 WHERE tag_id = %d AND contact_hashkey IN ( " . rtrim($contacts_to_update, ',') . ") ", $list_id);
                 $wpdb->query($q);
             }
             // Bulk push all the email addresses for the tag to the MailChimp API
             $tagger = new IR_Lead_List_Editor($list_id);
             $tagger->push_contacts_to_lead_list($list_id);
         } else {
             if ($contacts_to_update) {
                 // "Delete" the existing tags only
                 $q = $wpdb->prepare("UPDATE {$wpdb->ir_tag_relationships} SET tag_relationship_deleted = 1 WHERE tag_id = %d AND contact_hashkey IN ( " . rtrim($contacts_to_update, ',') . ") ", $list_id);
                 $wpdb->query($q);
             }
         }
     }
 }
    /**
     * Creates list table for Lead Lists page
     *
     */
    function inboundrocket_render_tag_list_page()
    {
        global $wp_version;
        if ($this->action == 'delete_list') {
            $list_id = isset($_GET['tag']) ? $_GET['tag'] : FALSE;
            $tagger = new IR_Lead_List_Editor($list_id);
            $tagger->delete_list($list_id);
        }
        //Create an instance of our package class...
        $inboundrocketTagsTable = new IR_Lead_List_Table();
        // Process any bulk actions before the contacts are grabbed from the database
        $inboundrocketTagsTable->process_bulk_action();
        //Fetch, prepare, sort, and filter our data...
        $inboundrocketTagsTable->data = $inboundrocketTagsTable->get_lead_lists();
        $inboundrocketTagsTable->prepare_items();
        ?>
        <div class="inboundrocket-contacts">

            <?php 
        $this->inboundrocket_header('' . __('Manage Inbound Rocket Lead Lists', 'inboundrocket') . ' <a href="' . wp_nonce_url(admin_url('/admin.php?page=inboundrocket_lead_lists&action=add_list')) . '" class="add-new-h2">' . __('Add New', 'inboundrocket') . '</a>', 'inboundrocket-contacts__header');
        ?>
            
            <div class="">

                <!-- Forms are NOT created automatically, so you need to wrap the table in one to use features like bulk actions -->
                <form id="" method="GET">
                    <input type="hidden" name="page" value="<?php 
        echo esc_attr($_REQUEST['page']);
        ?>
" />
                    
                    <div class="inboundrocket-contacts__table">
                        <?php 
        $inboundrocketTagsTable->display();
        ?>
                    </div>

                    <input type="hidden" name="contact_type" value="<?php 
        echo isset($_GET['contact_type']) ? esc_attr($_GET['contact_type']) : '';
        ?>
"/>
                   
                    <?php 
        if (isset($_GET['filter_content'])) {
            ?>
                        <input type="hidden" name="filter_content" value="<?php 
            echo isset($_GET['filter_content']) ? esc_attr($_GET['filter_content']) : '';
            ?>
"/>
                    <?php 
        }
        ?>

                    <?php 
        if (isset($_GET['filter_action'])) {
            ?>
                        <input type="hidden" name="filter_action" value="<?php 
            echo isset($_GET['filter_action']) ? esc_attr($_GET['filter_action']) : '';
            ?>
"/>
                    <?php 
        }
        ?>

                </form>
                
            </div>

        </div>

        <?php 
    }