jenkins-bot has submitted this change and it was merged.

Change subject: Replace drop down menus in Special:Newsletters with table
......................................................................


Replace drop down menus in Special:Newsletters with table

Added Javascript modules and API file to interact with the radio
buttons. First version of table with four columns - Name of newsletter
and description of the newsletter, Subscriber count, Subcribe/Unsubscribe

Bug: T103813
Change-Id: Ic52ead998363dc0a8860fcdb5d46bb30ae590dc0
---
M Newsletter.php
M i18n/en.json
M i18n/qqq.json
A includes/ApiNewsletter.php
M includes/SpecialNewsletters.php
A modules/ext.newsletter.js
6 files changed, 236 insertions(+), 152 deletions(-)

Approvals:
  01tonythomas: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/Newsletter.php b/Newsletter.php
index 87b260b..56b9a6f 100755
--- a/Newsletter.php
+++ b/Newsletter.php
@@ -30,11 +30,30 @@
 $wgAutoloadClasses['SpecialNewsletterManage'] = __DIR__ . 
'/includes/SpecialNewsletterManage.php';
 $wgAutoloadClasses['SpecialNewsletters'] = __DIR__ . 
'/includes/SpecialNewsletters.php';
 $wgAutoloadClasses['EchoNewsletterFormatter'] = __DIR__ . 
'/includes/EchoNewsletterFormatter.php';
+$wgAutoloadClasses['NewsletterTablePager'] = __DIR__ . 
'/includes/SpecialNewsletters.php';
+$wgAutoloadClasses['ApiNewsletter'] = __DIR__ . '/includes/ApiNewsletter.php';
 
 $wgSpecialPages['NewsletterCreate'] = 'SpecialNewsletterCreate';
 $wgSpecialPages['NewsletterManage'] = 'SpecialNewsletterManage';
 $wgSpecialPages['Newsletters'] = 'SpecialNewsletters';
 
+$wgAPIModules['newsletterapi'] = 'ApiNewsletter';
+
+$wgResourceModules['ext.newsletter'] = array(
+       'scripts' => 'modules/ext.newsletter.js',
+       'dependencies' => array(
+               'jquery.cookie',
+               'jquery.tabIndex',
+               'mediawiki.jqueryMsg',
+               'mediawiki.api',
+               'jquery.confirmable'
+       ),
+
+       'localBasePath' => __DIR__,
+       'remoteExtPath' => 'Newsletter',
+
+);
+
 //Register Hooks
 $wgHooks['LoadExtensionSchemaUpdates'][] = 
'NewsletterHooks::onLoadExtensionSchemaUpdates';
 $wgHooks['BeforeCreateEchoEvent'][] = 
'NewsletterHooks::onBeforeCreateEchoEvent';
diff --git a/i18n/en.json b/i18n/en.json
index 74c99e8..a9380ca 100755
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -11,18 +11,22 @@
        "createnewsletter-section": "Create newsletter",
        "newsletter-create-confirmation": "Good job! You just created a new 
newsletter.",
        "issue-announce-confirmation": "Good job! You just announced a new 
issue of your newsletter.",
-       "newsletters": "Subscribe or unsubscribe newsletters",
+       "newsletters": "Newsletters",
        "newsletter-subscribe-section": "Subscribe newsletters",
        "newsletter-unsubscribe-section": "Unsubscribe newsletters",
        "newsletter-subscribe-confirmation": "You are now subscribed to the 
newsletter.",
        "newsletter-unsubscribe-confirmation": "You are now unsubscribed from 
the newsletter.",
-       "subscribe-button-label": "Subscribe",
-       "unsubscribe-button-label": "Unsubscribe",
+       "subscribe-button-label": "Yes",
+       "unsubscribe-button-label": "No",
        "available-newsletters-field-label": "Available newsletters",
        "subscribed-newsletters-field-label": "Subscribed newsletters",
        "echo-category-title-newsletter": "Newsletters",
        "echo-pref-tooltip-newsletter": "Notify me when any of the newsletters 
to which I have subscribed to announces a new issue.",
        "notification-newsletter-title": "$1 has announced an issue",
        "notification-newsletter-flyout": "$1 has announced a new issue",
-       "notification-link-text-new-issue": "View new issue"
+       "notification-link-text-new-issue": "View new issue",
+       "newsletter-header-name": "Name of Newsletter",
+       "newsletter-header-description": "Description",
+       "newsletter-header-action": "Subscribed ?",
+       "newsletter-header-subscriber_count": "Subscriber count"
 }
