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 if(true == $wid_page[$dao->id
]['override']) {
193 $cust_templ = base64_decode($wid_page[$dao->id
]['custom_template']);
194 $this->setDefaults(array(
195 'custom_template' => $cust_templ));
200 /** Keep template in civicrm-wci/templates folder*/
201 $output = file_get_contents('templates/CRM/Wci/Page/Widget.tpl',FILE_USE_INCLUDE_PATH
);
202 $elem = $this->getElement('custom_template');
203 $elem->setValue($output);
205 parent
::buildQuickForm();
208 function postProcess() {
209 $values = $this->exportValues();
218 /** If override check is checked state then only save the custom_template to the
219 database. otherwise wci uses default tpl file.
221 if(isset($values['override'])){
222 $override = $values['override'];
223 $cust_tmpl = base64_encode(html_entity_decode($values['custom_template']));
224 $cust_tmpl_col = "custom_template";
229 if (isset($this->_id
)) {
230 $sql = "UPDATE civicrm_wci_widget SET title = '". $values['title']
231 . "', logo_image = '" . $values['logo_image'] . "', image = '"
232 . $values['image'] . "', button_title = '" . $values['button_title']
233 . "', button_link_to = '" . $values['button_link_to']
234 . "', progress_bar_id = '" . $values['progress_bar']
235 . "', description = '" . base64_encode($values['description'])
236 . "', email_signup_group_id = '" . $values['email_signup_group_id']
237 . "', size_variant = '" . $values['size_variant']
238 . "', color_title = '" . $values['color_title']
239 . "', color_title_bg = '" . $values['color_title_bg']
240 . "', color_progress_bar = '" . $values['color_bar']
241 . "', color_widget_bg = '" . $values['color_widget_bg']
242 . "', color_description = '" . $values['color_description']
243 . "', color_border = '" . $values['color_border']
244 . "', color_button = '" . $values['color_button']
245 . "', color_button_bg = '" . $values['color_button_bg']
246 . "', style_rules = '" . $values['style_rules'] . "', override = '"
247 . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id
;
250 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
251 button_title, button_link_to, progress_bar_id, description,
252 email_signup_group_id, size_variant, color_title, color_title_bg,
253 color_progress_bar, color_widget_bg, color_description, color_border,
254 color_button, color_button_bg, style_rules, override" . $coma . $cust_tmpl_col ." )
255 VALUES ('" . $values['title'] . "','" . $values['logo_image'] . "','" .
256 $values['image'] . "','" . $values['button_title'] . "','" .
257 $values['button_link_to'] . "','" . $values['progress_bar'] . "','" .
258 base64_encode($values['description']) . "','" .
259 $values['email_signup_group_id'] . "','" .
260 $values['size_variant'] . "','" . $values['color_title'] . "','" .
261 $values['color_title_bg'] . "','" . $values['color_bar'] . "','" .
262 $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
263 $values['color_border'] . "','" . $values['color_button'] . "','" .
264 $values['color_button_bg'] . "','" . $values['style_rules'] . "','" .
265 $override . $quote . $coma . $quote . $cust_tmpl
269 $errorScope = CRM_Core_TemporaryErrorScope
::useException();
271 $transaction = new CRM_Core_Transaction();
272 CRM_Core_DAO
::executeQuery($sql);
274 $transaction->commit();
276 catch (Exception
$e) {
278 print_r($e->getMessage());
279 $transaction->rollback();
282 parent
::postProcess();
285 function getProgressBars() {
287 '' => ts('- select -'),
289 $pbList = CRM_Wci_BAO_ProgressBar
::getProgressbarList();
290 foreach ($pbList as $pb) {
291 $options[$pb['id']] = $pb['name'];
296 function getContributionPageOptions() {
298 '' => ts('- select -'),
301 $result = civicrm_api3('contribution_page', 'get');
302 foreach ($result['values'] as $contribution_page) {
303 $options[$contribution_page['id']] = $contribution_page['title'];
309 function getGroupOptions() {
311 '' => ts('- select -'),
314 $result = civicrm_api3('group', 'get');
315 foreach ($result['values'] as $group) {
316 $options[$group['id']] = $group['title'];
322 function getSizeOptions() {
324 'thin' => ts('Thin'),
325 'normal' => ts('Normal'),
326 'wide' => ts('Wide'),
333 * Get the fields/elements defined in this form.
335 * @return array (string)
337 function getRenderableElementNames() {
338 // The _elements list includes some items which should not be
339 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
340 // items don't have labels. We'll identify renderable by filtering on
342 $elementNames = array();
343 foreach ($this->_elements
as $element) {
344 $label = $element->getLabel();
345 if (!empty($label)) {
346 $elementNames[] = $element->getName();
349 return $elementNames;