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