Skip to content

Commit

Permalink
start on collages rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
pjc09h committed May 22, 2024
1 parent efdf69b commit 356b0b7
Show file tree
Hide file tree
Showing 38 changed files with 221 additions and 418 deletions.
40 changes: 40 additions & 0 deletions app/Models/Collages.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,46 @@ class Collages extends ObjectCrud
];


/** crud */


/**
* delete
*
* @return void
*/
public function delete(): void
{
$app = App::go();

try {
# start a transaction
$app->dbNew->beginTransaction();

# delete the collage's torrents
$query = "update collages_torrents set deleted_at = now() where collageId = ?";
$app->dbNew->do($query, [$this->id]);

# delete the subscriptions
$query = "update subscriptions_collages set deleted_at = now() where collageId = ?";
$app->dbNew->do($query, [$this->id]);

# parent delete
parent::delete();

# write to the site log
\Misc::write_log("collage {$this->id} was deleted by {$app->user->core["username"]}");

# commit the transaction
$app->dbNew->commit();
} catch (\Throwable $e) {
# rollback and rethrow
$app->dbNew->rollBack();
throw $e;
}
}


/** relationships */


Expand Down
2 changes: 0 additions & 2 deletions app/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ private static function factory(array $options = []): \Twig\Environment
# request
$request = Http::request();
$twig->addGlobal("request", $request);
$twig->addGlobal("query", $request); # todo: delete
#!d($twig->getGlobals());exit;

# https://github.com/paragonie/anti-csrf
$twig->addFunction(new \Twig\TwigFunction(
Expand Down
48 changes: 48 additions & 0 deletions routes/web/collages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);


/**
* collages
*/


# browse
Flight::route("/collages", function () {
$app = Gazelle\App::go();
$app->middleware(["collages" => "read"]);
require_once "{$app->env->serverRoot}/sections/collages/browse.php";
});


# create
Flight::route("/collages/create", function () {
$app = Gazelle\App::go();
$app->middleware(["collages" => "create"]);
require_once "{$app->env->serverRoot}/sections/collages/updateOrCreate.php";
});


# read
Flight::route("/collages/@identifier", function (int|string $identifier = null) {
$app = Gazelle\App::go();
$app->middleware(["collages" => "read"]);
require_once "{$app->env->serverRoot}/sections/collages/details.php";
});


# update
Flight::route("/collages/@identifier/update", function (int|string $identifier = null) {
$app = Gazelle\App::go();
$app->middleware(["collages" => "updateAny"]);
require_once "{$app->env->serverRoot}/sections/collages/updateOrCreate.php";
});


# delete
Flight::route("/collages/@identifier/delete", function (int|string $identifier = null) {
$app = Gazelle\App::go();
$app->middleware(["collages" => "deleteAny"]);
require_once "{$app->env->serverRoot}/sections/collages/delete.php";
});
5 changes: 2 additions & 3 deletions sections/collages/browse.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}


/** torrent search handling */
/** search handling */


# collect the query
Expand Down Expand Up @@ -79,7 +79,6 @@
$manticore = new Gazelle\Manticore();
$searchResults = $manticore->search("collections", $get);
$resultCount = count($searchResults);
#!d($searchResults);


/** pagination */
Expand Down Expand Up @@ -158,7 +157,7 @@
"js" => ["vendor/tom-select.base.min", "browse", "collages"],
"css" => ["vendor/tom-select.bootstrap5.min"],

"categories" => $app->env->collageCategories,
"categories" => Gazelle\Collages::$categories,

"searchResults" => $searchResults,
"collages" => $collages,
Expand Down
64 changes: 17 additions & 47 deletions sections/collages/delete.php
Original file line number Diff line number Diff line change
@@ -1,55 +1,25 @@
<?php
#declare(strict_types=1);

$app = \Gazelle\App::go();
declare(strict_types=1);

$CollageID = $_GET['collageId'];
if (!is_numeric($CollageID) || !$CollageID) {
error(404);
}

$app->dbOld->query("
SELECT Name, CategoryID, UserID
FROM collages
WHERE ID = '$CollageID'");
list($Name, $CategoryID, $UserID) = $app->dbOld->next_record();
/**
* delete a collage
*/

$app = Gazelle\App::go();

if ($app->user->cant(["collages" => "deleteAny"]) && $UserID != $app->user->core['id']) {
error(403);
$identifier ??= null;
if (!$identifier) {
$app->error(404);
}

View::header('Delete collage');
?>
<div class="center">
<div class="box" style="width: 600px; margin: 0px auto;">
<div class="head colhead">
Delete collage
</div>
<div class="pad">
<form class="delete_form" name="collage" action="collages.php" method="post">
<input type="hidden" name="action" value="take_delete">
<input type="hidden" name="auth" value="<?=$app->user->extra['AuthKey']?>">
<input type="hidden" name="collageId" value="<?=$CollageID?>">
<?php
if ($CategoryID == 0) {
?>
<div class="alertbar" style="margin-bottom: 1em;">
<strong>Warning: This is a personal collage. If you delete this collage, it <em>cannot</em> be recovered!</strong>
</div>
<?php
try {
$collage = new Gazelle\Collages($identifier);
$collage->delete();
} catch (Throwable $e) {
$app->error(404);
}
?>
<div>
<strong>Reason: </strong>
<input type="text" name="reason" size="40">
</div>
<div class="submit_div">
<input value="Delete" type="submit">
</div>
</form>
</div>
</div>
</div>
<?php
View::footer();
?>

# redirect to collages index
Gazelle\Http::redirect("/collages");
28 changes: 14 additions & 14 deletions sections/collages/collage.php → sections/collages/details.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@


/**
* collage details page
* collage details
*/

$app = Gazelle\App::go();

# http request
$get = Gazelle\Http::get();
$id = $get["id"];

# collage details
$collage = new Gazelle\Collages($id);

if (!$collage->id) {
$identifier ??= null;
if (!$identifier) {
$app->error(404);
}

$torrentGroups = $collage->getTorrentGroups();
$isSubscribed = $collage->isSubscribed();
$stats = $collage->readStats();
try {
$collage = new Gazelle\Collages($identifier);
$torrentGroups = $collage->getTorrentGroups();
$isSubscribed = $collage->isSubscribed();
$stats = $collage->readStats();
} catch (Gazelle\Exception\ResourceNotFoundException $e) {
$app->error(404);
}

# create a conversation if it doesn't exist
$conversation = Gazelle\Conversations::createIfNotExists($collage->id, "collages");
#!d($conversation->relationships->messages);exit;

# twig template
$app->twig->display("collages/details.twig", [
"title" => $collage->attributes->title,
"sidebar" => true,

"js" => ["collages", "conversations"],

"collage" => $collage,
"torrentGroups" => $torrentGroups,
"isSubscribed" => $isSubscribed,
Expand All @@ -43,9 +43,9 @@
"conversation" => $conversation,
]);


exit;

/****** */

$CollageID = (int) $_GET['id'];

Expand Down
102 changes: 0 additions & 102 deletions sections/collages/router.php

This file was deleted.

Loading

0 comments on commit 356b0b7

Please sign in to comment.