<commit_before><?php

require_once 'contactcampaign.civix.php';












/**
 * Implements hook_civicrm_tabset().
 *
 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
 */
function contactcampaign_civicrm_tabset($tabsetName, &$tabs, $context) {
  //check if the tabset is Contact Summary Page
  if ($tabsetName == 'civicrm/contact/view') {
          $contactId = $context['contact_id'];
    $url = 'civicrm/campaignlist';

    $tabs[] = array( 'id'    => 'contactCampaignTab',
        'url'   => $url,
        'qs' => 'cid=%%$contactId%%',
        'title' => 'Contact Campaign',
        'weight' => 300,
    );
  }
}

/**
 * Implements hook_civicrm_dashboard().
 *
 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
 */
function contactcampaign_civicrm_dashboard( $contactID, &$contentPlacement )
{
    $contentPlacement = 3;
    //Declare campaigns
    $campaigns = [];

    try {
        //define Params
        $params = array(
            'contact_id' => $contactID,

            'api.ContributionSoft.get' => [
                'pcp_id' => '$value.id'
            ],
            'api.ContributionPage.get' => [
                'id' => '$value.page_id'
            ]
        );

          // Making Api Calls
        $campaigns = civicrm_api3('PCP', 'get', $params);
        $campaigns = $this->generatePcpList($campaigns);

    } catch (CiviCRM_API3_Exception $e) {
        $error = $e->getMessage();
    }

    return array(
        'Campaign List' => $campaigns,
    );

}
function generatePcpList($campaigns)
{
    if ($campaigns['count']){

        $campaignList = '<table>';
        $campaignList .= '<tr><th>Title</th><th>Status</th><th>Contribution Page / Event</th><th>Number of Contributions</th>
                            <th>Amount Raised</th><th>Target Amount</th>
                              <th></th></tr>';

    foreach ($campaigns['values'] as $key => $campaign) {
        $pcpStatus = CRM_Contribute_PseudoConstant::pcpStatus();

        // page id i.e contribution page or event
        $PageId = $campaign['page_id'];

        // page type  i.e contribution page or event
        $page_type = $campaign['page_type'];

        //link to edit page
        $editAction = CRM_Utils_System::url('civicrm/pcp/info', 'action=update&reset=1&id=' . $PageId . '&context=dashboard');

        if ($page_type == 'contribute') {
            $pageUrl = CRM_Utils_System::url('civicrm/' . $page_type . '/transact', 'reset=1&id=' . $PageId);
        } else {
            $pageUrl = CRM_Utils_System::url('civicrm/' . $page_type . '/register', 'reset=1&id=' . $PageId);
        }

        //PCP title
         $title = $campaign['title'];

        //add status
        $status = $pcpStatus[$campaign['status_id']];

        #check/get number of contribution for campaign
        $thereIs_contributions = $campaign["api.ContributionSoft.get"]['count'];

        if($thereIs_contributions){
            //contributions
            $contributions = $campaign["api.ContributionSoft.get"]['values'];

            //amount raised
            $amount_raised = array_sum(array_column($contributions, 'amount'));
            //raisedAmount
            $raisedAmount = CRM_Utils_Money::format($amount_raised, $campaign['currency']);

            //targetAmount
            $targetAmount   = CRM_Utils_Money::format($campaign['goal_amount'], $campaign['currency']);

            //no of contributions
            $noContributions = $thereIs_contributions;
        }

        $campaignList .= '<tr>';
        $campaignList .= '<td><a href="' . $pageUrl . '">' . $title . '</a></td>';
        $campaignList .= '<td>' . $status . '</td>';
        $campaignList .= '<td>'. $page_type.'</td>';
        $campaignList .= '<td>'. $noContributions.'</td>';
        $campaignList .=  '<td>'.$raisedAmount.'</td>';
        $campaignList .=  '<td>'.$targetAmount.'</td>';
        $campaignList .= '<td><a href="' . $editAction . '"> ' . 'Edit' . '</a></td>';

        $campaignList .= '</tr>';

    };

    $campaignList .= '</table>';
}
    return $campaignList;
}


/**
 * Implements hook_civicrm_config().
 *
 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
 */<commit_msg>removed generatPcpList and general debug<commit_after><?php

require_once 'contactcampaign.civix.php';

