Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for PCP | |
38 | * | |
39 | */ | |
40 | class CRM_PCP_Form_Event extends CRM_Event_Form_ManageEvent { | |
41 | ||
42 | /** | |
100fef9d | 43 | * The type of pcp component. |
6a488035 TO |
44 | * |
45 | * @var int | |
6a488035 TO |
46 | */ |
47 | public $_component = 'event'; | |
48 | ||
49 | ||
50 | public function preProcess() { | |
51 | parent::preProcess(); | |
e6ac9af9 | 52 | $this->assign('selectedChild', 'pcp'); |
6a488035 TO |
53 | } |
54 | ||
55 | /** | |
c490a46a | 56 | * Set default values for the form. |
6a488035 | 57 | * |
778ec191 | 58 | * @return array |
6a488035 TO |
59 | */ |
60 | public function setDefaultValues() { | |
6a488035 TO |
61 | $defaults = array(); |
62 | if (isset($this->_id)) { | |
e049d911 TO |
63 | $title = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_id, 'title'); |
64 | CRM_Utils_System::setTitle(ts('Personal Campaign Page Settings (%1)', array(1 => $title))); | |
6a488035 TO |
65 | |
66 | $params = array('entity_id' => $this->_id, 'entity_table' => 'civicrm_event'); | |
67 | CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCPBlock', $params, $defaults); | |
68 | $defaults['pcp_active'] = CRM_Utils_Array::value('is_active', $defaults); | |
69 | // Assign contribution page ID to pageId for referencing in PCP.hlp - since $id is overwritten there. dgg | |
70 | $this->assign('pageId', $this->_id); | |
71 | } | |
72 | ||
a7488080 | 73 | if (empty($defaults['id'])) { |
6a488035 TO |
74 | $defaults['target_entity_type'] = 'event'; |
75 | $defaults['is_approval_needed'] = 1; | |
76 | $defaults['is_tellfriend_enabled'] = 1; | |
77 | $defaults['tellfriend_limit'] = 5; | |
78 | $defaults['link_text'] = ts('Promote this event with a personal campaign page'); | |
12f92dbd | 79 | $defaults['owner_notify_id'] = CRM_Core_OptionGroup::getDefaultValue('pcp_owner_notify'); |
6a488035 | 80 | |
366fe2a3 | 81 | if ($this->_id && |
353ffa53 TO |
82 | $ccReceipt = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'cc_receipt') |
83 | ) { | |
6a488035 TO |
84 | $defaults['notify_email'] = $ccReceipt; |
85 | } | |
86 | } | |
87 | return $defaults; | |
88 | } | |
89 | ||
90 | /** | |
fe482240 | 91 | * Build the form object. |
6a488035 | 92 | * |
355ba699 | 93 | * @return void |
6a488035 TO |
94 | */ |
95 | public function buildQuickForm() { | |
96 | CRM_PCP_BAO_PCP::buildPCPForm($this); | |
97 | ||
98 | $this->addElement('checkbox', 'pcp_active', ts('Enable Personal Campaign Pages? (for this event)'), NULL, array('onclick' => "return showHideByValue('pcp_active',true,'pcpFields','table-row','radio',false);")); | |
99 | ||
100 | $this->add('select', 'target_entity_type', ts('Campaign Type'), | |
101 | array('' => ts('- select -'), 'event' => ts('Event'), 'contribute' => ts('Contribution')), | |
102 | NULL, array('onchange' => "return showHideByValue('target_entity_type','contribute','pcpDetailFields','block','select',false);") | |
103 | ); | |
104 | ||
105 | $this->add('select', 'target_entity_id', | |
106 | ts('Online Contribution Page'), | |
107 | array( | |
d5cc0fc2 | 108 | '' => ts('- select -'), |
353ffa53 | 109 | ) + |
6a488035 TO |
110 | CRM_Contribute_PseudoConstant::contributionPage() |
111 | ); | |
112 | ||
113 | parent::buildQuickForm(); | |
114 | ||
115 | // If at least one PCP has been created, don't allow changing the target | |
116 | $pcpBlock = new CRM_PCP_DAO_PCPBlock(); | |
117 | $pcpBlock->entity_table = 'civicrm_event'; | |
118 | $pcpBlock->entity_id = $this->_id; | |
119 | $pcpBlock->find(TRUE); | |
120 | ||
121 | if (!empty($pcpBlock->id) && CRM_PCP_BAO_PCP::getPcpBlockInUse($pcpBlock->id)) { | |
122 | foreach (array( | |
353ffa53 | 123 | 'target_entity_type', |
d5cc0fc2 | 124 | 'target_entity_id', |
353ffa53 | 125 | ) as $element_name) { |
6a488035 TO |
126 | $element = $this->getElement($element_name); |
127 | $element->freeze(); | |
128 | } | |
129 | } | |
130 | $this->addFormRule(array('CRM_PCP_Form_Event', 'formRule'), $this); | |
131 | } | |
132 | ||
133 | /** | |
fe482240 | 134 | * Validation. |
6a488035 | 135 | * |
db95eff6 TO |
136 | * @param array $params |
137 | * (ref.) an assoc array of name/value pairs. | |
6a488035 | 138 | * |
dd244018 EM |
139 | * @param $files |
140 | * @param $self | |
141 | * | |
72b3a70c CW |
142 | * @return bool|array |
143 | * mixed true or array of errors | |
6a488035 TO |
144 | */ |
145 | public static function formRule($params, $files, $self) { | |
146 | $errors = array(); | |
a7488080 | 147 | if (!empty($params['is_active'])) { |
6a488035 | 148 | |
a7488080 | 149 | if (!empty($params['is_tellfriend_enabled']) && |
6a488035 TO |
150 | (CRM_Utils_Array::value('tellfriend_limit', $params) <= 0) |
151 | ) { | |
152 | $errors['tellfriend_limit'] = ts('if Tell Friend is enable, Maximum recipients limit should be greater than zero.'); | |
153 | } | |
a7488080 | 154 | if (empty($params['supporter_profile_id'])) { |
6a488035 TO |
155 | $errors['supporter_profile_id'] = ts('Supporter profile is a required field.'); |
156 | } | |
157 | else { | |
158 | if (CRM_PCP_BAO_PCP::checkEmailProfile($params['supporter_profile_id'])) { | |
159 | $errors['supporter_profile_id'] = ts('Profile is not configured with Email address.'); | |
160 | } | |
161 | } | |
162 | ||
163 | if ($emails = CRM_Utils_Array::value('notify_email', $params)) { | |
164 | $emailArray = explode(',', $emails); | |
165 | foreach ($emailArray as $email) { | |
166 | if ($email && !CRM_Utils_Rule::email(trim($email))) { | |
167 | $errors['notify_email'] = ts('A valid Notify Email address must be specified'); | |
168 | } | |
169 | } | |
170 | } | |
171 | } | |
172 | return empty($errors) ? TRUE : $errors; | |
173 | } | |
174 | ||
175 | /** | |
fe482240 | 176 | * Process the form submission. |
6a488035 | 177 | * |
355ba699 | 178 | * @return void |
6a488035 TO |
179 | */ |
180 | public function postProcess() { | |
181 | // get the submitted form values. | |
182 | $params = $this->controller->exportValues($this->_name); | |
183 | ||
184 | // Source | |
185 | $params['entity_table'] = 'civicrm_event'; | |
186 | $params['entity_id'] = $this->_id; | |
187 | ||
188 | // Target | |
189 | $params['target_entity_type'] = CRM_Utils_Array::value('target_entity_type', $params, 'event'); | |
190 | if ($params['target_entity_type'] == 'event') { | |
191 | $params['target_entity_id'] = $this->_id; | |
192 | } | |
193 | else { | |
194 | $params['target_entity_id'] = CRM_Utils_Array::value('target_entity_id', $params, $this->_id); | |
195 | } | |
196 | ||
353ffa53 | 197 | $dao = new CRM_PCP_DAO_PCPBlock(); |
6a488035 | 198 | $dao->entity_table = $params['entity_table']; |
353ffa53 | 199 | $dao->entity_id = $this->_id; |
6a488035 TO |
200 | $dao->find(TRUE); |
201 | $params['id'] = $dao->id; | |
202 | $params['is_active'] = CRM_Utils_Array::value('pcp_active', $params, FALSE); | |
203 | $params['is_approval_needed'] = CRM_Utils_Array::value('is_approval_needed', $params, FALSE); | |
204 | $params['is_tellfriend_enabled'] = CRM_Utils_Array::value('is_tellfriend_enabled', $params, FALSE); | |
205 | ||
f6bc51fd | 206 | CRM_PCP_BAO_PCPBlock::create($params); |
6a488035 | 207 | |
5d92a7e7 CW |
208 | // Update tab "disabled" css class |
209 | $this->ajaxResponse['tabValid'] = !empty($params['is_active']); | |
210 | ||
6a488035 TO |
211 | parent::endPostProcess(); |
212 | } | |
213 | ||
214 | /** | |
215 | * Return a descriptive name for the page, used in wizard header | |
216 | * | |
217 | * @return string | |
6a488035 TO |
218 | */ |
219 | public function getTitle() { | |
220 | return ts('Enable Personal Campaign Pages'); | |
221 | } | |
96025800 | 222 | |
6a488035 | 223 | } |