Skip to content

Commit

Permalink
store tags as json
Browse files Browse the repository at this point in the history
  • Loading branch information
pjc09h committed May 22, 2024
1 parent 356b0b7 commit 74eb58e
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 30 deletions.
18 changes: 17 additions & 1 deletion app/Models/Collages.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Collages extends ObjectCrud
"userId" => "userId",
"title" => "title",
"description" => "description",
"tags" => "tags",
"tags" => "tags", # json
"torrentCount" => "torrentCount",
"subscriberCount" => "subscriberCount",
"maximumGroups" => "maximumGroups",
Expand All @@ -51,6 +51,22 @@ class Collages extends ObjectCrud
/** crud */


/**
* read
*
* @param int|string $identifier
* @return void
*/
public function read(int|string $identifier = null): void
{
# parent method
parent::read($identifier);

# decode the json fields
$this->attributes->tags = json_decode($this->attributes->tags ?? []);
}


/**
* delete
*
Expand Down
70 changes: 63 additions & 7 deletions app/Models/TorrentGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ class TorrentGroups extends ObjectCrud
# ["database" => "display"]
protected array $maps = [
"id" => "id",
"category_id" => "categoryId",
"categoryId" => "categoryId",
"revisionId" => "revisionId",
"identifier" => "identifier",
"title" => "title",
"subject" => "subject",
"object" => "object",
"year" => "year",
"workgroup" => "workgroup",
"location" => "location",
"identifier" => "identifier",
"tag_list" => "tags",
"revision_id" => "revisionId",
"year" => "year",
"description" => "description",
"picture" => "picture",
"tags" => "tags", # json
"created_at" => "createdAt",
"updated_at" => "updatedAt",
"deleted_at" => "deletedAt",
Expand All @@ -44,6 +44,26 @@ class TorrentGroups extends ObjectCrud
/** crud */


/**
* create
*
* @param array $data
* @return void
*/
public function create(array $data): void
{
throw new Exception("not implemented");

/** */

# encode the json fields
$data["tags"] = json_encode($data["tags"] ?? []);

# parent create
parent::create($data);
}


/**
* read
*/
Expand All @@ -52,8 +72,44 @@ public function read(int|string $identifier = null): void
# parent method
parent::read($identifier);

# tags
$this->attributes->tags = explode(" ", $this->attributes->tags ?? "");
# decode the json fields
$this->attributes->tags = json_decode($this->attributes->tags ?? []);
}


/**
* update
*
* @param array $data
* @return void
*/
public function update(array $data): void
{
throw new Exception("not implemented");

/** */

# encode the json fields
$data["tags"] = json_encode($data["tags"] ?? []);

# parent update
parent::update($data);
}


/**
* delete
*
* @return void
*/
public function delete(): void
{
throw new Exception("not implemented");

/** */

# parent delete
parent::delete();
}


Expand Down
14 changes: 8 additions & 6 deletions app/Models/Torrents.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ public static function getGroupsForReal(array $groupIds): array
$groupIds = implode(", ", $groupIds);

$query = "
select id, category_id, title, subject, object, year,
workgroup, location, identifier, tag_list, timestamp, picture
select id, categoryId, title, subject, object, year,
workgroup, location, identifier, tag, created_at, picture
from torrents_group where id in({$groupIds})
";
$ref = $app->dbNew->multi($query, []);
Expand Down Expand Up @@ -252,9 +252,9 @@ public static function get_groups($GroupIDs, $Return = true, $GetArtists = true,
`identifier`,
`workgroup`,
`location`,
`tag_list`,
`tags`,
`picture`,
`category_id`
`categoryId`
FROM
`torrents_group`
WHERE
Expand Down Expand Up @@ -374,12 +374,12 @@ public static function array_group(array &$Group)
'subject' => $Group['subject'],
'object' => $Group['object'],
'year' => $Group['year'],
'category_id' => $Group['category_id'],
'category_id' => $Group['categoryId'],
'identifier' => $Group['identifier'],
'workgroup' => $Group['workgroup'],
'location' => $Group['location'],
'GroupFlags' => ($Group['Flags'] ?? ''),
'tag_list' => $Group['tag_list'],
'tag_list' => json_decode($Group['tags'] ?? []),
'picture' => $Group['picture'],
'Torrents' => $Group['Torrents'],
'Artists' => $Group['Artists']
Expand Down Expand Up @@ -743,6 +743,7 @@ public static function update_hash(int $GroupID)

$QueryID = $app->dbOld->get_query_id();

/*
$app->dbOld->prepared_query("
UPDATE
`torrents_group`
Expand All @@ -766,6 +767,7 @@ public static function update_hash(int $GroupID)
WHERE
`ID` = '$GroupID'
");
*/


// Fetch album artists
Expand Down
62 changes: 62 additions & 0 deletions database/migrations/20240522191136_json_tags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class JsonTags extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change(): void
{
$app = Gazelle\App::go();

# get the tags from torrents_group
$query = "select id, tag_list from torrents_group";
$ref = $app->dbNew->multi($query, []);

# update the tags in torrents_group
foreach ($ref as $row) {
$tags = explode(" ", $row["tag_list"]);
$tags = array_map("trim", $tags);

# replace _ with .
foreach ($tags as $key => $tag) {
$tags[$key] = str_replace("_", ".", $tag);
}

# get the unique tags
$tags = array_unique($tags);

# update the tags
$query = "update torrents_group set tag_list = ? where id = ?";
$app->dbNew->do($query, [ json_encode($tags), $row["id"] ]);
}

# now do the same for collages
$query = "select id, tags from collages";
$ref = $app->dbNew->multi($query, []);

foreach ($ref as $row) {
$tags = explode(" ", $row["tags"]);
$tags = array_map("trim", $tags);

# get the unique tags
$tags = array_unique($tags);

# update the tags
$query = "update collages set tags = ? where id = ?";
$app->dbNew->do($query, [ json_encode($tags), $row["id"] ]);
}
}
}
16 changes: 9 additions & 7 deletions sections/collages/updateOrCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
# todo

