Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
joszz committed May 11, 2021
1 parent 5caf620 commit 2bf8f13
Show file tree
Hide file tree
Showing 28 changed files with 113 additions and 80 deletions.
6 changes: 6 additions & 0 deletions app/controllers/HyperVAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ public function getSitesAction($id)
die($this->_model->getSites($this->getDevice($id), false));
}

/**
* Retrieves a Device by id, or null when not found.
*
* @param int $id The Device Id to look for.
* @return Devices|null The Device when found, otherwise null.
*/
private function getDevice($id)
{
return Devices::findFirst([
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/JellyfinController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

class JellyfinController extends BaseController
{
/**
* Retrieves all Jellyfin libraries.
*/
public function viewsAction()
{
die(json_encode(array_flip((new Jellyfin())->getViews())));
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/PulsewayController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public function indexAction()
die(json_encode($result));
}

/**
* Retrieves all systems defined in the associated Pulseway account.
*/
public function systemsAction()
{
die(json_encode($this->_model->getSystems()));
Expand Down
7 changes: 3 additions & 4 deletions app/controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Chell\Models\Users;
use Chell\Models\Devices;
use Chell\Models\MenuItems;
use Chell\Models\MenuItemsUsers;
use Chell\Models\SnmpHosts;
use Chell\Models\SnmpRecords;
use Davidearl\WebAuthn\WebAuthn;
Expand Down Expand Up @@ -55,8 +54,8 @@ public function indexAction($activeTab = 'General')
{
$this->view->activeTab = $activeTab;
$this->view->forms = [
'General' => isset($this->generalForm) ? $this->generalForm : new SettingsGeneralForm(),
'Dashboard' => isset($this->dashboardForm) ? $this-> dashboardForm: new SettingsDashboardForm(),
'General' => $this->generalForm ?? new SettingsGeneralForm(),
'Dashboard' => $this->dashboardForm ?? new SettingsDashboardForm(),
];

$logsTotal = 0;
Expand Down Expand Up @@ -255,7 +254,7 @@ public function menuAction($id = 0)
}

$item->save();
$item->handlePost(isset($data['user_id']) ? $data['user_id'] : [-1]);
$item->handlePost($data['user_id'] ?? [-1]);

if ($file)
{
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/SpeedtestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public function telemetryAction()
if ($this->request->isPost())
{
$item = new Speedtest($this->request->getPost());
$item->ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$item->ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
$item->lang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
$item->ip = $_SERVER['REMOTE_ADDR'] ?? '';
$item->ua = $_SERVER['HTTP_USER_AGENT'] ?? '';
$item->lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '';
$item->extra = $this->whatIsMyBrowser();

die(var_dump($item->save()));
Expand Down
12 changes: 12 additions & 0 deletions app/forms/SettingsBaseForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,23 @@ private function renderElementInternal($element, $hidden = false)
return $html;
}

/**
* Renders the form
*
* @param string $id The HTML id attribute to set on the form.
*/
public function renderForm($id)
{
require APP_PATH . 'app/views/forms/form.phtml';
}

/**
* Summary of renderButton
*
* @param string $button
* @param string $name
* @param mixed $element
*/
public function renderButton($button, $name = '', $element = '')
{
require APP_PATH . 'app/views/forms/buttons/' . $button . '.phtml';
Expand Down
2 changes: 0 additions & 2 deletions app/forms/SettingsDashboardForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public function isValid($data = null, $entity = null) : bool

if ($valid)
{
$this->config->dashboard->checkDeviceStatesInterval = $data['check-devicestate-interval'];

foreach($this->formFieldClasses as $class)
{
$class->setPostData($this->config, $data);
Expand Down
2 changes: 1 addition & 1 deletion app/forms/SettingsGeneralForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function isValid($data = null, $entity = null) : bool
$this->config->application->debug = isset($data['debug']) && $data['debug']== 'on' ? '1' : '0';
$this->config->application->demoMode = isset($data['demo']) && $data['demo'] == 'on' ? '1' : '0';

foreach($this->formFieldClasses as $class)
foreach ($this->formFieldClasses as $class)
{
$class->setPostData($this->config, $data);
}
Expand Down
20 changes: 16 additions & 4 deletions app/forms/SettingsMenuItemForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,21 @@ public function initialize($entity = null)
'using' => ['id', 'name'],
'useEmpty' => true,
'emptyText' => 'None',
'emptyValue' => 0
'emptyValue' => null
]
);
$device->setLabel('Device');

$allUsers = Users::find();
$users = new Select(
'user_id[]' ,
Users::find(),
$allUsers,
[
'using' => ['id', 'username'],
'multiple' => 'multiple'
],
);
$users->setLabel('Users')->setDefault($this->getSelectedUsers($entity));
$users->setLabel('Users')->setDefault($this->getSelectedUsers($entity, $allUsers));

$this->add($name);
$this->add($url);
Expand All @@ -74,7 +75,13 @@ public function initialize($entity = null)
$this->add($users);
}

private function getSelectedUsers($entity)
/**
* Given the MenuItem entity, get the selected users.
*
* @param \Chell\Models\MenuItems $entity The MenuItem to get the selected users for.
* @return array An array of User Ids.
*/
private function getSelectedUsers($entity, $allUsers)
{
$selectedUsers = [];

Expand All @@ -87,6 +94,11 @@ private function getSelectedUsers($entity)
}
}

if (!count($selectedUsers) && $allUsers->count() === 1)
{
$selectedUsers = [$allUsers[0]->id];
}

return $selectedUsers;
}
}
7 changes: 1 addition & 6 deletions app/forms/SettingsSnmpRecordForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ public function initialize($entity)
$host = new Select(
'snmp_host_id' ,
SnmpHosts::find(),
[
'using' => ['id', 'name'],
'useEmpty' => true,
'emptyText' => 'None',
'emptyValue' => 0
]
['using' => ['id', 'name']]
);
$host->setLabel('Host');

Expand Down
1 change: 0 additions & 1 deletion app/forms/formfields/dashboard/JellyfinFormFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Chell\Forms\FormFields\IFormFields;
use Chell\Forms\Validators\PresenceOfConfirmation;
use Chell\Models\Jellyfin;
use Phalcon\Forms\Element\Check;
use Phalcon\Forms\Element\Numeric;
use Phalcon\Forms\Element\Password;
Expand Down
17 changes: 16 additions & 1 deletion app/models/MenuItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,26 @@ public function resizeIcon($filename)
$source = imagecreatefromstring(file_get_contents($filename));
$thumb = imagecreatetruecolor(16, 16);
imagealphablending($thumb, false);
imagesavealpha($thumb, true);
imagesavealpha($thumb, true );
imagecopyresampled($thumb, $source, 0, 0, 0, 0, 16, 16, $width, $height);
imagepng($thumb, $filename);
}

/**
* Adds and deletes MenuItems associated to the user.
*
* @param array $userIds The Ids of users to add or remove the MenuItem link for.
*/
public function handlePost($userIds){
$this->addToUsers($userIds);
$this->deleteFromUsers($userIds);
}

/**
* Given an array of user Ids, add a menu_items_user record linking this MenuItem to the users.
*
* @param array $userIds The Ids of users to link to this MenuItem.
*/
private function addToUsers($userIds)
{
$users = Users::find([
Expand All @@ -90,6 +100,11 @@ private function addToUsers($userIds)
}
}

/**
* Given an array of user Ids, removes menu_items_user records linking this MenuItem that are not equal to provided user Ids.
*
* @param array $userIds The Ids of users to remove the link to this MenuItem for.
*/
private function deleteFromUsers($userIds)
{
MenuItemsUsers::find([
Expand Down
2 changes: 1 addition & 1 deletion app/models/MenuItemsUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Phalcon\Mvc\Model;

/**
* The model responsible for all actions related to menu items.
* The model responsible for all actions related to menu_items_users.
*
* @package Models
*/
Expand Down
2 changes: 1 addition & 1 deletion app/models/Speedtest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private function setISPDetails()
$details = json_decode(curl_exec($curl));
curl_close($curl);

$this->isp .= isset($details->org) ? $details->org : 'Unknown ISP';
$this->isp .= $details->org ?? 'Unknown ISP';
$this->isp .= isset($details->country) ? ', ' . $details->country : '';
$this->clientLocaction = isset($details->loc) ? explode(',', $details->loc) : false;

Expand Down
24 changes: 0 additions & 24 deletions app/models/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,4 @@ public function initialize()
['alias' => 'menuitems']
);
}

public static function createNewMenuItemLinks($userIds, $itemId)
{
$users = Users::find([
'id IN ({userids:array})',
'bind' => ['userids' => $userIds]
]);

foreach ($users as $user)
{
$menuItems = $user->getMenuitems([
'menu_item_id = {id:int}',
'bind' => ['id' => $itemId]
]);

if ($menuItems->count() == 0)
{
(new MenuItemsUsers([
'user_id' => $user->id,
'menu_item_id' => $itemId
]))->save();
}
}
}
}
2 changes: 1 addition & 1 deletion app/views/forms/buttons/menuitem_icon.phtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php /** @var Chell\Forms\SettingsBaseForm $this */ ?>

<?php if(file_exists(APP_PATH . 'public/img/icons/menu/' . $element->getValue())) : ?>
<a class="btn btn-default input-group-addon menuitem_icon" href="#">
<a class="btn btn-default input-group-addon menuitem_icon disabled" href="#">
<img src="<?php echo $this->config->application->baseUri .'img/icons/menu/' . $element->getValue() ?>" title="Current icon" />
</a>
<?php endif ?>
2 changes: 1 addition & 1 deletion app/views/forms/form.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<?php $attributes = $this->getAttributes() ?>

<form id="form_<?php echo strtolower($id) ?>" action="<?php echo $this->getAction() ?>" method="post" <?php echo isset($attributes['enctype']) ? $attributes['enctype'] : null ?>>
<form id="form_<?php echo strtolower($id) ?>" action="<?php echo $this->getAction() ?>" method="post" <?php echo $attributes['enctype'] ?? null ?>>
<?php foreach($this AS $element) : ?>
<?php echo $this->renderElement($element) ?>
<?php endforeach ?>
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/empty.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
?>
</head>

<body data-bg-image="<?php echo isset($bgImage) ? $bgImage : null ?>"
<body data-bg-image="<?php echo $bgImage ?? null ?>"
class="<?php echo isset($overflow) && $overflow == true ? 'overflow' : null ?> <?php echo $this->config->application->demoMode ? 'demo-mode' : null ?>">
<div class="container-fluid no-gutter container-relative">
<?php echo $this->getContent() ?>
Expand Down
21 changes: 12 additions & 9 deletions app/views/navbar.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@
/**
* @var Phalcon\Mvc\Controller $this
* @var Phalcon\Paginator\Repository $menu
* @var Chell\Models\Users $user
*/
?>

<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container no-gutter-xs">
<div class="col-xs-12">
<div class="navbar-header">
<?php if (isset($user)) : ?>
<button type="button" id="menu_btn" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<?php endif ?>
<div class="btn-group pull-right">
<button type="submit" class="btn btn-success fa fa-save hidden_not_important pull-left" title="Save" aria-label="Save" form="form_general"></button>

<button type="submit" class="btn btn-success fa fa-save hidden_not_important" title="Save" aria-label="Save" form="form_general"></button>
<?php if (isset($user)) : ?>
<button type="button" id="menu_btn" class="btn navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<?php endif ?>
</div>

<a class="navbar-brand" href="<?php echo $this->config->application->baseUri ?>">
<img src="<?php echo $this->config->application->baseUri ?>img/icons/favicons/android-chrome-72x72.png" alt="Logo" width="32" height="32" />
Expand Down
2 changes: 1 addition & 1 deletion app/views/settings/partials/menuitems.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</a>
</td>
<td>
<?php echo (isset($item->device->name) ? $item->device->name : null) ?>
<?php echo $item->device->name ?? null ?>
</td>
<td class="hidden-xs">
<div class="btn-group pull-right">
Expand Down
2 changes: 1 addition & 1 deletion app/views/settings/snmprecord.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="panel panel-success" id="settings">
<div class="panel-heading">
<div class="btn-group pull-left back-btn">
<a href="<?php echo $this->config->application->baseUri ?>settings/snmphost/<?php echo $record->snmp_host_id ?>#records" class="btn btn-default fa fa-chevron-left" title="Go back"></a>
<a href="<?php echo $this->config->application->baseUri ?>settings/snmphost/<?php echo $record->snmp_host_id ?>#snmprecords" class="btn btn-default fa fa-chevron-left" title="Go back"></a>
</div>
<h4 class="pull-left">
<?php echo isset($record->id) ? 'Edit' : 'New' ?> SNMP record
Expand Down
Loading

0 comments on commit 2bf8f13

Please sign in to comment.