diff --git a/i18n/qqq.json b/i18n/qqq.json
index b69c281..da62ed5 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -24,5 +24,9 @@
        "echo-pref-tooltip-newsletter": "Short description of the newsletter 
notification category.\n{{Related|Echo-pref-tooltip}}",
        "notification-newsletter-title": "Title message of Echo notification to 
notify a user of a new issue from a newsletter he/she has subscribed to. 
Parameters:\n* $1 is is the name of the newsletter.",
        "notification-newsletter-flyout": "Flyout message of Echo notification 
to notify the user of a new issue from a newsletter he/she has subscribed to. 
Parameters:\n* $1 is the name of the newsletter.",
-       "notification-link-text-new-issue": "Label of the primary link of the 
notification-newsletter-flyout, which on clicking navigates the user to the 
newly announced issue of a newsletter."
+       "notification-link-text-new-issue": "Label of the primary link of the 
notification-newsletter-flyout, which on clicking navigates the user to the 
newly announced issue of a newsletter.",
+       "newsletter-header-name": "Label of the first column of table in 
[[Special:Newsletters]] which lists the names of newsletters",
+       "newsletter-header-description": "Label of the second column of table 
in [[Special:Newsletters]] which displays a description about the newsletter",
+       "newsletter-header-subscriber_count": "Label of the third column of 
table in [[Special:Newsletters]] which gives the subscriber count of 
corresponding newsletter",
+       "newsletter-header-action": "Label of the fourth column of table in 
[[Special:Newsletters]] which provides radio buttons to subscribe/unsubscribe 
newsletters"
 }
