From 1008246f0bf7f766a4b257cfbd170004d99eff53 Mon Sep 17 00:00:00 2001 From: Jagadedes Date: Tue, 18 Nov 2014 15:43:07 +0530 Subject: [PATCH] Beta 2 bug fixes. 31243. added agpl license and widget cache feature --- CRM/Wci/BAO/WidgetCache.php | 149 ++++++++++++++ CRM/Wci/DAO/WidgetCache.php | 255 ++++++++++++++++++++++++ CRM/Wci/Form/WCISettings.php | 7 +- CRM/Wci/Upgrader.php | 13 ++ CRM/Wci/WidgetCode.php | 44 ++++ extern/embed.php | 58 +++--- extern/wciembed.js | 39 ---- extern/wciwidget.php | 79 -------- settings/wci.setting.php | 12 ++ sql/install.sql | 10 + templates/CRM/Wci/Form/CreateWidget.tpl | 7 +- templates/CRM/Wci/Form/WCISettings.tpl | 9 +- 12 files changed, 524 insertions(+), 158 deletions(-) create mode 100644 CRM/Wci/BAO/WidgetCache.php create mode 100644 CRM/Wci/DAO/WidgetCache.php create mode 100644 CRM/Wci/WidgetCode.php delete mode 100644 extern/wciembed.js delete mode 100644 extern/wciwidget.php diff --git a/CRM/Wci/BAO/WidgetCache.php b/CRM/Wci/BAO/WidgetCache.php new file mode 100644 index 0000000..f33f5d1 --- /dev/null +++ b/CRM/Wci/BAO/WidgetCache.php @@ -0,0 +1,149 @@ +copyValues($params); + + $widget_cache->save(); + + return $widget_cache; + } + + /** + * Get a list of Widgets matching the params, where params keys are column + * names of civicrm_wci_widget. + * + * @param array $params + * @return array of CRM_Wci_BAO_ProgressBarFormula objects + */ + static function retrieve(array $params) { + $result = array(); + + $widget_cache = new CRM_Wci_BAO_WidgetCache(); + $widget_cache->copyValues($params); + $widget_cache->find(); + + while ($widget_cache->fetch()) { + $result[(int) $widget_cache->id] = clone $widget_cache; + } + + $widget_cache->free(); + + return $result; + } + + /** + * Wrapper method for retrieve + * + * @param mixed $id Int or int-like string representing widget ID + * @return CRM_Wci_BAO_ProgressBarFormula + */ + static function retrieveByID($id) { + if (!is_int($id) && !ctype_digit($id)) { + CRM_Core_Error::fatal(__CLASS__ . '::' . __FUNCTION__ . ' expects an integer.'); + } + $id = (int) $id; + + $widget_cache = self::retrieve(array('id' => $id)); + + if (!array_key_exists($id, $widget_cache)) { + CRM_Core_Error::fatal("No formula entry with ID $id exists."); + } + + return $widget_cache[$id]; + } + + public static function setWidgetCache($widgetId, $code) { + $query = "DELETE from civicrm_wci_widget_cache WHERE widget_id = %1"; + CRM_Core_DAO::executeQuery($query, array(1=>array($widgetId, 'Integer')), + TRUE, 'CRM_Wci_DAO_WidgetCache'); + + $query = "INSERT into civicrm_wci_widget_cache (widget_id, widget_code) VALUES (%1, %2)"; + $params = array(1=>array($widgetId, 'Integer'), + 2=>array($code, 'String'),); + CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_WidgetCache'); + } + public static function getCurrentTsDiff($widgetId) { + $tsDiff = 0; + $query = "SELECT TIMESTAMPDIFF(MINUTE, `ts`, NOW()) as tsDiff FROM + civicrm_wci_widget_cache where widget_id = %1"; + $dao = CRM_Core_DAO::executeQuery($query, array(1=>array($widgetId, 'Integer')), + TRUE, 'CRM_Wci_DAO_WidgetCache'); + if ($dao->fetch()) { + $tsDiff = $dao->tsDiff; + } else { + $tsDiff = PHP_INT_MAX; + } + return $tsDiff; + } + + public static function getWidgetCache($widgetId) { + $code = ""; + $query = "SELECT widget_code FROM civicrm_wci_widget_cache where widget_id = %1"; + $dao = CRM_Core_DAO::executeQuery($query, array(1=>array($widgetId, 'Integer')), + TRUE, 'CRM_Wci_DAO_WidgetCache'); + if ($dao->fetch()) { + $code = $dao->widget_code; + } + return $code; + } +} diff --git a/CRM/Wci/DAO/WidgetCache.php b/CRM/Wci/DAO/WidgetCache.php new file mode 100644 index 0000000..379385e --- /dev/null +++ b/CRM/Wci/DAO/WidgetCache.php @@ -0,0 +1,255 @@ +__table = 'civicrm_wci_widget_cache'; + parent::__construct(); + } + /** + * return foreign keys and entity references + * + * @static + * @access public + * @return array of CRM_Core_EntityReference + */ + static function getReferenceColumns() + { + return self::$_links; + } + /** + * returns all the column names of this table + * + * @access public + * @return array + */ + static function &fields() + { + if (!(self::$_fields)) { + self::$_fields = array( + 'widget_cache_id' => array( + 'name' => 'id', + 'type' => CRM_Utils_Type::T_INT, + 'title' => ts('cahce Id', array('domain' => 'org.civicrm.wci')) , + 'required' => true, + ) , + 'widget_id' => array( + 'name' => 'widget_id', + 'type' => CRM_Utils_Type::T_INT, + 'required' => false, + ) , + 'widget_code' => array( + 'name' => 'widget_code', + 'type' => CRM_Utils_Type::T_TEXT, + 'title' => ts('widget code', array('domain' => 'org.civicrm.wci')) , + 'required' => false, + ) , + 'ts' => array( + 'name' => 'ts', + 'type' => CRM_Utils_Type::T_TIMESTAMP, + 'title' => ts('timestamp', array('domain' => 'org.civicrm.wci')) , + 'required' => false, + 'maxlength' => 64, + ) , + + + ); + } + return self::$_fields; + } + /** + * Returns an array containing, for each field, the arary key used for that + * field in self::$_fields. + * + * @access public + * @return array + */ + static function &fieldKeys() + { + if (!(self::$_fieldKeys)) { + self::$_fieldKeys = array( + 'id' => 'widget_cache_id', + 'widget_id' => 'widget_id', + 'widget_code' => 'widget_code', + 'ts' => 'ts', + ); + } + return self::$_fieldKeys; + } + /** + * returns the names of this table + * + * @access public + * @static + * @return string + */ + static function getTableName() + { + return self::$_tableName; + } + /** + * returns if this table needs to be logged + * + * @access public + * @return boolean + */ + function getLog() + { + return self::$_log; + } + /** + * returns the list of fields that can be imported + * + * @access public + * return array + * @static + */ + static function &import($prefix = false) + { + if (!(self::$_import)) { + self::$_import = array(); + $fields = self::fields(); + foreach($fields as $name => $field) { + if (CRM_Utils_Array::value('import', $field)) { + if ($prefix) { + self::$_import['wci_progress_bar'] = & $fields[$name]; + } else { + self::$_import[$name] = & $fields[$name]; + } + } + } + } + return self::$_import; + } + /** + * returns the list of fields that can be exported + * + * @access public + * return array + * @static + */ + static function &export($prefix = false) + { + if (!(self::$_export)) { + self::$_export = array(); + $fields = self::fields(); + foreach($fields as $name => $field) { + if (CRM_Utils_Array::value('export', $field)) { + if ($prefix) { + self::$_export['wci_progress_bar'] = & $fields[$name]; + } else { + self::$_export[$name] = & $fields[$name]; + } + } + } + } + return self::$_export; + } +} diff --git a/CRM/Wci/Form/WCISettings.php b/CRM/Wci/Form/WCISettings.php index 8c23938..7e5001c 100644 --- a/CRM/Wci/Form/WCISettings.php +++ b/CRM/Wci/Form/WCISettings.php @@ -28,6 +28,7 @@ class CRM_Wci_Form_WCISettings extends CRM_Core_Form { );*/ $this->add('text', 'default_profile', ts('Default profile'),true)->setSize(45); + $this->add('text', 'widget_cache_timeout', ts('Widget cache timeout'),true)->setSize(45); $this->addButtons(array( array( 'type' => 'submit', @@ -36,13 +37,16 @@ class CRM_Wci_Form_WCISettings extends CRM_Core_Form { ), )); - $widgetId = civicrm_api3('setting', 'getValue', array('group' => 'Wci Preference', 'name' => 'default_wci_widget')); + $cacheTime = civicrm_api3('setting', 'getValue', array('group' => 'Wci Preference', 'name' => 'widget_cache_timeout')); $defProf = civicrm_api3('setting', 'getValue', array('group' => 'Wci Preference', 'name' => 'default_wci_profile')); /*$this->setDefaults(array( 'default_widget' => $widgetId));*/ $this->setDefaults(array( 'default_profile' => $defProf)); + $this->setDefaults(array( + 'widget_cache_timeout' => $cacheTime)); + // export form elements $this->assign('elementNames', $this->getRenderableElementNames()); @@ -55,6 +59,7 @@ class CRM_Wci_Form_WCISettings extends CRM_Core_Form { $values = $this->exportValues(); /*civicrm_api3('setting', 'create', array('domain_id' => 1, 'default_wci_widget' => $values['default_widget'],));*/ civicrm_api3('setting', 'create', array('domain_id' => 1, 'default_wci_profile' => $values['default_profile'],)); + civicrm_api3('setting', 'create', array('domain_id' => 1, 'widget_cache_timeout' => $values['widget_cache_timeout'],)); CRM_Core_Session::setStatus(ts('Widget settings are saved to database'), '', 'success'); parent::postProcess(); } diff --git a/CRM/Wci/Upgrader.php b/CRM/Wci/Upgrader.php index f6781fd..d80fdcc 100644 --- a/CRM/Wci/Upgrader.php +++ b/CRM/Wci/Upgrader.php @@ -60,6 +60,19 @@ class CRM_Wci_Upgrader extends CRM_Wci_Upgrader_Base { return TRUE; } + public function upgrade_1001() { + $this->ctx->log->info('Applying update 1001'); + CRM_Core_DAO::executeQuery(' + CREATE TABLE IF NOT EXISTS civicrm_wci_widget_cache ( + id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT "cache Id.", + widget_id int(10) unsigned DEFAULT NULL COMMENT "widget id.", + widget_code text DEFAULT NULL COMMENT "Widget code.", + ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; + '); + return TRUE; + } /** * Example: Run an external SQL script diff --git a/CRM/Wci/WidgetCode.php b/CRM/Wci/WidgetCode.php new file mode 100644 index 0000000..2ed2898 --- /dev/null +++ b/CRM/Wci/WidgetCode.php @@ -0,0 +1,44 @@ +assign('wciform', $data); + $template->assign('cpageId', $data['button_link_to']); +// $template->assign('preview', $preview); + + if ($data["override"] == '0') { + $template->template_dir[] = getWciWidgetTemplatePath(); + $wcidata = $template->fetch('wciwidget.tpl'); + } else { + $wcidata = $template->fetch('string:' . html_entity_decode($data['custom_template'])); + } + $code = json_encode($wcidata); + CRM_Wci_BAO_WidgetCache::setWidgetCache($widgetId, $code); + return $code; + } + + static function get_widget_code($embedId, $preview=0) { + + if($preview) { + /**On preview time controller is called from create widget + form so id will be widget id */ + $code = CRM_Wci_WidgetCode::get_widget_realtime_code($embedId); + } else { + $widgetId = CRM_Wci_BAO_EmbedCode::getWidgetId($embedId); + $code = CRM_Wci_BAO_WidgetCache::getWidgetCache($widgetId); + + $tsDiff = CRM_Wci_BAO_WidgetCache::getCurrentTsDiff($widgetId); + $cacheTime = civicrm_api3('setting', 'getValue', + array('group' => 'Wci Preference', 'name' => 'widget_cache_timeout')); + if(($tsDiff > $cacheTime)||(empty($code))) { + $code = CRM_Wci_WidgetCode::get_widget_realtime_code($widgetId); + } + } + return $code; + } +} + diff --git a/extern/embed.php b/extern/embed.php index 8455adb..a81a7f7 100644 --- a/extern/embed.php +++ b/extern/embed.php @@ -38,12 +38,27 @@ */ require_once '../../../civicrm.config.php'; -require_once '../wci-helper-functions.php'; -require_once 'CRM/Core/Config.php'; -require_once 'CRM/Contribute/BAO/Widget.php'; -require_once 'CRM/Utils/Request.php'; -$wciembed_js = '// Cleanup functions for the document ready method +$wciembed_js = ' +/* + @licstart The following is the entire license notice for the + JavaScript code in this page. + + Copyright (C) 2014 Zyxware Technologies + + The JavaScript code in this page is free software: you can + redistribute it and/or modify it under the terms of the GNU Affero + General Public License (GNU AGPL) as published by the Free Software + Foundation, either version 3 of the License, or (at your option) + any later version. The code is distributed WITHOUT ANY WARRANTY; + without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU AGPL for more details. + + @licend The above is the entire license notice + for the JavaScript code in this page. + */ + +// Cleanup functions for the document ready method if ( document.addEventListener ) { DOMContentLoaded = function() { document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false ); @@ -84,37 +99,12 @@ function onReady( ) { }'; $config = CRM_Core_Config::singleton(); -$template = CRM_Core_Smarty::singleton(); -$widgetId = CRM_Utils_Request::retrieve('widgetId', 'Positive', CRM_Core_DAO::$_nullObject); -if(empty($widgetId)) { - $embed = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullObject); - $widgetId = CRM_Wci_BAO_EmbedCode::getWidgetId($embed); - - if(empty($widgetId)) { - $widgetId = civicrm_api3('setting', 'getValue', array('group' => 'Wci Preference', 'name' => 'default_wci_widget')); - } -} +$embedId = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullObject); $preview = CRM_Utils_Request::retrieve('preview', 'Positive', CRM_Core_DAO::$_nullObject); +$output = 'var wciwidgetcode = ' . CRM_Wci_WidgetCode::get_widget_code($embedId, $preview) . ';'; -if (isset($format)) { - $jsonvar .= $cpageId; -} else { - $data = CRM_Wci_BAO_Widget::getWidgetData($widgetId); - $template->assign('wciform', $data); - $template->assign('cpageId', $data['button_link_to']); - $template->assign('preview', $preview); - - if ($data["override"] == '0') { - $template->template_dir[] = getWciWidgetTemplatePath(); - $wcidata = $template->fetch('wciwidget.tpl'); - } else { - $wcidata = $template->fetch('string:' . html_entity_decode($data['custom_template'])); - } - $output = 'var wciwidgetcode = ' . json_encode($wcidata) . ';'; - - $output = $output . $wciembed_js; - echo $output; -} +$output = $output . $wciembed_js; +echo $output; CRM_Utils_System::civiExit(); diff --git a/extern/wciembed.js b/extern/wciembed.js deleted file mode 100644 index 9fb0125..0000000 --- a/extern/wciembed.js +++ /dev/null @@ -1,39 +0,0 @@ -// Cleanup functions for the document ready method -if ( document.addEventListener ) { - DOMContentLoaded = function() { - document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false ); - onReady(); - }; -} else if ( document.attachEvent ) { - DOMContentLoaded = function() { - // Make sure body exists, at least, in case IE gets a little overzealous - if ( document.readyState === "complete" ) { - document.detachEvent( "onreadystatechange", DOMContentLoaded ); - onReady(); - } - }; -} -if ( document.readyState === "complete" ) { - // Handle it asynchronously to allow scripts the opportunity to delay ready - setTimeout( onReady, 1 ); -} - -// Mozilla, Opera and webkit support this event -if ( document.addEventListener ) { - // Use the handy event callback - document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false ); - // A fallback to window.onload, that will always work - window.addEventListener( "load", onReady, false ); - // If IE event model is used -} else if ( document.attachEvent ) { - // ensure firing before onload, - // maybe late but safe also for iframes - document.attachEvent("onreadystatechange", DOMContentLoaded); - - // A fallback to window.onload, that will always work - window.attachEvent( "onload", onReady ); -} - -function onReady( ) { - document.getElementById('widgetwci').innerHTML = wciwidgetcode; -} \ No newline at end of file diff --git a/extern/wciwidget.php b/extern/wciwidget.php deleted file mode 100644 index 303916c..0000000 --- a/extern/wciwidget.php +++ /dev/null @@ -1,79 +0,0 @@ - -*/ - -require_once '../../../civicrm.config.php'; -require_once '../wci-helper-functions.php'; -require_once 'CRM/Core/Config.php'; -require_once 'CRM/Contribute/BAO/Widget.php'; -require_once 'CRM/Utils/Request.php'; - -$config = CRM_Core_Config::singleton(); -$template = CRM_Core_Smarty::singleton(); - -$widgetId = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullObject); -if(empty($widgetId)) { - $widgetId = civicrm_api3('setting', 'getValue', array('group' => 'Wci Preference', 'name' => 'default_wci_widget')); -} -$embed = CRM_Utils_Request::retrieve('embed', 'Positive', CRM_Core_DAO::$_nullObject); - -if (isset($format)) { - $jsonvar .= $cpageId; -} else { - $widData = CRM_Wci_BAO_Widget::getWidgetData($widgetId); - $pbData = CRM_Wci_BAO_ProgressBar::getProgressbarData($widData["progress_bar_id"]); - $data = array_merge($widData, $pbData); - - $template->assign('wciform', $data); - $template->assign('cpageId', $data['button_link_to']); - $template->assign('embed', $embed); - - if ($data["override"] == '0') { - $template->template_dir[] = getWciWidgetTemplatePath(); - $wcidata = $template->fetch('wciwidget.tpl'); - } else { - $wcidata = $template->fetch('string:' . html_entity_decode($data['custom_template'])); - } - $output = 'var wciwidgetcode = ' . json_encode($wcidata) . ';'; - - $wciembed = file_get_contents('wciembed.js',FILE_USE_INCLUDE_PATH); - $output = $output . $wciembed; - echo $output; -} - -CRM_Utils_System::civiExit(); diff --git a/settings/wci.setting.php b/settings/wci.setting.php index ebf2c0f..12158ab 100644 --- a/settings/wci.setting.php +++ b/settings/wci.setting.php @@ -24,6 +24,18 @@ return array( 'description' => 'Default profile id', 'help_text' => 'Sets default profile id', ), + 'widget_cache_timeout' => array( + 'group_name' => 'Wci Preference', + 'group' => 'wci', + 'name' => 'widget_cache_timeout', + 'type' => 'Integer', + 'default' => 0, + 'add' => '4.3', + 'is_domain' => 1, + 'is_contact' => 0, + 'description' => 'widget timeout', + 'help_text' => 'widget timeout', + ), ); diff --git a/sql/install.sql b/sql/install.sql index d6c0cdc..a506792 100644 --- a/sql/install.sql +++ b/sql/install.sql @@ -64,3 +64,13 @@ CREATE TABLE IF NOT EXISTS civicrm_wci_embed_code ( PRIMARY KEY (`id`), UNIQUE KEY `unique_wci_name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; + +-- WCI widget cache. +CREATE TABLE IF NOT EXISTS civicrm_wci_widget_cache ( + id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'cache Id.', + widget_id int(10) unsigned DEFAULT NULL COMMENT 'widget id.', + widget_code text DEFAULT NULL COMMENT 'Widget code.', + ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`) + +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; diff --git a/templates/CRM/Wci/Form/CreateWidget.tpl b/templates/CRM/Wci/Form/CreateWidget.tpl index 24a401c..a920528 100644 --- a/templates/CRM/Wci/Form/CreateWidget.tpl +++ b/templates/CRM/Wci/Form/CreateWidget.tpl @@ -7,7 +7,7 @@ {if $form.title.value != ""} {php} if(isset($_REQUEST['id'])) { - $wid_id = $_REQUEST['id']; + /*$wid_id = $_REQUEST['id']; $data = CRM_Wci_BAO_Widget::getWidgetData($wid_id); $template = CRM_Core_Smarty::singleton(); $template->assign('wciform', $data); @@ -16,7 +16,7 @@ $wcidata = $template->fetch('wciwidget.tpl'); } else { $wcidata = $template->fetch('string:' . $wid_page[$dao->id]['custom_template']); - } + }*/ $widget_controller_path = getWciWidgetControllerPath(); $extension_root_path = getExtensionRootPath(); } @@ -30,7 +30,8 @@
Click Save & Preview to save any changes to your settings, and preview the widget again on this page.
- + +