Skip to content

Commit

Permalink
Dev: Fetch global variables from Cint at plugin activation
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Aug 9, 2016
1 parent be9515e commit aba1bd1
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 42 deletions.
111 changes: 69 additions & 42 deletions application/core/plugins/CintLink/CintLink.php
Expand Up @@ -5,6 +5,7 @@

require_once(__DIR__ . "/CintLinkAPI.php");
require_once(__DIR__ . "/model/CintLinkOrder.php");
require_once(__DIR__ . "/CintXml.php");

/**
* CintLink integration to be able to buy respondents
Expand Down Expand Up @@ -71,50 +72,76 @@ public function beforeActivate()
{
$oDB = Yii::app()->getDb();

if ($oDB->schema->getTable("{{plugin_cintlink_orders}}") === null)
$tableDoesNotExist = $oDB->schema->getTable("{{plugin_cintlink_orders}}") === null;
if ($tableDoesNotExist)
{
$oDB->schemaCachingDuration = 0; // Deactivate schema caching
$oTransaction = $oDB->beginTransaction();
try
{
$aFields = array(
'url' => 'string primary key',
'sid' => 'int', // Survey id
'raw' => 'text', // Order xml
'status' => 'string',
'ordered_by' => 'int', // User id
'deleted' => 'bool', // Soft delete
'created' => 'datetime',
'modified' => 'datetime',
);
$oDB->createCommand()->createTable('{{plugin_cintlink_orders}}', $aFields);
$oTransaction->commit();
}
catch(Exception $e)
{
$oTransaction->rollback();
// Activate schema caching
$oDB->schemaCachingDuration = 3600;
// Load all tables of the application in the schema
$oDB->schema->getTables();
// Clear the cache of all loaded tables
$oDB->schema->refresh();
$event = $this->getEvent();
$event->set('success', false);
$event->set(
'message',
$this->gT('An non-recoverable error happened during the update. Error details:')
. "<p>"
. htmlspecialchars($e->getMessage())
. "</p>"
);
return;
}
$this->createDatabase();
}

$this->fetchGlobalVariables();
}

/**
* Creates database table for Cint plugin
* @return void
*/
protected function createDatabase()
{
$oDB = Yii::app()->getDb();
$oDB->schemaCachingDuration = 0; // Deactivate schema caching
$oTransaction = $oDB->beginTransaction();
try
{
$aFields = array(
'url' => 'string primary key',
'sid' => 'int', // Survey id
'raw' => 'text', // Order xml
'status' => 'string',
'ordered_by' => 'int', // User id
'deleted' => 'bool', // Soft delete
'created' => 'datetime',
'modified' => 'datetime',
);
$oDB->createCommand()->createTable('{{plugin_cintlink_orders}}', $aFields);
$oTransaction->commit();
}
catch(Exception $e)
{
$oTransaction->rollback();
// Activate schema caching
$oDB->schemaCachingDuration = 3600;
// Load all tables of the application in the schema
$oDB->schema->getTables();
// Clear the cache of all loaded tables
$oDB->schema->refresh();
$event = $this->getEvent();
$event->set('success', false);
$event->set(
'message',
$this->gT('An non-recoverable error happened during the update. Error details:')
. "<p>"
. htmlspecialchars($e->getMessage())
. "</p>"
);
}
}

/**
* Fetch the global variables from Cint, like number of children,
* personal income etc.
* @return void
*/
protected function fetchGlobalVariables()
{
$cintXml = new CintXml($this->cintApiKey);
$gv = $cintXml->getGlobalVariables();

// Store raw XML in plugin settings
$this->set('cint-global-variables', $gv);
}

/**
* todo place somewhere else
* todo Place somewhere else
*/
public function beforeToolsMenuRender()
{
Expand Down Expand Up @@ -168,7 +195,7 @@ public function beforeAdminMenuRender()
}

/**
* Add quick menu icon
* Add quick menu icon.
*/
public function afterQuickMenuLoad()
{
Expand Down Expand Up @@ -447,7 +474,7 @@ public function actionIndexGlobal()
protected function registerCssAndJs() {
$assetsUrl = Yii::app()->assetManager->publish(dirname(__FILE__) . '/js');
App()->clientScript->registerScriptFile("$assetsUrl/cintlink.js");
App()->clientScript->registerScriptFile("http://" . $this->cintApiKey . ".cds.cintworks.net/assets/cint-link-1-0-0.js");
App()->clientScript->registerScriptFile("https://" . $this->cintApiKey . ".cds.cintworks.net/assets/cint-link-1-0-0.js");

// Need to include this manually so Ajax loading of gridview will work
App()->clientScript->registerScriptFile('/framework/zii/widgets/assets/gridview/jquery.yiigridview.js');
Expand Down Expand Up @@ -1236,7 +1263,7 @@ protected function hasTokenTable($surveyId)
{
try
{
$t = Token::model($surveyId);
Token::model($surveyId);
return true;
}
catch (Exception $ex)
Expand Down
74 changes: 74 additions & 0 deletions application/core/plugins/CintLink/CintXml.php
@@ -0,0 +1,74 @@
<?php

require_once(__DIR__ . "/CintLinkAPI.php");

/**
* Helper class to parse links in the
* Cint XML.
*
* @since 2016-08-09
* @author Olle Härstedt
*/
final class CintXml
{
/**
* Raw XML
* @var string
*/
private $raw;

/**
* @var SimpleXmlElement
*/
private $xml;

/**
* @param string $apiKey
*/
public function __construct($apiKey)
{
$url = 'https://' . $apiKey . '.cds.cintworks.net/';
$curl = new Curl();
$this->raw = $curl->get($url);
$this->xml = new SimpleXmlElement($this->raw);
}

/**
* Get global variables
* @return SimpleXmlElement|null
*/
public function getGlobalVariables()
{
$url = $this->getHrefFromRel('global-variables');
if ($url !== false)
{
$curl = new Curl();
$raw = $curl->get($url);
return $raw;
}
else
{
throw new Exception('Found no href from rel');
}

}

/**
* Get link from rel name
* @param string $relName Like 'global-variables' or 'quote'
* @return string|false
*/
private function getHrefFromRel($relName)
{
$rel = 'http://cds.cint.com/rel/' . $relName;
foreach ($this->xml->children() as $child)
{
if ($child['rel'] == $rel)
{
return $child['href'];
}
}

return false;
}
}

0 comments on commit aba1bd1

Please sign in to comment.