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