Skip to content

Commit

Permalink
Merge pull request #95 from humhub/enh/94-implement-download-counter
Browse files Browse the repository at this point in the history
Implement Download Counter
  • Loading branch information
luke- authored Nov 4, 2020
2 parents f31d029 + f813ba5 commit b0f93b3
Show file tree
Hide file tree
Showing 18 changed files with 145 additions and 8 deletions.
17 changes: 17 additions & 0 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use humhub\modules\cfiles\models\File;
use humhub\modules\cfiles\models\Folder;
use humhub\modules\file\actions\DownloadAction;
use Yii;
use yii\base\Event;

Expand Down Expand Up @@ -77,4 +78,20 @@ public static function onProfileMenuInit($event)
}
}

/**
* Callback on after file controller action
*
* @param Event $event
*/
public static function onAfterFileAction(Event $event)
{
if (isset($event->action) &&
$event->action instanceof DownloadAction &&
($downloadedFile = File::getFileByGuid(Yii::$app->request->get('guid')))
) {
$downloadedFile->download_count++;
$downloadedFile->save();
}
}

}
10 changes: 10 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,14 @@ public function getUploadBehaviour()
return $this->settings->get('uploadBehaviour', self::UPLOAD_BEHAVIOUR_INDEX);
}

/**
* Determines visible columns
*
* @return array
*/
public function getDisplayDownloadCount()
{
return $this->settings->get('displayDownloadCount', false);
}

}
4 changes: 3 additions & 1 deletion config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use humhub\commands\IntegrityController;
use humhub\modules\file\controllers\FileController;
use humhub\modules\space\widgets\Menu;
use humhub\modules\user\widgets\ProfileMenu;

Expand All @@ -11,7 +12,8 @@
'events' => [
[Menu::class, Menu::EVENT_INIT, ['humhub\modules\cfiles\Events', 'onSpaceMenuInit']],
[ProfileMenu::class, ProfileMenu::EVENT_INIT, ['humhub\modules\cfiles\Events', 'onProfileMenuInit']],
[IntegrityController::class, IntegrityController::EVENT_ON_RUN, ['humhub\modules\cfiles\Events', 'onIntegrityCheck']]
[IntegrityController::class, IntegrityController::EVENT_ON_RUN, ['humhub\modules\cfiles\Events', 'onIntegrityCheck']],
[FileController::class, FileController::EVENT_AFTER_ACTION, ['humhub\modules\cfiles\Events', 'onAfterFileAction']],
]
];
?>
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
--------------------
- Fix #87: ZIP Upload broken due legency ImageConverter usage
- Fix #74: Remove Temp Directory recursively in cleanup()
- Enh #94: Implement Download Counter


0.11.18 - December 4, 2019
Expand Down
25 changes: 25 additions & 0 deletions migrations/m201030_071320_add_download_counter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use yii\db\Migration;

/**
* Class m201030_071320_add_download_counter
*/
class m201030_071320_add_download_counter extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('cfiles_file', 'download_count', $this->integer()->unsigned()->defaultValue(0)->notNull());
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('cfiles_file', 'download_count');
}
}
5 changes: 5 additions & 0 deletions models/ConfigureForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ class ConfigureForm extends \yii\base\Model

public $disableZipSupport;
public $uploadBehaviour;
public $displayDownloadCount;

public function init()
{
parent::init();
$module = $this->getModule();
$this->disableZipSupport = !$module->isZipSupportEnabled();
$this->uploadBehaviour = $module->getUploadBehaviour();
$this->displayDownloadCount = $module->getDisplayDownloadCount();
}

/**
Expand All @@ -41,6 +43,7 @@ public function rules()
return [
['disableZipSupport', 'boolean'],
['uploadBehaviour', 'integer'],
['displayDownloadCount', 'boolean'],
];
}

Expand All @@ -54,6 +57,7 @@ public function attributeLabels()
return [
'disableZipSupport' => Yii::t('CfilesModule.base', 'Disable archive (ZIP) support'),
'uploadBehaviour' => Yii::t('CfilesModule.base', 'Upload behaviour for existing file names'),
'displayDownloadCount' => Yii::t('CfilesModule.base', 'Display a download count column'),
];
}

Expand All @@ -73,6 +77,7 @@ public function save()
$module = $this->getModule();
$module->settings->set('disableZipSupport', $this->disableZipSupport);
$module->settings->set('uploadBehaviour', $this->uploadBehaviour);
$module->settings->set('displayDownloadCount', $this->displayDownloadCount);
return true;
}
}
28 changes: 26 additions & 2 deletions models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @property integer $id
* @property integer $parent_folder_id
* @property string $description
* @property integer $download_count
*
* @property Folder $parentFolder
* @property \humhub\modules\file\models\File $baseFile
Expand Down Expand Up @@ -375,11 +376,19 @@ public function getTitle()
/**
* @return string
*/
function getDescription()
public function getDescription()
{
return $this->description;
}

