summaryrefslogtreecommitdiff
path: root/modules/mod_components.php
blob: 4115ed301aa627a53a04206b8f3e13d973aa6772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/**
 * @package stock
 * @subpackage modules
 */

namespace Bitweaver\Stock;

use Bitweaver\KernelTools;
use Bitweaver\Users\RoleUser;
global $gQueryUserId, $gContent, $moduleParams;

$component = new StockComponent();

$display = true;

$listHash = $moduleParams->value;

if( !empty( $gContent ) && $gContent->getField( 'content_type_guid' ) == STOCKASSEMBLY_CONTENT_TYPE_GUID ) {
	$displayCount = empty( $gContent->mItems ) ? 0 : count( $gContent->mItems );
	$thumbCount = $gContent->mInfo['rows_per_page'] * $gContent->mInfo['cols_per_page'];
	$listHash['assembly_content_id'] = $gContent->mContentId;
	$display = $displayCount >= $thumbCount;
}

if( $display ) {
	$listHash['max_records'] = $module_rows ?? 3;
	if( $gQueryUserId ) {
		$listHash['user_id'] = $gQueryUserId;
	} elseif( !empty( $_REQUEST['user_id'] ) ) {
		$gBitSmarty->assign( 'userComponents', $_REQUEST['user_id'] );
		$listHash['user_id'] = $_REQUEST['user_id'];
	} elseif( !empty( $listHash['recent_users'] ) ) {
		$listHash['recent_users'] = true;
	}

	$sort_options = [ 'hits', 'created' ];
	$sort_mode = !empty( $listHash['sort_mode'] ) && in_array( $listHash['sort_mode'], $sort_options )
		? $listHash['sort_mode'].'_desc' : 'random';

	$listHash['sort_mode'] = $sort_mode;

	$components = $component->getList( $listHash );

	if( empty( $title ) && $components ) {
		$moduleTitle = match( $sort_mode ) {
			'random'  => 'Random',
			'created_desc' => 'Recent',
			'hits_desc'    => 'Popular',
			default        => 'Random',
		};

		$moduleTitle .= ' Components';
		$moduleTitle = KernelTools::tra( $moduleTitle );

		if( !empty( $listHash['user_id'] ) ) {
			$moduleTitle .= ' '.KernelTools::tra('by').' '.RoleUser::getDisplayNameFromHash( current( $components ), true );
		} elseif( !empty( $listHash['recent_users'] ) ) {
			$moduleTitle .= ' '.KernelTools::tra( 'by' ).' <a href="'.USERS_PKG_URL.'">'.KernelTools::tra( 'New Users' ).'</a>';
		}

		$gBitSmarty->assign( 'moduleTitle', $moduleTitle );
	} else {
		$gBitSmarty->assign( 'moduleTitle', $title );
	}

	$gBitSmarty->assign( 'modComponents', $components );
	$gBitSmarty->assign( 'module_params', $listHash );
	$gBitSmarty->assign( 'maxlen', isset( $listHash['maxlen'] ) ? (int)$listHash['maxlen'] : 0 );
	$gBitSmarty->assign( 'maxlendesc', isset( $listHash['maxlendesc'] ) ? (int)$listHash['maxlendesc'] : 0 );
}