# check if locked
$isLocked = $post["isLocked"] ?? 0;
if ($isLocked) {
if ($collage->attributes->isLocked) {
throw new Exception("This collage is locked from further editing");
}

Expand All @@ -69,10 +68,13 @@

# handle a post request
if (!empty($post)) {
$identifier = $post["id"] ?? null;
$collage = new Gazelle\Collages($identifier);

$collage->updateOrCreate($post);
try {
$identifier = $post["id"] ?? null;
$collage = new Gazelle\Collages($identifier);
$collage->updateOrCreate($post);
} catch (Throwable $e) {
$errorMessage = $e->getMessage();
}
}

# official tags
Expand All @@ -88,7 +90,7 @@
"css" => ["vendor/easymde.min", "vendor/tom-select.bootstrap5.min"],

"collage" => $collage ?? null,
"collageId" => $collageId ?? null,
"categories" => Gazelle\Collages::$categories,
"errorMessage" => $errorMessage ?? null,
"isUpdate" => $isUpdate ?? null,
"tagList" => $tagList ?? [],
Expand Down
15 changes: 6 additions & 9 deletions templates/collages/updateOrCreate.twig
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@

{# hidden fields #}
{{ form_token(request.server.REQUEST_URI) }}
<input type="hidden" name="id" value={{ collageId }}>
<input type="hidden" name="auth" value="{{ user.extra.AuthKey }}">
<input type="hidden" name="id" value={{ collage.id }}>
<input type="hidden" name="userId" value="{{ user.core.id }}">

{# visible fields #}
Expand All @@ -53,7 +52,7 @@

<td>
<select name="category">
{% for key, value in env.collageCategories %}
{% for key, value in categories %}
<option value="{{ key }}" {{ (collage and collage.attributes.categoryId == key) ? " selected" : "" }}>{{ value }}</option>
{% endfor %}
</select>
Expand All @@ -66,18 +65,16 @@
</li>

<li>
<strong>Theme</strong> &mdash; A collage with thematically grouped torrents
<strong>Thematic</strong> &mdash; A collage with thematically grouped torrents
</li>

{#
<li>
<strong>Staff Picks</strong> &mdash; A list of staff recommendations for special occasions
</li>

<li>
<strong>Group Picks</strong> &mdash; A collage that appears on your profile page
</li>
#}
</ul>

</td>
Expand Down Expand Up @@ -114,15 +111,15 @@
<tr>
<th>Max groups</th>
<td>
<input type="number" name="maxGroups" value="{{ (collage and collage.attributes.maxGroups) ? collage.attributes.maxGroups : 0 }}">
<input type="number" name="maximumGroups" value="{{ (collage and collage.attributes.maximumGroups) ? collage.attributes.maximumGroups : 0 }}">
</td>
</tr>

{# max groups per user #}
<tr>
<th>Max groups per user</th>
<td>
<input type="number" name="maxGroupsPerUser" value="{{ (collage and collage.attributes.maxGroupsPerUser) ? collage.attributes.maxGroupsPerUser : 0 }}">
<input type="number" name="groupsPerUser" value="{{ (collage and collage.attributes.groupsPerUser) ? collage.attributes.groupsPerUser : 0 }}">
</td>
</tr>

Expand All @@ -135,7 +132,7 @@
</tr>

{# update button #}
{% if collage %}
{% if isUpdate %}
<tr>
<td colspan="2" class="center">
<input type="submit" class="button-orange" value="update">
Expand Down

0 comments on commit 74eb58e

Please sign in to comment.