INFRA-132 - CRM/Contribute - Convert single-line @param to multi-line
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Widget.php
CommitLineData
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 */
35class CRM_Contribute_Form_ContributionPage_Widget extends CRM_Contribute_Form_ContributionPage {
36 protected $_colors;
37
430ae6dd
TO
38 protected $_widget;
39
00be9182 40 public function preProcess() {
6a488035
TO
41 parent::preProcess();
42
43 $this->_widget = new CRM_Contribute_DAO_Widget();
44 $this->_widget->contribution_page_id = $this->_id;
45 if (!$this->_widget->find(TRUE)) {
46 $this->_widget = NULL;
47 }
48 else {
49 $this->assign('widget_id', $this->_widget->id);
50
51 // check of home url is set, if set then it flash widget might be in use.
52 $this->assign('showStatus', FALSE);
53 if ($this->_widget->url_homepage) {
54 $this->assign('showStatus', TRUE);
55 }
56 }
57
58 $this->assign('cpageId', $this->_id);
59
60 $config = CRM_Core_Config::singleton();
61 $title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
62 $this->_id,
63 'title'
64 );
65
66 $this->_fields = array('title' => array(ts('Title'),
67 'text',
68 FALSE,
69 $title,
70 ),
71 'url_logo' => array(ts('URL to Logo Image'),
72 'text',
73 FALSE,
74 NULL,
75 ),
76 'button_title' => array(ts('Button Title'),
77 'text',
78 FALSE,
79 ts('Contribute!'),
80 ),
81 );
82
83 $this->_colorFields = array('color_title' => array(ts('Title Text Color'),
84 'text',
85 FALSE,
86 '#2786C2',
87 ),
88 'color_bar' => array(ts('Progress Bar Color'),
89 'text',
90 FALSE,
91 '#FFFFFF',
92 ),
93 'color_main_text' => array(ts('Additional Text Color'),
94 'text',
95 FALSE,
96 '#FFFFFF',
97 ),
98 'color_main' => array(ts('Background Color'),
99 'text',
100 FALSE,
101 '#96C0E7',
102 ),
103 'color_main_bg' => array(ts('Background Color Top Area'),
104 'text',
105 FALSE,
106 '#B7E2FF',
107 ),
108 'color_bg' => array(ts('Border Color'),
109 'text',
110 FALSE,
111 '#96C0E7',
112 ),
113 'color_about_link' => array(ts('Button Link Color'),
114 'text',
115 FALSE,
116 '#556C82',
117 ),
118 'color_button' => array(ts('Button Background Color'),
119 'text',
120 FALSE,
121 '#FFFFFF',
122 ),
123 'color_homepage_link' => array(ts('Homepage Link Color'),
124 'text',
125 FALSE,
126 '#FFFFFF',
127 ),
128 );
129 }
130
186c9c17 131 /**
c490a46a 132 * Set default values for the form. Note that in edit/view mode
186c9c17
EM
133 * the default values are retrieved from the database
134 *
186c9c17
EM
135 *
136 * @return void
137 */
138 /**
139 *
140 */
00be9182 141 public function setDefaultValues() {
6a488035
TO
142 $defaults = array();
143 // check if there is a widget already created
144 if ($this->_widget) {
145 CRM_Core_DAO::storeValues($this->_widget, $defaults);
146 }
147 else {
148 foreach ($this->_fields as $name => $val) {
149 $defaults[$name] = $val[3];
150 }
151 foreach ($this->_colorFields as $name => $val) {
152 $defaults[$name] = $val[3];
153 }
154 $defaults['about'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
155 $this->_id,
156 'intro_text'
157 );
158 }
159
160 $showHide = new CRM_Core_ShowHideBlocks();
161 $showHide->addHide('id-colors');
162 $showHide->addToTemplate();
163 return $defaults;
164 }
165
00be9182 166 public function buildQuickForm() {
6a488035
TO
167 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Widget');
168
169 $this->addElement('checkbox',
170 'is_active',
171 ts('Enable Widget?'),
172 NULL,
173 array('onclick' => "widgetBlock(this)")
174 );
175
176 $this->addWysiwyg('about', ts('About'), $attributes['about']);
177
178 foreach ($this->_fields as $name => $val) {
179 $this->add($val[1],
180 $name,
181 $val[0],
182 $attributes[$name],
183 $val[2]
184 );
185 }
186 foreach ($this->_colorFields as $name => $val) {
187 $this->add($val[1],
188 $name,
189 $val[0],
190 $attributes[$name],
191 $val[2]
192 );
193 }
194
195 $this->assign_by_ref('fields', $this->_fields);
196 $this->assign_by_ref('colorFields', $this->_colorFields);
197
198 $this->_refreshButtonName = $this->getButtonName('refresh');
199 $this->addElement('submit',
200 $this->_refreshButtonName,
201 ts('Save and Preview')
202 );
203 parent::buildQuickForm();
204 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Widget', 'formRule'), $this);
205 }
206
207 /**
100fef9d 208 * Validation
6a488035 209 *
014c4014
TO
210 * @param array $params
211 * (ref.) an assoc array of name/value pairs.
6a488035 212 *
da6b46f4
EM
213 * @param $files
214 * @param $self
215 *
6a488035 216 * @return mixed true or array of errors
6a488035
TO
217 * @static
218 */
219 public static function formRule($params, $files, $self) {
220 $errors = array();
a7488080
CW
221 if (!empty($params['is_active'])) {
222 if (empty($params['title'])) {
6a488035
TO
223 $errors['title'] = ts('Title is a required field.');
224 }
a7488080 225 if (empty($params['about'])) {
6a488035
TO
226 $errors['about'] = ts('About is a required field.');
227 }
228
229 foreach ($params as $key => $val) {
8cc574cf 230 if (substr($key, 0, 6) == 'color_' && empty($params[$key])) {
6a488035
TO
231 $errors[$key] = ts('%1 is a required field.', array(1 => $self->_colorFields[$key][0]));
232 }
233 }
234 }
235 return empty($errors) ? TRUE : $errors;
236 }
237
00be9182 238 public function postProcess() {
6a488035
TO
239 //to reset quickform elements of next (pcp) page.
240 if ($this->controller->getNextName('Widget') == 'PCP') {
241 $this->controller->resetPage('PCP');
242 }
243
244 // get the submitted form values.
245 $params = $this->controller->exportValues($this->_name);
246
247 if ($this->_widget) {
248 $params['id'] = $this->_widget->id;
249 }
250 $params['contribution_page_id'] = $this->_id;
251 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
252 $params['url_homepage'] = 'null';
253
254 $widget = new CRM_Contribute_DAO_Widget();
255 $widget->copyValues($params);
256 $widget->save();
257
258 $buttonName = $this->controller->getButtonName();
259 if ($buttonName == $this->_refreshButtonName) {
260 return;
261 }
262 parent::endPostProcess();
263 }
264
265 /**
266 * Return a descriptive name for the page, used in wizard header
267 *
268 * @return string
6a488035
TO
269 */
270 public function getTitle() {
271 return ts('Widget Settings');
272 }
273}