#30645 fixed No remove link in progressbar form
[com.zyxware.civiwci.git] / CRM / Wci / Form / CreateWidget.php
1 <?php
2
3 require_once 'CRM/Core/Form.php';
4 require_once 'wci-helper-functions.php';
5 require_once 'CRM/Wci/BAO/ProgressBar.php';
6
7 /**
8 * Form controller class
9 *
10 * @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
11 */
12 class CRM_Wci_Form_CreateWidget extends CRM_Core_Form {
13
14 /**
15 * the widget id saved to the session for an update
16 *
17 * @var int
18 * @access protected
19 */
20 protected $_id;
21
22 function preProcess() {
23 CRM_Core_Resources::singleton()->addScriptFile('org.civicrm.wci', 'createwidget.js');
24 parent::preProcess();
25 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this,
26 FALSE, NULL, 'REQUEST' );
27
28 $this->_colorFields = array('color_title' => array(ts('Title Text Color'),
29 'text',
30 FALSE,
31 '#2786C2',
32 ),
33 'color_title_bg' => array(ts('Widget title background color'),
34 'text',
35 FALSE,
36 '#FFFFFF',
37 ),
38 'color_bar' => array(ts('Progress Bar Color'),
39 'text',
40 FALSE,
41 '#FFFFFF',
42 ),
43 'color_widget_bg' => array(ts('Widget background color'),
44 'text',
45 FALSE,
46 '#96C0E7',
47 ),
48 'color_description' => array(ts('Widget description color'),
49 'text',
50 FALSE,
51 '#96C0E7',
52 ),
53 'color_border' => array(ts('Widget border color'),
54 'text',
55 FALSE,
56 '#96C0E7',
57 ),
58 'color_button' => array(ts('Widget button text color'),
59 'text',
60 FALSE,
61 '#000000',
62 ),
63 'color_button_bg' => array(ts('Widget button background color'),
64 'text',
65 FALSE,
66 '#96C0E7',
67 ),
68 'color_button_bg' => array(ts('Widget button background color'),
69 'text',
70 FALSE,
71 '#96C0E7',
72 ),
73 );
74 }
75
76 function setDefaultValues() {
77 $defaults = array();
78 if (!isset($this->_id)) {
79 $defaults['size_variant'] = 'normal';
80 foreach ($this->_colorFields as $name => $val) {
81 $defaults[$name] = $val[3];
82 }
83 }
84 return $defaults;
85 }
86
87 function buildQuickForm() {
88 // add form elements
89 $this->add('text', 'title', ts('Title'),true);
90 $this->add('text', 'logo_image', ts('Logo image'));
91 $this->add('text', 'image', ts('Image'));
92 $this->add('select', 'button_link_to', ts('Contribution button'), getContributionPageOptions());
93 $this->add('text', 'button_title', ts('Contribution button title'));
94 $this->add('select', 'progress_bar', ts('Progress bar'), $this->getProgressBars());
95 $this->addWysiwyg('description', ts('Description'), '');
96 $this->add('select', 'email_signup_group_id', ts('Newsletter signup'), $this->getGroupOptions());
97 $this->add('select', 'size_variant', ts('Size variant'), $this->getSizeOptions());
98 foreach ($this->_colorFields as $name => $val) {
99 $this->add($val[1],
100 $name,
101 $val[0],
102 $name,
103 $val[2]
104 );
105 }
106 $this->add('textarea', 'style_rules', ts('Additional Style Rules'));
107 $this->add('checkbox', 'override', ts('Override default template'));
108 $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>'));
109
110 $this->addElement('submit','preview','name="Save and Preview" id="preview"');
111 $this->addButtons(array(
112 array(
113 'type' => 'submit',
114 'name' => ts('Save'),
115 'isDefault' => TRUE,
116 ),
117 array(
118 'type' => 'next',
119 'name' => ts('Save & Preview'),
120 ),
121 ));
122
123 // export form elements
124 $this->assign('elementNames', $this->getRenderableElementNames());
125
126 if (isset($this->_id)) {
127 /** Updating existing widget*/
128
129 $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
130 where w.id=" . $this->_id;
131 $params = array();
132
133 $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_Widget');
134
135 while ($dao->fetch()) {
136 $wid_page[$dao->id] = array();
137 CRM_Core_DAO::storeValues($dao, $wid_page[$dao->id]);
138
139 $this->setDefaults(array(
140 'title' => $wid_page[$dao->id]['title']));
141 $this->setDefaults(array(
142 'logo_image' => $wid_page[$dao->id]['logo_image']));
143 $this->setDefaults(array(
144 'image' => $wid_page[$dao->id]['image']));
145 $this->setDefaults(array(
146 'button_link_to' => $wid_page[$dao->id]['button_link_to']));
147 $this->setDefaults(array(
148 'button_title' => $wid_page[$dao->id]['button_title']));
149
150 $this->setDefaults(array(
151 'progress_bar' => $dao->pbid));
152 $description = base64_decode($wid_page[$dao->id]['description']);
153 $this->setDefaults(array(
154 'description' => $description));
155 $this->setDefaults(array(
156 'email_signup_group_id' => $wid_page[$dao->id]['email_signup_group_id']));
157 $this->setDefaults(array(
158 'size_variant' => $wid_page[$dao->id]['size_variant']));
159 $this->setDefaults(array(
160 'color_title' => $wid_page[$dao->id]['color_title']));
161 $this->setDefaults(array(
162 'color_title_bg' => $wid_page[$dao->id]['color_title_bg']));
163 $this->setDefaults(array(
164 'color_bar' => $wid_page[$dao->id]['color_progress_bar']));
165 $this->setDefaults(array(
166 'color_widget_bg' => $wid_page[$dao->id]['color_widget_bg']));
167 $this->setDefaults(array(
168 'color_description' => $wid_page[$dao->id]['color_description']));
169 $this->setDefaults(array(
170 'color_border' => $wid_page[$dao->id]['color_border']));
171 $this->setDefaults(array(
172 'color_button' => $wid_page[$dao->id]['color_button']));
173 $this->setDefaults(array(
174 'color_button_bg' => $wid_page[$dao->id]['color_button_bg']));
175 $this->setDefaults(array(
176 'style_rules' => $wid_page[$dao->id]['style_rules']));
177 $this->setDefaults(array(
178 'override' => $wid_page[$dao->id]['override']));
179 if(true == $wid_page[$dao->id]['override']) {
180 $cust_templ = base64_decode($wid_page[$dao->id]['custom_template']);
181 $this->setDefaults(array(
182 'custom_template' => $cust_templ));
183 } else {
184 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH);
185 $elem = $this->getElement('custom_template');
186 $elem->setValue($output);
187 }
188 }
189 }
190 else {
191 /** Keep template in civicrm-wci/templates folder*/
192 $output = file_get_contents('templates/CRM/Wci/Page/wciwidget.tpl',FILE_USE_INCLUDE_PATH);
193 $elem = $this->getElement('custom_template');
194 $elem->setValue($output);
195 }
196 parent::buildQuickForm();
197 }
198
199 function postProcess() {
200 $values = $this->exportValues();
201
202 $override = 0;
203 $cust_tmpl = "";
204 $cust_tmpl_col = "";
205 $sql = "";
206 $coma = "";
207 $equals = "";
208 $quote = "";
209 /** If override check is checked state then only save the custom_template to the
210 database. otherwise wci uses default tpl file.
211 */
212 if(isset($values['override'])){
213 $override = $values['override'];
214 $cust_tmpl = base64_encode(html_entity_decode($values['custom_template']));
215 $cust_tmpl_col = "custom_template";
216 $coma = ",";
217 $equals = " = ";
218 $quote = "'";
219 }
220
221 if (isset($this->_id)) {
222 $sql = "UPDATE civicrm_wci_widget SET title = '". $values['title']
223 . "', logo_image = '" . $values['logo_image'] . "', image = '"
224 . $values['image'] . "', button_title = '" . $values['button_title']
225 . "', button_link_to = '" . $values['button_link_to']
226 . "', progress_bar_id = '" . $values['progress_bar']
227 . "', description = '" . base64_encode($values['description'])
228 . "', email_signup_group_id = '" . $values['email_signup_group_id']
229 . "', size_variant = '" . $values['size_variant']
230 . "', color_title = '" . $values['color_title']
231 . "', color_title_bg = '" . $values['color_title_bg']
232 . "', color_progress_bar = '" . $values['color_bar']
233 . "', color_widget_bg = '" . $values['color_widget_bg']
234 . "', color_description = '" . $values['color_description']
235 . "', color_border = '" . $values['color_border']
236 . "', color_button = '" . $values['color_button']
237 . "', color_button_bg = '" . $values['color_button_bg']
238 . "', style_rules = '" . $values['style_rules'] . "', override = '"
239 . $override . $quote . $coma . $cust_tmpl_col . $equals . $quote . $cust_tmpl . "' where id =" . $this->_id ;
240 }
241 else {
242 $sql = "INSERT INTO civicrm_wci_widget (title, logo_image, image,
243 button_title, button_link_to, progress_bar_id, description,
244 email_signup_group_id, size_variant, color_title, color_title_bg,
245 color_progress_bar, color_widget_bg, color_description, color_border,
246 color_button, color_button_bg, style_rules, override" . $coma . $cust_tmpl_col ." )
247 VALUES ('" . $values['title'] . "','" . $values['logo_image'] . "','" .
248 $values['image'] . "','" . $values['button_title'] . "','" .
249 $values['button_link_to'] . "','" . $values['progress_bar'] . "','" .
250 base64_encode($values['description']) . "','" .
251 $values['email_signup_group_id'] . "','" .
252 $values['size_variant'] . "','" . $values['color_title'] . "','" .
253 $values['color_title_bg'] . "','" . $values['color_bar'] . "','" .
254 $values['color_widget_bg'] . "','" . $values['color_description'] . "','" .
255 $values['color_border'] . "','" . $values['color_button'] . "','" .
256 $values['color_button_bg'] . "','" . $values['style_rules'] . "','" .
257 $override . $quote . $coma . $quote . $cust_tmpl
258 . "')";
259 }
260
261 $errorScope = CRM_Core_TemporaryErrorScope::useException();
262 try {
263 $transaction = new CRM_Core_Transaction();
264 CRM_Core_DAO::executeQuery("SET foreign_key_checks = 0;");
265 CRM_Core_DAO::executeQuery($sql);
266 CRM_Core_DAO::executeQuery("SET foreign_key_checks = 1;");
267 $transaction->commit();
268
269 if(isset($_REQUEST['_qf_CreateWidget_next'])) {
270 (isset($this->_id)) ? $widget_id = $this->_id :
271 $widget_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
272 CRM_Utils_System::redirect('?action=update&reset=1&id=' . $widget_id);
273 } else {
274 CRM_Utils_System::redirect('widget?reset=1');
275 }
276 }
277 catch (Exception $e) {
278 //TODO
279 print_r($e->getMessage());
280 $transaction->rollback();
281 }
282
283 parent::postProcess();
284 }
285
286 function getProgressBars() {
287 $options = array(
288 '' => ts('- select -'),
289 );
290 $pbList = CRM_Wci_BAO_ProgressBar::getProgressbarList();
291 foreach ($pbList as $pb) {
292 $options[$pb['id']] = $pb['name'];
293 }
294
295 return $options;
296 }
297 function getContributionPageOptions() {
298 $options = array(
299 '' => ts('- select -'),
300 );
301
302 $result = civicrm_api3('contribution_page', 'get');
303 foreach ($result['values'] as $contribution_page) {
304 $options[$contribution_page['id']] = $contribution_page['title'];
305 }
306
307 return $options;
308 }
309
310 function getGroupOptions() {
311 $options = array(
312 '' => ts('- select -'),
313 );
314
315 $result = civicrm_api3('group', 'get', array(
316 'group_type' => '2'
317 ));
318
319 foreach ($result['values'] as $group) {
320 $options[$group['id']] = $group['title'];
321 }
322
323 return $options;
324 }
325
326 function getSizeOptions() {
327 $options = array(
328 'thin' => ts('Thin'),
329 'normal' => ts('Normal'),
330 'wide' => ts('Wide'),
331 );
332
333 return $options;
334 }
335
336 /**
337 * Get the fields/elements defined in this form.
338 *
339 * @return array (string)
340 */
341 function getRenderableElementNames() {
342 // The _elements list includes some items which should not be
343 // auto-rendered in the loop -- such as "qfKey" and "buttons". These
344 // items don't have labels. We'll identify renderable by filtering on
345 // the 'label'.
346 $elementNames = array();
347 foreach ($this->_elements as $element) {
348 $label = $element->getLabel();
349 if (!empty($label)) {
350 $elementNames[] = $element->getName();
351 }
352 }
353 return $elementNames;
354 }
355 }