Commit 8a568945 authored by BohdanDmytryshyn's avatar BohdanDmytryshyn
Browse files

Merge branch 'release_5_8_0' into 'master'

Release 5.8.0

See merge request !59
parents 1b9c7c4a 7c90b7d1
......@@ -26,7 +26,7 @@ class CRM_CiviMobileAPI_ApiWrapper_Contribution implements API_Wrapper {
|| $apiRequest['params']['contact_groups']
) {
$contactsId = (new CRM_CiviMobileAPI_Utils_ContributionFilter)->filterContributionContacts($apiRequest['params']);
$contactsId = (new CRM_CiviMobileAPI_Utils_ContactFieldsFilter)->filterContacts($apiRequest['params']);
if (!empty($contactsId)) {
$apiRequest['params']['contact_id'] = ['IN' => $contactsId];
......
......@@ -13,6 +13,30 @@ class CRM_CiviMobileAPI_ApiWrapper_Membership_Get implements API_Wrapper {
* @return array
*/
public function fromApiInput($apiRequest) {
if (is_mobile_request()) {
$apiRequest['params']['is_membership'] = 1;
$contactsId = (new CRM_CiviMobileAPI_Utils_ContactFieldsFilter())->filterContacts($apiRequest['params']);
$receiveDate = !empty($apiRequest['params']['receive_date']) ? (new CRM_CiviMobileAPI_Utils_Statistic_ChartBar())->getPrepareReceiveDate($apiRequest['params']) : NULL;
if (!empty($apiRequest['params']['activity_type_id']) && $apiRequest['params']['activity_type_id'] == 'renew') {
$renewalMembershipsId = CRM_CiviMobileAPI_Utils_Statistic_Utils::getRenewalMembershipIds();
$renewalMembershipsParam = (!empty($renewalMembershipsId)) ? ["IN" => $renewalMembershipsId] : NULL;
$apiRequest['params']['id'] = $renewalMembershipsParam;
}
if (!empty($contactsId)) {
$apiRequest['params']['contact_id'] = ['IN' => $contactsId];
if (!empty($receiveDate)) {
$apiRequest['params']['start_date'] = ['BETWEEN' => [$receiveDate['start_date'], $receiveDate['end_date']]];
}
} else {
$apiRequest['params']['contact_id'] = ['IS NULL' => 1];
}
}
return $apiRequest;
}
......@@ -29,6 +53,10 @@ class CRM_CiviMobileAPI_ApiWrapper_Membership_Get implements API_Wrapper {
$apiRequest['params']['return'] = explode(',', $apiRequest['params']['return']);
}
if (!empty($apiRequest['params']['options']['is_count']) && $apiRequest['params']['options']['is_count'] == 1) {
return $result;
}
$result = $this->fillAdditionalInfo($apiRequest, $result);
$result = $this->fillRelatedCount($apiRequest, $result);
$result = $this->fillByRelationship($apiRequest, $result);
......@@ -75,8 +103,15 @@ class CRM_CiviMobileAPI_ApiWrapper_Membership_Get implements API_Wrapper {
private function getAdditionalInfo($membership, $apiRequest) {
$config = CRM_Core_Config::singleton();
$additionalInfo = [];
$contactId = NULL;
if (!empty($apiRequest['params']['membership_contact_id'])) {
$contactId = $apiRequest['params']['membership_contact_id'];
} elseif (!empty($membership['contact_id'])) {
$contactId = $membership['contact_id'];
}
if (!empty($contactId)) {
try {
$lastPayment = civicrm_api3('MembershipPayment', 'getsingle', [
'return' => ["contribution_id.receive_date", "membership_id.contact_id", "contribution_id.payment_instrument_id"],
......
......@@ -75,7 +75,7 @@ class CRM_CiviMobileAPI_Install_Entity_CustomGroup extends CRM_CiviMobileAPI_Ins
'name' => self::ALLOW_MOBILE_REGISTRATION,
'title' => E::ts('Allow Online registration in CiviMobile'),
'extends' => 'Event',
'is_public' => 1,
'is_public' => 0,
'style' => 'Inline',
'collapse_display' => 1,
'is_reserved' => 1
......
......@@ -178,6 +178,24 @@ class CRM_CiviMobileAPI_Upgrader extends CRM_CiviMobileAPI_Upgrader_Base {
return TRUE;
}
public function upgrade_0021() {
try {
$customGroupId = (int) civicrm_api3('CustomGroup', 'getvalue', [
'name' => CRM_CiviMobileAPI_Install_Entity_CustomGroup::ALLOW_MOBILE_REGISTRATION,
'return' => 'id'
]);
civicrm_api3('CustomGroup', 'create', [
'id' => $customGroupId,
'is_public' => 0
]);
} catch (Exception $e) {
return FALSE;
}
return TRUE;
}
/**
* Installs scheduled job
*
......
......@@ -3,7 +3,7 @@
/**
* Class provide Contribution filter methods
*/
class CRM_CiviMobileAPI_Utils_ContributionFilter {
class CRM_CiviMobileAPI_Utils_ContactFieldsFilter {
/**
* List of contact Id
......@@ -18,9 +18,22 @@ class CRM_CiviMobileAPI_Utils_ContributionFilter {
* @param $params
* @return array
*/
public function filterContributionContacts($params) {
$listOfContributionContactsId = $this->getListOfContributionContactsId();
$this->filterContactByNameOrTypes($params['contact_display_name'], $params['contact_type'], $listOfContributionContactsId);
public function filterContacts($params) {
if (!empty($params['is_membership'])) {
if (!empty($params['membership_contact_id']) && !empty($params['membership_contact_id']['IN'])) {
$listOfContactsId = $params['membership_contact_id']['IN'];
} elseif (!empty($params['membership_contact_id']) && !empty((int)$params['membership_contact_id'])) {
$listOfContactsId = [$params['membership_contact_id']];
} else {
$listOfContactsId = CRM_CiviMobileAPI_Utils_Statistic_Utils::getListOfMembershipContactIds();
}
$listOfContactsId = CRM_Contact_BAO_Contact_Permission::allowList($listOfContactsId);
} else {
$listOfContactsId = $this->getListOfContributionContactsId();
}
$this->filterContactByNameOrTypes($params['contact_display_name'], $params['contact_type'], $listOfContactsId);
$this->filterContactByTags($params['contact_tags']);
$this->filterContactByGroup($params['contact_groups']["IN"]);
......@@ -136,9 +149,9 @@ class CRM_CiviMobileAPI_Utils_ContributionFilter {
$select = "SELECT DISTINCT(`contact_id`)";
$fromGroupContact = " FROM civicrm_group_contact";
$fromGroupContactCache = " FROM civicrm_group_contact_cache";
$where = " WHERE contact_id IN ( $prepareContactId ) ";
$and = " AND group_id IN ( $prepareSelectedGroupId ) ";
$sql = $select . $fromGroupContact . $where . $and . " UNION " . $select . $fromGroupContactCache . $where . $and;
$where = " WHERE contact_id IN ( $prepareContactId ) AND group_id IN ( $prepareSelectedGroupId ) ";
$and = " AND status = 'Added' ";
$sql = $select . $fromGroupContact . $where . $and . " UNION " . $select . $fromGroupContactCache . $where;
$contactGroupsRelationList = [];
try {
......@@ -168,7 +181,7 @@ class CRM_CiviMobileAPI_Utils_ContributionFilter {
*
* @return array
*/
public function getListOfContributionContactsId() {
public static function getListOfContributionContactsId() {
$contributionTable = CRM_Contribute_DAO_Contribution::getTableName();
$contactsId = [];
......@@ -186,4 +199,5 @@ class CRM_CiviMobileAPI_Utils_ContributionFilter {
return $contactsId;
}
}
......@@ -296,17 +296,6 @@ class CRM_CiviMobileAPI_Utils_Permission {
return FALSE;
}
/**
* Is enough permissions for getting Participant for Agenda
*/
public static function isEnoughPermissionForGetParticipant() {
if (CRM_Core_Permission::check('view Agenda')) {
return TRUE;
}
return FALSE;
}
/**
* Is enough permissions to set favourite EventSession
*/
......
<?php
class CRM_CiviMobileAPI_Utils_ContributionChartBar {
class CRM_CiviMobileAPI_Utils_Statistic_ChartBar {
/**
* Separator for currency formatting
......@@ -69,7 +69,12 @@ class CRM_CiviMobileAPI_Utils_ContributionChartBar {
$statistics = $this->yearPeriodDivide($listOfContactId, $params);
}
return self::addExtraAmountsToStatistics($statistics);
if ($params['is_membership'] == 1) {
return $statistics;
} else {
return self::addExtraAmountsToStatistics($statistics);
}
}
/**
......@@ -83,7 +88,7 @@ class CRM_CiviMobileAPI_Utils_ContributionChartBar {
$startDate = date_create($startDate);
$endDate = date_create($endDate);
$interval = date_diff($startDate, $endDate);
$isNextYear = $interval->y > 0 && !($interval->y == 1 && $endDate->format('m-d') == '01-01');
$isNextYear = $interval->y > 0 && !($interval->y == 1 && $interval->m == 0 && $endDate->format('m-d') == '01-01');
if ($interval->days <= 14) {
return 'days';
......@@ -139,6 +144,17 @@ class CRM_CiviMobileAPI_Utils_ContributionChartBar {
$secondDate = clone $firstDate;
$statistic = [];
if (!empty($params['is_membership'])) {
$membershipTypes = $this->getMembershipTypes($params);
foreach ($membershipTypes as $typeId => $typeName) {
$params['membership_type_id'] = $typeName;
$statistic[$typeId] = $this->getMembershipStatistic($firstDate, $secondDate, $endDate, $listOfContactId, $params, '1 days');
}
return $statistic;
}
while ($secondDate->getTimestamp() < $endDate->getTimestamp()) {
$secondDate = date_add($secondDate, date_interval_create_from_date_string("1 days"));
......@@ -167,11 +183,22 @@ class CRM_CiviMobileAPI_Utils_ContributionChartBar {
public function weekPeriodDivide($listOfContactId, $params) {
$preparedReceiveDate = $this->getPrepareReceiveDate($params);
$endDate = date_create($preparedReceiveDate['end_date']);
$firstDate = date_create($preparedReceiveDate['start_date']);
$secondDate = clone $firstDate;
$statistic = [];
if (!empty($params['is_membership'])) {
$membershipTypes = $this->getMembershipTypes($params);
foreach ($membershipTypes as $typeId => $typeName) {
$params['membership_type_id'] = $typeName;
$statistic[$typeId] = $this->getMembershipStatistic($firstDate, $secondDate, $endDate, $listOfContactId, $params, '7 days');
}
return $statistic;
}
while ($secondDate->getTimestamp() < $endDate->getTimestamp()) {
$secondDate = date_add($secondDate, date_interval_create_from_date_string('7 days'));
......@@ -203,14 +230,23 @@ class CRM_CiviMobileAPI_Utils_ContributionChartBar {
*/
public function monthPeriodDivide($listOfContactId, $params) {
$preparedReceiveDate = $this->getPrepareReceiveDate($params);
$endDate = date_create($preparedReceiveDate['end_date']);
$firstDate = date_create($preparedReceiveDate['start_date']);
$secondDate = date_create(date("Y-m", strtotime($preparedReceiveDate['start_date'])) . '-01');
$statistic = [];
if (!empty($params['is_membership'])) {
$membershipTypes = $this->getMembershipTypes($params);
foreach ($membershipTypes as $typeId => $typeName) {
$params['membership_type_id'] = $typeName;
$statistic[$typeId] = $this->getMembershipStatistic($firstDate, $secondDate, $endDate, $listOfContactId, $params, '1 month');
}
return $statistic;
}
while ($secondDate->getTimestamp() < $endDate->getTimestamp()) {
$secondDate = date_add($secondDate, date_interval_create_from_date_string('1 months'));
......@@ -248,6 +284,17 @@ class CRM_CiviMobileAPI_Utils_ContributionChartBar {
$secondDate = date_create(date('Y', $date->getTimestamp()) . '-01-01');
$statistic = [];
if (!empty($params['is_membership'])) {
$membershipTypes = $this->getMembershipTypes($params);
foreach ($membershipTypes as $typeId => $typeName) {
$params['membership_type_id'] = $typeName;
$statistic[$typeId] = $this->getMembershipStatistic($firstDate, $secondDate, $endDate, $listOfContactId, $params, '1 year');
}
return $statistic;
}
while ($secondDate->getTimestamp() < $endDate->getTimestamp()) {
$secondDate = date_add($secondDate, date_interval_create_from_date_string('1 year'));
......@@ -279,7 +326,7 @@ class CRM_CiviMobileAPI_Utils_ContributionChartBar {
*/
public function getContributeSelector($listOfContactId, $params, $firstDate, $secondDate) {
$contactQuery = ["IN" => $listOfContactId];
$contributionFields = (new CRM_CiviMobileAPI_Utils_ContactsContributionStatistic)->getContributionFieldsParams($params);
$contributionFields = (new CRM_CiviMobileAPI_Utils_Statistic_ContactsContribution)->getContributionFieldsParams($params);
$totalQueryParams = [
'contact_id' => (!empty($listOfContactId)) ? $contactQuery : ['IS NULL' => 1],
......@@ -296,9 +343,67 @@ class CRM_CiviMobileAPI_Utils_ContributionChartBar {
];
$selectedContributions = CRM_Contact_BAO_Query::convertFormValues($totalQueryParams);
$selector = new CRM_Contribute_Selector_Search($selectedContributions);
return new CRM_Contribute_Selector_Search($selectedContributions);
}
/**
* Get membership types
*
* @param $params
* @return array
*/
public function getMembershipTypes($params) {
$membershipTypes = [];
if (!empty($params['membership_type_id'])) {
foreach ($params['membership_type_id']['IN'] as $membershipId) {
$membershipDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($membershipId);
$membershipTypes[$membershipId] = $membershipDetails['name'];
}
} else {
$membershipTypes = CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE);
}
return $membershipTypes;
}
/**
* Get membership statistic
*
* @param $firstDate
* @param $secondDate
* @param $endDate
* @param $listOfContactId
* @param $params
* @param $period
* @return array
*/
public function getMembershipStatistic($firstDate, $secondDate, $endDate, $listOfContactId, $params, $period) {
$statistic = [];
$secondDate = clone $secondDate;
while ($secondDate->getTimestamp() < $endDate->getTimestamp()) {
$secondDate = date_add($secondDate, date_interval_create_from_date_string($period));
return $selector;
if ($secondDate->getTimestamp() > $endDate->getTimestamp()) {
$secondDate = clone $endDate;
}
$formattedFirstDate = $firstDate->format('Y-m-d');
$prevSecondDate = clone $secondDate;
$prevSecondDate->modify('-1 day');
$statistic[] = [
'count' => $this->getMembershipCount($listOfContactId, $params, $formattedFirstDate, $prevSecondDate->format('Y-m-d')),
'first_date' => $formattedFirstDate,
'second_date' => $secondDate->format('Y-m-d')
];
$firstDate = clone $secondDate;
}
return $statistic;
}
/**
......@@ -313,7 +418,7 @@ class CRM_CiviMobileAPI_Utils_ContributionChartBar {
$newAmount = [];
if (!empty($statistic['total'])) {
$statistic['total']['amount'] = static::transform($statistic['total']['amount']);
$statistic['total']['amount'] = CRM_CiviMobileAPI_Utils_Statistic_Utils::explodesString($statistic['total']['amount']);
$clearAmounts = [];
foreach ($statistic['total']['amount'] as $moneyString) {
$amountParts = explode(self::$currencySeparator, $moneyString);
......@@ -323,7 +428,7 @@ class CRM_CiviMobileAPI_Utils_ContributionChartBar {
$clearAmounts[] = $clearAmount;
$newAmount[] = [
'amount' => (float) $clearAmount,
'amount' => (float)$clearAmount,
'currency' => $currencyName,
'formattedAmount' => $formattedAmount,
];
......@@ -334,7 +439,7 @@ class CRM_CiviMobileAPI_Utils_ContributionChartBar {
}
if (!empty($statistic['cancel'])) {
$statistic['cancel']['amount'] = static::transform($statistic['cancel']['amount']);
$statistic['cancel']['amount'] = CRM_CiviMobileAPI_Utils_Statistic_Utils::explodesString($statistic['cancel']['amount']);
}
return [
......@@ -345,43 +450,6 @@ class CRM_CiviMobileAPI_Utils_ContributionChartBar {
];
}
/**
* Explodes and trims string
*
* @param $string
* @return array
*/
private static function transform($string) {
return !empty($string) ? explode(",&nbsp;", $string) : [];
}
/**
* Returns contribution date interval
*
* @return array
*/
public static function getDefaultContributionDateInterval() {
$select = 'SELECT MIN(civicrm_contribution.receive_date) as min_receive_date, MAX(civicrm_contribution.receive_date) as max_receive_date';
$from = ' FROM civicrm_contribution';
$sql = $select . $from;
$dates = [
'min_receive_date' => '',
'max_receive_date' => ''
];
try {
$dao = CRM_Core_DAO::executeQuery($sql);
if ($dao->fetch()) {
$dates['min_receive_date'] = $dao->min_receive_date;
$dates['max_receive_date'] = ((int)date('Y', strtotime($dao->max_receive_date)) + 1) . '-01-01';
}
} catch (Exception $e) {}
return $dates;
}
/**
* Returns prepared receive date
*
......@@ -389,7 +457,7 @@ class CRM_CiviMobileAPI_Utils_ContributionChartBar {
* @return array
*/
public function getPrepareReceiveDate($params) {
$defaultIntervals = CRM_CiviMobileAPI_Utils_ContributionChartBar::getDefaultContributionDateInterval();
$defaultIntervals = CRM_CiviMobileAPI_Utils_Statistic_Utils::getDefaultContributionDateInterval();
if (!empty($params['receive_date']['BETWEEN']) || empty($params['receive_date'])) {
$startDate = !empty($params['receive_date']['BETWEEN'][0]) ? $params['receive_date']['BETWEEN'][0] : $defaultIntervals['min_receive_date'];
......@@ -408,4 +476,26 @@ class CRM_CiviMobileAPI_Utils_ContributionChartBar {
];
}
/**
* Get count of membership for chart bar
*
* @param $contactsId
* @param $params
* @param $startDate
* @param $endDate
* @return int
*/
public function getMembershipCount($contactsId, $params, $startDate, $endDate) {
try {
return civicrm_api3('Membership', 'getcount', [
'options' => ['limit' => 0],
'start_date' => ['BETWEEN' => [$startDate, $endDate]],
'contact_id' => !empty($contactsId) ? ['IN' => $contactsId] : NULL,
'membership_type_id' => !empty($params['membership_type_id']) ? ['IN' => [$params['membership_type_id']]] : NULL,
]);
} catch (CiviCRM_API3_Exception $e) {
return 0;
}
}
}
<?php
class CRM_CiviMobileAPI_Utils_ContactsContributionStatistic {
class CRM_CiviMobileAPI_Utils_Statistic_ContactsContribution {
/**
* Get contribution statistic for one contact
......@@ -12,12 +12,12 @@ class CRM_CiviMobileAPI_Utils_ContactsContributionStatistic {
$contributionSelectors = $this->getContributionSelectors($params);
$selector = $contributionSelectors['selector'];
$currentYearSelector = $contributionSelectors['current_year_selector'];
$preparedReceiveDate = (new CRM_CiviMobileAPI_Utils_ContributionChartBar())->getPrepareReceiveDate($params);
$preparedReceiveDate = (new CRM_CiviMobileAPI_Utils_Statistic_ChartBar())->getPrepareReceiveDate($params);
$statistic = [
'all_time' => CRM_CiviMobileAPI_Utils_Contribution::transformStatistic($selector->getSummary()),
'current_year' => CRM_CiviMobileAPI_Utils_Contribution::transformStatistic($currentYearSelector->getSummary()),
'period' => (new CRM_CiviMobileAPI_Utils_ContributionChartBar())->findPeriod($preparedReceiveDate['start_date'], $preparedReceiveDate['end_date'])
'period' => (new CRM_CiviMobileAPI_Utils_Statistic_ChartBar())->findPeriod($preparedReceiveDate['start_date'], $preparedReceiveDate['end_date'])
];
return $statistic;
......@@ -33,16 +33,21 @@ class CRM_CiviMobileAPI_Utils_ContactsContributionStatistic {
* @return array
*/
public function getSelectedContactsContributionStatistic($params, $listOfContactId) {
$contributionSelectors = $this->getContributionSelectors($params, $listOfContactId);
$allTimeParams = $params;
if (!empty($allTimeParams['receive_date']['BETWEEN'][0]) && !empty($allTimeParams['receive_date']['BETWEEN'][1])) {
$allTimeParams['receive_date']['BETWEEN'][1] = date('YmdHis', strtotime($allTimeParams['receive_date']['BETWEEN'][1] . " -1 seconds"));
}
$contributionSelectors = $this->getContributionSelectors($allTimeParams, $listOfContactId);
$selector = $contributionSelectors['selector'];
$currentYearSelector = $contributionSelectors['current_year_selector'];
$preparedReceiveDate = (new CRM_CiviMobileAPI_Utils_ContributionChartBar())->getPrepareReceiveDate($params);
$preparedReceiveDate = (new CRM_CiviMobileAPI_Utils_Statistic_ChartBar())->getPrepareReceiveDate($params);
$statistic = [
'all_time' => CRM_CiviMobileAPI_Utils_Contribution::transformStatistic($selector->getSummary()),
'current_year' => CRM_CiviMobileAPI_Utils_Contribution::transformStatistic($currentYearSelector->getSummary()),
'chart_bar' => (new CRM_CiviMobileAPI_Utils_ContributionChartBar)->periodDivide($listOfContactId, $params),
'period' => (new CRM_CiviMobileAPI_Utils_ContributionChartBar())->findPeriod($preparedReceiveDate['start_date'], $preparedReceiveDate['end_date'])
'chart_bar' => (new CRM_CiviMobileAPI_Utils_Statistic_ChartBar)->periodDivide($listOfContactId, $params),
'period' => (new CRM_CiviMobileAPI_Utils_Statistic_ChartBar())->findPeriod($preparedReceiveDate['start_date'], $preparedReceiveDate['end_date'])
];
return $statistic;
......@@ -81,8 +86,8 @@ class CRM_CiviMobileAPI_Utils_ContactsContributionStatistic {
'contact_id' => $contactId,
'receive_date' => [
'BETWEEN' => [
CRM_Utils_Date::getToday(['month'=> 1, 'day' => 1, 'year' => date("Y")], 'Y-m-d H:i:s'),
CRM_Utils_Date::getToday(['month'=> 1, 'day' => 1, 'year' => (date("Y") + 1)], 'Y-m-d H:i:s'),
date('Y') . '-01-01',
date('Y-m-d H:i:s', strtotime('12/31 +23 hours 59 minutes 59 seconds')),
]
],
'financial_type_id' => $contributionFields['financial_type_id'],
......
<?php
class CRM_CiviMobileAPI_Utils_Statistic_ContactsMembership {
/**
* Get new or current membership count
*
* @param $contactsId
* @param $params
* @param $status
* @param bool $isRenewal
* @return int
*/
public static function getMembershipsCount($contactsId, $params, $status = NULL, $isRenewal = FALSE) {
$prepareReceiveDate = (new CRM_CiviMobileAPI_Utils_Statistic_ChartBar)->getPrepareReceiveDate($params);
$startDate = $prepareReceiveDate['start_date'];
$endDate = $prepareReceiveDate['end_date'];
$renewalMembershipsParam = (!empty(CRM_CiviMobileAPI_Utils_Statistic_Utils::getRenewalMembershipIds())) ? ["IN" => CRM_CiviMobileAPI_Utils_Statistic_Utils::getRenewalMembershipIds()] : NULL;
$count = 0;
try {
$memberships = civicrm_api3('Membership', 'get', [
'sequential' => 1,
'options' => ['limit' => 0],
'id' => ($isRenewal) ? $renewalMembershipsParam : NULL,
'contact_id' => ['IN' => $contactsId],
'membership_type_id' => !empty($params['membership_type_id']) ? $params['membership_type_id'] : NULL,
'status_id' => $status,
])['values'];
} catch (CiviCRM_API3_Exception $e) {
return 0;
}
if (!empty($memberships)) {
foreach ($memberships as $membership) {
if ($membership['start_date'] == NULL) {
$membership['start_date'] = $membership['join_date'];
}
if ($membership['start_date'] >= $startDate && $membership['start_date'] <= $endDate) {
$count++;
}
}
}
return $count;
}
}