Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LanguageReport module #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"yiisoft/yii2-redis": "2.0.2",
"creocoder/yii2-nested-sets": "dev-master",
"rych/bencode": "1.0.*",
"himiklab/yii2-recaptcha-widget" : "*"
"himiklab/yii2-recaptcha-widget" : "*",
"bower-asset/tagsinput" : "*",
"bower-asset/typeahead.js": "0.10.*"
},
"require-dev": {
"yiisoft/yii2-codeception": "*",
Expand Down
5 changes: 5 additions & 0 deletions environments/dev/frontend/config/main-local.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
'recordModel' => \common\models\torrent\Torrent::className(),
'salt' => ''
],
'languageReport' => [
'class' => 'frontend\modules\languageReport\LanguageReportModule',
'recordModel' => \common\models\torrent\Torrent::className(),
'salt' => ''
],
],
];

Expand Down
5 changes: 5 additions & 0 deletions environments/prod/frontend/config/main-local.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,10 @@
'recordModel' => \common\models\torrent\Torrent::className(),
'salt' => ''
],
'languageReport' => [
'class' => 'frontend\modules\languageReport\LanguageReportModule',
'recordModel' => \common\models\torrent\Torrent::className(),
'salt' => ''
],
],
];
7 changes: 7 additions & 0 deletions frontend/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@
'complain' => 'complain.php',
),
],
'languageReport*' => [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'fileMap' => array(
'complain' => 'languageReport.php',
),
],
],
],
'request' => [
Expand Down
34 changes: 34 additions & 0 deletions frontend/modules/languageReport/Asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace frontend\modules\languageReport;

use yii\web\AssetBundle;

/**
* Module asset bundle.
*/
class Asset extends AssetBundle {

/**
* @inheritdoc
*/
public $sourcePath = '@languageReport/assets';

/**
* @inheritdoc
*/
public $js = [
'js/language.js',
];
public $css = [
'css/language.css',
];

/**
* @inheritdoc
*/
public $depends = [
'yii\web\JqueryAsset'
];

}
28 changes: 28 additions & 0 deletions frontend/modules/languageReport/LanguageReportModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace frontend\modules\languageReport;

use yii\base\Exception;

class LanguageReportModule extends \yii\base\Module {

public $controllerNamespace = 'frontend\modules\languageReport\controllers';

const EVENT_LANGUAGEREPORT_ADD = 'languageReportAdd';

public $recordModel;
public $salt;

public function init() {
parent::init();

\Yii::setAlias('@languageReport', realpath(dirname(__FILE__) . '/'));
if (empty($this->recordModel)) {
throw new Exception("Unknown class recordModel");
}
if (empty($this->salt)) {
throw new Exception("Salt is incorrect");
}
}

}
35 changes: 35 additions & 0 deletions frontend/modules/languageReport/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Language report module

Language report is a module designed for [The Open Pirate Bay](https://oldpiratebay.org/100k)
to give user an opportunity to vote for content language.

## Dependencies
The module depends on the following components:
- yiisoft/yii2
- yiisoft/yii2-bootstrap
- bower-asset/tagsinput
- bower-asset/typeahead.js

## Installation
1. Checkout branch with the module.
2. Run **composer update**.
3. Apply migrations with *yii migrate --migrationPath=@frontend/modules/languageReport/migrations*.
4. You may copy module view file to *@themes/theme_name/modules/languageReport/* and rewrite it to design your own theme.

## Openbay core injections
1. Adds module record to **main-local.php**
2. Adds i18n record to **frontend/main.php**
3. Adds widget to **@frontend/themes/newopb/modules/torrent/views/default/view.php**

## Usage
Now the widget is displayed on torrent view page. Any registered user can select
language for torrent. When user starts typing language *typeahead.js* helps user to type.
After sending report to server the content languages are replaced with calculated ones.

## Improvements
At the moment languages are stored in separate table to decrease number of injection to core code.
It can be either replaced by field in *torrents* table or joined with *tags* field in *torrents* table,
handling the **EVENT_LANGUAGEREPORT_ADD** event.

When the list of added languages grows, it can be filtered using *confirmed* field in *language* table.
This field indicates how many users had selected the language.
37 changes: 37 additions & 0 deletions frontend/modules/languageReport/TagInputAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace frontend\modules\languageReport;

use yii\web\AssetBundle;

/**
* Module asset bundle.
*/
class TagInputAsset extends AssetBundle {

/**
* @inheritdoc
*/
public $sourcePath = '@bower/';

/**
* @inheritdoc
*/
public $js = [
// 'typeahead.js/dist/typeahead.bundle.js',
'tagsinput/dist/bootstrap-tagsinput.js',
];
public $css = [
'tagsinput/dist/bootstrap-tagsinput.css',
];

/**
* @inheritdoc
*/
public $depends = [
// 'yii\web\JqueryAsset',
'yii\bootstrap\BootstrapAsset',
'yii\bootstrap\BootstrapPluginAsset',
];

}
23 changes: 23 additions & 0 deletions frontend/modules/languageReport/TypeAheadAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace frontend\modules\languageReport;

use yii\web\AssetBundle;

/**
*
* @author Qiang Xue <[email protected]>
* @since 2.0
*/
class TypeAheadAsset extends AssetBundle {

public $sourcePath = '@bower/typeahead.js/dist';
public $js = [
'typeahead.bundle.js',
];
public $depends = [
'yii\bootstrap\BootstrapAsset',
'yii\bootstrap\BootstrapPluginAsset',
];

}
84 changes: 84 additions & 0 deletions frontend/modules/languageReport/assets/css/language.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
.languageReportWidget {
width: 750px;
}
.languageReportWidget span.language {

}
.languageReportWidget span.language {
font-style: italic;
}

.languageReportWidget span.reporterArea {
display: inline-block;
position: relative;
}

div.fader {
position: fixed;
top: 0;
bottom: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.3);
z-index: 100;
}

div.reporterForm {
width: 350px;
display: none;
position: absolute;
padding: 15px;
border-radius: 10px;
background: white;
top: 30px;
z-index: 101;
}

div.reporterForm .bubble-arrow {
position:absolute; left:25px; top:-10px;
display:block;
width: 0px;
height: 0px;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 10px solid #fff;
}

div.reporterForm input{
width: 100%;
}
div.reporterForm .bootstrap-tagsinput{
width: 100%;
}

.tt-dropdown-menu {
width: 422px;
margin-top: 2px;
padding: 8px 0;
background-color: #fff;
border: 1px solid #ccc;
border: 1px solid rgba(0, 0, 0, 0.2);
-webkit-box-shadow: 0 5px 10px rgba(0, 0, 0, .2);
-moz-box-shadow: 0 5px 10px rgba(0, 0, 0, .2);
box-shadow: 0 5px 10px rgba(0, 0, 0, .2);
}

.tt-suggestion {
padding: 3px 20px;
font-size: 18px;
line-height: 24px;
}

.tt-suggestion.tt-cursor {
color: #fff;
background-color: #0097cf;

}

.tt-hint {
opacity: 0.5 !important;
}

.tt-suggestion p {
margin: 0;
}
80 changes: 80 additions & 0 deletions frontend/modules/languageReport/assets/js/language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
$(document).ready(function() {
var langWidget = {
init: function() {
var that = this;
var langnames = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: {
url: '/languageReport/default/languages',
filter: function(list) {
return $.map(list, function(cityname) {
return {name: cityname};
});
},
ttl: 60
}
});
langnames.initialize();
$('#language-input').tagsinput({
typeaheadjs: {
name: 'langnames',
displayKey: 'name',
valueKey: 'name',
source: langnames.ttAdapter()
}
});

$(document).on('click', 'div.fader', function() {
that.close();
});

$('a.reportLanguage').click(function(event) {
event.preventDefault();
event.stopPropagation();
that.open();
return false;
});

$('#cancelReport').click(function() {
that.close();
});

$('#sendReport').click(function() {
that.save();
});
},
open: function() {
$('<div>')
.addClass('fader')
.hide()
.appendTo('body')
.fadeIn();
$('.reporterForm').fadeIn();
},
save: function() {
var data = {
id: $('#sendReport').data('id'),
language: $('#language-input').val()
};
$.get('/languageReport/default/report', data, function(r) {
$('#recordLanguage').html(r.languages);
alert(r.message);
$('.reporterForm').fadeOut(function() {
$('.reporterArea').remove();
});
}).fail(function() {
alert('Avast! Server is unreachable now like a far land, ya land lubber!');
$('.reporterForm').fadeOut();
});
this.close();
},
close: function() {
$('div.fader').fadeOut(function() {
$('div.fader').remove();
});
$('.reporterForm').fadeOut();
}
};
langWidget.init();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace frontend\modules\languageReport\components;

use yii\base\Event;

/**
* Event for handling language report
*/
class LanguageReportAddEvent extends Event {

public $recordId;
public $userId;
public $languages;

}
Loading