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