Skip to content

Commit

Permalink
Merge pull request #4 from mjwconsult/updates
Browse files Browse the repository at this point in the history
Fixes and updates
  • Loading branch information
veda-consulting authored Jan 26, 2021
2 parents ee65ffc + 09f21a3 commit 71652e1
Show file tree
Hide file tree
Showing 18 changed files with 1,310 additions and 1,089 deletions.
90 changes: 10 additions & 80 deletions CRM/Membershipchurnchart/Form/Settings.php
Original file line number Diff line number Diff line change
@@ -1,98 +1,28 @@
<?php

require_once 'CRM/Core/Form.php';
use CRM_Membershipchurnchart_ExtensionUtil as E;

/**
* Form controller class
*
* @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
*/
class CRM_Membershipchurnchart_Form_Settings extends CRM_Core_Form {

function buildQuickForm() {
class CRM_Membershipchurnchart_Form_Settings extends CRM_Admin_Form_Generic {

$settingsStr = CRM_Core_BAO_Setting::getItem('CiviCRM Membershipchurnchart Settings', 'membershipchurnchart_settings');

$settingsArray = unserialize($settingsStr);

// Chart start year
$this->add(
'text',
'start_year',
ts('Start Year'),
array('size' => 6),
TRUE
);

$this->addButtons(array(
array(
'type' => 'submit',
'name' => ts('Submit'),
'isDefault' => TRUE,
),
));

$this->setDefaults($settingsArray);

$this->addFormRule( array( 'CRM_Membershipchurnchart_Form_Settings', 'formRule' ) );

// export form elements
$this->assign('elementNames', $this->getRenderableElementNames());
public function buildQuickForm() {
parent::buildQuickForm();
$this->addFormRule(['CRM_Membershipchurnchart_Form_Settings', 'formRule']);
}

static function formRule( $values ){
$errors = array();
public static function formRule($values) {
$errors = [];

// Make sure the start year is not in future
if ($values['start_year'] > date('Y')) {
$errors['start_year'] = ts( "Start year should be current year or in past." );
if ($values['membershipchurnchart_startyear'] > date('Y')) {
$errors['membershipchurnchart_startyear'] = E::ts('Start year should be current year or in past.');
}

return $errors;
}

function postProcess() {
$values = $this->exportValues();

$settingsArray = array();
$settingsArray['start_year'] = $values['start_year'];
$settingsStr = serialize($settingsArray);

CRM_Core_BAO_Setting::setItem($settingsStr,
'CiviCRM Membershipchurnchart Settings',
'membershipchurnchart_settings'
);

// Call API to refresh churn data table
CRM_Membershipchurnchart_Utils::CiviCRMAPIWrapper('Membershipchurnchart', 'preparechurntable', array(
'sequential' => 1,
));

$message = "Settings saved.";
CRM_Core_Session::setStatus($message, 'Membership CHurn Chart Settings', 'success');

public function postProcess() {
parent::postProcess();
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/membership/membershipchurnchart', "reset=1"));
}

/**
* Get the fields/elements defined in this form.
*
* @return array (string)
*/
function getRenderableElementNames() {
// The _elements list includes some items which should not be
// auto-rendered in the loop -- such as "qfKey" and "buttons". These
// items don't have labels. We'll identify renderable by filtering on
// the 'label'.
$elementNames = array();
foreach ($this->_elements as $element) {
/** @var HTML_QuickForm_Element $element */
$label = $element->getLabel();
if (!empty($label)) {
$elementNames[] = $element->getName();
}
}
return $elementNames;
}
}
167 changes: 83 additions & 84 deletions CRM/Membershipchurnchart/Page/MembershipChurnChart.php
Original file line number Diff line number Diff line change
@@ -1,84 +1,83 @@
<?php

require_once 'CRM/Core/Page.php';

class CRM_Membershipchurnchart_Page_MembershipChurnChart extends CRM_Core_Page {
function run() {

$chartData = array();

// Get churn chart data
$sql = "SELECT * FROM membership_churn_monthly_table ORDER BY year, month";
$sqlRes = CRM_Core_DAO::executeQuery($sql);

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

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

$data = array();
$data['date'] = $sqlRes->month_year;
$data['year'] = $sqlRes->year;
$data['month'] = $sqlRes->month;
$data['membership_type_id'] = $sqlRes->membership_type_id;
$data['Current'] = $sqlRes->current;
$data['Joined'] = $sqlRes->joined;
$data['Resigned'] = $sqlRes->resigned;
$data['Rejoined'] = $sqlRes->rejoined;
$data['Brought_Forward'] = $data['Brought Forward'] = $sqlRes->brought_forward;
$data['Churn'] = $sqlRes->churn;

$chartData[$sqlRes->year][$sqlRes->membership_type_id][] = $data;
}

// Get membership churn chart settings
$settingsStr = CRM_Core_BAO_Setting::getItem('CiviCRM Membershipchurnchart Settings', 'membershipchurnchart_settings');
$settingsArray = unserialize($settingsStr);
$startYear = $currentYear = date('Y'); // Current
// Check if start date is set in settings page
if (!empty($settingsArray['start_year'])) {
$startYear = $settingsArray['start_year'];
}

// Start year filters
$startYearOptions = $endYearOptions = NULL;
foreach($years as $year) {
if ($year == $startYear) {
$startYearOptions .= "<option value=\"{$year}\" selected='selected'>{$year}</option>";
} else {
$startYearOptions .= "<option value=\"{$year}\">{$year}</option>";
}
}
// End year filters
foreach($years as $year) {
if ($year == $currentYear) {
$endYearOptions .= "<option value=\"{$year}\" selected='selected'>{$year}</option>";
} else {
$endYearOptions .= "<option value=\"{$year}\">{$year}</option>";
}
}
$this->assign('startYearRange', $startYearOptions);
$this->assign('endYearRange', $endYearOptions);

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

// All status to be displayed as legends
$allStatuses = CRM_Membershipchurnchart_Utils::getAllMemberStatusesForChart();
$allStatuses = json_encode(array_values($allStatuses));
$this->assign('allStatuses', $allStatuses);

// Minimum churn data
$minChurn = CRM_Membershipchurnchart_Utils::getMinChurnValuesForYaxis($chartData);
$jMinChurn = json_encode($minChurn, JSON_HEX_QUOT);
$this->assign('minChurn', $jMinChurn);

// Chart data
$chartData = json_encode($chartData);
$this->assign('chartData', $chartData);
$this->assign('currentYear', $currentYear);

parent::run();
}
}
<?php

require_once 'CRM/Core/Page.php';

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 = [];

// Get churn chart data
$sql = "SELECT * FROM civicrm_membership_churn_monthly_table ORDER BY year, month";
$sqlRes = CRM_Core_DAO::executeQuery($sql);

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

$data = [];
$data['date'] = $sqlRes->month_year;
$data['year'] = $sqlRes->year;
$data['month'] = $sqlRes->month;
$data['membership_type_id'] = $sqlRes->membership_type_id;
$data['Current'] = $sqlRes->current;
$data['Joined'] = $sqlRes->joined;
$data['Resigned'] = $sqlRes->resigned;
$data['Rejoined'] = $sqlRes->rejoined;
$data['Brought_Forward'] = $data['Brought Forward'] = $sqlRes->brought_forward;
$data['Churn'] = $sqlRes->churn;

$chartData[$sqlRes->year][$sqlRes->membership_type_id][] = $data;
}

// Get membership churn chart settings
$startYear = (int) \Civi::settings()->get('membershipchurnchart_startyear');
$currentYear = date('Y');
if (empty($startYear)) {
$startYear = $currentYear;
}

// Start year filters
$startYearOptions = $endYearOptions = NULL;
foreach($years as $year) {
if ($year == $startYear) {
$startYearOptions .= "<option value=\"{$year}\" selected='selected'>{$year}</option>";
} else {
$startYearOptions .= "<option value=\"{$year}\">{$year}</option>";
}
}
// End year filters
foreach($years as $year) {
if ($year == $currentYear) {
$endYearOptions .= "<option value=\"{$year}\" selected='selected'>{$year}</option>";
} else {
$endYearOptions .= "<option value=\"{$year}\">{$year}</option>";
}
}
$this->assign('startYearRange', $startYearOptions);
$this->assign('endYearRange', $endYearOptions);

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

// All status to be displayed as legends
$allStatuses = CRM_Membershipchurnchart_Utils::getAllMemberStatusesForChart();
$allStatuses = json_encode(array_values($allStatuses));
$this->assign('allStatuses', $allStatuses);

// Minimum churn data
$minChurn = CRM_Membershipchurnchart_Utils::getMinChurnValuesForYaxis($chartData);
$jMinChurn = json_encode($minChurn, JSON_HEX_QUOT);
$this->assign('minChurn', $jMinChurn);

// Chart data
$chartData = json_encode($chartData);
$this->assign('chartData', $chartData);
$this->assign('currentYear', $currentYear);

parent::run();
}
}
Loading

0 comments on commit 71652e1

Please sign in to comment.