Initial import of space_profiles module

This commit is contained in:
Kelin Rescue Hub
2026-04-04 13:11:50 -04:00
commit 87a59e5a0a
35 changed files with 1627 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
<?php
use humhub\components\Migration;
class m260401_130000_initial extends Migration
{
public function safeUp()
{
$this->safeCreateTable('rescue_space_profile', [
'id' => $this->primaryKey(),
'contentcontainer_id' => $this->integer()->notNull(),
'rescue_name' => $this->string(190)->notNull(),
'address' => $this->string(255)->notNull(),
'city' => $this->string(120)->notNull(),
'state' => $this->string(2)->notNull(),
'zip' => $this->string(10)->notNull(),
'email' => $this->string(190)->notNull(),
'phone' => $this->string(32)->notNull(),
'animals_we_accept' => $this->text()->notNull(),
'description' => $this->text()->notNull(),
'mission_statement' => $this->text()->notNull(),
'template_key' => $this->string(64)->defaultValue('rescue_center')->notNull(),
'header_html' => $this->text()->null(),
'body_html' => $this->text()->null(),
'footer_html' => $this->text()->null(),
'icon_path' => $this->string(255)->null(),
'background_image_path' => $this->string(255)->null(),
'created_at' => $this->dateTime()->null(),
'updated_at' => $this->dateTime()->null(),
]);
$this->safeCreateIndex('idx_rescue_space_profile_container', 'rescue_space_profile', 'contentcontainer_id', true);
$this->safeAddForeignKey(
'fk_rescue_space_profile_container',
'rescue_space_profile',
'contentcontainer_id',
'contentcontainer',
'id',
'CASCADE'
);
$this->seedFieldMetadata();
}
public function safeDown()
{
$this->safeDropTable('rescue_space_profile');
}
private function seedFieldMetadata(): void
{
if ($this->db->getSchema()->getTableSchema('rescue_field_definition', true) === null) {
return;
}
$createdAt = date('Y-m-d H:i:s');
$rows = [
['rescue_name', 'Rescue name', 'text', 1, 1, 110],
['address', 'Address', 'text', 1, 1, 120],
['city', 'City', 'text', 1, 1, 130],
['state', 'State', 'text', 1, 1, 140],
['zip', 'ZIP', 'text', 1, 1, 150],
['email', 'Email', 'email', 1, 1, 160],
['phone', 'Phone', 'text', 1, 1, 170],
['animals_we_accept', 'Animals we accept', 'textarea', 1, 1, 180],
['description', 'Description', 'textarea', 1, 1, 190],
['mission_statement', 'Mission statement', 'textarea', 1, 1, 200],
['template_key', 'Template', 'select', 1, 1, 205],
['header_html', 'Header HTML', 'html', 0, 1, 210],
['body_html', 'Body HTML', 'html', 0, 1, 220],
['footer_html', 'Footer HTML', 'html', 0, 1, 230],
['icon_path', 'Icon', 'image', 0, 1, 240],
['background_image_path', 'Background image', 'image', 0, 1, 250],
];
foreach ($rows as $row) {
[$fieldKey, $label, $inputType, $required, $isCore, $sortOrder] = $row;
$exists = (new \yii\db\Query())
->from('rescue_field_definition')
->where(['module_id' => 'space_profiles', 'field_key' => $fieldKey])
->exists($this->db);
if ($exists) {
continue;
}
$this->insert('rescue_field_definition', [
'module_id' => 'space_profiles',
'group_key' => 'space_profile',
'field_key' => $fieldKey,
'label' => $label,
'input_type' => $inputType,
'required' => $required,
'is_core' => $isCore,
'is_active' => 1,
'visibility' => 'public',
'options' => '{}',
'sort_order' => $sortOrder,
'created_at' => $createdAt,
'updated_at' => $createdAt,
]);
}
}
}

View File

@@ -0,0 +1,26 @@
<?php
use humhub\components\Migration;
class m260401_141000_add_template_key extends Migration
{
public function safeUp()
{
$schema = $this->db->getSchema()->getTableSchema('rescue_space_profile', true);
if ($schema === null) {
return;
}
if (!isset($schema->columns['template_key'])) {
$this->addColumn('rescue_space_profile', 'template_key', $this->string(64)->defaultValue('rescue_center')->notNull());
}
}
public function safeDown()
{
$schema = $this->db->getSchema()->getTableSchema('rescue_space_profile', true);
if ($schema !== null && isset($schema->columns['template_key'])) {
$this->dropColumn('rescue_space_profile', 'template_key');
}
}
}

