Skip to content
Browse files

Updating to include array -> string conversion automatically.

  • Loading branch information...
1 parent bc25b9e commit c10cec42597c20eec31104febbb478343a34a44f @MisterPhilip MisterPhilip committed Oct 8, 2012
Showing with 68 additions and 55 deletions.
  1. +68 −55 TPSiteCatalyst/components/TPSiteCatalyst.php
View
123 TPSiteCatalyst/components/TPSiteCatalyst.php
@@ -8,29 +8,21 @@
* @link https://github.com/TagPlanet/yii-analytics
* @copyright Copyright © 2012 Philip Lawrence
* @license http://www.apache.org/licenses/LICENSE-2.0
- * @version 1.0.0
+ * @version 1.0.1
*/
class TPSiteCatalyst extends CApplicationComponent
{
- protected $settings = array(
- 'namespace' => 'string',
- 'createObject' => 'bool',
- 'rsids' => 'array',
- 's_codeLocation' => 'string',
- 'autoRender' => 'bool',
- 'autoPageview' => 'bool',
- 'renderMoble' => 'bool',
- 'renderHead' => 'bool',
- );
+ protected $settings = array('namespace','createObject','s_account','s_codeLocation','autoRender','autoPageview','renderMoble','renderHead');
protected $namespace = 's';
protected $createObject = true;
- protected $rsids = array();
+ protected $s_account = array();
protected $s_codeLocation = '';
protected $autoRender = false;
protected $autoPageview = true;
protected $renderMobile = false;
protected $renderHead = false;
+
/**
* Type of quotes to use for values
@@ -72,12 +64,12 @@ public function render()
// Do we need to create the object?
if($this->createObject)
- $js.= 'var ' . $n . ' = ' . $n . '_account(' . self::Q . implode($this->rsids, ',') . self::Q . ');' . PHP_EOL;
+ $js.= 'var ' . $n . ' = ' . $n . '_account(' . self::Q . $this->_formatVariable('s_account', $this->s_account) . self::Q . ');' . PHP_EOL;
// Go through the data
foreach($this->_data as $var => $value)
{
- $js.= $n . '.' . $var . ' = ' . self::Q . preg_replace('~(?<!\\\)'. self::Q . '~', '\\'. self::Q, $value) . self::Q . ';' . PHP_EOL;
+ $js.= $n . '.' . $var . ' = ' . self::Q . preg_replace('~(?<!\\\)'. self::Q . '~', '\\'. self::Q, $this->_formatVariable($var, $value)) . self::Q . ';' . PHP_EOL;
}
// Should we add s.t()?
@@ -113,19 +105,14 @@ public function render()
*/
public function setting($name, $value = null)
{
- // Get value
- if($value === null)
+ if(in_array($name, $this->settings))
{
- if(isset($this->settings[$name]))
+ // Get value
+ if($value === null)
{
return $this->$name;
}
- return null;
- }
-
- // Set value
- if(isset($this->settings[$name]) && gettype($value) == $this->settings[$name])
- {
+
$this->$name = $value;
return true;
}
@@ -140,7 +127,7 @@ public function setting($name, $value = null)
*/
public function __set($name, $value)
{
- if(isset($this->settings[$name]))
+ if(in_array($name, $this->settings))
return $this->setting($name, $value);
if($this->_validVariableName($name))
@@ -160,7 +147,7 @@ public function __set($name, $value)
protected function _validVariableName($name)
{
// @TODO: Update this list with more
- $named = array('pageName','channel','server','campaigns','products','TnT','events','pageType','purchaseID');
+ $named = array('pageName','channel','server','campaign','products','TnT','events','pageType','purchaseID', 'transactionID','state','zip','currencyCode','pageType');
$count = array('hier', 'eVar', 'prop');
// Check for named
@@ -175,41 +162,67 @@ protected function _validVariableName($name)
// No matches :(
return false;
}
-
+
/**
- * Magic Method for options
+ * Format variable (non-mobile)
+ * Formats the variable for output
+ *
* @param string $name
- * @param array $arguments
- */
- public function __call($name, $arguments)
- {
- if($name[0] != '_')
- $name = '_' . $name;
- if(in_array($name, $this->_availableOptions))
- {
- $this->_push($name, $arguments);
- return true;
- }
- return false;
- }
-
- /**
- * Push data into the array
- * @param string $variable
- * @param array $arguments
- * @protected
+ * @returns bool
*/
- protected function _push($variable, $arguments)
+ protected function _formatVariable($variable, $data)
{
- $data = array_merge(array($variable), $arguments);
- if($variable == '_cookiePathCopy' || $variable == '_trackTrans')
+ switch($variable)
{
- array_push($this->_delayedData, $data);
- }
- else
- {
- array_push($this->_data, $data);
+ // s.products variable is probably the trickiest
+ case 'products':
+ // Do we have a string already, or an array?
+ if(is_array($data))
+ {
+ $allowed = array('category', 'sku', 'quantity', 'price', 'events', 'evars');
+ $products = array();
+
+ // Loop through the incoming data
+ foreach($data as $key => $product)
+ {
+ // Which data source to use?
+ $dn = 'product';
+ if(!is_array($data[$key]))
+ $dn = 'data';
+
+ $d =& $$dn;
+
+ // Build the string
+ $p = array();
+ foreach($allowed as $v)
+ $p[] = (isset($d[$v]) ? $d[$v] : '');
+ $products[] = implode(';', $p);
+ }
+ return implode(',', $products);
+ }
+
+ // A regular string was passed in
+ return $data;
+ break;
+
+ // Events could be an array
+ case 'events':
+ if(is_array($data))
+ $data = implode(',', $data);
+ return $data;
+ break;
+
+ // RSID list could be an array
+ case 's_account':
+ if(is_array($data))
+ $data = implode(',', $data);
+ return $data;
+ break;
+
+ // No formatting required
+ default:
+ return $data;
+ break;
}
- $this->_calledOptions[] = $variable;
}
}

0 comments on commit c10cec4

Please sign in to comment.
Something went wrong with that request. Please try again.