/**
* @return integer
*/
public function getDownloadCount()
{
return $this->download_count;
}

public function setTitle($title)
{
if (!empty($this->baseFile)) {
Expand Down Expand Up @@ -533,7 +542,7 @@ public static function getPostedFiles($contentContainer, $filesOrder = ['file.up
return $query->orderBy($filesOrder);
}

/**
/**
* @return file of given name in given parent folder
*/
public static function getFileByName($name, $parentFolderId, $contentContainer)
Expand All @@ -547,4 +556,19 @@ public static function getFileByName($name, $parentFolderId, $contentContainer)
]);
return $filesQuery->one();
}

/**
* Get File by guid
*
* @param $guid
* @return array|File|\yii\db\ActiveRecord
*/
public static function getFileByGuid($guid)
{
return self::find()
->innerJoin('file', 'object_id = ' . self::tableName() . '.id')
->where(['guid' => $guid])
->andWhere(['object_model' => self::class])
->one();
}
}
8 changes: 6 additions & 2 deletions models/FileSystemItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,20 @@ public function rules()

abstract function updateVisibility($visibility);
abstract function getSize();
abstract function getItemType();
abstract function getItemType();
abstract function getDescription();
abstract function getDownloadCount();
abstract function getVisibilityTitle();

/**
* @inheritdoc
*/
public function attributeLabels()
{
return ['visibility' => Yii::t('CfilesModule.models_FileSystemItem', 'Is Public')];
return [
'visibility' => Yii::t('CfilesModule.models_FileSystemItem', 'Is Public'),
'download_count' => Yii::t('CfilesModule.models_FileSystemItem', 'Downloads'),
];
}

/**
Expand Down
8 changes: 8 additions & 0 deletions models/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,14 @@ public function getDescription()
return $this->description;
}

/**
* @return string
*/
public function getDownloadCount()
{
return '';
}

public function getSize()
{
return 0;
Expand Down
9 changes: 9 additions & 0 deletions models/rows/AbstractFileSystemItemRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ abstract class AbstractFileSystemItemRow extends Model
const COLUMN_TITLE = 'title';
const COLUMN_SIZE = 'size';
const COLUMN_TIMESTAMP = 'timestamp';
const COLUMN_DOWNLOAD_COUNT = 'download_count';
const COLUMN_SOCIAL = 'social';
const COLUMN_CREATOR = 'creator';

const ORDER_TYPE_NAME = 'name';
const ORDER_TYPE_UPDATED_AT = 'updated_at';
const ORDER_TYPE_DOWNLOAD_COUNT = 'download_count';
const ORDER_TYPE_SIZE = 'size';

/**
Expand All @@ -45,6 +47,7 @@ abstract class AbstractFileSystemItemRow extends Model
const ORDER_MAPPING = [
self::ORDER_TYPE_NAME => null,
self::ORDER_TYPE_UPDATED_AT => null,
self::ORDER_TYPE_DOWNLOAD_COUNT => null,
self::ORDER_TYPE_SIZE => null,
];

Expand All @@ -54,6 +57,7 @@ abstract class AbstractFileSystemItemRow extends Model
self::COLUMN_TITLE,
self::COLUMN_SIZE,
self::COLUMN_TIMESTAMP,
self::COLUMN_DOWNLOAD_COUNT,
self::COLUMN_SOCIAL,
self::COLUMN_CREATOR
];
Expand Down Expand Up @@ -188,6 +192,11 @@ public abstract function getEditor();
*/
public abstract function getDescription();

/**
* @return integer
*/
public abstract function getDownloadCount();

/**
* @return string
*/
Expand Down
10 changes: 9 additions & 1 deletion models/rows/BaseFileRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function isSocialActionsAvailable()
public function getColumns()
{
//self::COLUMN_VISIBILITY
return [self::COLUMN_TITLE, self::COLUMN_VISIBILITY, self::COLUMN_SIZE, self::COLUMN_TIMESTAMP, self::COLUMN_CREATOR];
return [self::COLUMN_TITLE, self::COLUMN_VISIBILITY, self::COLUMN_SIZE, self::COLUMN_TIMESTAMP, self::COLUMN_DOWNLOAD_COUNT, self::COLUMN_CREATOR];
}

/**
Expand Down Expand Up @@ -186,6 +186,14 @@ public function getDescription()
return '';
}

/**
* @return integer
*/
public function getDownloadCount()
{
return 0;
}

/**
* @return string
*/
Expand Down
1 change: 1 addition & 0 deletions models/rows/FileRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class FileRow extends FileSystemItemRow
const ORDER_MAPPING = [
self::ORDER_TYPE_NAME => 'file.file_name',
self::ORDER_TYPE_UPDATED_AT => 'file.updated_at',
self::ORDER_TYPE_DOWNLOAD_COUNT => 'cfiles_file.download_count',
self::ORDER_TYPE_SIZE => 'cast(file.size as unsigned)',
];

Expand Down
8 changes: 8 additions & 0 deletions models/rows/FileSystemItemRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ public function getDescription()
return $this->item->getDescription();
}