View File

@@ -0,0 +1,92 @@
<?php
use humhub\components\Migration;
class m260401_200000_add_slug extends Migration
{
public function safeUp()
{
$schema = $this->db->getSchema()->getTableSchema('rescue_space_profile', true);
if ($schema === null) {
return;
}
if (!isset($schema->columns['slug'])) {
$this->addColumn('rescue_space_profile', 'slug', $this->string(190)->null()->after('mission_statement'));
}
$rows = (new \yii\db\Query())
->select([
'id' => 'rsp.id',
'rescue_name' => 'rsp.rescue_name',
'space_name' => 's.name',
])
->from(['rsp' => 'rescue_space_profile'])
->leftJoin(['cc' => 'contentcontainer'], 'cc.id = rsp.contentcontainer_id')
->leftJoin(['s' => 'space'], 's.contentcontainer_id = cc.id')
->orderBy(['rsp.id' => SORT_ASC])
->all($this->db);
foreach ($rows as $row) {
$source = trim((string)($row['space_name'] ?? ''));
if ($source === '') {
$source = trim((string)($row['rescue_name'] ?? ''));
}
$slug = $this->createUniqueSlug($source, (int)$row['id']);
$this->update('rescue_space_profile', ['slug' => $slug], ['id' => (int)$row['id']]);
}
$this->alterColumn('rescue_space_profile', 'slug', $this->string(190)->notNull());
$this->safeCreateIndex('ux_rescue_space_profile_slug', 'rescue_space_profile', 'slug', true);
}
public function safeDown()
{
$schema = $this->db->getSchema()->getTableSchema('rescue_space_profile', true);
if ($schema === null || !isset($schema->columns['slug'])) {
return;
}
$this->safeDropIndex('ux_rescue_space_profile_slug', 'rescue_space_profile');
$this->dropColumn('rescue_space_profile', 'slug');
}
private function createUniqueSlug(string $source, int $rowId): string
{
$base = $this->normalizeToSlug($source);
$candidate = $base;
$counter = 1;
while ($this->slugExists($candidate, $rowId)) {
$candidate = $base . '-' . $counter;
$counter++;
}
return $candidate;
}
private function normalizeToSlug(string $value): string
{
$normalized = trim($value);
$ascii = @iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $normalized);
if ($ascii !== false) {
$normalized = $ascii;
}
$slug = strtolower($normalized);
$slug = preg_replace('/[^a-z0-9]+/', '-', $slug) ?? '';
$slug = trim($slug, '-');
return $slug !== '' ? $slug : 'rescue';
}
private function slugExists(string $slug, int $rowId): bool
{
return (new \yii\db\Query())
->from('rescue_space_profile')
->where(['slug' => $slug])
->andWhere(['!=', 'id', $rowId])
->exists($this->db);
}
}

View File

@@ -0,0 +1,64 @@
<?php
use humhub\components\Migration;
class m260401_201000_set_rescues_default_home extends Migration
{
public function safeUp()
{
$rows = (new \yii\db\Query())
->select(['contentcontainer_id', 'url', 'guid'])
->from('space')
->where(['in', 'contentcontainer_id', (new \yii\db\Query())
->select('contentcontainer_id')
->from('rescue_space_profile')])
->all($this->db);
foreach ($rows as $row) {
$spacePath = trim((string)($row['url'] ?? ''));
if ($spacePath === '') {
$spacePath = trim((string)($row['guid'] ?? ''));
}
if ($spacePath === '') {
continue;
}
$homeUrl = '/s/' . $spacePath . '/rescues';
$contentContainerId = (int)$row['contentcontainer_id'];
$this->upsert('contentcontainer_setting', [
'module_id' => 'space',
'contentcontainer_id' => $contentContainerId,
'name' => 'indexUrl',
'value' => $homeUrl,
], [
'value' => $homeUrl,
]);
$this->upsert('contentcontainer_setting', [
'module_id' => 'space',
'contentcontainer_id' => $contentContainerId,
'name' => 'indexGuestUrl',
'value' => $homeUrl,
], [
'value' => $homeUrl,
]);
}
}
public function safeDown()
{
$this->delete('contentcontainer_setting', [
'and',
['module_id' => 'space', 'name' => 'indexUrl'],
['like', 'value', '/s/%/rescues', false],
]);
$this->delete('contentcontainer_setting', [
'and',
['module_id' => 'space', 'name' => 'indexGuestUrl'],
['like', 'value', '/s/%/rescues', false],
]);
}
}