Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] [Search-kit-actions] Cleanup around contribution pdf common #19904

Merged
merged 5 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 3 additions & 26 deletions CRM/Contribute/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ class CRM_Contribute_Form_Task extends CRM_Core_Form_Task {
*/
protected $_contributionIds;

/**
* The array that holds all the mapping contribution and contact ids.
*
* @var array
*/
protected $_contributionContactIds = [];

/**
* Build all the data structures needed to build the form.
*/
Expand All @@ -56,25 +49,9 @@ public static function preProcessCommon(&$form): void {

$form->_task = $values['task'] ?? NULL;

$ids = $form->getSelectedIDs($values);
if (!$ids) {
$result = $form->getSearchQueryResults();
while ($result->fetch()) {
$ids[] = $result->contribution_id;
if ($form->isQueryIncludesSoftCredits()) {
$form->_contactIds[$result->contact_id] = $result->contact_id;
$form->_contributionContactIds["{$result->contact_id}-{$result->contribution_id}"] = $result->contribution_id;
}
}
$form->assign('totalSelectedContributions', $form->get('rowCount'));
}

if (!empty($ids)) {
$form->_componentClause = ' civicrm_contribution.id IN ( ' . implode(',', $ids) . ' ) ';

$form->assign('totalSelectedContributions', count($ids));
}

$ids = $form->getIDs();
$form->_componentClause = $form->getComponentClause();
$form->assign('totalSelectedContributions', count($ids));
$form->_contributionIds = $form->_componentIds = $ids;
$form->set('contributionIds', $form->_contributionIds);
$form->setNextUrl('contribute');
Expand Down
20 changes: 3 additions & 17 deletions CRM/Contribute/Form/Task/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,15 @@ class CRM_Contribute_Form_Task_PDF extends CRM_Contribute_Form_Task {
* Build all the data structures needed to build the form.
*/
public function preProcess() {
$id = CRM_Utils_Request::retrieve('id', 'Positive',
$this, FALSE
);

if ($id) {
$this->_contributionIds = [$id];
$this->_componentClause = " civicrm_contribution.id IN ( $id ) ";
$this->_single = TRUE;
$this->assign('totalSelectedContributions', 1);
}
else {
parent::preProcess();
}

parent::preProcess();
// check that all the contribution ids have pending status
$query = "
SELECT count(*)
FROM civicrm_contribution
WHERE contribution_status_id != 1
AND {$this->_componentClause}";
$count = CRM_Core_DAO::singleValueQuery($query);
if ($count != 0) {
CRM_Core_Error::statusBounce("Please select only online contributions with Completed status.");
if (CRM_Core_DAO::singleValueQuery($query)) {
CRM_Core_Error::statusBounce("Please select only contributions with Completed status.");
}

$this->assign('single', $this->_single);
Expand Down
10 changes: 8 additions & 2 deletions CRM/Contribute/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public static function buildQuickForm(&$form) {
*
* @param \CRM_Contribute_Form_Task_PDFLetter $form
* @param array $formValues
*
* @throws \CRM_Core_Exception
*/
public static function postProcess(&$form, $formValues = NULL) {
if (empty($formValues)) {
Expand Down Expand Up @@ -79,8 +81,12 @@ public static function postProcess(&$form, $formValues = NULL) {
$skipDeceased = $form->skipDeceased ?? TRUE;
$contributionIDs = $form->getVar('_contributionIds');
if ($form->isQueryIncludesSoftCredits()) {
//@todo - comment on what is stored there
$contributionIDs = $form->getVar('_contributionContactIds');
$contributionIDs = [];
$result = $form->getSearchQueryResults();
while ($result->fetch()) {
$form->_contactIds[$result->contact_id] = $result->contact_id;
$contributionIDs["{$result->contact_id}-{$result->contribution_id}"] = $result->contribution_id;
}
}
[$contributions, $contacts] = self::buildContributionArray($groupBy, $contributionIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator, $form->isQueryIncludesSoftCredits());
$html = [];
Expand Down
97 changes: 68 additions & 29 deletions CRM/Contribute/Form/Task/TaskTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
trait CRM_Contribute_Form_Task_TaskTrait {

/**
* Query result object.
* Selected IDs for the action.
*
* @var \CRM_Core_DAO
* @var array
*/
protected $queryBAO;
protected $ids;

/**
* Get the results from the BAO_Query object based search.
Expand All @@ -35,34 +35,31 @@ trait CRM_Contribute_Form_Task_TaskTrait {
* @throws \CRM_Core_Exception
*/
public function getSearchQueryResults(): CRM_Core_DAO {
if (!$this->queryBAO) {
$form = $this;
$queryParams = $this->getQueryParams();
$returnProperties = ['contribution_id' => 1];
$sortOrder = $sortCol = NULL;
if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
$sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
//Include sort column in select clause.
$sortCol = trim(str_replace(['`', 'asc', 'desc'], '', $sortOrder));
$returnProperties[$sortCol] = 1;
}
$form = $this;
$queryParams = $this->getQueryParams();
$returnProperties = ['contribution_id' => 1];
$sortOrder = $sortCol = NULL;
if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
$sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
//Include sort column in select clause.
$sortCol = trim(str_replace(['`', 'asc', 'desc'], '', $sortOrder));
$returnProperties[$sortCol] = 1;
}

$query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
CRM_Contact_BAO_Query::MODE_CONTRIBUTE
);
// @todo the function CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled should handle this
// can we remove? if not why not?
if ($this->isQueryIncludesSoftCredits()) {
$query->_rowCountClause = ' count(civicrm_contribution.id)';
$query->_groupByComponentClause = ' GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ';
}
else {
$query->_distinctComponentClause = ' civicrm_contribution.id';
$query->_groupByComponentClause = ' GROUP BY civicrm_contribution.id ';
}
$this->queryBAO = $query->searchQuery(0, 0, $sortOrder);
$query = new CRM_Contact_BAO_Query($queryParams, $returnProperties, NULL, FALSE, FALSE,
CRM_Contact_BAO_Query::MODE_CONTRIBUTE
);
// @todo the function CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled should handle this
// can we remove? if not why not?
if ($this->isQueryIncludesSoftCredits()) {
$query->_rowCountClause = ' count(civicrm_contribution.id)';
$query->_groupByComponentClause = ' GROUP BY contribution_search_scredit_combined.id, contribution_search_scredit_combined.contact_id, contribution_search_scredit_combined.scredit_id ';
}
return $this->queryBAO;
else {
$query->_distinctComponentClause = ' civicrm_contribution.id';
$query->_groupByComponentClause = ' GROUP BY civicrm_contribution.id ';
}
return $query->searchQuery(0, 0, $sortOrder);
}

/**
Expand Down Expand Up @@ -99,4 +96,46 @@ public function isQueryIncludesSoftCredits(): bool {
return (bool) CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled($this->getQueryParams());
}

/**
* Get ids selected for the task.
*
* @return array|bool
* @throws \CRM_Core_Exception
*/
public function getIDs() {
if (!$this->ids) {
$this->ids = $this->calculateIDS();
}
return $this->ids;
}

/**
* @return array|bool|string[]
* @throws \CRM_Core_Exception
*/
protected function calculateIDS() {
if ($this->controller->get('id')) {
return explode(',', $this->controller->get('id'));
}
$ids = $this->getSelectedIDs($this->getSearchFormValues());
if (!$ids) {
$result = $this->getSearchQueryResults();
while ($result->fetch()) {
$ids[] = $result->contribution_id;
}
}
return $ids;
}

/**
* Get the clause to add to queries to hone the results.
*
* In practice this generally means the query to limit by selected ids.
*
* @throws \CRM_Core_Exception
*/
public function getComponentClause(): string {
return ' civicrm_contribution.id IN ( ' . implode(',', $this->getIDs()) . ' ) ';
}

}