Skip to content

Commit

Permalink
Merge pull request #62 from Tealium/PHP-8.1-Support
Browse files Browse the repository at this point in the history
PHP 8.1 Support
  • Loading branch information
milsom committed Oct 13, 2023
2 parents 49c7d4f + 160a404 commit 30c2487
Show file tree
Hide file tree
Showing 26 changed files with 1,849 additions and 127 deletions.
Binary file added .DS_Store
Binary file not shown.
96 changes: 75 additions & 21 deletions Block/Tealium.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Tealium extends \Magento\Framework\View\Element\Template
// Declare related properties and define constructor
private $account; // account name
private $profile; // profile name
private $fpurl;
private $target;
private $udo; // object (assoc array) of udo variables (key/val pairs)
private $udoElements;
Expand All @@ -21,20 +22,25 @@ class Tealium extends \Magento\Framework\View\Element\Template
protected $_helper;
protected $_request;

public $_storeManager;

public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Tealium\Tags\Helper\TealiumData $helper,
\Magento\Framework\App\Request\Http $request,
\Magento\Store\Model\StoreManagerInterface $storeManager,
array $data = []
) {
$this->_helper = $helper;
$this->_request = $request;
$this->_storeManager=$storeManager;
parent::__construct($context, $data);
}

public function init(
$accountInit = false,
$profileInit = false,
$fp_url = false,
$targetInit = false,
$pageType = "Home",
&$data = []
Expand Down Expand Up @@ -80,6 +86,7 @@ public function init(
$this->udoElements = $udoElements;
$this->account = $accountInit;
$this->profile = $profileInit;
$this->fpurl = $fp_url;
$this->target = $targetInit;

if (!($this->udo = $this->udoElements[$pageType])
Expand Down Expand Up @@ -166,9 +173,18 @@ public function render($type = null, $external = false, $sync = "sync")
// instead of setting utag_data with a udo object, include
// the external script instead
$type = "udo";

$base_urls = $this->_storeManager->getStore()->getBaseUrl();

$is_async = ($sync == "sync") ? "" : "async";
$udo = "<script type=\"text/javascript\" src=\"";
$udo .= $_SERVER["REQUEST_URI"];

if ($_SERVER["REQUEST_URI"] != "/") {
$udo = $udo . $base_urls . $_SERVER["REQUEST_URI"];
} else {
$udo .= $base_urls;
}


if ($_SERVER["QUERY_STRING"]) {
// append more query params with '&' if query params exist
Expand All @@ -180,6 +196,9 @@ public function render($type = null, $external = false, $sync = "sync")

// append the "tealium_api=true" query param to the url
$udo .= "tealium_api=true\" $is_async></script>";

//var_dump($udo);
//exit;
} else {
// Either using the api, the udo is not an external script, or both.
// Therefore the udo object must be generated as javascript code.
Expand Down Expand Up @@ -209,33 +228,68 @@ public function render($type = null, $external = false, $sync = "sync")
} else {
$udoJson = "{}";
}
/* echo "test<pre>";
/* echo "test<pre>";
print_r($udoObject);
die; */
// create the javascript for utag_data
$udoJs = "var utag_data = $udoJson;";

// create the entire script tag to render for utag_data
$udo = <<<EOD
<!-- Tealium Universal Data Object / Data Layer -->
<div class="utagLib" style="display:none;">//tags.tiqcdn.com/utag/$this->account/$this->profile/$this->target/utag.js</div>
<script type="text/javascript">
$udoJs
console.log(window);
</script>
<!-- ****************************************** -->
EOD;

$this->fpurl = trim($this->fpurl);
if (!empty($this->fpurl) && $this->fpurl != "") {
// create the entire script tag to render for utag_data
$udo = <<<EOD
<!-- Tealium Universal Data Object / Data Layer -->
<script type="text/javascript">
var utag_jsurl = "https://$this->fpurl/$this->profile/$this->target/utag.js";
$udoJs
</script>
<!-- ****************************************** -->
EOD;
} else {
// create the entire script tag to render for utag_data
$udo = <<<EOD
<!-- Tealium Universal Data Object / Data Layer -->
<script type="text/javascript">
var utag_jsurl = "//tags.tiqcdn.com/utag/$this->account/$this->profile/$this->target/utag.js";
$udoJs
</script>
<!-- ****************************************** -->
EOD;
}


}

// Render Tealium tag in javaScript
$insert_tag = <<<EOD
(function(a,b,c,d){
a='//tags.tiqcdn.com/utag/$this->account/$this->profile/$this->target/utag.js';
b=document;c='script';d=b.createElement(c);d.src=a;d.type='text/java'+c;
d.async=true;
a=b.getElementsByTagName(c)[0];a.parentNode.insertBefore(d,a);
})();
EOD;

if (!empty($this->fpurl) && $this->fpurl != "") {
// Render Tealium tag in javaScript
$insert_tag = <<<EOD
(function(a,b,c,d){
a='https://$this->fpurl/$this->profile/$this->target/utag.js';
b=document;c='script';d=b.createElement(c);d.src=a;d.type='text/java'+c;
d.async=true;
a=b.getElementsByTagName(c)[0];a.parentNode.insertBefore(d,a);
})();
EOD;
} else {
// Render Tealium tag in javaScript
$insert_tag = <<<EOD
(function(a,b,c,d){
a='//tags.tiqcdn.com/utag/$this->account/$this->profile/$this->target/utag.js';
b=document;c='script';d=b.createElement(c);d.src=a;d.type='text/java'+c;
d.async=true;
a=b.getElementsByTagName(c)[0];a.parentNode.insertBefore(d,a);
})();
EOD;
}



// enclose the tealium tag js in a <script></script> tag
$tag = <<<EOD
Expand Down
13 changes: 10 additions & 3 deletions Controller/Cart/Quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ public function execute()
$ItemsQty = $this->_cart->getQuote()->getItemsQty();
$GrandTotal =$this->_cart->getQuote()->getGrandTotal();


$result['cart_total_items'] = number_format($ItemsQty, 2, ".", "") ? : '';
$result['cart_total_value'] = number_format($GrandTotal, 2, ".", "") ? : '';
$result['cart_total_items'] = "";
$result['cart_total_value'] = "";
if ($ItemsQty !== null) {
$result['cart_total_items'] = number_format($ItemsQty, 2, ".", "");
}

if ($GrandTotal !== null) {
$result['cart_total_value'] = number_format($GrandTotal, 2, ".", "");
}

$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$resultJson->setData($result);
return $resultJson;
Expand Down
1 change: 1 addition & 0 deletions Controller/Cart/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function execute()
// echo json_encode(['id' => [$id], 'type' => [$groupName], 'email' => [$email] ]);

// exit;
//$email = hash('sha256', strtolower($email));
$result = ['customer_id' => $id, 'customer_type' => $groupName, 'customer_email' => $email ];
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
$resultJson->setData($result);
Expand Down
5 changes: 2 additions & 3 deletions CustomerData/JsAddToCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(

public function getSectionData()
{
$productImage = false;
$productImage = array();
$product_id=$this->_customerSession->getTealiumAddProductId();
$this->_customerSession->unsTealiumAddProductId();

Expand All @@ -52,9 +52,8 @@ public function getSectionData()
$CartSku[] = $item->getSku();
$CartQty[] = $item->getQty();
$CartPrice[] = number_format($item->getPrice(), 2, '.', '');

$productCat = $objectManager->create('Magento\Catalog\Model\Product')->load($item->getProductId());
$productImage[] = $mediaUrl.$productCat->getImage();
$productImage[] = $mediaUrl.$productCat->getImage();
}


Expand Down
16 changes: 15 additions & 1 deletion CustomerData/JsCreateAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Magento\Customer\CustomerData\SectionSourceInterface;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\Session\SessionManagerInterface as CoreSession;
use Tealium\Tags\Helper\Data as TealiumDataHelper;

class JsCreateAccount implements SectionSourceInterface
{
Expand All @@ -13,12 +14,19 @@ class JsCreateAccount implements SectionSourceInterface

protected $_coreSession;

/**
* @var TealiumDataHelper
*/
protected $tealiumDataHelper;

public function __construct(
CustomerSession $customerSession,
CoreSession $coreSession
CoreSession $coreSession,
TealiumDataHelper $tealiumDataHelper
) {
$this->_customerSession = $customerSession;
$this->_coreSession = $coreSession;
$this->tealiumDataHelper = $tealiumDataHelper;
}

public function getSectionData()
Expand All @@ -35,6 +43,12 @@ public function getSectionData()
$result = [];

if ($id) {

$store = $this->tealiumDataHelper->getStore();
$customertxtvalue = $this->tealiumDataHelper->getCustomerTxtEmail($store);
if ($customertxtvalue == null || $customertxtvalue == 0) {
$email = hash('sha256', strtolower($email));
}
$result['data']['customer_email'] = (string)$email;
$result['data']['customer_id'] = (string)$id;
$result['data']['customer_type'] = (string)$type;
Expand Down
21 changes: 20 additions & 1 deletion CustomerData/JsLoginAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@

use Magento\Customer\CustomerData\SectionSourceInterface;
use Magento\Framework\Session\SessionManagerInterface as CoreSession;
use Tealium\Tags\Helper\Data as TealiumDataHelper;

class JsLoginAccount implements SectionSourceInterface
{

protected $_coreSession;
/**
* @var TealiumDataHelper
*/
protected $tealiumDataHelper;

public function __construct(
CoreSession $coreSession
CoreSession $coreSession,
TealiumDataHelper $tealiumDataHelper
) {
$this->_coreSession = $coreSession;
$this->tealiumDataHelper = $tealiumDataHelper;
}

public function getSectionData()
Expand All @@ -30,6 +37,18 @@ public function getSectionData()
$result = [];

if ($id) {

$store = $this->tealiumDataHelper->getStore();
$customertxtvalue = $this->tealiumDataHelper->getCustomerTxtEmail($store);
if ($customertxtvalue == null || $customertxtvalue == 0) {
$email = hash('sha256', strtolower($email));
}

$result['data']['customer_email_txt'] = $customertxtvalue;




$result['data']['customer_email'] = $email;
$result['data']['customer_id'] = $id;
$result['data']['customer_type'] = $type;
Expand Down
16 changes: 15 additions & 1 deletion CustomerData/JsLogoutAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@

use Magento\Customer\CustomerData\SectionSourceInterface;
use Magento\Framework\Session\SessionManagerInterface as CoreSession;
use Tealium\Tags\Helper\Data as TealiumDataHelper;

class JsLogoutAccount implements SectionSourceInterface
{

protected $_coreSession;

/**
* @var TealiumDataHelper
*/
protected $tealiumDataHelper;

public function __construct(
CoreSession $coreSession
CoreSession $coreSession,
TealiumDataHelper $tealiumDataHelper
) {
$this->_coreSession = $coreSession;
$this->tealiumDataHelper = $tealiumDataHelper;
}

public function getSectionData()
Expand Down Expand Up @@ -46,6 +54,12 @@ public function getSectionData()
$result = [];

if (isset($id)) {

$store = $this->tealiumDataHelper->getStore();
$customertxtvalue = $this->tealiumDataHelper->getCustomerTxtEmail($store);
if ($customertxtvalue == null || $customertxtvalue == 0) {
$email = hash('sha256', strtolower($email));
}
$result['data']['customer_email'] = $email;
$result['data']['customer_id'] = $id;
$result['data']['customer_type'] = $type;
Expand Down

0 comments on commit 30c2487

Please sign in to comment.