<?php

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

namespace Dotclear\Plugin\pages;

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

/**
 * @brief   The module install process.
 * @ingroup pages
 */
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 (App::version()->getVersion(My::id()) === '') {
            // Create a first pending page, only on a new installation of this plugin
            $params = [
                'post_type'  => 'page',
                'no_content' => true,
            ];
            $counter = App::blog()->getPosts($params, true);

            if ($counter->cardinal() === 0 && My::settings()->get('firstpage') === null) {
                My::settings()->put('firstpage', true, App::blogWorkspace()::NS_BOOL);

                $cur = App::blog()->openPostCursor();
                $cur->setStrField('user_id', App::auth()->userID());
                $cur->setStrField('post_type', 'page');
                $cur->setStrField('post_format', 'xhtml');
                $cur->setStrField('post_lang', App::blog()->settings()->get('system')->getStr('lang'));
                $cur->setStrField('post_title', __('My first page'));
                $cur->setStrField('post_content', '<p>' . __('This is your first page. When you\'re ready to blog, log in to edit or delete it.') . '</p>');
                $cur->setStrField('post_content_xhtml', $cur->strField('post_content'));
                $cur->setStrField('post_excerpt', '');
                $cur->setStrField('post_excerpt_xhtml', $cur->strField('post_excerpt'));
                $cur->setIntField('post_status', App::status()->post()::PENDING);
                $cur->setBoolField('post_open_comment', false);
                $cur->setBoolField('post_open_tb', false);
                App::blog()->addPost($cur);
            }
        }

        return true;
    }
}
