Skip to content

Commit

Permalink
use database active records to retrieve file manager lists #137
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Jun 18, 2018
1 parent 1e904b7 commit 1f61398
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 20 deletions.
41 changes: 26 additions & 15 deletions src/apis/StorageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use luya\admin\file\Query;
use yii\data\ArrayDataProvider;
use luya\admin\models\StorageImage;
use luya\admin\file\Item;

/**
* Filemanager and Storage API.
Expand Down Expand Up @@ -87,28 +88,36 @@ public function actionDataFolders()
*/
public function actionDataFiles($folderId = 0, $page = 0)
{
$perPage = 10;
$totalCount = (new Query())->where(['folder_id' => $folderId, 'is_hidden' => false, 'is_deleted' => false])->count();
$perPage = 20;
//$totalCount = (new Query())->where(['folder_id' => $folderId, 'is_hidden' => false, 'is_deleted' => false])->count();
$totalCount = StorageFile::find()->where(['folder_id' => $folderId, 'is_hidden' => false, 'is_deleted' => false])->count();

$files = $this->getOrSetHasCache(['storageApiDataFiles', $folderId, $page], function() use ($folderId, $page, $perPage) {
$tinyCrop = Yii::$app->storage->getFiltersArrayItem(TinyCrop::identifier());
$mediumThumbnail = Yii::$app->storage->getFiltersArrayItem(MediumThumbnail::identifier());

$files = $this->getOrSetHasCache(['storageApiDataFiles', $folderId, $page], function() use ($folderId, $page, $perPage, $tinyCrop, $mediumThumbnail) {
$files = [];
$fileQuery = (new Query())->where(['folder_id' => $folderId, 'is_hidden' => false, 'is_deleted' => false])->offset($page*50)->limit($perPage)->all();
$fileQuery = StorageFile::find()
->where(['folder_id' => $folderId, 'is_hidden' => false, 'is_deleted' => false])->offset($page*$perPage)->limit($perPage)->asArray()->all();
//$fileQuery = (new Query())->where(['folder_id' => $folderId, 'is_hidden' => false, 'is_deleted' => false])->offset($page*$perPage)->limit($perPage)->all();

foreach ($fileQuery as $file) {

foreach ($fileQuery as $fileArray) {

$file = Item::create($fileArray);
$data = $file->toArray(['id', 'folderId', 'name', 'isImage', 'sizeReadable', 'extension', 'uploadTimestamp']);
if ($file->isImage) {
/* TODO: as this will consum the full admin_storage_file and admin_storage_image table */
// add tiny thumbnail
$filter = Yii::$app->storage->getFiltersArrayItem(TinyCrop::identifier());
if ($filter) {
$thumbnail = Yii::$app->storage->addImage($file->id, $filter['id']);
if ($tinyCrop) {
$thumbnail = Yii::$app->storage->addImage($file->id, $tinyCrop['id']);
if ($thumbnail) {
$data['thumbnail'] = $thumbnail->toArray(['source']);
}
}
// add meidum thumbnail
$filter = Yii::$app->storage->getFiltersArrayItem(MediumThumbnail::identifier());
if ($filter) {
$thumbnail = Yii::$app->storage->addImage($file->id, $filter['id']);
if ($mediumThumbnail) {
$thumbnail = Yii::$app->storage->addImage($file->id, $mediumThumbnail['id']);
if ($thumbnail) {
$data['thumbnailMedium'] = $thumbnail->toArray(['source']);
}
Expand All @@ -127,10 +136,12 @@ public function actionDataFiles($folderId = 0, $page = 0)
private function generatePaginationArrayResponse($currentPage, $perPage, $totalCount, array $files)
{
return [
'page' => (int) $currentPage,
'perPage' => $perPage,
'totalCount' => $totalCount,
'count' => count($files),
'__meta' => [
'currentPage' => (int) $currentPage,
'perPage' => $perPage,
'totalPages' => ceil($totalCount/$perPage),
'totalFilesCount' => $totalCount,
],
'data' => $files,
];
}
Expand Down
21 changes: 19 additions & 2 deletions src/resources/dist/js/main.uglified.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/resources/dist/js/main.uglified.js.map

Large diffs are not rendered by default.

21 changes: 19 additions & 2 deletions src/resources/js/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -2665,11 +2665,13 @@

$scope.foldersDataReload = function() {
return ServiceFoldersData.load(true);
}
};

// ServiceFilesData inheritance

$scope.filesData = [];

$scope.paginations = [];

// load files data for a given folder id
$scope.$watch('currentFolderId', function(folderId) {
Expand All @@ -2678,9 +2680,24 @@

$scope.getFilesForPageAndFolder = function(folderId, pageId) {
$http.get('admin/api-admin-storage/data-files?folderId='+folderId+'&page='+pageId).then(function(response) {
$scope.filesData = response.data.data;
$scope.filesData = response.data.data;
$scope.filesMetaToPagination(response.data.__meta);
});
};

$scope.filesMetaToPagination = function(meta) {
var pages = [];
console.log(meta);
for (i = 0; i < meta.totalPages; i++) {
var isActive = meta.currentPage == i;
pages.push({isActive: isActive, label: i+1, index: i});
}
$scope.paginations = pages;
};

$scope.getFilesForPage = function(pageId) {
$scope.getFilesForPageAndFolder($scope.currentFolderId, pageId);
};

// ServiceFolderId

Expand Down
3 changes: 3 additions & 0 deletions src/views/layouts/_angulardirectives.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@
</tr>
</tbody>
</table>
<ul class="pagination">
<li ng-repeat="page in paginations" ng-class="{'active': page.isActive}" class="page-item"><a class="page-link" ng-click="getFilesForPage(page.index)">{{ page.label }}</a></li>
</ul>
</div>
</div>
<!-- /Files -->
Expand Down

0 comments on commit 1f61398

Please sign in to comment.