Skip to content

Commit

Permalink
Update Extension list
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Aug 19, 2020
1 parent 2ebb187 commit 231c17b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
16 changes: 16 additions & 0 deletions Block/Adminhtml/System/Config/Field/Extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,20 @@ public function getExtensionList()
{
return $this->module->getOutDatedExtension();
}

/**
* @return array
*/
public function getRelatedProduct()
{
$result = [];
$installedExtensions = $this->module->getMyExtensionList();
foreach ($this->module->getProductFeed() as $key => $item) {
if (isset($item['upsell']) && $item['upsell'] == 1 && !array_key_exists($key, $installedExtensions)) {
$result[$key] = $item;
}
}

return $result;
}
}
33 changes: 26 additions & 7 deletions Model/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Module
{
const URL = "https://updates.magepal.com/extensions.json";
const CACHE_KEY = 'magepal_extension_installed_list';
const DATA_VERSION = '1.0.2';
const SESSION_KEY = 'magepal-core-notification-data';
const DATA_VERSION = '1.0.3';
const LIFE_TIME = 604800;

/** @var int $updateCounter */
Expand Down Expand Up @@ -67,6 +68,10 @@ class Module
private $session;

private $timeStamp;
/**
* @var array|mixed
*/
private $latestVersions = [];

/**
* @param ModuleListInterface $moduleList
Expand Down Expand Up @@ -139,7 +144,7 @@ public function getCachedUpdateCount()
{
$now = $this->getTimeStamp();
$hash = $this->getHash();
$data = (array) $this->session->getData('magepal-core-notification-data');
$data = (array) $this->session->getData(self::SESSION_KEY);

if (empty($data) || !is_array($data)
|| $this->getArrayKeyIfExist('hash', $data, '') !== $hash
Expand All @@ -163,7 +168,7 @@ protected function setCountCache($amount)
'ttl' => $this->getTtl()
];

$this->session->setData('magepal-core-notification-data', $data);
$this->session->setData(self::SESSION_KEY, $data);
return $data;
}

Expand All @@ -175,6 +180,15 @@ public function getHash()
return md5(json_encode($this->getPostData()));
}

public function getProductFeed()
{
if (empty($this->latestVersions)) {
$this->getOutDatedExtension();
}

return (array) $this->latestVersions;
}

/**
* @return array
*/
Expand Down Expand Up @@ -202,6 +216,10 @@ public function getOutDatedExtension()
if (array_key_exists('extensions', $dataObject)) {
$this->outDatedExtensionList = $dataObject['extensions'];
}

if (array_key_exists('latestVersions', $dataObject)) {
$this->latestVersions = $dataObject['latestVersions'];
}
}
}

Expand All @@ -216,7 +234,7 @@ public function loadOutDatedExtensionCollection()
try {
$extensionList = $this->getMyExtensionList();
$feed = $this->callApi(self::URL, $this->getPostData());
$latestVersions = $feed['extensions'] ?? [];
$this->latestVersions = $feed['extensions'] ?? [];
$hasUpdate = false;

foreach ($extensionList as $item) {
Expand All @@ -225,8 +243,8 @@ public function loadOutDatedExtensionCollection()
$item['url'] = 'https://www.magepal.com/extensions.com';
$item['name'] = $this->getTitleFromModuleName($item['moduleName']);

if (array_key_exists($item['composer_name'], $latestVersions)) {
$latest = $latestVersions[$item['composer_name']];
if (array_key_exists($item['composer_name'], $this->latestVersions)) {
$latest = $this->latestVersions[$item['composer_name']];
$item['latest_version'] = $latest['latest_version'];
$item['has_update'] = version_compare($item['latest_version'], $item['install_version']) > 0;
$item['url'] = $latest['url'];
Expand All @@ -249,7 +267,8 @@ public function loadOutDatedExtensionCollection()
'count' => $this->updateCounter,
'extensions' => $this->outDatedExtensionList,
'data_version' => self::DATA_VERSION,
'hash' => $this->getHash()
'hash' => $this->getHash(),
'latestVersions' => $this->latestVersions
];

$this->cache->save(json_encode($dataObject), self::CACHE_KEY, [], self::LIFE_TIME);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"magento/framework": "100.0.*|100.1.*|101.0.*|102.0.*|103.0.*"
},
"type": "magento2-module",
"version": "1.1.7",
"version": "1.1.8",
"autoload": {
"files": [
"registration.php"
Expand Down
20 changes: 19 additions & 1 deletion view/adminhtml/templates/system/config/field/extensions.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</div>
<?php if (isset($item['url'])): ?>
<div class="info">
<a href="<?= $block->escapeUrl($item['url']) ?>">Learn More</a>
<a href="<?= $block->escapeUrl($item['url']) ?>" target="_blank">Learn More</a>
</div>
<?php endif; ?>
</div>
Expand Down Expand Up @@ -56,6 +56,24 @@
</div>
<?php endif; ?>
<?php endforeach; ?>

<?php foreach ($block->getRelatedProduct() as $item): ?>
<div class="card">
<div class="headerline"><?= $block->escapeHtml($item['name'] ?? '') ?></div>
<div class="wrapper">
<div class="latest-version">
<?= __('Not Installed') ?>
</div>

<?php if (isset($item['url'])): ?>
<div class="info">
<a href="<?= $block->escapeUrl($item['url']) ?>" target="_blank">Get Now!</a>
</div>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>

</div>
<?php endif; ?>

Expand Down
6 changes: 6 additions & 0 deletions view/adminhtml/web/css/config.less
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@
color: green;
}

.latest-version {
margin-top: 12px;
text-align: center;
color: red;
}

.update {
color: red;
}
Expand Down

0 comments on commit 231c17b

Please sign in to comment.