Merge pull request #12316 from mattwire/core_task_code_cleanup
[civicrm-core.git] / CRM / Mailing / Form / Task.php
CommitLineData
2cc569f2
PJ
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
2cc569f2 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
2cc569f2
PJ
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
2cc569f2
PJ
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
2cc569f2
PJ
32 */
33
34/**
31aaf096
MW
35 * Class for mailing form task actions.
36 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
2cc569f2 37 */
31aaf096 38class CRM_Mailing_Form_Task extends CRM_Core_Form_Task {
2cc569f2 39
2cc569f2 40 /**
fe482240 41 * Build all the data structures needed to build the form.
2cc569f2 42 */
00be9182 43 public function preProcess() {
2cc569f2
PJ
44 self::preProcessCommon($this);
45 }
46
e0ef6999 47 /**
c490a46a 48 * @param CRM_Core_Form $form
e0ef6999
EM
49 * @param bool $useTable
50 */
00be9182 51 public static function preProcessCommon(&$form, $useTable = FALSE) {
2cc569f2
PJ
52 $values = $form->controller->exportValues($form->get('searchFormName'));
53
54 $form->_task = CRM_Utils_Array::value('task', $values);
55 $mailingTasks = CRM_Mailing_Task::tasks();
56 $form->assign('taskName', CRM_Utils_Array::value('task', $values));
57
47546fcf 58 // ids are mailing event queue ids
2cc569f2
PJ
59 $ids = array();
60 if ($values['radio_ts'] == 'ts_sel') {
61 foreach ($values as $name => $value) {
62 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
63 $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
64 }
65 }
66 }
67 else {
68 $queryParams = $form->get('queryParams');
35f7561f 69 $sortOrder = NULL;
353ffa53 70 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
481a74f4 71 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
2cc569f2
PJ
72 }
73
74 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
75 CRM_Contact_BAO_Query::MODE_MAILING
76 );
77
78 $result = $query->searchQuery(0, 0, $sortOrder);
79 while ($result->fetch()) {
335038eb 80 $ids[] = $result->mailing_recipients_id;
2cc569f2
PJ
81 }
82 $form->assign('totalSelectedMailingRecipients', $form->get('rowCount'));
83 }
84
85 if (!empty($ids)) {
35f7561f 86 $form->_componentClause = ' civicrm_mailing_recipients.id IN ( ' . implode(',', $ids) . ' ) ';
2cc569f2
PJ
87 }
88
25606795 89 // set the context for redirection for any task actions
2cc569f2
PJ
90 $session = CRM_Core_Session::singleton();
91
92 $fragment = 'search';
93 if ($form->_action == CRM_Core_Action::ADVANCED) {
94 $fragment .= '/advanced';
95 }
96
97 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
98 $urlParams = 'force=1';
99 if (CRM_Utils_Rule::qfKey($qfKey)) {
100 $urlParams .= "&qfKey=$qfKey";
101 }
102
103 $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams);
104 $session = CRM_Core_Session::singleton();
105 $session->replaceUserContext($url);
106 }
107
108 /**
100fef9d 109 * Simple shell that derived classes can call to add buttons to
2cc569f2
PJ
110 * the form with a customized title for the main Submit
111 *
90c8230e
TO
112 * @param string $title
113 * Title of the main button.
fd31fa4c
EM
114 * @param string $nextType
115 * @param string $backType
116 * @param bool $submitOnce
2cc569f2 117 */
00be9182 118 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
2cc569f2
PJ
119 $this->addButtons(array(
120 array(
121 'type' => $nextType,
122 'name' => $title,
123 'isDefault' => TRUE,
124 ),
125 array(
126 'type' => $backType,
127 'name' => ts('Cancel'),
128 ),
129 )
130 );
131 }
96025800 132
fd31fa4c 133}