Improved the structure of caching system.
Fixed minor bugs in the caching system.
}
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'),);
+ $cacheTime = civicrm_api3('setting', 'getValue',
+ array('group' => 'Wci Preference', 'name' => 'widget_cache_timeout'));
+ $expire_on = time() + ($cacheTime * 60);
+ $query = "INSERT INTO civicrm_wci_widget_cache (widget_id, widget_code, expire)
+ VALUES (%1, %2, %3)
+ ON DUPLICATE KEY UPDATE widget_id = %1, widget_code = %2, expire = %3";
+ $params = array(
+ 1 => array($widgetId, 'Integer'),
+ 2 => array($code, 'String'),
+ 3 => array($expire_on, 'Integer')
+ );
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');
+ $query = "SELECT widget_code FROM civicrm_wci_widget_cache where widget_id = %1
+ AND expire >= %2";
+ $cacheTime = civicrm_api3('setting', 'getValue',
+ array('group' => 'Wci Preference', 'name' => 'widget_cache_timeout'));
+ $expire_on = time() + ($cacheTime * 60);
+ $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($widgetId, 'Integer'),
+ 2 => array($expire_on, 'Integer')), TRUE, 'CRM_Wci_DAO_WidgetCache');
if ($dao->fetch()) {
$code = $dao->widget_code;
}
return $code;
}
+
+ public static function deleteWidgetCache($widgetId) {
+ $code = "";
+ $query = "DELETE 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 (!(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,
+ 'required' => true,
) ,
'widget_code' => array(
'name' => 'widget_code',
'title' => ts('widget code', array('domain' => 'org.civicrm.wci')) ,
'required' => false,
) ,
- 'ts' => array(
+ 'expire' => array(
'name' => 'ts',
- 'type' => CRM_Utils_Type::T_TIMESTAMP,
+ 'type' => CRM_Utils_Type::T_INT,
'title' => ts('timestamp', array('domain' => 'org.civicrm.wci')) ,
'required' => false,
- 'maxlength' => 64,
) ,
{
if (!(self::$_fieldKeys)) {
self::$_fieldKeys = array(
- 'id' => 'widget_cache_id',
'widget_id' => 'widget_id',
'widget_code' => 'widget_code',
- 'ts' => 'ts',
+ 'expire' => 'expire',
);
}
return self::$_fieldKeys;
CRM_Core_DAO::executeQuery($sql, $params);
CRM_Core_DAO::executeQuery("SET foreign_key_checks = 1;");
$transaction->commit();
+ if (isset($this->_id)) {
+ $widget_id = $this->_id;
+ CRM_Wci_BAO_WidgetCache::deleteWidgetCache($widget_id);
+ }
+ else {
+ $widget_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
+ }
CRM_Core_Session::setStatus(ts('Widget created successfuly'), '', 'success');
+
if(isset($_REQUEST['_qf_CreateWidget_next'])) {
- (isset($this->_id)) ? $widget_id = $this->_id :
- $widget_id = CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()');
CRM_Utils_System::redirect('?action=update&reset=1&id=' . $widget_id);
} else {
CRM_Utils_System::redirect('widget?reset=1');
*/
class CRM_Wci_Form_WCISettings extends CRM_Core_Form {
function buildQuickForm() {
-
- // add form elements
- /*$this->add(
- 'select', // field type
- 'default_profile', // field name
- 'Default profile', // field label
- $this->getProfiles(), // list of options
- true // is required
- );*/
-
- /*$this->add(
- 'select', // field type
- 'default_widget', // field name
- 'Default widget', // field label
- $this->getWidgets(), // list of options
- false // is required
- );*/
-
$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(
'isDefault' => TRUE,
),
));
-
+$cacheTime = 1;
$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'));
-
+ $defProf = civicrm_api3('setting', 'getValue', array('group' => 'Wci Preference', 'name' => 'default_wci_profile'));
/*$this->setDefaults(array(
'default_widget' => $widgetId));*/
$this->setDefaults(array(
function postProcess() {
$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'],));
+ civicrm_api3('setting', 'create', array('sequential' => 1, 'default_wci_profile' => $values['default_profile']));
+ civicrm_api3('setting', 'create', array('sequential' => 1, 'widget_cache_timeout' => $values['widget_cache_timeout']));
CRM_Core_Session::setStatus(ts('Widget settings are saved to database'), '', 'success');
parent::postProcess();
}
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`)
+ CREATE TABLE IF NOT EXISTS `civicrm_wci_widget_cache` (
+ `widget_id` int(10) unsigned NOT NULL COMMENT "widget id.",
+ `widget_code` text DEFAULT NULL COMMENT "Widget code.",
+ `expire` int(10) DEFAULT 0 COMMENT "A Unix timestamp indicating when the cache entry should expire.",
+ PRIMARY KEY (`widget_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
');
return TRUE;
}
- /**
- * Example: Run an external SQL script
- *
- * @return TRUE on success
- * @throws Exception
- public function upgrade_4201() {
- $this->ctx->log->info('Applying update 4201');
- // this path is relative to the extension base dir
- $this->executeSqlFile('sql/upgrade_4201.sql');
- return TRUE;
- } // */
-
-
- /**
- * Example: Run a slow upgrade process by breaking it up into smaller chunk
- *
- * @return TRUE on success
- * @throws Exception
- public function upgrade_4202() {
- $this->ctx->log->info('Planning update 4202'); // PEAR Log interface
-
- $this->addTask(ts('Process first step'), 'processPart1', $arg1, $arg2);
- $this->addTask(ts('Process second step'), 'processPart2', $arg3, $arg4);
- $this->addTask(ts('Process second step'), 'processPart3', $arg5);
- return TRUE;
- }
- public function processPart1($arg1, $arg2) { sleep(10); return TRUE; }
- public function processPart2($arg3, $arg4) { sleep(10); return TRUE; }
- public function processPart3($arg5) { sleep(10); return TRUE; }
- // */
-
-
- /**
- * Example: Run an upgrade with a query that touches many (potentially
- * millions) of records by breaking it up into smaller chunks.
- *
- * @return TRUE on success
- * @throws Exception
- public function upgrade_4203() {
- $this->ctx->log->info('Planning update 4203'); // PEAR Log interface
-
- $minId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(min(id),0) FROM civicrm_contribution');
- $maxId = CRM_Core_DAO::singleValueQuery('SELECT coalesce(max(id),0) FROM civicrm_contribution');
- for ($startId = $minId; $startId <= $maxId; $startId += self::BATCH_SIZE) {
- $endId = $startId + self::BATCH_SIZE - 1;
- $title = ts('Upgrade Batch (%1 => %2)', array(
- 1 => $startId,
- 2 => $endId,
- ));
- $sql = '
- UPDATE civicrm_contribution SET foobar = whiz(wonky()+wanker)
- WHERE id BETWEEN %1 and %2
- ';
- $params = array(
- 1 => array($startId, 'Integer'),
- 2 => array($endId, 'Integer'),
- );
- $this->addTask($title, 'executeSql', $sql, $params);
- }
- return TRUE;
- } // */
-
}
class CRM_Wci_WidgetCode {
- static function get_widget_realtime_code($widgetId, $preview) {
+ static function generate_widget_code($widgetId, $preview = 0) {
$data = CRM_Wci_BAO_Widget::getWidgetData($widgetId);
$template = CRM_Core_Smarty::singleton();
$template->assign('wciform', $data);
$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, $preview);
- } else {
+ static function get_widget_code($embedId, $preview = 0) {
+ $code = '';
+ if ($preview) {
+ return CRM_Wci_WidgetCode::generate_widget_code($embedId, $preview);
+ }
+ 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, $preview);
- }
}
+
+ if (!$code) {
+ $code = CRM_Wci_WidgetCode::generate_widget_code($widgetId);
+ CRM_Wci_BAO_WidgetCache::setWidgetCache($widgetId, $code);
+ }
+
return $code;
}
}
* $Id$
*/
-
-/*
-<script type="text/javascript" src="http://drupal.local/sites/all/modules/civicrm/extensions/civicrm-wci/extern/wciwidget.php?widgetId=1"></script>
-*/
-
require_once '../../../civicrm.config.php';
-$wciembed_js = '
-/*
+$license_text = '
+ /*
@licstart
Copyright (C) 2014 Zyxware Technologies
@licend
*/
-
+ ';
+$wciembed_js = '
// Cleanup functions for the document ready method
if ( document.addEventListener ) {
DOMContentLoaded = function() {
$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) . ';';
-$output = $output . $wciembed_js;
+$output = $license_text;
+$output .= 'var wciwidgetcode = ' . CRM_Wci_WidgetCode::get_widget_code($embedId, $preview) . ';';
+$output .= $wciembed_js;
+
echo $output;
CRM_Utils_System::civiExit();
<version>1.0-beta3</version>
<develStage>beta</develStage>
<compatibility>
- <ver>4.2</ver>
+ <ver>4.4</ver>
</compatibility>
- <comments></comments>
+ <comments>Contributed by Zyxware Technologies - www.zyxware.com</comments>
<civix>
<namespace>CRM/Wci</namespace>
</civix>
<?php
return array(
- 'default_wci_widget' => array(
- 'group_name' => 'Wci Preference',
- 'group' => 'wci',
- 'name' => 'default_wci_widget',
- 'type' => 'Integer',
- 'default' => 0,
- 'add' => '4.3',
- 'is_domain' => 1,
- 'is_contact' => 0,
- 'description' => 'Default widget id',
- 'help_text' => 'Sets default widget id',
- ),
'default_wci_profile' => array(
'group_name' => 'Wci Preference',
'group' => 'wci',
'name' => 'default_wci_profile',
'type' => 'Integer',
'default' => 0,
- 'add' => '4.3',
- 'is_domain' => 1,
- 'is_contact' => 0,
'description' => 'Default profile id',
'help_text' => 'Sets default profile id',
),
'name' => 'widget_cache_timeout',
'type' => 'Integer',
'default' => 0,
- 'add' => '4.3',
- 'is_domain' => 1,
- 'is_contact' => 0,
- 'description' => 'widget timeout',
+ 'description' => 'Widget timeout',
'help_text' => 'widget timeout',
),
);
-- 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_id int(10) id int(10) unsigned NOT NULL COMMENT 'widget id.',
widget_code text DEFAULT NULL COMMENT 'Widget code.',
- ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- PRIMARY KEY (`id`)
+ expire int(10) DEFAULT 0 COMMENT 'A Unix timestamp indicating when the cache entry should expire.';,
+ PRIMARY KEY (`widget_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
{if $form.title.value != ""}
{php}
if(isset($_REQUEST['id'])) {
- /*$wid_id = $_REQUEST['id'];
- $data = CRM_Wci_BAO_Widget::getWidgetData($wid_id);
- $template = CRM_Core_Smarty::singleton();
- $template->assign('wciform', $data);
- if($data["override"] == 0) {
- $template->template_dir[] = getWciWidgetTemplatePath();
- $wcidata = $template->fetch('wciwidget.tpl');
- } else {
- $wcidata = $template->fetch('string:' . $wid_page[$dao->id]['custom_template']);
- }*/
$widget_controller_path = getWciWidgetControllerPath();
$extension_root_path = getExtensionRootPath();
}
<div class="description">
Click <strong>Save & Preview</strong> to save any changes to your settings, and preview the widget again on this page.
</div>
-<!-- <script type="text/javascript" src="{php}echo $widget_controller_path;{/php}?widgetId={php}echo $wid_id;{/php}&preview=1&referalid=2442"></script></script> -->
- <script type="text/javascript" src="{php}echo $widget_controller_path;{/php}?id={php}echo $_REQUEST['id'];{/php}&preview=1&referalid=2442"></script></script>
+ <script type="text/javascript" src="{php}echo $widget_controller_path;{/php}?id={php}echo $_REQUEST['id'];{/php}&preview=1"></script>
<div id='widgetwci'></div>
</div>
-<!-- <div class="col2">
- <div class="description">
- Add this widget to any web page by copying and pasting the code below.
- </div>
- <textarea rows="8" cols="40" name="widget_code" id="widget_code"><script type="text/javascript" src="{php}echo $widget_controller_path;{/php}?widgetId={php}echo $wid_id;{/php}&embed=1&referalId=2442"> </script>
-<div id='widgetwci'></div></textarea>
- <br>
- <strong>
- <a href="#" onclick="CreateWidget.widget_code.select(); return false;">ยป Select Code</a>
- </strong>
- </div> -->
</div>
<div class="clear"></div>
</div>
if(isset($_REQUEST['id'])) {
$emb_id = $_REQUEST['id'];
$wid_id = CRM_Wci_BAO_EmbedCode::getWidgetId($emb_id);
- $data = CRM_Wci_BAO_Widget::getWidgetData($wid_id);
- $template = CRM_Core_Smarty::singleton();
- $template->assign('wciform', $data);
- if($data["override"] == 0) {
- $template->template_dir[] = getWciWidgetTemplatePath();
- $wcidata = $template->fetch('wciwidget.tpl');
- } else {
- $wcidata = $template->fetch('string:' . $wid_page[$dao->id]['custom_template']);
- }
$widget_controller_path = getWciWidgetControllerPath();
$extension_root_path = getExtensionRootPath();
}
<div class="description">
Click <strong>Save & Preview</strong> to save any changes to your settings, and preview the widget again on this page.
</div>
- <script type="text/javascript" src="{php}echo $widget_controller_path;{/php}?id={php}echo $emb_id;{/php}&embed=0&referal_id=2442"></script></script>
+ <script type="text/javascript" src="{php}echo $widget_controller_path;{/php}?id={php}echo $wid_id;{/php}&preview=1"></script></script>
<div id='widgetwci'></div>
</div>
<div class="col2">