Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Nov 30, 2020
1 parent 964dc8f commit 2a08054
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 54 deletions.
6 changes: 2 additions & 4 deletions CRM/Membershipchurnchart/Page/MembershipChurnChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
class CRM_Membershipchurnchart_Page_MembershipChurnChart extends CRM_Core_Page {

function run() {

CRM_Core_Session::singleton()->replaceUserContext(CRM_Utils_System::url('civicrm/membership/membershipchurnchart', "reset=1"));

$chartData = [];
Expand All @@ -14,9 +13,8 @@ function run() {
$sql = "SELECT * FROM civicrm_membership_churn_monthly_table ORDER BY year, month";
$sqlRes = CRM_Core_DAO::executeQuery($sql);

$totalStats = $years = [];
$years = [];
while($sqlRes->fetch()) {

$years[$sqlRes->year] = $sqlRes->year;

$data = [];
Expand Down Expand Up @@ -62,7 +60,7 @@ function run() {
$this->assign('endYearRange', $endYearOptions);

// Membership types filter
$memTypes = CRM_Membershipchurnchart_Utils::getAllmembershipTypes();
$memTypes = CRM_Member_PseudoConstant::membershipType();
$this->assign('memTypes', $memTypes);

// All status to be displayed as legends
Expand Down
54 changes: 9 additions & 45 deletions CRM/Membershipchurnchart/Utils.php
Original file line number Diff line number Diff line change
@@ -1,68 +1,36 @@
<?php

require_once 'CRM/Core/Page.php';

class CRM_Membershipchurnchart_Utils {
/**
* CiviCRM API wrapper
*
* @param string $entity
* @param string $action
* @param string $params
*
* @return array of API results
*/
public static function CiviCRMAPIWrapper($entity, $action, $params) {

if (empty($entity) || empty($action) || empty($params)) {
return;
}

try {
$result = civicrm_api3($entity, $action, $params);
}
catch (Exception $e) {
CRM_Core_Error::debug_log_message('CiviCRM API Call Failed');
CRM_Core_Error::debug_var('CiviCRM API Call Error', $e);
return;
}

return $result;
}

/**
* Function to prepare churn table data
*/
public static function prepareChurnTable() {

// Truncate membership churn data table
CRM_Core_DAO::executeQuery("TRUNCATE TABLE `civicrm_membership_churn_table`");

// Get membership churn chart settings
$settingsStr = CRM_Core_BAO_Setting::getItem('CiviCRM Membershipchurnchart Settings', 'membershipchurnchart_settings');
$settingsArray = unserialize($settingsStr);
// Check if start date is set in settings page
if (!empty($settingsArray['start_year'])) {
$start_year = $settingsArray['start_year'];
} else {
$startYear = (int) \Civi::settings()->get('membershipchurnchart_startyear');
if (empty($startYear)) {
// Start year not set
// So get min and max year from civicrm_membership table
$sql = "SELECT YEAR(MIN(join_date)) as min_join_date FROM civicrm_membership";
$sqlRes = CRM_Core_DAO::executeQuery($sql);
$sqlRes->fetch();
$start_year = $sqlRes->min_join_date;
$startYear = $sqlRes->min_join_date;
\Civi::settings()->set('membershipchurnchart_startyear', $startYear);
}

$end_year = date('Y'); // Current year
$end_month = date('n'); // Current month

// Get data for one month before the start year, to get the brought forward value
$startMonthYear = "{$start_year}-01";
$startMonthYear = "{$startYear}-01";
$previous_month = date("m", strtotime($startMonthYear. " -1 months"));
$previous_year = date("Y", strtotime($startMonthYear. " -1 months"));
self::insertDataIntoChurnTable($previous_year, $previous_month);

for ($i = $start_year; $i <= $end_year; $i++) {
for ($i = $startYear; $i <= $end_year; $i++) {
for ($j = 1; $j <= 12; $j++) {
self::insertDataIntoChurnTable($i, $j);
}
Expand Down Expand Up @@ -149,13 +117,12 @@ public static function prepareChurnTable() {
// to get brought forward value
$deleteSql = "DELETE FROM civicrm_membership_churn_monthly_table WHERE year < %1";
$deleteParams = [
1 => [$start_year, 'Integer'],
1 => [$startYear, 'Integer'],
];
CRM_Core_DAO::executeQuery($deleteSql, $deleteParams);
}

public static function insertDataIntoChurnTable($year, $month) {

if (empty($year) || empty($month)) {
return;
}
Expand Down Expand Up @@ -219,8 +186,8 @@ public static function insertDataIntoChurnTable($year, $month) {
CRM_Core_DAO::executeQuery($rejoinedSql);
}

public static function getAllMemberStatusesForChart(){
return [/*'Brought Forward',*/'Current', 'Joined', 'Resigned', 'Rejoined'];
public static function getAllMemberStatusesForChart() {
return ['Current', 'Joined', 'Resigned', 'Rejoined'];
}

public static function getMinChurnValuesForYaxis($row){
Expand All @@ -237,7 +204,4 @@ public static function getMinChurnValuesForYaxis($row){
return $allChurns;
}

public static function getAllmembershipTypes(){
return CRM_Member_PseudoConstant::membershipType();
}
}
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: uk.co.vedaconsulting.membershipchurnchart
Copyright (C) 2016, FIXME <FIXME>
Copyright (C) 2020, CiviCRM LLC
Licensed under the GNU Affero Public License 3.0 (below).

-------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion settings/membershipchurnchart.setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'name' => 'membershipchurnchart_startyear',
'type' => 'String',
'html_type' => 'text',
'default' => '2010',
'default' => '',
'is_domain' => 1,
'is_contact' => 0,
'title' => E::ts('Start Year'),
Expand Down
3 changes: 0 additions & 3 deletions sql/auto_uninstall.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
-- Delete churn data tables
DROP TABLE IF EXISTS `civicrm_membership_churn_table`;
DROP TABLE IF EXISTS `civicrm_membership_churn_monthly_table`;

-- Delete scheduled job
DELETE FROM civicrm_job WHERE api_entity = 'membershipchurnchart' AND api_action = 'preparechurntable';

0 comments on commit 2a08054

Please sign in to comment.