Version 5.0.13
This commit is contained in:
1 parent
b89858b2ff
commit
5efe744c5f
1407 files changed
+69690
-30177
No files matched your search
@@ -13,22 +13,27 @@ namespace IPS\downloads\modules\admin\stats;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
|
||||
use IPS\DateTime;
|
||||
use IPS\Db;
|
||||
use IPS\Dispatcher;
|
||||
use IPS\Dispatcher\Controller;
|
||||
use IPS\downloads\Field;
|
||||
use IPS\downloads\File;
|
||||
use IPS\Helpers\Form;
|
||||
use IPS\Helpers\Form\DateRange;
|
||||
use IPS\Helpers\Table\Db as Table;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Member;
|
||||
use IPS\Output;
|
||||
use IPS\Request;
|
||||
use IPS\Session;
|
||||
use IPS\Theme;
|
||||
use UnderflowException;
|
||||
use function count;
|
||||
use function defined;
|
||||
use function intval;
|
||||
use function file_get_contents;
|
||||
use const IPS\Helpers\Table\SEARCH_BOOL;
|
||||
use const IPS\Helpers\Table\SEARCH_CONTAINS_TEXT;
|
||||
use const IPS\Helpers\Table\SEARCH_DATE_RANGE;
|
||||
use const IPS\Helpers\Table\SEARCH_MEMBER;
|
||||
use const IPS\Helpers\Table\SEARCH_NUMERIC;
|
||||
use const IPS\Helpers\Table\SEARCH_SELECT;
|
||||
use const IPS\TEMP_DIRECTORY;
|
||||
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
@@ -51,11 +56,6 @@ class topdownloads extends Controller
|
||||
*/
|
||||
public static bool $allowRWSeparation = TRUE;
|
||||
|
||||
/**
|
||||
* @brief Number of results per page
|
||||
*/
|
||||
const PER_PAGE = 25;
|
||||
|
||||
/**
|
||||
* Execute
|
||||
*
|
||||
@@ -74,102 +74,149 @@ class topdownloads extends Controller
|
||||
*/
|
||||
protected function manage() : void
|
||||
{
|
||||
$where = array();
|
||||
$table = new Table( 'downloads_files', Url::internal( 'app=downloads&module=stats&controller=topdownloads' ) );
|
||||
$table->langPrefix = 'downloads_';
|
||||
$customFields = Field::roots();
|
||||
|
||||
if ( isset( Request::i()->form ) )
|
||||
/* Joins */
|
||||
$joinSelect = array();
|
||||
foreach( $customFields as $field )
|
||||
{
|
||||
$form = new Form( 'form', 'go' );
|
||||
$joinSelect[] = 'downloads_ccontent.field_' . $field->id;
|
||||
}
|
||||
|
||||
$default = array(
|
||||
'start' => Request::i()->start ? DateTime::ts( Request::i()->start ) : NULL,
|
||||
'end' => Request::i()->end ? DateTime::ts( Request::i()->end ) : NULL
|
||||
/* Columns */
|
||||
if( count( $customFields ) )
|
||||
{
|
||||
$table->joins = array(
|
||||
array( 'select' => implode(',', $joinSelect), 'from' => 'downloads_ccontent', 'where' => "downloads_ccontent.file_id=downloads_files.file_id" )
|
||||
);
|
||||
}
|
||||
|
||||
$form->add( new DateRange( 'stats_date_range', $default, FALSE, array( 'start' => array( 'max' => DateTime::ts( time() )->setTime( 0, 0, 0 ), 'time' => FALSE ), 'end' => array( 'max' => DateTime::ts( time() )->setTime( 23, 59, 59 ), 'time' => FALSE ) ) ) );
|
||||
$table->include = array( 'file_name', 'file_downloads' );
|
||||
$table->advancedSearch = array(
|
||||
'file_downloads' => SEARCH_NUMERIC
|
||||
);
|
||||
|
||||
if ( !$values = $form->values() )
|
||||
/* Add our custom fields */
|
||||
foreach ( $customFields as $field )
|
||||
{
|
||||
$customField = 'field_' . $field->id;
|
||||
switch ( $field->type )
|
||||
{
|
||||
Output::i()->output = $form;
|
||||
return;
|
||||
case 'Checkbox':
|
||||
case 'YesNo':
|
||||
$table->advancedSearch[ $customField ] = SEARCH_BOOL;
|
||||
break;
|
||||
|
||||
case 'Upload':
|
||||
case 'Color':
|
||||
case 'Password':
|
||||
/* These fields make no sense to be searchable */
|
||||
break;
|
||||
|
||||
case 'CheckboxSet':
|
||||
case 'Select':
|
||||
case 'Radio':
|
||||
$options = array();
|
||||
foreach ( json_decode( $field->content ) as $option )
|
||||
{
|
||||
$options[ $option ] = $option;
|
||||
}
|
||||
$table->advancedSearch[ $customField ] = array( SEARCH_SELECT, array( 'options' => array_merge(["" => "Any"], $options ), 'multiple' => $field->multiple ) );
|
||||
break;
|
||||
|
||||
case 'Date':
|
||||
$table->advancedSearch[ $customField ] = SEARCH_DATE_RANGE;
|
||||
break;
|
||||
|
||||
case 'Member':
|
||||
$table->advancedSearch[ $customField ] = array( SEARCH_MEMBER, array( 'multiple' => $field->multiple ) );
|
||||
break;
|
||||
|
||||
case 'Number':
|
||||
case 'Rating':
|
||||
$table->advancedSearch[ $customField ] = SEARCH_NUMERIC;
|
||||
break;
|
||||
|
||||
default:
|
||||
$table->advancedSearch[ $customField ] = SEARCH_CONTAINS_TEXT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Figure out start and end parameters for links */
|
||||
$params = array(
|
||||
'start' => !empty( $values['stats_date_range']['start'] ) ? $values['stats_date_range']['start']->getTimestamp() : Request::i()->start,
|
||||
'end' => !empty( $values['stats_date_range']['end'] ) ? $values['stats_date_range']['end']->getTimestamp() : Request::i()->end
|
||||
$table->mainColumn = 'file_name';
|
||||
$table->quickSearch = 'file_name';
|
||||
|
||||
$table->sortBy = $table->sortBy ?: 'file_downloads';
|
||||
$table->sortDirection = $table->sortDirection ?: 'desc';
|
||||
|
||||
/* Parsers */
|
||||
$table->parsers = array(
|
||||
'file_name' => function( $val, $row ) {
|
||||
$file = File::constructFromData( $row );
|
||||
return Theme::i()->getTemplate( 'global', 'core', 'global' )->basicUrl( $file->url(), TRUE, $file->name );
|
||||
}
|
||||
);
|
||||
|
||||
if ( $params['start'] )
|
||||
Output::i()->sidebar['actions']['download'] = [
|
||||
'icon' => 'download',
|
||||
'title' => 'download',
|
||||
'link' => Url::internal( "app=downloads&module=stats&controller=topdownloads&do=download" )->csrf()
|
||||
];
|
||||
|
||||
/* Display */
|
||||
Output::i()->output = (string) $table;
|
||||
Output::i()->title = Member::loggedIn()->language()->addToStack('menu__downloads_stats_topdownloads');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function download() : void
|
||||
{
|
||||
Session::i()->csrfCheck();
|
||||
|
||||
$headers = [ 'file_name' => 'File', 'file_downloads' => 'Downloads' ];
|
||||
|
||||
$customFields = Field::roots();
|
||||
$customFieldNames = array();
|
||||
foreach ( $customFields as $field )
|
||||
{
|
||||
$where[] = array( 'dtime>?', $params['start'] );
|
||||
$headers['field_' . $field->id] = Member::loggedIn()->language()->get( 'downloads_field_' . $field->id );
|
||||
}
|
||||
|
||||
if ( $params['end'] )
|
||||
{
|
||||
$where[] = array( 'dtime<?', $params['end'] );
|
||||
}
|
||||
$tmpFile = tempnam( TEMP_DIRECTORY, 'IPS' );
|
||||
$fh = fopen( $tmpFile, 'w' );
|
||||
|
||||
$page = isset( Request::i()->page ) ? intval( Request::i()->page ) : 1;
|
||||
fputcsv($fh, $headers);
|
||||
|
||||
if ( $page < 1 )
|
||||
$files = Db::i()->select('*', 'downloads_files', null, 'file_downloads desc' )->join('downloads_ccontent', 'downloads_ccontent.file_id=downloads_files.file_id');
|
||||
foreach ( $files as $file )
|
||||
{
|
||||
$page = 1;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$total = Db::i()->select( 'COUNT(DISTINCT(dfid))', 'downloads_downloads', $where )->first();
|
||||
}
|
||||
catch ( UnderflowException $e )
|
||||
{
|
||||
$total = 0;
|
||||
}
|
||||
|
||||
/* Add date range selector */
|
||||
Output::i()->sidebar['actions'] = array(
|
||||
'settings' => array(
|
||||
'title' => 'stats_date_range',
|
||||
'icon' => 'calendar',
|
||||
'link' => Url::internal( 'app=downloads&module=stats&controller=topdownloads&form=1' )->setQueryString( $params ),
|
||||
'data' => array( 'ipsDialog' => '', 'ipsDialog-title' => Member::loggedIn()->language()->addToStack( 'stats_date_range' ) )
|
||||
)
|
||||
);
|
||||
|
||||
if ( $total > 0 )
|
||||
{
|
||||
$select = iterator_to_array( Db::i()->select( 'dfid, count(*) as downloads', 'downloads_downloads', $where, 'downloads DESC', array( ( $page - 1 ) * static::PER_PAGE, static::PER_PAGE ), 'dfid' )->join( 'downloads_files', 'downloads_files.file_id=downloads_downloads.dfid' ) );
|
||||
$files = iterator_to_array( Db::i()->select( '*', 'downloads_files', Db::i()->in( 'file_id', array_column( $select, 'dfid' ) ) )->setKeyField( 'file_id' ) );
|
||||
$downloads = array();
|
||||
if ( count( $select ) )
|
||||
$row = [];
|
||||
foreach ( $headers as $key => $header )
|
||||
{
|
||||
foreach ( $select as $row )
|
||||
if( isset( $file[ $key ] ) )
|
||||
{
|
||||
$file = File::constructFromData( $files[ $row['dfid'] ] );
|
||||
$downloads[] = array(
|
||||
'dfid' => $row['dfid'],
|
||||
'downloads' => $row['downloads'],
|
||||
'file' => Theme::i()->getTemplate( 'global', 'core', 'global' )->basicUrl( $file->url(), TRUE, $file->name )
|
||||
);
|
||||
$row[] = $file[ $key ];
|
||||
}
|
||||
else
|
||||
{
|
||||
$row[] = '';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$pagination = Theme::i()->getTemplate( 'global', 'core', 'global' )->pagination(
|
||||
Url::internal( 'app=downloads&module=stats&controller=topdownloads' )->setQueryString( $params ),
|
||||
ceil( $total / static::PER_PAGE ),
|
||||
$page,
|
||||
static::PER_PAGE,
|
||||
FALSE
|
||||
);
|
||||
fputcsv($fh, $row);
|
||||
}
|
||||
|
||||
Output::i()->output .= Theme::i()->getTemplate( 'global', 'core' )->message( Member::loggedIn()->language()->addToStack( 'stats_include_hidden_content' ), 'info' );
|
||||
Output::i()->output .= Theme::i()->getTemplate( 'stats' )->topDownloadsTable( $downloads, $pagination );
|
||||
Output::i()->title = Member::loggedIn()->language()->addToStack( 'menu__downloads_stats_topdownloads' );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Return the no results message */
|
||||
Output::i()->output .= Theme::i()->getTemplate( 'global', 'core' )->block( Member::loggedIn()->language()->addToStack( 'menu__downloads_stats_topdownloads' ), Member::loggedIn()->language()->addToStack( 'no_results' ), FALSE, 'i-padding_3', NULL, TRUE );
|
||||
}
|
||||
fclose( $fh );
|
||||
$csv = file_get_contents( $tmpFile );
|
||||
$name = 'topdownloads_' . date('Y-m-d');
|
||||
|
||||
Member::loggedIn()->language()->parseOutputForDisplay( $name );
|
||||
Member::loggedIn()->language()->parseOutputForDisplay( $csv );
|
||||
Output::i()->sendOutput( $csv, 200, 'text/csv', array( 'Content-Disposition' => Output::getContentDisposition( 'attachment', "{$name}.csv" ) ), FALSE, FALSE, FALSE );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user