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