/**
 * Implements hook_civicrm_tabset().
 *
 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
 */
function contactcampaign_civicrm_tabset($tabsetName, &$tabs, $context) {
  //check if the tabset is Contact Summary Page
  if ($tabsetName == 'civicrm/contact/view') {
          $contactId = $context['contact_id'];
    $url = 'civicrm/campaignlist';

    $tabs[] = array( 'id'    => 'contactCampaignTab',
        'url'   => $url,
        'qs' => 'cid=%%$contactId%%',
        'title' => 'Contact Campaign',
        'weight' => 300,
    );
  }
}



/**
 * Implements hook_civicrm_dashboard().
 *
 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
 */
function contactcampaign_civicrm_dashboard( $contactID, &$contentPlacement )
{
    $contentPlacement = 3;

    try {
        //define Params
       $params = array(
            'contact_id' => $contactID,

            'api.ContributionSoft.get' => [
                'pcp_id' => '$value.id'
            ],
            'api.ContributionPage.get' => [
                'id' => '$value.page_id'
            ]
        );

               // Making Api Calls
               $campaigns = civicrm_api3('PCP', 'get', $params);
        if ($campaigns['count']){

            $campaignList = '<table>';
            $campaignList .= '<tr><th>Title</th><th>Status</th><th>Contribution Page / Event</th><th>Number of Contributions</th>
                                     <th>Amount Raised</th><th>Target Amount</th>
                                      <th></th></tr>';

            foreach ($campaigns['values'] as $campaign) {
                $pcpStatus = CRM_Contribute_PseudoConstant::pcpStatus();

                // page id i.e contribution page or event
                $PageId = $campaign['page_id'];

                // page type  i.e contribution page or event
                $page_type = $campaign['page_type'];

                //link to edit page
                $editAction = CRM_Utils_System::url('civicrm/pcp/info', 'action=update&reset=1&id=' . $PageId . '&context=dashboard');

                if ($page_type == 'contribute') {
                    $pageUrl = CRM_Utils_System::url('civicrm/' . $page_type . '/transact', 'reset=1&id=' . $PageId);
                } else {
                    $pageUrl = CRM_Utils_System::url('civicrm/' . $page_type . '/register', 'reset=1&id=' . $PageId);
                }

                //PCP title
                $title = $campaign['title'];

                //add status
                $status = $pcpStatus[$campaign['status_id']];

                #check/get number of contribution for campaign
                $thereIs_contributions = $campaign["api.ContributionSoft.get"]['count'];

                if($thereIs_contributions){
                    //contributions
                    $contributions = $campaign["api.ContributionSoft.get"]['values'];

                    //amount raised
                    $amountRaised = array_sum(array_column($contributions, 'amount'));
                    //raisedAmount
                    $raisedAmount = CRM_Utils_Money::format($amountRaised, $campaign['currency']);

                    //targetAmount
                    $targetAmount   = CRM_Utils_Money::format($campaign['goal_amount'], $campaign['currency']);

                    //no of contributions
                    $noContributions = $thereIs_contributions;
                }

                $campaignList .= '<tr>';
                $campaignList .= '<td><a href="' . $pageUrl . '">' . $title . '</a></td>';
                $campaignList .= '<td>' . $status . '</td>';
                $campaignList .= '<td>'. $page_type.'</td>';
                $campaignList .= '<td>'. $noContributions.'</td>';
                $campaignList .=  '<td>'.$raisedAmount.'</td>';
                $campaignList .=  '<td>'.$targetAmount.'</td>';
                $campaignList .= '<td><a href="' . $editAction . '"> Edit </a></td>';

                $campaignList .= '</tr>';

            };

            $campaignList .= '</table>';


        }else{
            $campaignList = 'user has not created a pcp';
        }


    } catch (CiviCRM_API3_Exception $e) {
        $error = $e->getMessage();
    }

    return array(
        $campaignList
    );

}

/**
 * Implements hook_civicrm_config().
 *
 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
 */
{'fraction_non_alphanumeric': 0.1666096866096866, 'fraction_numerical': 0.0020512820512820513, 'mean_word_length': 2.0567746429815394, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 104, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2598033', 'n_tokens_mistral': 2834, 'n_tokens_neox': 2611, 'n_words': 452}