activityReport/src/Install.php

87 lines
2.8 KiB
PHP
Raw Normal View History

<?php
2023-10-17 20:47:35 +00:00
declare(strict_types=1);
namespace Dotclear\Plugin\activityReport;
2023-10-17 20:47:35 +00:00
use Dotclear\App;
2023-08-17 14:25:19 +00:00
use Dotclear\Core\Process;
2023-04-23 08:31:49 +00:00
use Dotclear\Database\Structure;
use Dotclear\Database\Statement\{
DropStatement,
TruncateStatement
};
use Exception;
/**
2023-10-17 20:47:35 +00:00
* @brief activityReport install class.
* @ingroup activityReport
*
* @author Jean-Christian Denis (author)
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2023-08-17 14:25:19 +00:00
class Install extends Process
{
public static function init(): bool
{
2023-08-17 14:25:19 +00:00
return self::status(My::checkContext(My::INSTALL));
}
public static function process(): bool
{
2023-08-17 14:25:19 +00:00
if (!self::status()) {
return false;
}
try {
self::beforeGrowUp();
2022-11-18 20:24:30 +00:00
2023-10-17 20:47:35 +00:00
$s = new Structure(App::con(), App::con()->prefix());
2023-04-23 08:31:49 +00:00
$s->__get(My::ACTIVITY_TABLE_NAME)
->field('activity_id', 'bigint', 0, false)
->field('activity_type', 'varchar', 32, false, "'" . My::id() . "'")
->field('blog_id', 'varchar', 32, true)
->field('activity_group', 'varchar', 32, false)
->field('activity_action', 'varchar', 32, false)
->field('activity_logs', 'text', 0, false)
->field('activity_dt', 'timestamp', 0, false, 'now()')
->field('activity_status', 'smallint', 0, false, 0)
->primary('pk_activity', 'activity_id')
->index('idx_activity_type', 'btree', 'activity_type')
->index('idx_activity_blog_id', 'btree', 'blog_id')
->index('idx_activity_action', 'btree', 'activity_group', 'activity_action')
->index('idx_activity_status', 'btree', 'activity_status');
2023-10-17 20:47:35 +00:00
(new Structure(App::con(), App::con()->prefix()))->synchronize($s);
return true;
} catch (Exception $e) {
2023-10-17 20:47:35 +00:00
App::error()->add($e->getMessage());
return false;
}
}
/**
* Do some action on previous version before install.
*/
private static function beforeGrowUp(): void
{
// sorry not sorry we restart from scratch
2023-10-17 20:47:35 +00:00
if (is_string(App::version()->getVersion('activityReport'))
&& version_compare(App::version()->getVersion('activityReport'), '3.0', '<')
2023-05-11 22:37:45 +00:00
) {
2023-10-17 20:47:35 +00:00
$struct = new Structure(App::con(), App::con()->prefix());
if ($struct->tableExists('activity')) {
2023-10-17 20:47:35 +00:00
(new TruncateStatement())->from(App::con()->prefix() . 'activity')->truncate();
}
if ($struct->tableExists('activity_settings')) {
2023-10-17 20:47:35 +00:00
(new TruncateStatement())->from(App::con()->prefix() . 'activity_settings')->truncate();
(new DropStatement())->from(App::con()->prefix() . 'activity_settings')->drop();
}
}
}
}