Skip to content

Commit

Permalink
refactor: use class for graph isplay params
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-peugnet committed Dec 3, 2024
1 parent 46c95bd commit 8dbb7eb
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 40 deletions.
14 changes: 3 additions & 11 deletions app/class/Controllerhome.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,10 @@ public function desktop()
$vars['hiddencolumncount'] = count(User::HOME_COLUMNS) - count($this->user->columns());

if ($display === 'graph') {
$vars['layout'] = $_GET['layout'] ?? 'cose-bilkent';
$vars['showorphans'] = boolval($_GET['showorphans'] ?? false);
$vars['showredirection'] = boolval($_GET['showredirection'] ?? false);
$vars['showexternallinks'] = boolval($_GET['showexternallinks'] ?? false);
$datas = $this->modelhome->cytodata(
$pagelistopt,
$vars['layout'],
$vars['showorphans'],
$vars['showredirection'],
$vars['showexternallinks']
);
$graph = new Graph($_GET);
$datas = $this->modelhome->cytodata($pagelistopt, $graph);
$vars['json'] = json_encode($datas);
$vars['graph'] = $graph;
}

if ($display === 'map') {
Expand Down
73 changes: 73 additions & 0 deletions app/class/Graph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Wcms;

class Graph extends Item
{
protected bool $showorphans = true;
protected bool $showredirection = false;
protected bool $showexternallinks = false;
protected string $layout = 'euler';

public const LAYOUTS = [
'cose' => 'cose',
'fcose' => 'fcose',
'cose-bilkent' => 'cose-bilkent',
'euler' => 'euler',
'circle' => 'circle',
'breadthfirst' => 'breadthfirst',
'concentric' => 'concentric',
'grid' => 'grid',
'random' => 'random',
];

/**
* @param mixed[] $datas
*/
public function __construct(array $datas)
{
$this->hydrate($datas);
}

public function showorphans(): bool
{
return $this->showorphans;
}

public function showredirection(): bool
{
return $this->showredirection;
}

public function showexternallinks(): bool
{
return $this->showexternallinks;
}

public function layout(): string
{
return $this->layout;
}

public function setshowredirection($showredirection): void
{
$this->showredirection = boolval($showredirection);
}

public function setshoworphans($showorphans): void
{
$this->showorphans = boolval($showorphans);
}

public function setshowexternallinks($showexternallinks): void
{
$this->showexternallinks = boolval($showexternallinks);
}

public function setlayout($layout): void
{
if (key_exists($layout, $this::LAYOUTS)) {
$this->layout = $layout;
}
}
}
36 changes: 11 additions & 25 deletions app/class/Modelhome.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,26 @@

class Modelhome extends Model
{
public const GRAPH_LAYOUTS = [
'cose' => 'cose',
'fcose' => 'fcose',
'cose-bilkent' => 'cose-bilkent',
'euler' => 'euler',
'circle' => 'circle',
'breadthfirst' => 'breadthfirst',
'concentric' => 'concentric',
'grid' => 'grid',
'random' => 'random',
];

/**
* Transform list of page into list of nodes and edges
*
* @param Page[] $pagelist associative array of pages as `id => Page`
* @param string $layout
* @param bool $showorphans if `false`, remove orphans pages
* @param bool $showredirection if `true`, add redirections
* @param Page[] $pagelist associative array of pages as `id => Page`
* @param Graph $graph Graph settings
*
* @return mixed[]
*/
public function cytodata(
array $pagelist,
string $layout = 'random',
bool $showorphans = false,
bool $showredirection = false,
bool $showexternallinks = false
): array {
$datas['elements'] = $this->mapdata($pagelist, $showorphans, $showredirection, $showexternallinks);
public function cytodata(array $pagelist, Graph $graph): array
{
$datas['elements'] = $this->mapdata(
$pagelist,
$graph->showorphans(),
$graph->showredirection(),
$graph->showexternallinks()
);

$datas['wheelSensitivity'] = 0.1;
$datas['layout'] = [
'name' => $layout,
'name' => $graph->layout(),
'quality' => 'proof',
// 'fit' => true,
'randomize' => true,
Expand Down
11 changes: 7 additions & 4 deletions app/view/templates/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@
<div id="deepsearchbar">
<form action="" method="get">
<input type="hidden" name="display" value="graph">
<input type="checkbox" name="showorphans" value="1" id="showorphans" <?= $showorphans ? 'checked' : '' ?>>
<input type="hidden" name="showorphans" value="0">
<input type="checkbox" name="showorphans" value="1" id="showorphans" <?= $graph->showorphans() ? 'checked' : '' ?>>
<label for="showorphans">show orphans pages</label>
<input type="checkbox" name="showredirection" value="1" id="showredirection" <?= $showredirection ? 'checked' : '' ?>>
<input type="hidden" name="showredirection" value="0">
<input type="checkbox" name="showredirection" value="1" id="showredirection" <?= $graph->showredirection() ? 'checked' : '' ?>>
<label for="showredirection">show redirections</label>
<input type="checkbox" name="showexternallinks" value="1" id="showexternallinks" <?= $showexternallinks ? 'checked' : '' ?>>
<input type="hidden" name="showexternallinks" value="0">
<input type="checkbox" name="showexternallinks" value="1" id="showexternallinks" <?= $graph->showexternallinks() ? 'checked' : '' ?>>
<label for="showexternallinks">show external links</label>
<select name="layout" id="layout">
<?= options(Wcms\Modelhome::GRAPH_LAYOUTS, $layout) ?>
<?= options(Wcms\Graph::LAYOUTS, $graph->layout()) ?>
</select>
<label for="layout">graph layout</label>
<input type="submit" value="update">
Expand Down

0 comments on commit 8dbb7eb

Please sign in to comment.