summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/UploadWizard/includes/CampaignPageFormatter.php
blob: 14f512852ffc1156d7541c960a0882617d9ce060 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
 * Upload Campaign Formatter
 *
 * @file
 * @ingroup Extensions
 * @ingroup UploadWizard
 *
 * @author Yuvi Panda <yuvipanda@gmail.com>
 */

/**
 * Helper class to produce formatted HTML output for Campaigns
 */
class CampaignPageFormatter {
	/** @var UploadWizardCampaign|null $campaign */
	protected $campaign = null;
	/** @var IContextSource|null $context */
	protected $context = null;

	public function __construct( $campaign, $context = null ) {
		$this->campaign = $campaign;
		if ( $context === null ) {
			$this->context = RequestContext::getMain();
		} else {
			$this->context = $context;
		}
	}

	private function isCampaignExtensionEnabled() {
		global $wgResourceModules;
		return isset( $wgResourceModules['ext.campaigns'] );
	}

	public function generateReadHtml() {
		$config = $this->campaign->getParsedConfig();

		$campaignTitle = array_key_exists( 'title', $config ) ?
			$config['title'] :
			$this->campaign->getName();
		$campaignDescription = array_key_exists( 'description', $config ) ? $config['description'] : '';
		$campaignViewMoreLink = $this->campaign->getTrackingCategory()->getFullURL();

		$gallery = ImageGalleryBase::factory( 'packed-hover' );
		$gallery->setContext( $this->context );
		$gallery->setWidths( 180 );
		$gallery->setHeights( 180 );
		$gallery->setShowBytes( false );

		$this->context->getOutput()->setCdnMaxage(
			UploadWizardConfig::getSetting( 'campaignSquidMaxAge' )
		);
		$this->context->getOutput()->setHTMLTitle( $this->context->msg( 'pagetitle', $campaignTitle ) );
		$this->context->getOutput()->enableOOUI();

		$images = $this->campaign->getUploadedMedia();

		if ( $this->context->getUser()->isAnon() ) {
			$urlParams = [ 'returnto' => $this->campaign->getTitle()->getPrefixedText() ];

			if ( $this->isCampaignExtensionEnabled() ) {
				$campaignTemplate = UploadWizardConfig::getSetting( 'campaignCTACampaignTemplate' );
				$urlParams['campaign'] = str_replace( '$1', $this->campaign->getName(), $campaignTemplate );
			}
			$createAccountUrl = Skin::makeSpecialUrlSubpage( 'UserLogin', 'signup', $urlParams );
			$uploadLink = new OOUI\ButtonWidget( [
				'label' => wfMessage( 'mwe-upwiz-campaign-create-account-button' )->text(),
				'flags' => [ 'progressive', 'primary' ],
				'href' => $createAccountUrl
			] );
		} else {
			$uploadUrl = Skin::makeSpecialUrl(
				'UploadWizard', [ 'campaign' => $this->campaign->getName() ]
			);
			$uploadLink = new OOUI\ButtonWidget( [
				'label' => wfMessage( 'mwe-upwiz-campaign-upload-button' )->text(),
				'flags' => [ 'progressive', 'primary' ],
				'href' => $uploadUrl
			] );
		}

		if ( count( $images ) === 0 ) {
			$body = Html::element(
				'div',
				[ 'id' => 'mw-campaign-no-uploads-yet' ],
				wfMessage( 'mwe-upwiz-campaign-no-uploads-yet' )->plain()
			);
		} else {
			foreach ( $images as $image ) {
				$gallery->add( $image );
			}

			$body =
				Html::rawElement( 'div', [ 'id' => 'mw-campaign-images' ], $gallery->toHTML() ) .
				Html::rawElement( 'a',
					[ 'id' => 'mw-campaign-view-all', 'href' => $campaignViewMoreLink ],
					Html::rawElement(
						'span',
						[ 'class' => 'mw-campaign-chevron mw-campaign-float-left' ], '&nbsp'
					) .
					wfMessage( 'mwe-upwiz-campaign-view-all-media' )->escaped() .
					Html::rawElement(
						'span',
						[ 'class' => 'mw-campaign-chevron mw-campaign-float-right' ], '&nbsp'
					)
				);
		}

		if ( UploadWizardConfig::getSetting( 'campaignExpensiveStatsEnabled' ) === true ) {
			$uploaderCount = $this->campaign->getTotalContributorsCount();
			$campaignExpensiveStats =
				Html::rawElement( 'div', [ 'class' => 'mw-campaign-number-container' ],
					Html::element( 'div', [ 'class' => 'mw-campaign-number' ],
						$this->context->getLanguage()->formatNum( $uploaderCount ) ) .
					Html::element( 'span',
						[ 'class' => 'mw-campaign-number-desc' ],
						wfMessage( 'mwe-upwiz-campaign-contributors-count-desc' )
						->numParams( $uploaderCount )
						->text()
					)
				);
		} else {
			$campaignExpensiveStats = '';
		}

		$uploadCount = $this->campaign->getUploadedMediaCount();
		$result =
			Html::rawElement( 'div', [ 'id' => 'mw-campaign-container' ],
				Html::rawElement( 'div', [ 'id' => 'mw-campaign-header' ],
					Html::rawElement( 'div', [ 'id' => 'mw-campaign-primary-info' ],
						// No need to escape these, since they are just parsed wikitext
						// Any stripping that needed to be done should've been done by the parser
						Html::rawElement( 'p', [ 'id' => 'mw-campaign-title' ], $campaignTitle ) .
						Html::rawElement( 'p', [ 'id' => 'mw-campaign-description' ], $campaignDescription ) .
					$uploadLink
					) .
					Html::rawElement( 'div', [ 'id' => 'mw-campaign-numbers' ],
						$campaignExpensiveStats .
						Html::rawElement( 'div', [ 'class' => 'mw-campaign-number-container' ],
							Html::element( 'div', [ 'class' => 'mw-campaign-number' ],
								$this->context->getLanguage()->formatNum( $uploadCount )
							) .
							Html::element( 'span',
								[ 'class' => 'mw-campaign-number-desc' ],
								wfMessage( 'mwe-upwiz-campaign-media-count-desc' )
								->numParams( $uploadCount )
								->text()
							)
						)
					)
				) .
				$body
			);
		return $result;
	}
}