App framework: How to save value from app frontend to systemValue


#1

Hello!

I am using owncloud 10 and want to develop an app with the app framework that uses hooks to send data to a backend service. I want the url of this backend service to be editable in a settings page.
My question is: how do i save this value properly?
Do i have to use OCP\Config::setSystemValue() to save the value?
If yes, how do i this? Do i have to write a javascript function?

This is the main.php:

<h1>Configure the settings of the connector here</h1>
<br>
<div>
    IP-Address of backend service: <input type="text" name="ip" id="ip_address_field"><br>
    Port number of backend service: <input type="number" id="port_field" name="port"><br>
    <button name="save" onclick="sendValues()">Click me!</button>
</div>

This is the PageController.php

class PageController extends Controller
{
    private $logger;

    public function __construct($AppName, IRequest $request, ILogger $logger)
    {
        parent::__construct($AppName, $request);
        $this->logger = $logger;
    }

    /**
     * @NoAdminRequired
     * @NoCSRFRequired
     */
    public function index()
    {
        //returns templates/main.php
        return new TemplateResponse('my-owncloud-app', 'main');
    }

}

This is the JavaScript file:

$(document).ready(function () {
    function sendValues() {
        const ip = document.getElementById('ip_address_field').innerText
        const port = document.getElementById('port_field').innerText
        console.debug(ip)
    }
});

What do I have to do in the js file? Do i have to call another php file and pass the values or can i save the values right here?

Is this even the right approach to save values like this? Or is there a different way to do this?

Many Thanks

Fabian


#2

Hi, I suggest you to use OCP\IConfig interface to save your app specific value in the backend as appconfig instead of system config. Here is an example usage of this interface: https://github.com/owncloud/brute_force_protection/blob/master/lib/BruteForceProtectionConfig.php#L27 . Also, you can directly use appconfigs from javascript. See: https://github.com/owncloud/brute_force_protection/blob/master/js/settings-admin.js#L33 . Good luck.


#3

Thank you very much!
I will look into it and reply if it works or not :slight_smile:


#4

You are welcome :slight_smile:


#5

I’m not sure if i understood the OCP\IConfig interface correctly. Saving the value via JavaScript worked, but now i want to get the value with php.

This is Application.php

class Application extends App
    {

    public function __construct(array $urlParams = array())
    {
        parent::__construct('my-owncloud-app', $urlParams);

        $container = $this->getContainer();

        /**
         * Controllers
         */

        $container->registerService('FileSystem', function ($c) {
            return new FileSystem(
                $c->query('ServerContainer')->getRootFolder(),
                $c->query('Logger'),
                $c->query('Config')
            );
        });

        $container->registerService('Logger', function ($c) {
            return $c->query('ServerContainer')->getLogger();
        });

        $container->registerService('PageController', function ($c) {
            return new PageController(
                $c->query('AppName'),
                $c->query('Request'),
                $c->query('Logger')
            );
        });

    }
}

where i register my services. I want to use the IConfig in FileSystem, which looks like this

use OCP\IConfig;

class FileSystem
{
    private $fileManager;
    private $logger;
    private $config;

    public function __construct($fileManager, ILogger $logger, IConfig $config)
    {
        $this->fileManager = $fileManager;
        $this->logger = $logger;
	    $this->config = $config;
    }

    public function register()
    {
        $callbackDelete = function ($node) {
            $dataJson = $this->generateBody($node, 'delete');
            $this->sendPostRequest($dataJson);
	    $this->config->getAppValue('my-owncloud-app', 'ip_address', 'localhost')
        };

        $callbackTouch = function ($node) {
            $dataJson = $this->generateBody($node, 'touch');
            $this->sendPostRequest($dataJson);
        };

        $callbackWrite = function ($node) {
            $dataJson = $this->generateBody($node, 'write');
            $this->sendPostRequest($dataJson);
        };

        $callbackCreate = function ($node) {
            $dataJson = $this->generateBody($node, 'create');
            $this->sendPostRequest($dataJson);
        };

        $this->fileManager->listen('\OC\Files', 'postDelete', $callbackDelete);
        $this->fileManager->listen('\OC\Files', 'postTouch', $callbackTouch);
        $this->fileManager->listen('\OC\Files', 'postWrite', $callbackWrite);
        $this->fileManager->listen('\OC\Files', 'postCreate', $callbackCreate);
    }

}

I get the following error message: Could not resolve Config! Class Config does not exist.

How do i implement the IConfig interface correctly?


#6

Try this:

$c->query(‘ServerContainer’)->getConfig()


#7

As far as I see, all the classes that you want to use already in the DIContainer. They are predefined core services. The framework can resolve these dependencies auto-magically. You can find the documentation in here : https://doc.owncloud.org/server/10.0/developer_manual/app/fundamentals/container.html#use-automatic-dependency-assembly-recommended . @alfredb’s solution should work also.