/**
* @inheritdoc
*/
public function getDownloadCount()
{
return $this->item->getDownloadCount();
}

/**
* @inheritdoc
*/
Expand Down
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"organisation",
"sharing"
],
"version": "0.11.19",
"version": "0.11.20",
"humhub": {
"minVersion": "1.6"
},
Expand Down
2 changes: 2 additions & 0 deletions views/config/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

<?= $form->field($model, 'disableZipSupport')->checkbox(null, false); ?>

<?= $form->field($model, 'displayDownloadCount')->checkbox(null, false); ?>

<?= Button::save()->submit() ?>
<?php ActiveForm::end(); ?>
</div>
Expand Down
1 change: 1 addition & 0 deletions views/edit/modal_edit_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<?= $form->field($file->baseFile, 'file_name'); ?>
<?= $form->field($file, 'description'); ?>
<?= $form->field($file, 'visibility')->checkbox(['disabled' => $file->parentFolder->content->isPrivate()]) ?>
<?= $form->field($file, 'download_count')->staticControl(['style' => 'display:inline']); ?>
</div>

<div class="modal-footer">
Expand Down
5 changes: 4 additions & 1 deletion widgets/views/fileList.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
</th>


<th class="hidden-xxs"></th>
<th class="hidden-xs"></th>

<th class="hidden-xs text-right" data-ui-sort="<?= FileSystemItemRow::ORDER_TYPE_SIZE ?>" <?= $sort === FileSystemItemRow::ORDER_TYPE_SIZE ? 'data-ui-order="'.Html::encode($order).'"' : '' ?>><?= Yii::t('CfilesModule.base', 'Size'); ?></th>
<th class="hidden-xxs text-right" data-ui-sort="<?= FileSystemItemRow::ORDER_TYPE_UPDATED_AT ?>" <?= $sort === FileSystemItemRow::ORDER_TYPE_UPDATED_AT ? 'data-ui-order="'.Html::encode($order).'"' : '' ?>><?= Yii::t('CfilesModule.base', 'Updated'); ?></th>
<?php if(Yii::$app->getModule('cfiles')->settings->get('displayDownloadCount')): ?>
<th class="hidden-xs text-right" data-ui-sort="<?= FileSystemItemRow::ORDER_TYPE_DOWNLOAD_COUNT ?>" <?= $sort === FileSystemItemRow::ORDER_TYPE_DOWNLOAD_COUNT ? 'data-ui-order="'.Html::encode($order).'"' : '' ?>><?= Yii::t('CfilesModule.base', 'Downloads'); ?></th>
<?php endif; ?>

<?php if (!$folder->isAllPostedFiles()): // Files currently have no content object but the Post they may be connected to. ?>
<th class="text-right"><?= Yii::t('CfilesModule.base', 'Likes/Comments'); ?></th>
Expand Down
9 changes: 9 additions & 0 deletions widgets/views/fileSystemItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@
</td>
<?php endif; ?>

<?php if (Yii::$app->getModule('cfiles')->settings->get('displayDownloadCount') &&
$row->isRenderColumn(FileSystemItemRow::COLUMN_DOWNLOAD_COUNT)) : ?>
<td class="hidden-xs text-right">
<div class="pull-right">
<?= $row->getDownloadCount(); ?>
</div>
</td>
<?php endif; ?>

<?php if ($row->isRenderColumn(FileSystemItemRow::COLUMN_SOCIAL)): ?>
<td class="text-right">
<?php if ($row->isSocialActionsAvailable()): ?>
Expand Down

0 comments on commit b0f93b3

Please sign in to comment.