Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
refactor(torrent/snatch): Separate Torrent::getSnatchDetails to torre…
Browse files Browse the repository at this point in the history
…nt\SnatchForm Pager
  • Loading branch information
Rhilip committed Aug 6, 2019
1 parent ae72ce3 commit 853a743
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 36 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
- **gitignore:** Add ignore of `/backup` folder

### Feat
- **Category:** Add Image and class_name support
- **Category:** Add Categories Manage Pane
- **Category:** Add Default sprite image of category
- **Category:** Add Image and class_name support
- **Category:** Add Categories Support when upload torrent
- **Crontab:** Move From Timer to Process
- **Editor:** Support wysibb editor
- **Gravatar:** Add support of gravatar
- **Pager:** Add Pager Support
- **Pager:** Torrents/{SearchForm,TagsForm}
- **Process:** Clean Components before sleep
- **Process:** Add custom Process Support
- **Redis:** Add mutiDelete() function for Redis
Expand Down
11 changes: 8 additions & 3 deletions apps/controllers/TorrentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace apps\controllers;

use apps\models\Torrent;
use apps\models\form\Torrent as TorrentForm;

use Rid\Http\Controller;

Expand All @@ -29,10 +30,14 @@ public function actionEdit() // TODO

public function actionSnatch() // TODO
{
$tid = app()->request->get('id');
$torrent = new Torrent($tid);
$pager = new TorrentForm\SnatchForm();
$pager->setData(app()->request->get());
$success = $pager->validate();
if (!$success) {
return $this->render('action/action_fail');
}

return $this->render('torrent/snatch', ['torrent' => $torrent]);
return $this->render('torrent/snatch', ['pager' => $pager]);
}

public function actionDownload()
Expand Down
Empty file removed apps/messages/.gitkeep
Empty file.
15 changes: 5 additions & 10 deletions apps/models/Torrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,9 @@ public function getPinnedTags(): array
});
}

public function hasNfo() {
return (boolean) $this->nfo;
public function hasNfo()
{
return (boolean)$this->nfo;
}

/**
Expand Down Expand Up @@ -368,7 +369,8 @@ protected static function nfoConvert($ibm_437, $swedishmagic = false)
return $s;
}

public function getComments() {
public function getComments()
{
return $this->comments;
}

Expand All @@ -382,11 +384,4 @@ public function getLastCommentsDetails()
});
}

public function getSnatchDetails() {
return $this->getCacheValue('snatched_details', function () {
return app()->pdo->createCommand('SELECT * FROM `snatched` WHERE `torrent_id` = :tid ORDER BY finish_at,create_at DESC;')->bindParams([
'tid' => $this->id
])->queryAll();
});
}
}
7 changes: 5 additions & 2 deletions apps/models/api/v1/form/TorrentsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@

namespace apps\models\api\v1\form;

use apps\models\form\Base\TorrentForm;
use apps\models\form\Traits\isValidTorrentTrait;

class TorrentsForm extends TorrentForm
use Rid\Validators\Validator;

class TorrentsForm extends Validator
{
use isValidTorrentTrait;

public static function inputRules()
{
Expand Down
45 changes: 45 additions & 0 deletions apps/models/form/Torrent/SnatchForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 8/6/2019
* Time: 8:40 PM
*/

namespace apps\models\form\Torrent;

use apps\models\form\Traits\isValidTorrentTrait;
use Rid\Validators\Pager;

class SnatchForm extends Pager
{
use isValidTorrentTrait;

public static function inputRules()
{
return array_merge(parent::inputRules(), [
'id' => 'required | Integer'
]);
}

public static function callbackRules()
{
return ['checkPager', 'isExistTorrent'];
}

protected function getRemoteTotal()
{
$tid = $this->getData('id');
return app()->pdo->createCommand('SELECT COUNT(`id`) FROM `snatched` WHERE `torrent_id` = :tid')->bindParams([
'tid' => $tid
])->queryScalar();
}

protected function getRemoteData()
{
return app()->pdo->createCommand([
['SELECT * FROM `snatched` WHERE `torrent_id` = :tid ORDER BY finish_at,create_at DESC ', 'params' => ['tid' => $this->id]],
['LIMIT :offset, :limit', 'params' => ['offset' => $this->offset, 'limit' => $this->limit]]
])->queryAll();
}
}
2 changes: 1 addition & 1 deletion apps/models/form/Torrents/UploadForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ private function insertTags()
}
}

