<?php
/**
 * Pi Engine (http://piengine.org)
 *
 * @link            http://code.piengine.org for the Pi Engine source repository
 * @copyright       Copyright (c) Pi Engine http://piengine.org
 * @license         http://piengine.org/license.txt BSD 3-Clause License
 */

namespace Module\Demo\Installer\Action;

use Pi;
use Pi\Application\Installer\Action\Install as BasicInstall;
use Laminas\EventManager\Event;

class Install extends BasicInstall
{
    protected function attachDefaultListeners()
    {
        $events = $this->events;
        $events->attach('install.pre', [$this, 'preInstall'], 1000);
        $events->attach('install.post', [$this, 'postInstall'], 1);
        parent::attachDefaultListeners();

        return $this;
    }

    public function preInstall(Event $e)
    {
        $this->setResult('pre-install', [
            'status'  => true,
            'message' => sprintf('Called from %s', __METHOD__),
        ]);
    }

    public function postInstall(Event $e)
    {
        $model = Pi::model($this->module . '/test');
        $data  = [
            'message' => sprintf(
                __('The module is installed on %s'),
                date('Y-m-d H:i:s')
            ),
        ];
        $model->insert($data);

        $model = Pi::model($this->module . '/page');
        for ($page = 1; $page <= 1000; $page++) {
            $model->insert([
                'uid'          => rand(1, 5),
                'time_created' => time() - rand(100, 10000000),
                'title'        => sprintf('Page #%d', $page),
                'content'      => sprintf('Demo content of page #%d', $page),
                'flag'         => rand(0, 1),
            ]);
        }

        $this->setResult('post-install', [
            'status'  => true,
            'message' => sprintf('Called from %s', __METHOD__),
        ]);
    }
}
