Skip to content

Commit

Permalink
Modification ajax et manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvaner committed Feb 2, 2020
1 parent dd4d068 commit 053f0be
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 99 deletions.
11 changes: 6 additions & 5 deletions assets/js/core/update.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ nextdom.update.saves = function (queryParams) {
};

nextdom.update.number = function (queryParams) {
nextdom.private.ajax('Update', 'nbUpdate', queryParams, false, false, false);
};

nextdom.update.numbers = function (queryParams) {
nextdom.private.ajax('Update', 'nbUpdates', queryParams, false, false, false);
var params = $.extend({}, nextdom.private.default_params, queryParams || {});
var ajaxParams = nextdom.private.getParamsAJAX(params, 'Update', 'nbUpdate');
if (queryParams !== undefined && queryParams.hasOwnProperty('filter')) {
ajaxParams.data['filter'] = json_encode(queryParams.filter);
}
nextdom.private.ajaxCall(ajaxParams);
};
55 changes: 20 additions & 35 deletions assets/js/desktop/gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,46 +350,31 @@ function refreshUpdateNumber() {
}
});
if($('#spanNbUpdates').length){
nextdom.update.numbers({
nextdom.update.number({
filter: ['core', 'plugin', 'widget', 'script'],
error: function (error) {
notify("Erreur", error.message, 'error');
},
success: function (_numbers) {
if (_numbers.all == 0 || _numbers.all == '0') {
$('#spanNbUpdates').hide();
} else {
$('#spanNbUpdates').html(_numbers.all);
$('#spanNbUpdates').show();
}
if (_numbers.core == 0 || _numbers.core == '0') {
$('#spanNbUpdatesCore').hide();
} else {
$('#spanNbUpdatesCore').html(_numbers.core);
$('#spanNbUpdatesCore').show();
}
if (_numbers.plugin == 0 || _numbers.plugin == '0') {
$('#spanNbUpdatesPlugins').hide();
} else {
$('#spanNbUpdatesPlugins').html(_numbers.plugin);
$('#spanNbUpdatesPlugins').show();
}
if (_numbers.widget == 0 || _numbers.widget == '0') {
$('#spanNbUpdatesWidgets').hide();
} else {
$('#spanNbUpdatesWidgets').html(_numbers.widget);
$('#spanNbUpdatesWidgets').show();
success: function (updatesData) {
var updateSum = 0;
for (var updateIndex in updatesData) {
var target = $('#spanNbUpdates-' + updatesData[updateIndex].type);
var targetCount = updatesData[updateIndex].count;
updateSum += targetCount;
if (targetCount == 0) {
target.hide();
}
else {
target.html(targetCount);
target.show();
}
}
if (_numbers.script == 0 || _numbers.script == '0') {
$('#spanNbUpdatesScripts').hide();
} else {
$('#spanNbUpdatesScripts').html(_numbers.script);
$('#spanNbUpdatesScripts').show();
if (updateSum == 0) {
$('#spanNbUpdates').hide();
}
if (_numbers.others == 0 || _numbers.others == '0') {
$('#spanNbUpdatesOthers').hide();
} else {
$('#spanNbUpdatesOthers').html(_numbers.others);
$('#spanNbUpdatesOthers').show();
else {
$('#spanNbUpdates').html(updateSum);
$('#spanNbUpdates').hide();
}
}
});
Expand Down
8 changes: 2 additions & 6 deletions src/Ajax/UpdateAjax.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,8 @@ class UpdateAjax extends BaseAjax

public function nbUpdate()
{
$this->ajax->success(UpdateManager::nbNeedUpdate());
}

public function nbUpdates()
{
$this->ajax->success(UpdateManager::nbNeedUpdates());
$filter = json_decode(Utils::init(AjaxParams::FILTER, ''));
$this->ajax->success(UpdateManager::nbNeedUpdate($filter));
}

public function all()
Expand Down
24 changes: 24 additions & 0 deletions src/Helpers/DBHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

namespace NextDom\Helpers;

use NextDom\Enums\LogTarget;
use NextDom\Exceptions\CoreException;

/**
Expand Down Expand Up @@ -219,6 +220,29 @@ public static function exec(string $query, array $params = [])
return false;
}

/**
* Generate IN params for query
*
* @param array $params
*
* @return bool|string
*
* @throws \Exception
*/
public static function getInParamFromArray($params)
{
if (count($params) > 0) {
$stringifiedParams = "'" . implode("', '", $params) . "'";
preg_match_all('/^\'\w*\'(?:, \'\w*\')*$/', $stringifiedParams, $checkValidity);
if (count($checkValidity) > 0 && count($checkValidity[0]) === 1) {
return '(' . $stringifiedParams . ')';
}
else {
LogHelper::addError(LogTarget::NEXTDOM, __('Bad SQL WHERE IN params ') . $stringifiedParams);
}
}
return false;
}
/**
* Begin transaction
*/
Expand Down
76 changes: 28 additions & 48 deletions src/Managers/UpdateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,56 +369,36 @@ public static function nbNeedUpdate($filter = '')
Common::STATUS => 'update',
Common::CONFIGURATION => '%"doNotUpdate":"1"%'
];
$sql = 'SELECT count(*)
FROM ' . self::DB_CLASS_NAME . '
WHERE `status` = :status
AND `configuration` NOT LIKE :configuration';
if ($filter != '') {
$params[Common::TYPE] = $filter;
$sql .= ' AND `type` = :type';
if (is_array($filter)) {
$likeParams = DBHelper::getInParamFromArray($filter);
if ($params) {
$sql = 'SELECT `type`, SUM(CASE WHEN status = :status AND configuration NOT LIKE :configuration THEN 1 ELSE 0 END) AS count
FROM ' . self::DB_CLASS_NAME . '
WHERE `type` IN ' . $likeParams . '
GROUP BY `type`';
$result = DBHelper::getAll($sql, $params);
$sql = 'SELECT SUM(CASE WHEN status = :status AND configuration NOT LIKE :configuration THEN 1 ELSE 0 END) AS count
FROM ' . self::DB_CLASS_NAME . '
WHERE `type` NOT IN ' . $likeParams . '
GROUP BY `type`';
$othersCount = DBHelper::getOne($sql, $params);
array_push($result, ['type' => 'others', 'count' => intval($othersCount['count'])]);
return $result;
}
}

$result = DBHelper::getOne($sql, $params);
return $result[SQLField::COUNT];
}

/**
* Get the number of pending updates
*
* @param string $filter Type filter
*
* @return array arrays of pending updates type count
*
* @throws \NextDom\Exceptions\CoreException
*/
public static function nbNeedUpdates()
{
$filters = [
UpdateType::CORE,
UpdateType::PLUGIN,
UpdateType::WIDGET,
UpdateType::SCRIPT
];
$params = [
Common::STATUS => 'update',
Common::CONFIGURATION => '%"doNotUpdate":"1"%'
];
$sqlbase = 'SELECT count(*)
FROM ' . self::DB_CLASS_NAME . '
WHERE `status` = :status
AND `configuration` NOT LIKE :configuration';
$sql = $sqlbase;
$sqlResult = DBHelper::getOne($sql, $params);
$countAll = $sqlResult[SQLField::COUNT];
foreach ($filters as $filter) {
$params[Common::TYPE] = $filter;
$sql = $sqlbase . ' AND `type` = :type';
$sqlResult = DBHelper::getOne($sql, $params);
$result[$filter] = $sqlResult[SQLField::COUNT];
else {
$sql = 'SELECT count(*)
FROM ' . self::DB_CLASS_NAME . '
WHERE `status` = :status
AND `configuration` NOT LIKE :configuration';
if ($filter != '') {
$params[Common::TYPE] = $filter;
$sql .= ' AND `type` = :type';
}
$result = DBHelper::getOne($sql, $params);
return $result[SQLField::COUNT];
}
$result[UpdateType::ALL] = $countAll;
$result[UpdateType::OTHERS] = $countAll - $result[UpdateType::CORE] - $result[UpdateType::PLUGIN] - $result[UpdateType::WIDGET] - $result[UpdateType::SCRIPT];
return $result;
return false;
}

/**
Expand Down
76 changes: 76 additions & 0 deletions tests/phpunit_tests/Managers/UpdateManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public static function tearDownAfterClass(): void
DBHelper::exec('DELETE FROM `update` WHERE id > 2');
}

public function setUp(): void
{
DBHelper::exec('UPDATE `update` SET configuration = \'{"doNotUpdate":"1"}\'');
DBHelper::exec('UPDATE `update` SET status = \'ok\' WHERE id = 1');
}

public function testFindNewUpdateObject()
{
UpdateManager::findNewUpdateObject();
Expand Down Expand Up @@ -111,4 +117,74 @@ public function testListRepo()
$this->assertEquals('\NextDom\Repo\RepoNextDom', $repoList['nextdom']['class']);
$this->assertEquals('input', $repoList['samba']['configuration']['configuration']['backup::ip']['type']);
}

public function testNbUpdateWithoutFilter()
{
$this->assertEquals(0, UpdateManager::nbNeedUpdate());
$coreUpdate = UpdateManager::byId(1);
$coreUpdate->setConfiguration('doNotUpdate', 0);
$coreUpdate->save();
$this->assertEquals(0, UpdateManager::nbNeedUpdate());
$pluginUpdate = UpdateManager::byId(2);
$pluginUpdate->setStatus('update');
$pluginUpdate->save();
$this->assertEquals(0, UpdateManager::nbNeedUpdate());
$pluginUpdate->setConfiguration('doNotUpdate', 0);
$pluginUpdate->save();
$this->assertEquals(1, UpdateManager::nbNeedUpdate());
}

public function testNbUpdateWithTextFilter()
{
$this->assertEquals(0, UpdateManager::nbNeedUpdate('core'));
$coreUpdate = UpdateManager::byId(1);
$coreUpdate->setConfiguration('doNotUpdate', 0);
$coreUpdate->save();
$this->assertEquals(0, UpdateManager::nbNeedUpdate('core'));
$pluginUpdate = UpdateManager::byId(2);
$pluginUpdate->setStatus('update');
$pluginUpdate->save();
$this->assertEquals(0, UpdateManager::nbNeedUpdate('plugin'));
$pluginUpdate->setConfiguration('doNotUpdate', 0);
$pluginUpdate->save();
$this->assertEquals(1, UpdateManager::nbNeedUpdate('plugin'));
}

public function testNbUpdateWithArrayFilter()
{
$result = UpdateManager::nbNeedUpdate(['core']);
$this->assertEquals('core', $result[0]['type']);
$this->assertEquals(0, $result[0]['count']);
$this->assertEquals('others', $result[1]['type']);
$this->assertEquals(0, $result[1]['count']);
$coreUpdate = UpdateManager::byId(1);
$coreUpdate->setConfiguration('doNotUpdate', 0);
$coreUpdate->save();
$result = UpdateManager::nbNeedUpdate(['core', 'plugin']);
$this->assertEquals('core', $result[0]['type']);
$this->assertEquals('plugin', $result[1]['type']);
$this->assertEquals(0, $result[0]['count']);
$this->assertEquals(0, $result[1]['count']);
$this->assertEquals('others', $result[2]['type']);
$this->assertEquals(0, $result[2]['count']);
$pluginUpdate = UpdateManager::byId(2);
$pluginUpdate->setStatus('update');
$pluginUpdate->save();
$coreUpdate->setStatus('update');
$coreUpdate->save();
$result = UpdateManager::nbNeedUpdate(['plugin']);
$this->assertEquals('plugin', $result[0]['type']);
$this->assertEquals('others', $result[1]['type']);
$this->assertEquals(0, $result[0]['count']);
$this->assertEquals(1, $result[1]['count']);
$pluginUpdate->setConfiguration('doNotUpdate', 0);
$pluginUpdate->save();
$result = UpdateManager::nbNeedUpdate(['core', 'plugin']);
$this->assertEquals('core', $result[0]['type']);
$this->assertEquals('plugin', $result[1]['type']);
$this->assertEquals('others', $result[2]['type']);
$this->assertEquals(1, $result[0]['count']);
$this->assertEquals(1, $result[1]['count']);
$this->assertEquals(0, $result[2]['count']);
}
}
10 changes: 5 additions & 5 deletions views/desktop/tools/update-view.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@
<div class="nav-tabs-custom config">
<!-- Tabs title -->
<ul class="nav nav-tabs pull-right" role="tablist">
<li role="presentation"><a href="#others" aria-controls="profile" role="tab" data-toggle="tab"><i class="fas fa-align-justify"></i><span>{{ 'update-view.any' | trans }}</span>{% if numberOfUpdatesOthers>0 %}<label id="spanNbUpdatesOthers" class="badge bg-yellow spacing-left">{{ numberOfUpdatesOthers }}</span>{% endif %}</label></a></li>
<li role="presentation"><a href="#scripts" aria-controls="profile" role="tab" data-toggle="tab"><i class="fas fa-subscript"></i><span>{{ 'update-view.scripts' | trans }}</span>{% if numberOfUpdatesScripts>0 %}<label id="spanNbUpdatesScripts" class="badge bg-yellow spacing-left">{{ numberOfUpdatesScripts }}</span>{% endif %}</label></a></li>
<li role="presentation"><a href="#widgets" aria-controls="profile" role="tab" data-toggle="tab"><i class="fas fa-boxes"></i><span>{{ 'common.widgets' | trans }}</span>{% if numberOfUpdatesWidgets>0 %}<label id="spanNbUpdatesWidgets" class="badge bg-yellow spacing-left">{{ numberOfUpdatesWidgets }}</span>{% endif %}</label></a></li>
<li role="presentation"><a href="#plugins" aria-controls="profile" role="tab" data-toggle="tab"><i class="fas fa-puzzle-piece"></i><span>{{ 'update-view.plugins' | trans }}</span>{% if numberOfUpdatesPlugins>0 %}<label id="spanNbUpdatesPlugins" class="badge bg-yellow spacing-left">{{ numberOfUpdatesPlugins }}</span>{% endif %}</label></a></li>
<li role="presentation" class="active"><a href="#core" aria-controls="home" role="tab" data-toggle="tab"><i class="fas fa-microchip"></i><span>{{ 'update-view.core' | trans }}</span>{% if numberOfUpdatesCore>0 %}<label id="spanNbUpdatesCore" class="badge bg-yellow spacing-left">{{ numberOfUpdatesCore }}</span>{% endif %}</label></a></li>
<li role="presentation"><a href="#others" aria-controls="profile" role="tab" data-toggle="tab"><i class="fas fa-align-justify"></i><span>{{ 'update-view.any' | trans }}</span>{% if numberOfUpdatesOthers>0 %}<label id="spanNbUpdates-others" class="badge bg-yellow spacing-left">{{ numberOfUpdatesOthers }}</span>{% endif %}</label></a></li>
<li role="presentation"><a href="#scripts" aria-controls="profile" role="tab" data-toggle="tab"><i class="fas fa-subscript"></i><span>{{ 'update-view.scripts' | trans }}</span>{% if numberOfUpdatesScripts>0 %}<label id="spanNbUpdates-script" class="badge bg-yellow spacing-left">{{ numberOfUpdatesScripts }}</span>{% endif %}</label></a></li>
<li role="presentation"><a href="#widgets" aria-controls="profile" role="tab" data-toggle="tab"><i class="fas fa-boxes"></i><span>{{ 'common.widgets' | trans }}</span>{% if numberOfUpdatesWidgets>0 %}<label id="spanNbUpdates-widget" class="badge bg-yellow spacing-left">{{ numberOfUpdatesWidgets }}</span>{% endif %}</label></a></li>
<li role="presentation"><a href="#plugins" aria-controls="profile" role="tab" data-toggle="tab"><i class="fas fa-puzzle-piece"></i><span>{{ 'update-view.plugins' | trans }}</span>{% if numberOfUpdatesPlugins>0 %}<label id="spanNbUpdates-plugin" class="badge bg-yellow spacing-left">{{ numberOfUpdatesPlugins }}</span>{% endif %}</label></a></li>
<li role="presentation" class="active"><a href="#core" aria-controls="home" role="tab" data-toggle="tab"><i class="fas fa-microchip"></i><span>{{ 'update-view.core' | trans }}</span>{% if numberOfUpdatesCore>0 %}<label id="spanNbUpdates-core" class="badge bg-yellow spacing-left">{{ numberOfUpdatesCore }}</span>{% endif %}</label></a></li>
<li class="header pull-left"><i class="fas fa-download"></i>{{ 'update-view.update_s_in_days' | trans }}{% if numberOfUpdates>0 %}<span id="spanNbUpdates" class="badge bg-red spacing-left">{{ numberOfUpdates }}</span>{% endif %}</li>
</ul>

Expand Down

0 comments on commit 053f0be

Please sign in to comment.