// TODO sep to Trait
// TODO sep to Traits
private function setTorrentBuff($operator_id = 0, $beneficiary_id = 0, $buff_type = 'mod', $ratio_type = 'Normal', $upload_ratio = 1, $download_ratio = 1)
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@
* Created by PhpStorm.
* User: Rhilip
* Date: 8/6/2019
* Time: 5:20 PM
* Time: 11:06 PM
*/

namespace apps\models\form\Base;
namespace apps\models\form\Traits;


use apps\models\Torrent;
use Rid\Validators\Validator;

class TorrentForm extends Validator
trait isValidTorrentTrait
{

public $id;
public $tid;

/** @var Torrent */
protected $torrent;

/**
* @return Torrent
*/
public function getTorrent(): Torrent
{
return $this->torrent;
}

protected function isExistTorrent() {
$tid = $this->getData('tid') ?? $this->getData('id');
$torrent_exist = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `torrents` WHERE `id` = :tid')->bindParams([
Expand Down
18 changes: 5 additions & 13 deletions apps/views/torrent/snatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
* Time: 17:10
*
* @var League\Plates\Template\Template $this
* @var \apps\models\Torrent $torrent
* @var \apps\models\form\Torrent\SnatchForm $pager
*/

$timenow = time();
$torrent = $pager->getTorrent();
?>

<?= $this->layout('layout/base') ?>
Expand All @@ -28,16 +29,7 @@
<div class="panel-heading"><b>Torrent Snatched Details</b></div>
<div class="panel-body" id="torrent_snatched_details_body">
<div id="torrent_snatched_details">
<?php if ($torrent->getSnatchDetails()): ?>
<?php // FIXME by pager
$snatchDetails_all = $torrent->getSnatchDetails();
$count = count($torrent->getSnatchDetails());
$limit = app()->request->get('limit',50);
if ($limit > 50) $limit = 50;
$page = app()->request->get('page',1);
$offset = ($page - 1) * $limit;
if ($offset > $count / $limit) $offset = intval($count/$limit);
?>
<?php if ($pager->getDataTotal()): ?>
<table class="table table-hover table-condensed">
<thead>
<tr>
Expand All @@ -54,7 +46,7 @@
</tr>
</thead>
<tbody>
<?php foreach (array_slice($torrent->getSnatchDetails(), $offset, $limit) as $snatchDetail): ?>
<?php foreach ($pager->getPagerData() as $snatchDetail): ?>
<tr>
<td><?= $this->insert('helper/username',['user'=>app()->site->getUser($snatchDetail['user_id'])]) ?></td>
<td><?= inet_ntop($snatchDetail['ip']) ?></td>
Expand Down Expand Up @@ -84,7 +76,7 @@
</tbody>
</table>
<div class="text-center">
<ul class="pager pager-unset-margin" data-ride="remote_pager" data-rec-total="<?= $count ?>" data-rec-per-page="50"></ul>
<ul class="pager pager-unset-margin" data-ride="remote_pager" data-rec-total="<?= $pager->getTotal() ?>" data-rec-per-page="<?= $pager->getLimit() ?>"></ul>
</div>
<?php else: ?>
No Snatched Records exist.
Expand Down
2 changes: 1 addition & 1 deletion apps/views/torrents/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
</tbody>
</table>
<div class="text-center">
<ul class="pager pager-unset-margin" data-ride="remote_pager" data-rec-total="<?= $pager->getDataTotal() ?>" data-rec-per-page="<?= $pager->getLimit() ?>"></ul>
<ul class="pager pager-unset-margin" data-ride="remote_pager" data-rec-total="<?= $pager->getTotal() ?>" data-rec-per-page="<?= $pager->getLimit() ?>"></ul>
</div>
</div>
</div>
Expand Down

0 comments on commit 853a743

Please sign in to comment.