<?php

$reports["_MAILCHIMP_CAMPAIGN_CLICKS"] = Array( 
  "ClassName" => "MailChimpCampaignClicks", 
  "Category" => "_MAILCHIMP", 
  "Options" => "displaymode",
  "Distribution" => "Premium",
  "EmailAlerts" => false,
  "Update" => false,
  "Filename" => "mailchimp_campaign_clicks" 
); 


use DrewM\MailChimp\MailChimp;

class MailChimpCampaignClicks extends Report {

    function Settings() {
        $this->DisplayModes = "table,barchart";
        $this->DefaultDisplay = "barchart";
        $this->columnDefinitions[] = array("Label" => "Title");
        $this->columnDefinitions[] = array("Label" => "Clicks Total");
        $this->columnDefinitions[] = array("Label" => "Unique Clicks");
        $this->columnDefinitions[] = array("Label" => "Unique Subscriber Clicks");
        $this->columnDefinitions[] = array("Label" => "Click Rate");
        $this->help = "This will show the clicks for each campaign for mailchimp.";
        $old_api_key = $this->profile->GetOtherSettings("MailChimpApiKey", "");
        if(isset($this->ApiKey) && $this->ApiKey != $old_api_key) {
           $this->profile->SetOtherSettings("MailChimpApiKey", $this->ApiKey);
        } else {
            $this->ApiKey = $old_api_key;
        }
    }
    function DisplayCustomForm(){
        $form = "<div class='form-group'>" .
                "<label for='author'>API Key:</label>
                <input type='text' name='ApiKey'
                value='".@$this->ApiKey."' class='report_option_field form-control' /> " .
                "</div>";
        return $form;
    } 
    function GetReportData(){
        include_once 'includes/MailChimp.php';

        $mc = new MailChimp($this->ApiKey);

        $mc->verify_ssl = false; 

        $result = $mc->get('reports');
        
        $data = [];

        if(empty($result)){
            return $data;
        }

        $i = 0;
        foreach($result['reports'] as $campaign){
            $data[$i][0] = $campaign['campaign_title']; 
            $data[$i][1] = $campaign['clicks']['clicks_total']; 
            $data[$i][2] = $campaign['clicks']['unique_clicks']; 
            $data[$i][3] = $campaign['clicks']['unique_subscriber_clicks'];
            $data[$i][4] = $campaign['clicks']['click_rate'];
            $i++;   
        }
        return $data;
    }  
    function DisplayReport(){
        if (empty($this->ApiKey)) {
            echoNotice($this->SettingsButton(). "The Key field is empty. <br><br> for more information: check <a href='https:///kb.mailchimp.com/accounts/management/about-api-keys?utm_source=mc-api&utm_medium=docs&utm_campaign=apidocs&_ga=1.268391566.1298809865.1456133150' target='_blank'>the documentation</a>. "
            );
            return false;
        }elseif(strpos($this->ApiKey, '-') === false){
            echoWarning($this->SettingsButton(). "The Key field is invalid. <br><br> for more information: check <a href='https:///kb.mailchimp.com/accounts/management/about-api-keys?utm_source=mc-api&utm_medium=docs&utm_campaign=apidocs&_ga=1.268391566.1298809865.1456133150' target='_blank'>the documentation</a>. "
            );
            return false;
        }
        else{
                parent::DisplayReport();
        }    
    }       
}
?>