Merge pull request #2398 from kurund/CRM-13981
[civicrm-core.git] / CRM / Contribute / Form / ContributionPage / Widget.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35 class CRM_Contribute_Form_ContributionPage_Widget extends CRM_Contribute_Form_ContributionPage {
36 protected $_colors;
37
38 protected $_widget;
39
40 function preProcess() {
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
131 function setDefaultValues() {
132 $defaults = array();
133 // check if there is a widget already created
134 if ($this->_widget) {
135 CRM_Core_DAO::storeValues($this->_widget, $defaults);
136 }
137 else {
138 foreach ($this->_fields as $name => $val) {
139 $defaults[$name] = $val[3];
140 }
141 foreach ($this->_colorFields as $name => $val) {
142 $defaults[$name] = $val[3];
143 }
144 $defaults['about'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage',
145 $this->_id,
146 'intro_text'
147 );
148 }
149
150 $showHide = new CRM_Core_ShowHideBlocks();
151 $showHide->addHide('id-colors');
152 $showHide->addToTemplate();
153 return $defaults;
154 }
155
156 function buildQuickForm() {
157 $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Widget');
158
159 $this->addElement('checkbox',
160 'is_active',
161 ts('Enable Widget?'),
162 NULL,
163 array('onclick' => "widgetBlock(this)")
164 );
165
166 $this->addWysiwyg('about', ts('About'), $attributes['about']);
167
168 foreach ($this->_fields as $name => $val) {
169 $this->add($val[1],
170 $name,
171 $val[0],
172 $attributes[$name],
173 $val[2]
174 );
175 }
176 foreach ($this->_colorFields as $name => $val) {
177 $this->add($val[1],
178 $name,
179 $val[0],
180 $attributes[$name],
181 $val[2]
182 );
183 }
184
185 $this->assign_by_ref('fields', $this->_fields);
186 $this->assign_by_ref('colorFields', $this->_colorFields);
187
188 $this->_refreshButtonName = $this->getButtonName('refresh');
189 $this->addElement('submit',
190 $this->_refreshButtonName,
191 ts('Save and Preview')
192 );
193 parent::buildQuickForm();
194 $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Widget', 'formRule'), $this);
195 }
196
197 /**
198 * Function for validation
199 *
200 * @param array $params (ref.) an assoc array of name/value pairs
201 *
202 * @return mixed true or array of errors
203 * @access public
204 * @static
205 */
206 public static function formRule($params, $files, $self) {
207 $errors = array();
208 if (!empty($params['is_active'])) {
209 if (empty($params['title'])) {
210 $errors['title'] = ts('Title is a required field.');
211 }
212 if (empty($params['about'])) {
213 $errors['about'] = ts('About is a required field.');
214 }
215
216 foreach ($params as $key => $val) {
217 if (substr($key, 0, 6) == 'color_' && empty($params[$key])) {
218 $errors[$key] = ts('%1 is a required field.', array(1 => $self->_colorFields[$key][0]));
219 }
220 }
221 }
222 return empty($errors) ? TRUE : $errors;
223 }
224
225 function postProcess() {
226 //to reset quickform elements of next (pcp) page.
227 if ($this->controller->getNextName('Widget') == 'PCP') {
228 $this->controller->resetPage('PCP');
229 }
230
231 // get the submitted form values.
232 $params = $this->controller->exportValues($this->_name);
233
234 if ($this->_widget) {
235 $params['id'] = $this->_widget->id;
236 }
237 $params['contribution_page_id'] = $this->_id;
238 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
239 $params['url_homepage'] = 'null';
240
241 $widget = new CRM_Contribute_DAO_Widget();
242 $widget->copyValues($params);
243 $widget->save();
244
245 $buttonName = $this->controller->getButtonName();
246 if ($buttonName == $this->_refreshButtonName) {
247 return;
248 }
249 parent::endPostProcess();
250 }
251
252 /**
253 * Return a descriptive name for the page, used in wizard header
254 *
255 * @return string
256 * @access public
257 */
258 public function getTitle() {
259 return ts('Widget Settings');
260 }
261 }
262