<?php

/**
 * @package     Dotclear
 *
 * @copyright   Olivier Meunier & Association Dotclear
 * @copyright   AGPL-3.0
 */
declare(strict_types=1);

namespace Dotclear\Plugin\widgets;

use Dotclear\App;
use Dotclear\Helper\Process\TraitProcess;
use Exception;

/**
 * @brief   The module install process.
 * @ingroup widgets
 */
class Install
{
    use TraitProcess;

    public static function init(): bool
    {
        return self::status(My::checkContext(My::INSTALL));
    }

    public static function process(): bool
    {
        if (!self::status()) {
            return false;
        }

        if (class_exists(__NAMESPACE__ . '\Widgets')) {
            Widgets::init();
        } else {
            throw new Exception(__('Unable to initialize default widgets.'));
        }

        $s = My::settings();
        if ($s->widgets_nav != null) {
            $s->put('widgets_nav', WidgetsStack::load($s->widgets_nav)->store());
        } else {
            $s->put('widgets_nav', '', App::blogWorkspace()::NS_STRING, 'Navigation widgets', false);
        }
        if ($s->widgets_extra != null) {
            $s->put('widgets_extra', WidgetsStack::load($s->widgets_extra)->store());
        } else {
            $s->put('widgets_extra', '', App::blogWorkspace()::NS_STRING, 'Extra widgets', false);
        }
        if ($s->widgets_custom != null) {
            $s->put('widgets_custom', WidgetsStack::load($s->widgets_custom)->store());
        } else {
            $s->put('widgets_custom', '', App::blogWorkspace()::NS_STRING, 'Custom widgets', false);
        }

        return true;
    }
}
