[pLog-svn] r2868 - in plugins/branches/lifetype-1.0/mobile: class/action class/controller class/view templates/html-light

oscar at devel.lifetype.net oscar at devel.lifetype.net
Sun Jan 29 18:27:31 GMT 2006


Author: oscar
Date: 2006-01-29 18:27:30 +0000 (Sun, 29 Jan 2006)
New Revision: 2868

Added:
   plugins/branches/lifetype-1.0/mobile/class/action/mobileaddcommentaction.class.php
Modified:
   plugins/branches/lifetype-1.0/mobile/class/controller/mobilecontrollermap.properties.php
   plugins/branches/lifetype-1.0/mobile/class/view/mobileviewarticleview.class.php
   plugins/branches/lifetype-1.0/mobile/templates/html-light/postandcomments.template
Log:
Implemented support for posting comments via the 'mobile' plugin, as requested here: http://forums.lifetype.net/viewtopic.php?p=22663#22663


Added: plugins/branches/lifetype-1.0/mobile/class/action/mobileaddcommentaction.class.php
===================================================================
--- plugins/branches/lifetype-1.0/mobile/class/action/mobileaddcommentaction.class.php	2006-01-29 18:15:13 UTC (rev 2867)
+++ plugins/branches/lifetype-1.0/mobile/class/action/mobileaddcommentaction.class.php	2006-01-29 18:27:30 UTC (rev 2868)
@@ -0,0 +1,180 @@
+<?php
+
+    include_once( MOBILE_PLOG_CLASS_PATH."class/action/mobileaction.class.php" );
+    include_once( MOBILE_PLOG_CLASS_PATH."class/view/mobileerrorview.class.php" );
+    include_once( MOBILE_PLOG_CLASS_PATH."class/view/mobileviewarticleview.class.php" );
+    include_once( PLOG_CLASS_PATH."class/dao/articles.class.php" );
+    include_once( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
+    include_once( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );
+    include_once( PLOG_CLASS_PATH."class/data/validator/emailvalidator.class.php" );    
+    include_once( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );        
+    include_once( PLOG_CLASS_PATH."class/data/validator/httpurlvalidator.class.php" );
+    include_once( PLOG_CLASS_PATH."class/dao/articlenotifications.class.php" );
+    include_once( PLOG_CLASS_PATH."class/dao/articlecomments.class.php" );
+    include_once( PLOG_CLASS_PATH."class/template/cachecontrol.class.php" );
+    include_once( PLOG_CLASS_PATH."class/config/config.class.php" );
+    
+    class MobileAddCommentAction extends MobileAction
+    {
+        function MobileAddCommentAction( $actionInfo, $request )
+        {
+            $this->MobileAction( $actionInfo, $request ); 
+        
+			// change the validation mode of the form
+			$this->registerFieldValidator( "articleId", new IntegerValidator());
+			$this->_form->setFieldErrorMessage( "articleId", $this->_locale->tr("error_incorrect_article_id" ));
+			$this->registerFieldValidator( "blogId", new IntegerValidator());
+			$this->_form->setFieldErrorMessage( "blogId", $this->_locale->tr("error_incorrect_blog_id" ));
+			$this->registerFieldValidator( "userEmail", new EmailValidator(), true );
+			$this->_form->setFieldErrorMessage( "userEmail", $this->_locale->tr("error_incorrect_email_address" ));
+			$this->registerFieldValidator( "userName", new StringValidator());
+			$this->_form->setFieldErrorMessage( "userName", $this->_locale->tr("error_comment_without_name" ));
+			$this->registerFieldValidator( "commentText", new StringValidator());
+			$this->_form->setFieldErrorMessage( "commentText", $this->_locale->tr("error_comment_without_text"));
+			$this->registerFieldValidator( "userUrl", new HttpUrlValidator(), true );
+			$this->_form->setFieldErrorMessage( "userUrl", $this->_locale->tr("Invalid URL" ));
+			$view = new MobileErrorView( $this->_blogInfo, $this->_tInfo );
+			$view->setErrorMessage( "There has been an error validating the data!" );
+			$this->setValidationErrorView( $view );
+
+            $this->_fetchFields();
+        }
+
+        function _fetchFields()
+        {
+            $this->_articleId = $this->_request->getValue( "articleId" );
+            $this->_blogId    = $this->_request->getValue( "blogId" );
+            $this->_opId      = $this->_request->getValue( "op" );
+            $this->_parentId  = $this->_request->getValue( "parentId" );
+            if( $this->_parentId == null || $this->_parentId == "" )
+                $this->_parentId = 0;
+            $this->_userEmail = Textfilter::filterAllHTML($this->_request->getValue( "userEmail" ));
+            $this->_userUrl   = Textfilter::filterAllHTML($this->_request->getValue( "userUrl" ));
+            if( (strlen($this->_userUrl) != 0) &&
+                  (substr($this->_userUrl, 0, 7 ) != "http://" )){
+                $this->_userUrl = "http://".$this->_userUrl;
+            }
+            $this->_userName  = Textfilter::filterAllHTML($this->_request->getValue( "userName" ));
+            $this->_commentText = trim($this->_request->getValue( "commentText" ));
+            $this->_commentTopic = trim(Textfilter::filterAllHTML($this->_request->getValue( "commentTopic" )));
+            // remove all weird stuff from the comment text
+            $tf = new TextFilter();
+            $this->_commentText = $tf->xhtmlize($tf->filterHTML( $this->_commentText ));
+            // now, if the option is set, we 'beautify' the text typed by users
+            $config =& Config::getConfig();
+            if( $config->getValue( "beautify_comments_text" )) {
+            	$this->_commentText = $tf->autop($this->_commentText);
+            }
+        }
+        
+        /**
+         * Since this function contains this method, the controller will automatically
+         * call it before calling perform()
+         *
+         * @return True if all fields ok or false otherwise.
+         */
+        function validate()
+        {
+            // check if comments are enabled
+            $blogSettings = $this->_blogInfo->getSettings();
+            if( !$blogSettings->getValue( "comments_enabled" )) {
+            	$this->_view = new MobileErrorView( $this->_blogInfo, $this->_tInfo );
+            	$this->_view->setErrorMessage( $this->_locale->tr( "error_comments_not_enabled" ));
+                $this->setCommonData();
+
+                return false;
+            }
+			
+			return( parent::validate());
+        }
+        
+		/**
+		 * prepare a nicer error message. This method is only going to be executed whenver a validation
+		 * error happens (see Action::validate())
+		 *
+		 * @see Action::validate()
+		 */
+		function validationErrorProcessing()
+		{
+			// collect all the errors from all the fields and for those that failed validation,
+			// put them in a nicer string.
+			$results = $this->_form->getFormValidationResults();
+			foreach( $results as $field => $result ) {
+				if( !$result ) {
+					$errorMessage .= $this->_form->getFieldErrorMessage( $field )."<br/><br/>";
+				}
+			}
+			
+			$this->_view->setErrorMessage( $errorMessage );
+			
+			return true;
+		}                
+                
+        function perform()
+        {
+        	// need to check the ip of the client
+            $clientIp = Client::getIp();
+
+            // fetch the same article again so that we can have all the comments from
+            // the database, plus this last one
+            $articles = new Articles();
+            $article  = $articles->getBlogArticle( $this->_articleId, $this->_blogInfo->getId());
+            
+            // was it correct?
+            if( !$article ) {
+                $this->_view = new MobileErrorView( $this->_blogInfo, $this->_tInfo );
+                $this->_view->setErrorMessage( $this->_locale->tr( "incorrect_blog_id" ));
+                $this->setCommonData();
+                return( false );
+            }
+
+            // check if the user wanted to receive comments for this article
+            // or not...
+            if( $article->getCommentsEnabled() == false ) {
+                $this->_view = new MobileErrorView( $this->_blogInfo, $this->_tInfo );
+                $this->_view->setErrorMessage( $this->_locale->tr( "incorrect_article_id" ));
+                $this->setCommonData();
+                return false;
+            }
+
+        	// we have already checked all the data, so we are sure that everything's in place
+            $comments = new ArticleComments();			
+			$comment = new UserComment( $this->_articleId, $this->_parentId, $this->_commentTopic, $this->_commentText,
+			                            null, $this->_userName, $this->_userEmail, $this->_userUrl,
+									    $clientIp );			
+
+            // check if there is already a comment with the same text, topic and made from the same
+            // IP already in the database because if so, then we will not add the comment that
+            // the user is trying to add (a reload button mistake, perhaps?)
+            if( !$comments->getIdenticalComment( $this->_commentTopic, $this->_commentText,
+                                                 $this->_articleId, $this->_parentId,
+                                                 $this->_userName, $this->_userEmail,
+                                                 $this->_userUrl, $clientIp )) {
+												 												 
+				if( !$comments->addComment( $comment )) {
+                    $this->_view = new MobileErrorView( $this->_blogInfo, $this->_tInfo );
+                    $this->_view->setErrorMessage( $this->_locale->tr( "error_adding_comment" ));
+                    $this->setCommonData();
+                    return false;
+                }
+            }
+			
+            // finally, check if there was any user who wanted to be notified of new comments
+            // to this post...
+            $notifier = new ArticleNotifications();
+            $notifier->notifyUsers( $article->getId(), $this->_blogInfo);
+    
+            // even though the mobile implementation does not use caching, we do have to reset the cache
+            // because there is a new comment
+			CacheControl::resetBlogCache( $this->_blogInfo->getId());        
+        
+            // if everything went well, display the article
+            $this->_view = new MobileViewArticleView( $this->_blogInfo, $this->_tInfo );
+            $this->_view->setValue( "post", $article );
+            $this->_view->setLastPage();
+            $this->setCommonData();
+                        
+            return( true );
+        }
+    }
+?>
\ No newline at end of file

Modified: plugins/branches/lifetype-1.0/mobile/class/controller/mobilecontrollermap.properties.php
===================================================================
--- plugins/branches/lifetype-1.0/mobile/class/controller/mobilecontrollermap.properties.php	2006-01-29 18:15:13 UTC (rev 2867)
+++ plugins/branches/lifetype-1.0/mobile/class/controller/mobilecontrollermap.properties.php	2006-01-29 18:27:30 UTC (rev 2868)
@@ -4,4 +4,6 @@
     $actions["Default"] = "MobileDefaultAction";
     // load a post
     $actions["ViewArticle"] = "MobileViewArticleAction";
+    // add a comment
+    $actions["AddComment"] = "MobileAddCommentAction";
 ?>

Modified: plugins/branches/lifetype-1.0/mobile/class/view/mobileviewarticleview.class.php
===================================================================
--- plugins/branches/lifetype-1.0/mobile/class/view/mobileviewarticleview.class.php	2006-01-29 18:15:13 UTC (rev 2867)
+++ plugins/branches/lifetype-1.0/mobile/class/view/mobileviewarticleview.class.php	2006-01-29 18:27:30 UTC (rev 2868)
@@ -8,35 +8,60 @@
     {
     
         var $_page;
+        var $_goToLastPage;
     
         function MobileViewArticleView( $blogInfo, $tInfo )
         {
             $this->MobileView( $blogInfo, $tInfo, "postandcomments" );
             
             $this->_page = $this->getCurrentPageFromRequest();
+            $this->_goToLastPage = false;            
         }
         
+        /**
+         * instead of relying on whatever getCurrentPageFromRequest() says, this method
+         * tells this view to set the article page as the last one.
+         *
+         * It is used from MobileAddCommentAction so that users get to see the comment that they
+         * just typed.
+         */
+        function setLastPage()
+        {
+            $this->_goToLastPage = true;
+        }
+        
         function render()
         {
             // load a paginated view of the article comments
             $comments = new ArticleComments();
             $article = $this->getValue( "post" );
-            $articleComments = $comments->getPostComments( $article->getId(),
-                                                           COMMENT_ORDER_OLDEST_FIRST,
-                                                           COMMENT_STATUS_NONSPAM,
-                                                           $this->_page,
-                                                           $this->_tInfo->getNumItemsPerPage());
+            
             // get the number of comments
             $numComments = $comments->getNumPostComments( $article->getId(),
                                                           COMMENT_STATUS_NONSPAM );
                                                           
-            // and finally, generate the pager
+            // generate the pager
             $murl = new MobileRequestGenerator( $this->_blogInfo );
             $pager = new Pager( $murl->getMobileUrl()."?op=ViewArticle&amp;blogId=".
                                 $this->_blogInfo->getId()."&amp;articleId=".$article->getId()."&amp;page=",
                                 $this->_page,
                                 $numComments,
                                 $this->_tInfo->getNumItemsPerPage());
+            
+            // check whether we have to go to the last page
+            if( $this->_goToLastPage ) {
+                // this is a bit of a hack, but or else the pager will not display that the current
+                // page is the last one (we should *not* access private attributes!!, but since we
+                // forgot to add a setter for this attribute, there is no other way around...
+                $this->_page = $pager->getEndPage();
+                $pager->_curPage = $this->_page;
+            }
+            
+            $articleComments = $comments->getPostComments( $article->getId(),
+                                                           COMMENT_ORDER_OLDEST_FIRST,
+                                                           COMMENT_STATUS_NONSPAM,
+                                                           $this->_page,
+                                                           $this->_tInfo->getNumItemsPerPage());
                                 
             // pass everything to the templates
             $this->setValue( "comments", $articleComments );

Modified: plugins/branches/lifetype-1.0/mobile/templates/html-light/postandcomments.template
===================================================================
--- plugins/branches/lifetype-1.0/mobile/templates/html-light/postandcomments.template	2006-01-29 18:15:13 UTC (rev 2867)
+++ plugins/branches/lifetype-1.0/mobile/templates/html-light/postandcomments.template	2006-01-29 18:27:30 UTC (rev 2868)
@@ -6,9 +6,30 @@
 <TD WIDTH="100%"><FONT FACE="Arial" SIZE=2 COLOR="#B8B2AC"><B>{$comment->getTopic()}</B></FONT></TD>
 </TR>
 {assign var=commentDate value=$comment->getDateObject()}
-<TR><TD WIDTH="100%"><FONT FACE="Arial" SIZE=1><B>{$comment->getUsername()}</B> | {$locale->formatDate($commentDate,"%d/%m/%Y %H:%M")}</FONT></TD></TR>
+<TR><TD WIDTH="100%"><FONT FACE="Arial" SIZE=1><B>{$comment->getUserNAME()}</B> | {$locale->formatDate($commentDate,"%d/%m/%Y %H:%M")}</FONT></TD></TR>
 <TR><TD VALIGN="TOP" WIDTH="100%"><FONT FACE="Arial" SIZE=2 color="#000000">{$comment->getText()}</FONT></TD></TR></TABLE>
 <BR>
 {/foreach}
 {include file="html-light/pager.template"}
+<BR>
+<FONT FACE="Arial" SIZE=2 COLOR="#B8B2AC"><B>{$locale->tr("add_comment")}</B></FONT>
+<FORM ACTION="{$murl->getMobileUrl()}" METHOD="POST" NAME="NewComment">
+<FONT FACE="Arial" SIZE=2 color="#000000">
+{$locale->tr("comment_topic")}:<BR>
+<INPUT NAME="commentTopic" VALUE="{$commentTopic}"><BR>
+{$locale->tr("comment_username")}:<BR>
+<INPUT NAME="userName" VALUE="{$userName}"><BR>
+{$locale->tr("comment_email")}:<BR>
+<INPUT NAME="userEmail" VALUE="{$userEmail}"><BR>
+{$locale->tr("comment_url")}:<BR>
+<INPUT NAME="userUrl" VALUE="{$userUrl}"><BR>
+{$locale->tr("comment_text")}:<BR>
+<TEXTAREA NAME="commentText" ROWS="5" COLS="30"></TEXTAREA><BR>
+<INPUT TYPE="submit" NAME="post" VALUE="&nbsp;{$locale->tr("comment_send")}&nbsp;"><BR><BR>
+<INPUT TYPE="hidden" NAME="op" VALUE="AddComment">
+<INPUT TYPE="hidden" NAME="articleId" VALUE="{$post->getId()}">
+<INPUT TYPE="hidden" NAME="blogId" VALUE="{$blog->getId()}">
+<INPUT TYPE="hidden" NAME="parentId" VALUE="{$parentId}">
+</FONT>
+</FORM>
 {include file="html-light/footer.template"}
\ No newline at end of file



More information about the pLog-svn mailing list