# ngauge-datadash

Yii2 module for NGauge that provides data-feed endpoints plus a configurable multi-connection external database UI and model-generation workflow.

## Features

- Existing data-feed endpoints for uploads, order downloads, CSV exports, and tour-operator feeds.
- Multi-connection admin UI at `/ngauge-datadash/config/index` for MySQL, PostgreSQL, SQL Server, and SQLite sources.
- Per-connection test, table listing, create, update, delete, and model-generation actions.
- Automatic Yii component bootstrap for each saved connection as `datadash_{name}`.
- Gii-backed ActiveRecord generation from external tables into a chosen namespace.

## Requirements

| Dependency | Version |
|---|---|
| PHP | ≥ 8.0 |
| yiisoft/yii2 | ~2.0.45 |
| ext-pdo | * |
| ext-json | * |
| ext-mbstring | * |

## Installation

Add to your application's `composer.json`:

```json
"portalpeople/ngauge-datadash": "^1.2"
```

Then register the bootstrap in your application config:

```php
'bootstrap' => ['ngauge-datadash'],
'modules' => [
    'ngauge-datadash' => [
        'class' => PortalPeople\datadash\Module::class,
    ],
],
```

## Configuration

### Via the web UI

Navigate to `/ngauge-datadash/config/index`.

Each saved connection stores:

| Field | Description |
|---|---|
| `name` | Unique connection key used in the generated component id |
| `driver` | `mysql`, `pgsql`, `sqlsrv`, or `sqlite` |
| `host` / `port` | Remote database connection details |
| `dbname` | Database/schema name |
| `charset` | Charset for supported drivers |
| `username` / `password` | Credentials, with optional encryption at rest |
| `persistent` | Whether to use persistent PDO connections |

The Generate Models screen lists available tables for the selected connection and sends them through the Chalvington Gii model template.

### Via params-local.php

```php
return [
    'plugins' => [
        'ngauge-datadash' => [
            'connections' => [
                'reporting' => [
                    'driver' => 'mysql',
                    'host' => 'db.example.com',
                    'port' => 3306,
                    'dbname' => 'reporting',
                    'charset' => 'utf8mb4',
                    'username' => 'report_user',
                    'password' => 'encrypted-or-plain-password',
                    'encryptAtRest' => true,
                    'persistent' => false,
                ],
            ],
        ],
    ],
];
```

## Bootstrapped DB Components

Each saved connection is registered automatically using the `datadash_{name}` pattern:

```php
$db = Yii::$app->get('datadash_reporting');
$rows = $db->createCommand('SELECT * FROM my_table')->queryAll();
```

## Routes

### Config UI

| Method | URL | Action |
|---|---|---|
| GET | `/ngauge-datadash/config/index` | List configured connections |
| GET / POST | `/ngauge-datadash/config/create` | Create a connection |
| GET / POST | `/ngauge-datadash/config/update?name=...` | Edit a connection |
| POST | `/ngauge-datadash/config/delete` | Delete a connection |
| GET | `/ngauge-datadash/config/generate?name=...` | Choose tables for model generation |
| POST | `/ngauge-datadash/config/do-generate` | Generate models |
| POST | `/ngauge-datadash/config/test-connection` | JSON connection test |
| POST | `/ngauge-datadash/config/list-tables` | JSON table inventory |

### Data-feed endpoints

| Method | URL | Description |
|---|---|---|
| POST | `/df/data/upload` | Accept compressed data file upload |
| GET | `/df/data/downloadorders` | Un-downloaded Paid/Ordered orders |
| GET | `/df/data/downloadpaidorders` | Paid orders download |
| GET | `/df/data/downloadcsv` | Orders as CSV |
| GET | `/df/data/downloadpaidcsv` | Paid orders as CSV |
| GET | `/df/data/downloadcsvgroup` | Group CSV download |
| POST | `/df/data/downloaded` | Mark orders as downloaded |
| GET | `/df/data-services/slt` | Tour Operator SLT feed |
| GET | `/df/data-services/soh` | Tour Operator SOH feed |
| GET | `/df/data-services/sod` | Tour Operator SOD feed |
| GET | `/df/data-services/sta` | Tour Operator STA feed |
| GET | `/df/data-services/sla` | Tour Operator SLA feed |

## Changelog

See [CHANGELOG.md](CHANGELOG.md).
