Skip to content

Commit

Permalink
Merge branch 'release/1.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Dec 9, 2018
2 parents 7990afe + 42d7ebf commit e8d1d62
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 38 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# v1.0.1
## 12/09/2018

1. [](#new)
* Added a new `type` column to support multiple view types concurrently
* Updated README.md

# v1.0.0
## 12/08/2018

Expand Down
58 changes: 53 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,49 @@ The configuration options are as follows:

The default behavior is for the **view** plugin to track all page requests and keep a running total of how many times the pages have been hit. You can change this behavior by first _disabling_ the `autotrack:` configuration option, then using the Twig function to track a page hit, or if you want to track via another plugin, you can use a simple PHP command.

### Manual Twig Tracking
> In Views `1.0.1` we re-worked the database structure to allow multiple view types. If you installed and tested version `1.0.0`, please delete your `user/data/views/views.db` so it can be regenerated

To track via Twig, you can use the default `track_views(id)` twig function, but an `id` is required. For example, to track the current page from a twig template:
### Twig Tracking

To track via Twig, you can use the default `track_views(id)` twig function, an `id` is required, and a `type` is optional because it defaults to `pages`. For example, to track the current page from a twig template:

```twig
{% do track_views(page.route) %}
```

### Manual PHP Tracking
or specify a custom type

```twig
{% do track_views(page.route, 'widgets') %}
```

### PHP Tracking

To track via PHP, you can use the default `views` object with a required `id` attribute. For example, to track the current page from a PHP file:
To track via PHP, you can use the default `views` object with a required `id` attribute. A `type` is optional because it defaults to `pages`. For example, to track the current page from a PHP file:

```php
Grav::instance()['views']->track($page->route());
```

or

```php
Grav::instance()['views']->track($page->route(), 'widgets');
```

## Viewing

Via the administrator plugin, you can view the current counts of the top 20 views by Visiting the **Reports** tab in the **Tools** section. There will be an entry called `Grav Views`. Alternatively you can query via the CLI or accessing the `Views` object directly and iterating over the values:

```php
Grav::instance()['views']->getAll(null, 20, 'desc')
```

## CLI Commands

There are currently two built in commands:

`ls` will list all the views currently tracked.
#### List (ls) Command

```bash
bin/plugin views ls
Expand All @@ -82,8 +104,34 @@ This will return the information for a specific `slug`
bin/plugin views ls --limit 10 --sort asc
```

This will return the information for a specific `type`

```bash
bin/plugin views ls --type widgets --limit 10 --sort asc
```

the `limit`, will limit the amount of rows returned, and the `sort` will can be either `asc` or `desc`

#### Set Command

The set command allows you to set a specific value for an entry:

```bash
bin/plugin views set /bar 17
```

Will set the number of views with id '/bar' to the value `17` and defaults to type `pages`

or

```bash
bin/plugin views set /bar 17 foos
```

Will set the number of views with id '/bar' to the value `17` and defaults to type `foos`






Expand Down
2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Views
version: 1.0.0
version: 1.0.1
description: Simple View tracking and reporting
icon: eye
author:
Expand Down
48 changes: 36 additions & 12 deletions classes/Views.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,73 +36,86 @@ public function __construct($config)
}
}

public function track($id, $amount = 1)
public function track($id, $type = 'pages', $amount = 1)
{
// Support SQLite < 3.24
if (!$this->supportOnConflict()) {
$query = "UPDATE {$this->table_total_views} SET count = count + :amount WHERE id = :id";
$query = "UPDATE {$this->table_total_views} SET count = count + :amount, type = :type WHERE id = :id";

$statement = $this->db->prepare($query);
$statement->bindValue(':id', $id, PDO::PARAM_STR);
$statement->bindValue(':amount', $amount, PDO::PARAM_INT);
$statement->bindValue(':type', $type, PDO::PARAM_STR);
$statement->execute();

if ($statement->rowCount() === 0) {
$query = "INSERT INTO {$this->table_total_views} (id, count) VALUES (:id, :amount)";
$query = "INSERT INTO {$this->table_total_views} (id, count, type) VALUES (:id, :amount, :type)";

$statement = $this->db->prepare($query);
$statement->bindValue(':id', $id, PDO::PARAM_STR);
$statement->bindValue(':amount', $amount, PDO::PARAM_INT);
$statement->bindValue(':type', $type, PDO::PARAM_STR);
$statement->execute();
}

return;
}

$query = "INSERT INTO {$this->table_total_views} (id, count) VALUES (:id, :amount) ON CONFLICT(id) DO UPDATE SET count = count + :amount";
$query = "INSERT INTO {$this->table_total_views} (id, count, type) VALUES (:id, :amount, :type) ON CONFLICT(id) DO UPDATE SET count = count + :amount";

$statement = $this->db->prepare($query);
$statement->bindValue(':id', $id, PDO::PARAM_STR);
$statement->bindValue(':amount', $amount, PDO::PARAM_INT);
$statement->bindValue(':type', $type, PDO::PARAM_STR);
$statement->execute();
}

public function set($id, $amount = 0)
public function set($id, $type = 'pages', $amount = 0)
{
// Support SQLite < 3.24
if (!$this->supportOnConflict()) {
$query = "UPDATE {$this->table_total_views} SET count = :amount WHERE id = :id";
$query = "UPDATE {$this->table_total_views} SET count = :amount, type = :type WHERE id = :id";

$statement = $this->db->prepare($query);
$statement->bindValue(':id', $id, PDO::PARAM_STR);
$statement->bindValue(':amount', $amount, PDO::PARAM_INT);
$statement->bindValue(':type', $type, PDO::PARAM_STR);
$statement->execute();

if ($statement->rowCount() === 0) {
$query = "INSERT INTO {$this->table_total_views} (id, count) VALUES (:id, :amount)";
$query = "INSERT INTO {$this->table_total_views} (id, count, type) VALUES (:id, :amount, :type)";

$statement = $this->db->prepare($query);
$statement->bindValue(':id', $id, PDO::PARAM_STR);
$statement->bindValue(':amount', $amount, PDO::PARAM_INT);
$statement->bindValue(':type', $type, PDO::PARAM_STR);
$statement->execute();
}

return;
}

$query = "INSERT INTO {$this->table_total_views} (id, count) VALUES (:id, :amount) ON CONFLICT(id) DO UPDATE SET count = :amount";
$query = "INSERT INTO {$this->table_total_views} (id, count, type) VALUES (:id, :amount, :type) ON CONFLICT(id) DO UPDATE SET count = :amount";

$statement = $this->db->prepare($query);
$statement->bindValue(':id', $id, PDO::PARAM_STR);
$statement->bindValue(':amount', $amount, PDO::PARAM_INT);
$statement->bindValue(':type', $type, PDO::PARAM_STR);
$statement->execute();
}

public function get($id)
public function get($id, $type = null)
{
$query = "SELECT count FROM {$this->table_total_views} WHERE id = :id";

if (!is_null($type)) {
$query .= ' AND type = :type';
}

$statement = $this->db->prepare($query);
if (!is_null($type)) {
$statement->bindValue(':type', $type, PDO::PARAM_STR);
}
$statement->bindValue(':id', $id, PDO::PARAM_STR);
$statement->execute();

Expand All @@ -111,14 +124,25 @@ public function get($id)
return $results['count'] ?? 0;
}

public function getAll($limit = 0, $order = 'ASC')
public function getAll($type = null, $limit = 0, $order = 'ASC')
{
$order = strtoupper($order) === 'ASC' ? 'ASC' : 'DESC';
$offset = 0;

$query = "SELECT id, count FROM {$this->table_total_views} ORDER BY count {$order} LIMIT :limit OFFSET :offset";
$query = "SELECT id, count, type FROM {$this->table_total_views} ";

if (!is_null($type)) {
$query .= "WHERE type = :type ";
}

$query .= "ORDER BY count {$order}, type LIMIT :limit OFFSET :offset";


$statement = $this->db->prepare($query);

if (!is_null($type)) {
$statement->bindValue(':type', $type, PDO::PARAM_STR);
}
$statement->bindValue(':limit', $limit, PDO::PARAM_INT);
$statement->bindValue(':offset', $offset, PDO::PARAM_INT);
$statement->execute();
Expand All @@ -129,7 +153,7 @@ public function getAll($limit = 0, $order = 'ASC')
public function createTables()
{
$commands = [
"CREATE TABLE IF NOT EXISTS {$this->table_total_views} (id VARCHAR(255) PRIMARY KEY, count INTEGER DEFAULT 0)",
"CREATE TABLE IF NOT EXISTS {$this->table_total_views} (id VARCHAR(255) PRIMARY KEY, count INTEGER DEFAULT 0, type VARCHAR(255))",
];

// execute the sql commands to create new tables
Expand Down
26 changes: 17 additions & 9 deletions cli/LsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,32 @@ protected function configure()
->addArgument(
'slug',
InputArgument::OPTIONAL,
'The page slug',
'The page slug or unique ID',
''
)
->addOption(
'type',
't',
InputOption::VALUE_OPTIONAL,
'Limit the type of views',
null
)
->addOption(
'limit',
'l',
InputOption::VALUE_OPTIONAL,
'Limit the list of page views',
'Limit the list of views',
10
)
->addOption(
'sort',
's',
InputOption::VALUE_OPTIONAL,
'Sort the list of page views (desc / asc)',
'Sort the list of views (desc / asc)',
'desc'
)
->setDescription('List the page views count')
->setHelp('The <info>list</info> command displays the page views count')
->setDescription('List the views count')
->setHelp('The <info>list</info> command displays the views count')
;
}

Expand All @@ -77,20 +84,21 @@ protected function serve()
$slug = $this->input->getArgument('slug');
$limit = $this->input->getOption('limit');
$sort = $this->input->getOption('sort');
$type = $this->input->getOption('type');

$views = $grav['views'];

$table = new Table($this->output);
$table->setStyle('box');
$table->setHeaders(['Slug', 'Views Count']);
$table->setHeaders(['Slug', 'Type', 'Views Count']);
$rows = [];

if ($slug) {
$rows[] = [$slug, $views->get($slug)];
$rows[] = [$slug, $type, $views->get($slug, $type)];
} else {
$total = $views->getAll($limit, $sort);
$total = $views->getAll($type, $limit, $sort);
foreach ($total as $view) {
$rows[] = [$view['id'], $view['count']];
$rows[] = [$view['id'], $view['type'], $view['count']];
}
}

Expand Down
19 changes: 13 additions & 6 deletions cli/SetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,21 @@ protected function configure()
->addArgument(
'slug',
InputArgument::REQUIRED,
'The page slug'
'The page slug or unique ID'
)
->addArgument(
'count',
InputArgument::REQUIRED,
'The page views count'
'The views count'
)
->setDescription('Set the views count for a page')
->setHelp('The <info>set</info> command allow to manually set a page views count')
->addArgument(
'type',
InputArgument::OPTIONAL,
'The view type',
'pages'

)
->setHelp('Set the views count for a anything, although it tracks page views by default')
;
}

Expand All @@ -64,13 +70,14 @@ protected function serve()

$slug = $this->input->getArgument('slug');
$count = $this->input->getArgument('count');
$type = $this->input->getArgument('type');

$views = $grav['views'];

$views->set($slug, $count);
$views->set($slug, $type, $count);

$io->title('Set Page View Count');
$io->text('<green>'. $slug . '</green> page view updated to <cyan>' . $count . '</cyan>');
$io->text('<green>'. $slug . '</green> ' . $type . ' view updated to <cyan>' . $count . '</cyan>');
$io->newLine();
}
}
6 changes: 5 additions & 1 deletion templates/reports/views-report.html.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<table class="views-list">
<thead>
<tr>
<th>Slug</th>
<th>ID</th>
<th>Type</th>
<th>Views Count</th>
</tr>
</thead>
Expand All @@ -11,6 +12,9 @@
<td>
<a href="{{ url(view.id) }}" target="_blank">{{ view.id }}</a>
</td>
<td>
<span>{{ view.type }}</span>
</td>
<td>
<span class="badge">{{ view.count }}</span>
</td>
Expand Down
Loading

0 comments on commit e8d1d62

Please sign in to comment.