Skip to content

Commit c4148e6

Browse files
committed
minor refactorings, fixed admin page
1 parent 9fc71b4 commit c4148e6

File tree

6 files changed

+58
-79
lines changed

6 files changed

+58
-79
lines changed

src/app/code/community/Matomo/Analytics/Block/Script.php

Lines changed: 35 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
1111
*/
1212

13+
/**
14+
* @method array<string> getOrderIds()
15+
*/
1316
final class Matomo_Analytics_Block_Script extends Mage_Core_Block_Template
1417
{
1518
/**
@@ -40,22 +43,6 @@ public function getOrdersTrackingCode(): string
4043
foreach ($collection as $order) {
4144
/** @var Mage_Sales_Model_Order_Item $item */
4245
foreach ($order->getAllVisibleItems() as $item) {
43-
44-
//get category name
45-
$productId = $item->product_id;
46-
47-
/** @var Mage_Catalog_Model_Product $product */
48-
$product = Mage::getModel('catalog/product')->load($productId);
49-
$categoryName = '';
50-
$categoryIds = $product->getCategoryIds();
51-
52-
if (!empty($categoryIds)) {
53-
$categoryId = $categoryIds[0];
54-
/** @var Mage_Catalog_Model_Category $category */
55-
$category = Mage::getModel('catalog/category')->load($categoryId);
56-
$categoryName = $category->getName();
57-
}
58-
5946
if ($item->getQtyOrdered()) {
6047
$qty = number_format((float)$item->getQtyOrdered(), 0, '.', '');
6148
} else {
@@ -64,11 +51,10 @@ public function getOrdersTrackingCode(): string
6451
$result[] = sprintf('_paq.push([\'addEcommerceItem\', \'%s\', \'%s\', \'%s\', %s, %s]);',
6552
$this->jsQuoteEscape($item->getSku()),
6653
$this->jsQuoteEscape($item->getName()),
67-
$categoryName,
54+
$this->getFirstCategoryName($item->getProductId()),
6855
$item->getBasePrice(),
6956
$qty
7057
);
71-
7258
}
7359

7460
if ($order->getGrandTotal()) {
@@ -97,37 +83,22 @@ public function getEcommerceCartUpdate(): string
9783

9884
/** @var Mage_Checkout_Model_Cart $cart */
9985
$cart = Mage::getModel('checkout/cart');
100-
/** @var Mage_Sales_Model_Quote $quote */
10186
$quote = $cart->getQuote();
10287

10388
/** @var Mage_Sales_Model_Quote_Item $cartItem */
10489
foreach ($quote->getAllVisibleItems() as $cartItem) {
105-
106-
//get category name
107-
$productId = $cartItem->product_id;
108-
109-
/** @var Mage_Catalog_Model_Product $product */
110-
$product = Mage::getModel('catalog/product')->load($productId);
111-
$categoryName = '';
112-
$categoryIds = $product->getCategoryIds();
113-
if (!empty($categoryIds)) {
114-
$categoryId = $categoryIds[0];
115-
/** @var Mage_Catalog_Model_Category $category */
116-
$category = Mage::getModel('catalog/category')->load($categoryId);
117-
$categoryName = $category->getName();
118-
}
11990
$productName = $cartItem->getName();
12091
$productName = str_replace('"', "", $productName);
12192

122-
if ($cartItem->getPrice() == 0 || $cartItem->getPrice() < 0.00001):
93+
if ($cartItem->getPrice() == 0 || $cartItem->getPrice() < 0.00001) {
12394
continue;
124-
endif;
95+
}
12596

12697
$result[] = sprintf(
12798
"_paq.push(['addEcommerceItem', '%s', '%s', '%s', %s, %s]);",
12899
$this->jsQuoteEscape($cartItem->getSku()),
129100
$this->jsQuoteEscape($productName),
130-
$categoryName,
101+
$this->getFirstCategoryName($cartItem->getProductId()),
131102
$cartItem->getPrice(),
132103
$cartItem->getQty()
133104
);
@@ -152,21 +123,11 @@ public function getProductPageview(): string
152123
$product = Mage::registry('current_product');
153124

154125
if ($product instanceof Mage_Catalog_Model_Product) {
155-
$categoryName = '';
156-
$categoryIds = $product->getCategoryIds();
157-
if (!empty($categoryIds)) {
158-
$categoryId = $categoryIds[0];
159-
/** @var Mage_Catalog_Model_Category $category */
160-
$category = Mage::getModel('catalog/category')->load($categoryId);
161-
$categoryName = $category->getName();
162-
}
163-
$productName = $product->getName();
164-
165126
return sprintf(
166127
"_paq.push(['setEcommerceView', '%s', '%s', '%s', %s]);",
167128
$this->jsQuoteEscape($product->getSku()),
168-
$this->jsQuoteEscape($productName),
169-
$categoryName,
129+
$this->jsQuoteEscape($product->getName()),
130+
$this->getFirstCategoryName((int)$product->getId()),
170131
$product->getFinalPrice()
171132
);
172133
}
@@ -204,17 +165,17 @@ public function getInstallPath(): string
204165

205166
public function getSearchResultCount(): int
206167
{
207-
$count = 0;
208-
209-
if ($this->getRequest()->getControllerName() === 'result') {
210-
$queryText = $this->helper('catalogsearch')->getQuery()->getQueryText();
211-
$count = (int)$this->helper('catalogsearch')->getEngine()
212-
->getResultCollection()
213-
->addSearchFilter($queryText)
214-
->getSize();
168+
if ($this->getRequest()->getControllerName() !== 'result') {
169+
return 0;
215170
}
216171

217-
return $count;
172+
$queryText = $this->helper('catalogsearch')->getQuery()->getQueryText();
173+
/** @var \Mage_CatalogSearch_Model_Resource_Fulltext_Engine $engine */
174+
$engine = $this->helper('catalogsearch')->getEngine();
175+
176+
return $engine->getResultCollection()
177+
->addSearchFilter($queryText)
178+
->getSize();
218179
}
219180

220181
public function is404(): bool
@@ -230,4 +191,21 @@ public function _toHtml(): string
230191

231192
return parent::_toHtml();
232193
}
194+
195+
private function getFirstCategoryName(int $productId): string
196+
{
197+
/** @var Mage_Catalog_Model_Product $product */
198+
$product = Mage::getModel('catalog/product')->load($productId);
199+
200+
$categoryIds = $product->getCategoryIds();
201+
if (empty($categoryIds)) {
202+
return '';
203+
}
204+
205+
$categoryId = $categoryIds[0];
206+
/** @var Mage_Catalog_Model_Category $category */
207+
$category = Mage::getModel('catalog/category')->load($categoryId);
208+
209+
return $category->getName();
210+
}
233211
}

src/app/code/community/Matomo/Analytics/Model/Observer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class Matomo_Analytics_Model_Observer
1717
/**
1818
* Add order information into Piwik block to render on checkout success pages
1919
*/
20-
public function setMatomoAnalyticsOnOrderSuccessPageView(Varien_Event_Observer $observer)
20+
public function setMatomoAnalyticsOnOrderSuccessPageView(Varien_Event_Observer $observer): void
2121
{
2222
if (! Mage::helper('matomoanalytics')->isEnabled()) {
2323
return;

src/app/code/community/Matomo/Analytics/controllers/Adminhtml/IndexController.php renamed to src/app/code/community/Matomo/Analytics/controllers/Adminhtml/Matomo/IndexController.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@
1010
*
1111
*/
1212

13-
class Matomo_Analytics_Adminhtml_IndexController
14-
extends Mage_Adminhtml_Controller_Action
13+
final class Matomo_Analytics_Adminhtml_Matomo_IndexController extends Mage_Adminhtml_Controller_Action
1514
{
16-
public function indexAction()
15+
public function indexAction(): void
1716
{
1817
$this->loadLayout();
1918

2019
$siteId = Mage::getStoreConfig(Matomo_Analytics_Helper_Data::XML_PATH_SITE);
2120
$installPath = Mage::getStoreConfig(Matomo_Analytics_Helper_Data::XML_PATH_INSTALL);
2221
$token = Mage::getStoreConfig(Matomo_Analytics_Helper_Data::XML_PATH_TOKEN);
2322

24-
if ($token && $siteId) {
23+
$block = $this->getLayout()->createBlock('core/text', 'matomo-block');
24+
if ($block === false) {
25+
return;
26+
}
2527

28+
if ($token && $siteId) {
2629
$params = '?module=Widgetize' .
2730
'&action=iframe' .
2831
'&moduleToWidgetize=Dashboard' .
@@ -32,19 +35,15 @@ public function indexAction()
3235
'&date=yesterday' .
3336
'&token_auth=' . $token;
3437

35-
$block = $this->getLayout()->createBlock('core/text', 'matomo-block')->setText(
36-
'<iframe src="' . $installPath . '/index.php' . $params . '"
37-
frameborder="0"
38-
marginheight="0"
39-
marginwidth="0"
40-
width="100%"
41-
height="1000px"></iframe>'
38+
$block->setText('<iframe src="' . $installPath . '/index.php' . $params . '"
39+
frameborder="0"
40+
marginheight="0"
41+
marginwidth="0"
42+
width="100%"
43+
height="1000px"></iframe>'
4244
);
43-
4445
} else {
45-
$block = $this->getLayout()->createBlock('core/text', 'matomo-block')->setText(
46-
$this->__('You are missing your Matomo Token Auth Key.')
47-
);
46+
$block->setText($this->__('You are missing your Matomo Token Auth Key.'));
4847
}
4948

5049
$this->_addContent($block);

src/app/code/community/Matomo/Analytics/etc/adminhtml.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<matomo translate="title" module="reports">
1919
<title>Matomo</title>
2020
<sort_order>99999</sort_order>
21-
<action>matomoanalytics/index/index</action>
21+
<action>adminhtml/matomo_index/index</action>
2222
<depends>
2323
<config>matomo/analytics/active</config>
2424
</depends>

src/app/code/community/Matomo/Analytics/etc/config.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@
8787

8888
<admin>
8989
<routers>
90-
<Matomo_Analytics>
91-
<use>admin</use>
90+
<adminhtml>
9291
<args>
93-
<module>Matomo_Analytics_Adminhtml</module>
94-
<frontName>matomoanalytics</frontName>
92+
<modules>
93+
<Matomo_Analytics after="Mage_Adminhtml">Matomo_Analytics_Adminhtml</Matomo_Analytics>
94+
</modules>
9595
</args>
96-
</Matomo_Analytics>
96+
</adminhtml>
9797
</routers>
9898
</admin>
9999
</config>

src/app/design/frontend/base/default/template/matomoanalytics/script.phtml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
_paq.push(['setSiteId', <?= $this->getSiteId() ?>]);
2121
_paq.push(['setTrackerUrl', u + 'matomo.php']);
2222

23+
<?php if ($this->getRequest()->getRouteName() === 'checkout'): ?>
2324
<?= $this->getEcommerceCartUpdate() ?>
25+
<?php endif ?>
2426
<?= $this->getOrdersTrackingCode() ?>
2527
<?= $this->getProductPageview() ?>
2628
<?= $this->getCategoryPageview() ?>

0 commit comments

Comments
 (0)