follow-up to r85876: fix some coding style issues (some of which were not introduced...
[lhc/web/wiklou.git] / includes / specials / SpecialEmailuser.php
index 2d058a9..b3ba9c5 100644 (file)
  */
 class SpecialEmailUser extends UnlistedSpecialPage {
        protected $mTarget;
-       
-       public function __construct(){
+
+       public function __construct() {
                parent::__construct( 'Emailuser' );
        }
-       
-       protected function getFormFields(){
+
+       protected function getFormFields() {
                global $wgUser;
                return array(
                        'From' => array(
                                'type' => 'info',
                                'raw' => 1,
-                               'default' => $wgUser->getSkin()->link( 
-                                       $wgUser->getUserPage(), 
-                                       htmlspecialchars( $wgUser->getName() ) 
+                               'default' => $wgUser->getSkin()->link(
+                                       $wgUser->getUserPage(),
+                                       htmlspecialchars( $wgUser->getName() )
                                ),
                                'label-message' => 'emailfrom',
                                'id' => 'mw-emailuser-sender',
@@ -49,8 +49,8 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                        'To' => array(
                                'type' => 'info',
                                'raw' => 1,
-                               'default' => $wgUser->getSkin()->link( 
-                                       $this->mTargetObj->getUserPage(), 
+                               'default' => $wgUser->getSkin()->link(
+                                       $this->mTargetObj->getUserPage(),
                                        htmlspecialchars( $this->mTargetObj->getName() )
                                ),
                                'label-message' => 'emailto',
@@ -82,26 +82,17 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                        ),
                );
        }
-       
+
        public function execute( $par ) {
                global $wgRequest, $wgOut, $wgUser;
 
                $this->setHeaders();
                $this->outputHeader();
                $wgOut->addModuleStyles( 'mediawiki.special' );
-
                $this->mTarget = is_null( $par )
                        ? $wgRequest->getVal( 'wpTarget', $wgRequest->getVal( 'target', '' ) )
                        : $par;
-                       
-               $ret = self::getTarget( $this->mTarget );
-               if( $ret instanceof User ){
-                       $this->mTargetObj = $ret;
-               } else {
-                       $wgOut->showErrorPage( "{$ret}title", "{$ret}text" );
-                       return false;
-               }
-       
+               // error out if sending user cannot do this
                $error = self::getPermissionsError( $wgUser, $wgRequest->getVal( 'wpEditToken' ) );
                switch ( $error ) {
                        case null:
@@ -126,7 +117,19 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                                $wgOut->showErrorPage( $title, $msg, $params );
                                return;
                }
-               
+               // Got a valid target user name? Else ask for one.
+               $ret = self::getTarget( $this->mTarget );
+               if( !$ret instanceof User ) {
+                       if( $this->mTarget != '' ) {
+                               $ret = ( $ret == 'notarget' ) ? 'emailnotarget' : ( $ret . 'text' );
+                               $wgOut->addHTML( '<p class="error">' . wfMessage( $ret )->parse() . '</p>' );
+                       }
+                       $wgOut->addHTML( self::userForm( $this->mTarget ) );
+                       return false;
+               }
+
+               $this->mTargetObj = $ret;
+
                $form = new HTMLForm( $this->getFormFields() );
                $form->addPreText( wfMsgExt( 'emailpagetext', 'parseinline' ) );
                $form->setSubmitText( wfMsg( 'emailsend' ) );
@@ -134,16 +137,16 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                $form->setSubmitCallback( array( __CLASS__, 'submit' ) );
                $form->setWrapperLegend( wfMsgExt( 'email-legend', 'parsemag' ) );
                $form->loadData();
-               
-               if( !wfRunHooks( 'EmailUserForm', array( &$form ) ) ){
+
+               if( !wfRunHooks( 'EmailUserForm', array( &$form ) ) ) {
                        return false;
                }
-               
-               $wgOut->setPagetitle( wfMsg( 'emailpage' ) );
+
+               $wgOut->setPageTitle( wfMsg( 'emailpage' ) );
                $result = $form->show();
-               
-               if( $result === true || ( $result instanceof Status && $result->isGood() ) ){
-                       $wgOut->setPagetitle( wfMsg( 'emailsent' ) );
+
+               if( $result === true || ( $result instanceof Status && $result->isGood() ) ) {
+                       $wgOut->setPageTitle( wfMsg( 'emailsent' ) );
                        $wgOut->addWikiMsg( 'emailsenttext' );
                        $wgOut->returnToMain( false, $this->mTargetObj->getUserPage() );
                }
@@ -160,15 +163,15 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                        wfDebug( "Target is empty.\n" );
                        return 'notarget';
                }
-               
+
                $nu = User::newFromName( $target );
                if( !$nu instanceof User || !$nu->getId() ) {
                        wfDebug( "Target is invalid user.\n" );
                        return 'notarget';
-               } else if ( !$nu->isEmailConfirmed() ) {
+               } elseif ( !$nu->isEmailConfirmed() ) {
                        wfDebug( "User has no valid email.\n" );
                        return 'noemail';
-               } else if ( !$nu->canReceiveEmail() ) {
+               } elseif ( !$nu->canReceiveEmail() ) {
                        wfDebug( "User does not allow user emails.\n" );
                        return 'nowikiemail';
                }
@@ -185,15 +188,15 @@ class SpecialEmailUser extends UnlistedSpecialPage {
         */
        public static function getPermissionsError( $user, $editToken ) {
                global $wgEnableEmail, $wgEnableUserEmail;
-               if( !$wgEnableEmail || !$wgEnableUserEmail ){
+               if( !$wgEnableEmail || !$wgEnableUserEmail ) {
                        return 'usermaildisabled';
                }
-               
+
                if( !$user->isAllowed( 'sendemail' ) ) {
                        return 'badaccess';
                }
-               
-               if( !$user->isEmailConfirmed() ){
+
+               if( !$user->isEmailConfirmed() ) {
                        return 'mailnologin';
                }
 
@@ -217,9 +220,26 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                return null;
        }
 
+       /**
+        * Form to ask for target user name.
+        *
+        * @param $name String: user name submitted.
+        * @return String: form asking for user name.
+        */
+       static function userForm( $name ) {
+               $string = Xml::openElement( 'form', array( 'method' => 'get', 'action' => '', 'id' => 'askusername' ) ) .
+                               Xml::openElement( 'fieldset' ) .
+                               Html::rawElement( 'legend', null, wfMessage( 'emailtarget' )->parse() ) .
+                               Xml::inputLabel( wfMessage( 'emailusername' )->text(), 'target', 'emailusertarget', 30, $name ) . ' ' .
+                               Xml::submitButton( wfMessage( 'emailusernamesubmit' )->text() ) .
+                               Xml::closeElement( 'fieldset' ) .
+                               Xml::closeElement( 'form' ) . "\n";
+               return $string;
+       }
+
        /**
         * Really send a mail. Permissions should have been checked using
-        * getPermissionsError(). It is probably also a good 
+        * getPermissionsError(). It is probably also a good
         * idea to check the edit token and ping limiter in advance.
         *
         * @return Mixed: Status object, or potentially a String on error
@@ -229,7 +249,7 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                global $wgUser, $wgUserEmailUseReplyTo;
 
                $target = self::getTarget( $data['Target'] );
-               if( !$target instanceof User ){
+               if( !$target instanceof User ) {
                        return wfMsgExt( $target . 'text', 'parse' );
                }
                $to = new MailAddress( $target );
@@ -239,17 +259,17 @@ class SpecialEmailUser extends UnlistedSpecialPage {
 
                // Add a standard footer and trim up trailing newlines
                $text = rtrim( $text ) . "\n\n-- \n";
-               $text .= wfMsgExt( 
+               $text .= wfMsgExt(
                        'emailuserfooter',
-                       array( 'content', 'parsemag' ), 
-                       array( $from->name, $to->name ) 
+                       array( 'content', 'parsemag' ),
+                       array( $from->name, $to->name )
                );
 
                $error = '';
                if( !wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$text, &$error ) ) ) {
                        return $error;
                }
-               
+
                if( $wgUserEmailUseReplyTo ) {
                        // Put the generic wiki autogenerated address in the From:
                        // header and reserve the user for Reply-To.
@@ -284,12 +304,12 @@ class SpecialEmailUser extends UnlistedSpecialPage {
                        return $status;
                } else {
                        // if the user requested a copy of this mail, do this now,
-                       // unless they are emailing themselves, in which case one 
+                       // unless they are emailing themselves, in which case one
                        // copy of the message is sufficient.
                        if ( $data['CCMe'] && $to != $from ) {
                                $cc_subject = wfMsg(
-                                       'emailccsubject', 
-                                       $target->getName(), 
+                                       'emailccsubject',
+                                       $target->getName(),
                                        $subject
                                );
                                wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );