<?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\System\Installer\Schema;

use Pi;
use Pi\Application\Installer\Schema\AbstractUpdator;

/**
 * System schema update handler
 *
 * @author Taiwen Jiang <taiwenjiang@tsinghua.org.cn>
 */
class Updator330 extends AbstractUpdator
{
    /**
     * Update system table schema
     *
     * @param string $version
     *
     * @return bool
     */
    public function upgrade($version)
    {
        if (version_compare($version, '3.2.5', '<')) {
            $updator = new Updator325($this->handler);
            $result  = $updator->upgrade($version);
            if (false === $result) {
                return $result;
            }
        }
        $result = $this->from325($version);

        return $result;
    }

    /**
     * Upgrade from previous version
     *
     * @param string $version
     *
     * @return bool
     */
    protected function from325($version)
    {
        $status = true;
        if (version_compare($version, '3.3.0', '<')) {
            $table  = Pi::db()->prefix('block');
            $sql
                    = <<<'EOT'
ALTER TABLE %s
ADD `body_fullsize` tinyint(1) unsigned NOT NULL default '0' AFTER `title_hidden`;
EOT;
            $sql    = sprintf($sql, $table);
            $status = $this->queryTable($sql);

            if (false === $status) {
                return $status;
            }
        }

        return $status;
    }
}
