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