3 require_once 'CRM/Core/Form.php';
4 require_once 'wci-helper-functions.php';
5 require_once 'CRM/Wci/BAO/ProgressBar.php';
8 <script type
="text/javascript">
11 if ($
('#override').is(':checked') == true) {
12 $
('#custom_template').attr("disabled",false);
15 $
('#custom_template').attr("disabled",true);
18 $
(document
).ready(setState
)
19 $
('#override').click(setState
);
26 * Form controller class
28 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
30 class CRM_Wci_Form_CreateWidget
extends CRM_Core_Form
{
33 * the widget id saved to the session for an update
40 function preProcess() {
42 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this,
43 FALSE, NULL, 'REQUEST' );
45 $this->_colorFields
= array('color_title' => array(ts('Title Text Color'),
50 'color_title_bg' => array(ts('Widget title background color'),
55 'color_bar' => array(ts('Progress Bar Color'),
60 'color_widget_bg' => array(ts('Widget background color'),
65 'color_description' => array(ts('Widget description color'),
70 'color_border' => array(ts('Widget border color'),
75 'color_button' => array(ts('Widget button text color'),
80 'color_button_bg' => array(ts('Widget button background color'),
85 'color_button_bg' => array(ts('Widget button background color'),
93 function setDefaultValues() {
96 $defaults['size_variant'] = 'normal';
97 foreach ($this->_colorFields
as $name => $val) {
98 $defaults[$name] = $val[3];
104 function buildQuickForm() {
106 $this->add('text', 'title', ts('Title'),true);
107 $this->add('text', 'logo_image', ts('Logo image'));
108 $this->add('text', 'image', ts('Image'));
109 $this->add('select', 'button_link_to', ts('Contribution button'), getContributionPageOptions());
110 $this->add('text', 'button_title', ts('Contribution button title'));
111 $this->add('select', 'progress_bar', ts('Progress bar'), $this->getProgressBars());
112 $this->addWysiwyg('description', ts('Description'), '');
113 $this->add('select', 'email_signup_group_id', ts('Newsletter signup'), $this->getGroupOptions());
114 $this->add('select', 'size_variant', ts('Size variant'), $this->getSizeOptions());
115 foreach ($this->_colorFields
as $name => $val) {
123 $this->add('textarea', 'style_rules', ts('Additional Style Rules'));
124 $this->add('checkbox', 'override', ts('Override default template'));
125 $this->add('textarea', 'custom_template', ts('Custom template:<br><SMALL><font color="red">Please customize the smarty v2 template only if you know what you are doing</font></SMALL>'));
127 $this->addButtons(array(
130 'name' => ts('Save'),
135 // export form elements
136 $this->assign('elementNames', $this->getRenderableElementNames());
138 $smarty = CRM_Core_Smarty
::singleton();
139 if (isset($this->_id
)) {
140 /** Updating existing widget*/
142 $query = "SELECT pb.id as pbid, w.* FROM civicrm_wci_widget w INNER JOIN civicrm_wci_progress_bar pb on pb.id = w.progress_bar_id
143 where w.id=" . $this->_id
;
146 $dao = CRM_Core_DAO
::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
148 while ($dao->fetch()) {
149 $wid_page[$dao->id
] = array();
150 CRM_Core_DAO
::storeValues($dao, $wid_page[$dao->id
]);
152 $this->setDefaults(array(
153 'title' => $wid_page[$dao->id
]['title']));
154 $this->setDefaults(array(
155 'logo_image' => $wid_page[$dao->id
]['logo_image']));
156 $this->setDefaults(array(
157 'image' => $wid_page[$dao->id
]['image']));
158 $this->setDefaults(array(
159 'button_link_to' => $wid_page[$dao->id
]['button_link_to']));
160 $this->setDefaults(array(
161 'button_title' => $wid_page[$dao->id
]['button_title']));
163 $this->setDefaults(array(
164 'progress_bar' => $dao->pbid
));
165 $description = base64_decode($wid_page[$dao->id
]['description']);
166 $this->setDefaults(array(
167 'description' => $description));
168 $this->setDefaults(array(
169 'email_signup_group_id' => $wid_page[$dao->id
]['email_signup_group_id']));
170 $this->setDefaults(array(
171 'size_variant' => $wid_page[$dao->id
]['size_variant']));
172 $this->setDefaults(array(
173 'color_title' => $wid_page[$dao->id
]['color_title']));
174 $this->setDefaults(array(
175 'color_title_bg' => $wid_page[$dao->id
]['color_title_bg']));
176 $this->setDefaults(array(
177 'color_bar' => $wid_page[$dao->id
]['color_progress_bar']));
178 $this->setDefaults(array(
179 'color_widget_bg' => $wid_page[$dao->id
]['color_widget_bg']));
180 $this->setDefaults(array(
181 'color_description' => $wid_page[$dao->id
]['color_description']));
182 $this->setDefaults(array(
183 'color_border' => $wid_page[$dao->id
]['color_border']));
184 $this->setDefaults(array(
185 'color_button' => $wid_page[$dao->id
]['color_button']));
186 $this->setDefaults(array(
187 'color_button_bg' => $wid_page[$dao->id
]['color_button_bg']));
188 $this->setDefaults(array(
189 'style_rules' => $wid_page[$dao->id
]['style_rules']));
190 $this->setDefaults(array(
191 'override' => $wid_page[$dao->id
]['override']));
192 $cust_templ = base64_decode($wid_page[$dao->id
]['custom_template']);
194 $this->setDefaults(array(
195 'custom_template' => $cust_templ));
199 /** Keep template in civicrm-wci/templates folder*/
200 $output = file_get_contents('templates/CRM/Wci/Page/Widget.tpl',FILE_USE_INCLUDE_PATH
);
201 $elem = $this->getElement('custom_template');
202 $elem->setValue($output);
204 parent
::buildQuickForm();
207 function postProcess() {
208 $values = $this->exportValues();
214 /** If override check is checked state then only save the custom_template to the
215 database. otherwise wci uses default tpl file.
217 if(isset($values['override'])){
218 $override = $values['override'];
219 $cust_tmpl = base64_encode(html_entity_decode($values['custom_template']));
220 $cust_tmpl_col = ", custom_template";
222 if (isset($this->_id
)) {
223 $cust_tmpl_col = "'" . $cust_tmpl_col . "='";
224 $sql = "UPDATE civicrm_wci_widget SET title = '". $values['title']
225 . "', logo_image = '" . $values['logo_image'] . "', image = '"
226 . $values['image'] . "', button_title = '" . $values['button_title']
227 . "', button_link_to = '" . $values['button_link_to']
228 . "', progress_bar_id = '" . $values['progress_bar']
229 . "', description = '" . base64_encode($values['description'])
230 . "', email_signup_group_id = '" . $values['email_signup_group_id']
231 . "', size_variant = '" . $values['size_variant']
232 . "', color_title = '" . $values['color_title']
233 . "', color_title_bg = '" . $values['color_title_bg']
234 . "', color_progress_bar = '" . $values['color_bar']
235 . "', color_widget_bg = '" . $values['color_widget_bg']
236 . "', color_description = '" . $values['color_description']
237 . "', color_border = '" . $values['color_border']
238 . "', color_button = '" . $values['color_button']
239 . "', color_button_bg = '" . $values['color_button_bg']
240 . "', style_rules = '" . $values['style_rules'] . "', override = '"
241 . $override . $cust_tmpl_col . $cust_tmpl . "'";
244 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
245 button_title, button_link_to, progress_bar_id, description,
246 email_signup_group_id, size_variant, color_title, color_title_bg,
247 color_progress_bar, color_widget_bg, color_description, color_border,
248 color_button, color_button_bg, style_rules, override" . $cust_tmpl_col ." )
249 VALUES ('" . $values['title'] . "','" . $values['logo_image'] . "','" .
250 $values['image'] . "','" . $values['button_title'] . "','" .
251 $values['button_link_to'] . "','" . $values['progress_bar'] . "','" .
252 base64_encode($values['description']) . "','" .
253 $values['email_signup_group_id'] . "','" .
254 $values['size_variant'] . "','" . $values['color_title'] . "','" .
255 $values['color_title_bg'] . "','" . $values['color_bar'] . "','" .
256 $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
257 $values['color_border'] . "','" . $values['color_button'] . "','" .
258 $values['color_button_bg'] . "','" . $values['style_rules'] . "','" .
259 $override . "','" . $cust_tmpl
262 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
264 $transaction = new CRM_Core_Transaction();
265 CRM_Core_DAO
::executeQuery($sql);
267 $transaction->commit();
269 catch (Exception
$e) {
271 print_r($e->getMessage());
272 $transaction->rollback();
275 parent
::postProcess();
278 function getProgressBars() {
280 '' => ts('- select -'),
282 $pbList = CRM_Wci_BAO_ProgressBar
::getProgressbarList();
283 foreach ($pbList as $pb) {
284 $options[$pb['id']] = $pb['name'];
289 function getContributionPageOptions() {
291 '' => ts('- select -'),
294 $result = civicrm_api3('contribution_page', 'get');
295 foreach ($result['values'] as $contribution_page) {
296 $options[$contribution_page['id']] = $contribution_page['title'];
302 function getGroupOptions() {
304 '' => ts('- select -'),
307 $result = civicrm_api3('group', 'get');
308 foreach ($result['values'] as $group) {
309 $options[$group['id']] = $group['title'];
315 function getSizeOptions() {
317 'thin' => ts('Thin'),
318 'normal' => ts('Normal'),
319 'wide' => ts('Wide'),
326 * Get the fields/elements defined in this form.
328 * @return array (string)
330 function getRenderableElementNames() {
331 // The _elements list includes some items which should not be
332 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
333 // items don't have labels. We'll identify renderable by filtering on
335 $elementNames = array();
336 foreach ($this->_elements
as $element) {
337 $label = $element->getLabel();
338 if (!empty($label)) {
339 $elementNames[] = $element->getName();
342 return $elementNames;