Handle the case where there is no embed code id in the embed code
[com.zyxware.civiwci.git] / CRM / Wci / WidgetCode.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM Widget Creation Interface (WCI) Version 1.0 |
5 +--------------------------------------------------------------------+
6 | Copyright Zyxware Technologies (c) 2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM WCI. |
9 | |
10 | CiviCRM WCI is free software; you can copy, modify, and distribute |
11 | it under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007. |
13 | |
14 | CiviCRM WCI is distributed in the hope that it will be useful, |
15 | but 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 along with this program; if not, contact Zyxware |
21 | Technologies at info[AT]zyxware[DOT]com. |
22 +--------------------------------------------------------------------+
23 */
24
25 require_once 'wci-helper-functions.php';
26
27 class CRM_Wci_WidgetCode {
28
29 static function generate_widget_code($widgetId, $preview = 0) {
30 $code = '';
31 $dynamic_content = FALSE;
32 $data = CRM_Wci_BAO_Widget::getWidgetData($widgetId);
33 $template = CRM_Core_Smarty::singleton();
34 $template->assign('wciform', $data);
35 $template->assign('cpageId', $data['button_link_to']);
36 $template->assign('preview', $preview);
37
38 if ($data["override"] == '0') {
39 $template->template_dir[] = getWciWidgetTemplatePath();
40 $wcidata = $template->fetch('WCIWidget.tpl');
41 } else {
42 $wcidata = $template->fetch('string:' . html_entity_decode($data['custom_template']));
43 }
44
45 $code = json_encode($wcidata);
46
47 if ($data['progress_bar_id'] != 0) {
48 $dynamic_content = TRUE;
49 }
50 return array('dynamic' => $dynamic_content, 'code' => $code);
51 }
52
53 static function get_widget_code($embedId, $preview = 0) {
54 $code = '';
55 if ($preview) {
56 $widget = CRM_Wci_WidgetCode::generate_widget_code($embedId, $preview);
57 return $widget['code'];
58 }
59 else {
60 $widgetId = CRM_Wci_BAO_EmbedCode::getWidgetId($embedId);
61 $code = CRM_Wci_BAO_WidgetCache::getWidgetCache($widgetId);
62 }
63 if (!$code) {
64 $widget = CRM_Wci_WidgetCode::generate_widget_code($widgetId);
65 CRM_Wci_BAO_WidgetCache::setWidgetCache($widgetId, $widget);
66 $code = $widget['code'];
67 }
68
69 return $code;
70 }
71 }