\ No newline at end of file
diff --git a/includes/ApiNewsletter.php b/includes/ApiNewsletter.php
new file mode 100644
index 0000000..985748e
--- /dev/null
+++ b/includes/ApiNewsletter.php
@@ -0,0 +1,37 @@
+<?php
+
+class ApiNewsletter extends ApiBase {
+       public function execute() {
+               if ( $this->getMain()->getVal( 'todo' ) === 'subscribe' ) {
+                       $dbw = wfGetDB( DB_MASTER );
+                       $rowData = array(
+                               'newsletter_id' => $this->getMain()->getVal( 
'newsletterId' ),
+                               'subscriber_id' => $this->getUser()->getId()
+                       );
+                       $dbw->insert( 'nl_subscriptions', $rowData, __METHOD__ 
);
+               }
+
+               if ( $this->getMain()->getVal( 'todo' ) === 'unsubscribe' ) {
+                       $dbw = wfGetDB( DB_MASTER );
+                       $rowData = array(
+                               'newsletter_id' => $this->getMain()->getVal( 
'newsletterId' ),
+                               'subscriber_id' => $this->getUser()->getId(),
+                       );
+                       $dbw->delete( 'nl_subscriptions', $rowData, __METHOD__ 
);
+               }
+
+       }
+
+       public function getAllowedParams() {
+               return array_merge( parent::getAllowedParams(), array(
+                       'newsletterId' => array (
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
+                       'todo' => array (
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       )
+               ) );
+       }
+}
\ No newline at end of file
diff --git a/includes/SpecialNewsletters.php b/includes/SpecialNewsletters.php
index 7b979d7..52ce13c 100644
--- a/includes/SpecialNewsletters.php
+++ b/includes/SpecialNewsletters.php
@@ -4,187 +4,172 @@
  *
  */
 class SpecialNewsletters extends SpecialPage {
+
+       static $fields = array(
+               'nl_name' => 'name',
+               'nl_desc' => 'description',
+               'subscriber_count' => 'subscriber_count',
+               'action' => 'action'
+       );
+
+       # Array containing all newsletter ids in nl_subscriptions table
+       static $allSubscribedNewsletterId = array();
+
+       # Array containing all newsletter ids to which the logged in user is 
subscribed to
+       static $subscribedNewsletterId = array();
+
+       # Subscriber count
+       static $subscriberCount = array();
+
        public function __construct() {
                parent::__construct( 'Newsletters' );
+               self::getSubscribedNewsletters( $this->getUser()->getId() );
        }
 
        public function execute( $par ) {
                $this->setHeaders();
                $this->requireLogin();
-               $subscribeNewsletterArray = $this->getSubscribeFormFields();
+               $out = $this->getOutput();
+               $this->getOutput()->addModules( 'ext.newsletter' );
+               $pager = new NewsletterTablePager();
 
-               # Create HTML form
-               $subscribeNewsletterForm = new HTMLForm( 
$subscribeNewsletterArray, $this->getContext(), 'subscribenewsletterform' );
-               $subscribeNewsletterForm->setSubmitCallback( array( 
'SpecialNewsletters', 'onSubscribe' ) );
-               $subscribeNewsletterForm->setWrapperLegendMsg( 
'newsletter-subscribe-section' );
-               $subscribeNewsletterForm->setSubmitText( $this->msg( 
'subscribe-button-label' )->text() );
-               $subscribeNewsletterForm->show();
-
-               $userSubscriptionsArray = $this->getSubscriptionsFormFields( 
$this->getUser()->getId() );
-               $userSubscriptionsForm = new HTMLForm( $userSubscriptionsArray, 
$this->getContext(), 'usersubscriptionsform' );
-               $userSubscriptionsForm->setSubmitCallback( array( 
'SpecialNewsletters', 'onUnSubscribe' ) );
-               $userSubscriptionsForm->setWrapperLegendMsg( 
'newsletter-unsubscribe-section' );
-               $userSubscriptionsForm->setSubmitText( $this->msg( 
'unsubscribe-button-label' )->text() );
-               $userSubscriptionsForm->show();
-       }
-
-       /**
-        * Function to get user entries from HTML form for subscribing to a 
newsletter
-        *
-        * @return array
-        */
-       protected function getSubscribeFormFields() {
-               $dbr = wfGetDB( DB_SLAVE );
-               $res = $dbr->select(
-                       'nl_newsletters',
-                       array( 'nl_name'),
-                       '',
-                       __METHOD__
-               );
-               $newsletterNames = array();
-               $defaultOption = array( '' => null );
-               foreach( $res as $row ) {
-                       $newsletterNames[$row->nl_name] = $row->nl_name;
-               }
-
-               return array(
-                       'available-newsletters' => array(
-                               'required' => true,
-                               'type' => 'select',
-                               'label' => $this->msg( 
'available-newsletters-field-label' )->text(),
-                               'options' => array_merge( $defaultOption, 
$newsletterNames ),
-                       ),
-                       'subscriber' => array(
-                               'type' => 'hidden',
-                               'default' => $this->getUser()->getId()
-                       )
-               );
-       }
-
-       /**
-        * Perform insert query on subscriptions table with data retrieved from 
HTML
-        * form when a user subscribes to a newsletter
-        *
-        * @param array $formData The data entered by user in the form
-        * @return bool
-        */
-       static function onSubscribe( array $formData ) {
-               if ( isset( $formData['available-newsletters'] ) && isset( 
$formData['subscriber'] ) ) {
-                       $dbr = wfGetDB( DB_SLAVE );
-                       //get newsletter id user is subscribing to
-                       $res = $dbr->select(
-                               'nl_newsletters',
-                               array('nl_id'),
-                               array('nl_name' => 
$formData['available-newsletters']),
-                               __METHOD__
+               if ( $pager->getNumRows() > 0 ) {
+                       $out->addHTML(
+                               $pager->getNavigationBar() .
+                               $pager->getBody() .
+                               $pager->getNavigationBar()
                        );
-                       foreach ( $res as $row ) {
-                               $newsletterId = $row->nl_id;
-                       }
-                       if ( isset( $newsletterId ) ) {
-                               $dbw = wfGetDB( DB_MASTER );
-                               $rowData = array(
-                                       'newsletter_id' => $newsletterId,
-                                       'subscriber_id' => 
$formData['subscriber'],
-                               );
-                               try {
-                                       $dbw->insert( 'nl_subscriptions', 
$rowData, __METHOD__ );
-                               } catch ( DBQueryError $e ) {
-                                       return 'You are already subscribed to 
this newsletter!';
-                               }
-                               
RequestContext::getMain()->getOutput()->addWikiMsg( 
'newsletter-subscribe-confirmation' );
-
-                               return true;
-                       } else {
-                               return 'Invalid newsletter name entered. Please 
try again';
-                       }
                }
-
-               return false;
        }
 
-       /**
-        *Get user entries from HTML form to un-subscribe from newsletters
-        *
-        * @param integer $id User id of logged in user
-        * @return array
-        */
-       protected function getSubscriptionsFormFields( $id ) {
+       static function getSubscribedNewsletters( $id ) {
                $dbr = wfGetDB( DB_SLAVE );
-               //get newsletter ids to which user is subscribed to
                $res = $dbr->select(
                        'nl_subscriptions',
                        array( 'newsletter_id' ),
                        array( 'subscriber_id' => $id ),
                        __METHOD__
                );
-               $newsletterIds = array();
                foreach( $res as $row ) {
-                       $newsletterIds[] = $row->newsletter_id;
+                       self::$subscribedNewsletterId[] = $row->newsletter_id;
                }
 
-               $newsletterNames = array();
-               $defaultOption = array( '' => null );
-               //get newsletter names
-               foreach ( $newsletterIds as $value ) {
-                       $result = $dbr->select(
-                               'nl_newsletters',
-                               array( 'nl_name' ),
-                               array( 'nl_id' => $value ),
+               $resl = $dbr->select(
+                       'nl_subscriptions',
+                       array( 'newsletter_id' ),
+                       array(),
+                       __METHOD__
+               );
+
+               foreach( $resl as $row ){
+                       $result = $dbr->selectRowCount(
+                               'nl_subscriptions',
+                               array(),
+                               array( 'newsletter_id' => $row->newsletter_id ),
                                __METHOD__
                        );
-                       foreach( $result as $row ) {
-                               $newsletterNames[$row->nl_name] = $row->nl_name;
+                       self::$allSubscribedNewsletterId[] = 
$row->newsletter_id;
+                       self::$subscriberCount[$row->newsletter_id] = $result;
+               }
+       }
+}
+
+
+
+
+class NewsletterTablePager extends TablePager {
+
+       function getFieldNames() {
+               static $headers = null;
+               if ( is_null( $headers ) ) {
+                       $headers = array();
+                       foreach( SpecialNewsletters::$fields as $field => 
$property ) {
+                               $headers[$field] = $this->msg( 
"newsletter-header-$property" )->text();
                        }
                }
-               return array(
-                       'subscribed-newsletters' => array(
-                               'required' => true,
-                               'type' => 'select',
-                               'label' => $this->msg( 
'subscribed-newsletters-field-label' )->text(),
-                               'options' => array_merge( $defaultOption, 
$newsletterNames )
-                       ),
-                       'un-subscriber' => array(
-                               'type' => 'hidden',
-                               'default' => $this->getUser()->getId()
+
+               return $headers;
+       }
+
+       function getQueryInfo() {
+               $info = array(
+                       'tables' => array( 'nl_newsletters' ),
+                       'fields' => array(
+                               'nl_name',
+                               'nl_desc',
+                               'nl_id'
                        )
                );
+
+               return $info;
        }
 
-       /**
-        * Perform deletion on subscriptions table when a user un-subscribes
-        *
-        * @param array $formData The data entered by user in the form
-        * @return bool
-        */
-       static function onUnSubscribe( array $formData ) {
-               if ( isset( $formData['subscribed-newsletters'] ) && isset( 
$formData['un-subscriber'] ) ) {
-                       $dbr = wfGetDB( DB_SLAVE );
-                       //remove entry from subscriptions table
-                       $res = $dbr->select(
-                               'nl_newsletters',
-                               array( 'nl_id' ),
-                               array( 'nl_name' => 
$formData['subscribed-newsletters'] ),
-                               __METHOD__
-                       );
-                       foreach ( $res as $row ) {
-                               $newsletterId = $row->nl_id;
-                       }
-                       if ( isset ( $newsletterId ) ) {
-                               $dbw = wfGetDB( DB_MASTER );
-                               $rowData = array(
-                                       'newsletter_id' => $newsletterId,
-                                       'subscriber_id' => 
$formData['un-subscriber'],
+       function formatValue( $field, $value ) {
+               switch( $field ) {
+                       case 'nl_name':
+                               $dbr = wfGetDB( DB_SLAVE );
+                               $res = $dbr->select(
+                                       'nl_newsletters',
+                                       array( 'nl_main_page_id' ),
+                                       array( 'nl_name' => $value ),
+                                       __METHOD__
                                );
-                               $dbw->delete( 'nl_subscriptions', $rowData, 
__METHOD__ );
-                               
RequestContext::getMain()->getOutput()->addWikiMsg( 
'newsletter-unsubscribe-confirmation' );
 
-                               return true;
-                       } else {
-                               return 'Invalid newsletter name entered. Please 
try again';
-                       }
+                               $mainPageId = '';
+                               foreach( $res as $row ) {
+                                       $mainPageId = $row->nl_main_page_id;
+                               }
+
+                               $url = $mainPageId ? Title::newFromID( 
$mainPageId )->getFullURL() : "#";
+
+                               return '<a href="' . $url . '">'. $value . 
'</a>';
+                       case 'nl_desc': return $value;
+                       case 'subscriber_count':
+                               return HTML::element( 'input',
+                                       array(
+                                       'type' => 'textbox',
+                                       'readonly' => 'true',
+                                       'id' => 
'newsletter-'.$this->mCurrentRow->nl_id,
+                                       'value' => in_array( 
$this->mCurrentRow->nl_id, SpecialNewsletters::$allSubscribedNewsletterId ) ?
+                                               
SpecialNewsletters::$subscriberCount[$this->mCurrentRow->nl_id] : 0,
+
+                                       ) );
+                       case 'action' :
+                               $radioSubscribe = Html::element(
+                                                       'input',
+                                                       array(
+                                                       'type' => 'radio',
+                                                       'name' => 'nl_id-' . 
$this->mCurrentRow->nl_id,
+                                                       'value' => 'subscribe',
+                                                       'checked' => in_array( 
$this->mCurrentRow->nl_id,
+                                                                       
SpecialNewsletters::$subscribedNewsletterId ) ? true : false,
+                                                       )
+                                       ).$this->msg( 'subscribe-button-label' 
);
+                               $radioUnSubscribe = Html::element(
+                                                       'input',
+                                                       array(
+                                                       'type' => 'radio',
+                                                       'name' => 'nl_id-' . 
$this->mCurrentRow->nl_id,
+                                                       'value' => 
'unsubscribe',
+                                                       'checked' => in_array( 
$this->mCurrentRow->nl_id,
+                                                                       
SpecialNewsletters::$subscribedNewsletterId ) ? false : true,
+                                                       )
+                               ).$this->msg( 'unsubscribe-button-label' );
+
+                               return $radioSubscribe . $radioUnSubscribe;
                }
+       }
 
+       function endQuery( $value ) {
+               $this->getOutput()->addWikiMsg( 
'newsletter-create-confirmation' );
+       }
+
+       function getDefaultSort() {
+               return 'nl_name';
+       }
+
+       function isFieldSortable( $field ) {
                return false;
        }
+
 }
\ No newline at end of file
diff --git a/modules/ext.newsletter.js b/modules/ext.newsletter.js
new file mode 100644
index 0000000..8bdb608
--- /dev/null
+++ b/modules/ext.newsletter.js
@@ -0,0 +1,35 @@
+/**
+ * Javascript for radio buttons
+ *
+ */
+( function ( $, mw ) {
+       'use strict';
+
+       var api = new mw.Api();
+       $( 'input[type=radio][value=subscribe]' ).change( function() {
+               var newsletterId = ( this.name ).substr( ( this.name ).indexOf( 
"-" ) + 1 );
+               api.post( {
+                       action: 'newsletterapi',
+                       newsletterId: newsletterId,
+                       todo: 'subscribe'
+               } ).done( function ( data ) {
+                       console.log( data );
+               } );
+        document.getElementById( 'newsletter-' + newsletterId).value++;
+
+       } );
+
+       $( 'input[type=radio][value=unsubscribe]' ).change( function() {
+               var newsletterId = ( this.name ).substr( ( this.name ).indexOf( 
"-" ) + 1 );
+               api.post( {
+                       action: 'newsletterapi',
+                       newsletterId: newsletterId,
+                       todo: 'unsubscribe'
+
+               } ).done( function ( data ) {
+                       console.log( data );
+               } );
+        document.getElementById( 'newsletter-' + newsletterId).value--;
+       } );
+
+} )( jQuery, mediaWiki );

-- 
To view, visit https://gerrit.wikimedia.org/r/223951
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic52ead998363dc0a8860fcdb5d46bb30ae590dc0
Gerrit-PatchSet: 18
Gerrit-Project: mediawiki/extensions/Newsletter
Gerrit-Branch: master
Gerrit-Owner: Tinaj1234 <[email protected]>
Gerrit-Reviewer: 01tonythomas <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: Qgil <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: Tinaj1234 <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to