Google Drive addon

Hi there!
I’m new to Cockpit, and currently setting up an addon to “integrate” Google Drive (dashboard widget / drive picker + services like uploading/retrieving files to/from google drive, generating/updating accouting Sheets; generating Google Docs from templates --like invoices…).

For now, I have two silly questions:

1/ how in all heavens do you make your additionnal assets (css and js) load and work?
I’ve looked up how other addons did it; mimicked them; but for an unknown reason it’s not working on my side. I’m missing something obviously.

bootstrap.php:

$app->path('googledrive', 'addons/GoogleDrive/');
$this->module("cockpit")->extend([]);
include_once(__DIR__ . '/admin.php');

in admin.php:

require_once(__DIR__ . '/Controller/googleservices.php');

$app->on('admin.init', function() {}, 0);

$app->on("admin.dashboard.widgets", function($widgets) {

    if ($this->module('cockpit')->hasaccess('cockpit', 'GoogleDrive')) {
        $this->helper('admin')->addAssets('googledrive:assets/js/googledrive.js');
        $this->helper('admin')->addAssets('googledrive:assets/css/googledrive.css');
        $title = 'GoogleDrive yo';
        $gclient = new GoogleServices();
        $gat = $gclient->getgAccessToken();

        $widgets[] = [
            "name" => 'GoogleDrive',
            "content" => $this->view("googledrive:views/partials/widget.php", compact('title','gat')),
            "area" => 'main'
        ];
    }
}, 100);

$app('admin')->init();

I’ve tried direct url; I’ve tried adding @route… nothing works. :face_with_monocle:

(I actually struggle quite a lot with routes; I wanted to make a proper “cockpit” class with public function index() but loading the Google Api from there would fail (bad route); so for now it’s just a good old __construct()…)

2/ How do you access data contained in a specific singleton? Do you make an… api call?
Reasons: I would like to retrieve the google .json credentials file (service account) from a “user config” singleton. I would prefer to avoid the user to manually edit the config.yaml.

Thanks! :pray:

(I will obviously share the addon once done!)

Hello @VirHeroicus,

You don’t need that line. Paths for addons/modules follow a naming pattern. Let’s assume the following folder structure:

.
├── addons/
│   └── GoogleDriveConnector/
│       ├── assets/
│       |   ├── style.css
│       |   └── script.js
│       ├── Controller/
│       |   └── Admin.php
│       ├── Helper/
│       ├── views/
│       |   └── index.php
│       ├── bootstrap.php
│       └── admin.php

For $app->path(), the folder name is case insensitive. For Controllers and Helpers (classes), proper casing is mandatory.

$path = $app->path('googledriveconnector:views/index.php');
# /var/www/virtual/user/html/addons/GoogleDriveConnector/views/index.php

$path = $app->pathToUrl('googledriveconnector:views/index.php');
# /addons/GoogleDriveConnector/views/index.php

The Admin controller can be autoloaded.

Controller/Admin.php:

<?php

// must match the folder name!
namespace GoogleDriveConnector\Controller;

class Admin extends \Cockpit\AuthController {

    public $data = [
        'don\'t be evil' => false,
        'GDPR-compliant' => false,
        'spyware'        => 'kind of',
        'alternatives' => [
            'Drive' => ['Nextcloud'],
            'Docs'  => ['Etherpad', 'OnlyOffice'],
        ],
    ]

    public function index() {

        return $this->render('googledriveconnector:views/index.php', ['data' => $this->data]);

    }

    public function getData() {

        return $this->data;

    }

}

admin.php:

<?php

$this->on('admin.init', function() {

    // add assets
    $this->helper('admin')->addAssets([
        'googledriveconnector:assets/style.css',
        'googledriveconnector:assets/script.js',
    ]);

    // bind admin routes
    // --> "/gdc" points to index()
    // --> "/gdc/getData" points to getData()
    $this->bindClass('GoogleDriveConnector\\Controller\\Admin', 'gdc');

});

see above

Code inside admin.php is only for backend access, so don’t load it if you don’t need it. The name has nothing to do with the “admin” group permission.

if (COCKPIT_ADMIN_CP) include_once(__DIR__ . '/admin.php');

Don’t start with widgets, if you want to learn Cockpit. Create pages and routes, call them directly in your browser to see the json responses. If your core logic works, think about widgets. Be aware, that widgets have a drag event by default.

$config = $app->module('singletons')->getData('name-of-singleton');

see: https://github.com/agentejo/cockpit/blob/next/modules/Singletons/bootstrap.php#L136

Further notes:

It looks like you used the Groups addon for inspiration. It isn’t very well structured (at least to get started). I think, this one is easier for understanding the folder and path structure:

More resources:

2 Likes

Thank you for your very extensive answer.

we are waiting first working version for testing.good to see people trying to improve…