Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
f299f7db | 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
6a488035 TO |
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 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
ca5cec67 | 31 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for Tell A Friend | |
38 | * | |
39 | */ | |
40 | class CRM_PCP_Form_Contribute extends CRM_Contribute_Form_ContributionPage { | |
41 | ||
42 | /** | |
100fef9d | 43 | * The type of pcp component. |
6a488035 TO |
44 | * |
45 | * @var int | |
6a488035 TO |
46 | */ |
47 | public $_component = 'contribute'; | |
48 | ||
49 | public function preProcess() { | |
50 | parent::preProcess(); | |
51 | } | |
52 | ||
53 | /** | |
c490a46a | 54 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
55 | * the default values are retrieved from the database |
56 | * | |
6a488035 | 57 | * |
355ba699 | 58 | * @return void |
6a488035 TO |
59 | */ |
60 | public function setDefaultValues() { | |
be2fb01f | 61 | $defaults = []; |
6a488035 TO |
62 | |
63 | if (isset($this->_id)) { | |
be2fb01f | 64 | $params = ['entity_id' => $this->_id, 'entity_table' => 'civicrm_contribution_page']; |
6a488035 TO |
65 | CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCPBlock', $params, $defaults); |
66 | $defaults['pcp_active'] = CRM_Utils_Array::value('is_active', $defaults); | |
67 | // Assign contribution page ID to pageId for referencing in PCP.hlp - since $id is overwritten there. dgg | |
68 | $this->assign('pageId', $this->_id); | |
69 | } | |
70 | ||
a7488080 | 71 | if (empty($defaults['id'])) { |
6a488035 TO |
72 | $defaults['target_entity_type'] = 'contribute'; |
73 | $defaults['is_approval_needed'] = 1; | |
74 | $defaults['is_tellfriend_enabled'] = 1; | |
75 | $defaults['tellfriend_limit'] = 5; | |
76 | $defaults['link_text'] = ts('Create your own fundraising page'); | |
12f92dbd | 77 | $defaults['owner_notify_id'] = CRM_Core_OptionGroup::getDefaultValue('pcp_owner_notify'); |
6a488035 TO |
78 | |
79 | if ($ccReceipt = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'cc_receipt')) { | |
80 | $defaults['notify_email'] = $ccReceipt; | |
81 | } | |
82 | } | |
83 | return $defaults; | |
84 | } | |
85 | ||
86 | /** | |
fe482240 | 87 | * Build the form object. |
6a488035 | 88 | * |
355ba699 | 89 | * @return void |
6a488035 TO |
90 | */ |
91 | public function buildQuickForm() { | |
5fe87df6 | 92 | $this->_last = TRUE; |
6a488035 TO |
93 | CRM_PCP_BAO_PCP::buildPCPForm($this); |
94 | ||
be2fb01f | 95 | $this->addElement('checkbox', 'pcp_active', ts('Enable Personal Campaign Pages? (for this contribution page)'), NULL, ['onclick' => "return showHideByValue('pcp_active',true,'pcpFields','table-row','radio',false);"]); |
6a488035 TO |
96 | |
97 | parent::buildQuickForm(); | |
be2fb01f | 98 | $this->addFormRule(['CRM_PCP_Form_Contribute', 'formRule'], $this); |
6a488035 TO |
99 | } |
100 | ||
101 | /** | |
fe482240 | 102 | * Validation. |
6a488035 | 103 | * |
db95eff6 TO |
104 | * @param array $params |
105 | * (ref.) an assoc array of name/value pairs. | |
6a488035 | 106 | * |
dd244018 EM |
107 | * @param $files |
108 | * @param $self | |
109 | * | |
72b3a70c CW |
110 | * @return bool|array |
111 | * mixed true or array of errors | |
6a488035 TO |
112 | */ |
113 | public static function formRule($params, $files, $self) { | |
be2fb01f | 114 | $errors = []; |
a7488080 | 115 | if (!empty($params['is_active'])) { |
6a488035 | 116 | |
a7488080 | 117 | if (!empty($params['is_tellfriend_enabled']) && |
6a488035 TO |
118 | (CRM_Utils_Array::value('tellfriend_limit', $params) <= 0) |
119 | ) { | |
120 | $errors['tellfriend_limit'] = ts('if Tell Friend is enabled, Maximum recipients limit should be greater than zero.'); | |
121 | } | |
a7488080 | 122 | if (empty($params['supporter_profile_id'])) { |
6a488035 TO |
123 | $errors['supporter_profile_id'] = ts('Supporter profile is a required field.'); |
124 | } | |
125 | else { | |
126 | if (CRM_PCP_BAO_PCP::checkEmailProfile($params['supporter_profile_id'])) { | |
127 | $errors['supporter_profile_id'] = ts('Profile is not configured with Email address.'); | |
128 | } | |
129 | } | |
130 | ||
131 | if ($emails = CRM_Utils_Array::value('notify_email', $params)) { | |
132 | $emailArray = explode(',', $emails); | |
133 | foreach ($emailArray as $email) { | |
134 | if ($email && !CRM_Utils_Rule::email(trim($email))) { | |
135 | $errors['notify_email'] = ts('A valid Notify Email address must be specified'); | |
136 | } | |
137 | } | |
138 | } | |
139 | } | |
140 | return empty($errors) ? TRUE : $errors; | |
141 | } | |
142 | ||
143 | /** | |
fe482240 | 144 | * Process the form submission. |
6a488035 | 145 | * |
6a488035 | 146 | * |
355ba699 | 147 | * @return void |
6a488035 TO |
148 | */ |
149 | public function postProcess() { | |
150 | // get the submitted form values. | |
151 | $params = $this->controller->exportValues($this->_name); | |
152 | ||
153 | // Source | |
154 | $params['entity_table'] = 'civicrm_contribution_page'; | |
155 | $params['entity_id'] = $this->_id; | |
156 | ||
157 | // Target | |
158 | $params['target_entity_type'] = CRM_Utils_Array::value('target_entity_type', $params, 'contribute'); | |
159 | $params['target_entity_id'] = $this->_id; | |
160 | ||
353ffa53 | 161 | $dao = new CRM_PCP_DAO_PCPBlock(); |
6a488035 | 162 | $dao->entity_table = $params['entity_table']; |
353ffa53 | 163 | $dao->entity_id = $this->_id; |
6a488035 TO |
164 | $dao->find(TRUE); |
165 | $params['id'] = $dao->id; | |
166 | $params['is_active'] = CRM_Utils_Array::value('pcp_active', $params, FALSE); | |
167 | $params['is_approval_needed'] = CRM_Utils_Array::value('is_approval_needed', $params, FALSE); | |
168 | $params['is_tellfriend_enabled'] = CRM_Utils_Array::value('is_tellfriend_enabled', $params, FALSE); | |
169 | ||
f6bc51fd | 170 | CRM_PCP_BAO_PCPBlock::create($params); |
6a488035 TO |
171 | |
172 | parent::endPostProcess(); | |
173 | } | |
174 | ||
175 | /** | |
176 | * Return a descriptive name for the page, used in wizard header | |
177 | * | |
178 | * @return string | |
6a488035 TO |
179 | */ |
180 | public function getTitle() { | |
181 | return ts('Enable Personal Campaign Pages'); | |
182 | } | |
96025800 | 183 | |
6a488035 | 184 | } |