| [ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 lt_include( PLOG_CLASS_PATH."class/action/blogaction.class.php" ); 4 lt_include( PLOG_CLASS_PATH."class/dao/users.class.php" ); 5 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" ); 6 lt_include( PLOG_CLASS_PATH."class/dao/articlecategories.class.php" ); 7 lt_include( PLOG_CLASS_PATH."class/dao/articlenotifications.class.php" ); 8 lt_include( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" ); 9 lt_include( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" ); 10 lt_include( PLOG_CLASS_PATH."class/data/validator/emailvalidator.class.php" ); 11 lt_include( PLOG_CLASS_PATH."class/data/validator/httpurlvalidator.class.php" ); 12 lt_include( PLOG_CLASS_PATH."class/data/textfilter.class.php" ); 13 lt_include( PLOG_CLASS_PATH."class/data/filter/htmlfilter.class.php" ); 14 lt_include( PLOG_CLASS_PATH."class/data/filter/urlconverter.class.php" ); 15 lt_include( PLOG_CLASS_PATH."class/data/filter/allowedhtmlfilter.class.php" ); 16 lt_include( PLOG_CLASS_PATH."class/data/filter/xhtmlizefilter.class.php" ); 17 lt_include( PLOG_CLASS_PATH."class/view/errorview.class.php" ); 18 lt_include( PLOG_CLASS_PATH."class/view/redirectview.class.php" ); 19 lt_include( PLOG_CLASS_PATH."class/bayesian/bayesianfiltercore.class.php" ); 20 lt_include( PLOG_CLASS_PATH."class/template/cachecontrol.class.php" ); 21 22 /** 23 * \ingroup Action 24 * @private 25 * 26 * Takes care of validating the form to add new comments to an article 27 */ 28 class AddCommentAction extends BlogAction 29 { 30 31 var $_articleId; 32 var $_blogId; 33 var $_opId; 34 var $_userName; 35 var $_userEmail; 36 var $_userUrl; 37 var $_commentText; 38 var $_commentTopic; 39 var $_parentId; 40 41 /** 42 * Constructor 43 */ 44 function AddCommentAction( $blogInfo, $request ) 45 { 46 $this->BlogAction( $blogInfo, $request ); 47 48 // input filters 49 $f = new HtmlFilter(); 50 $this->_request->registerFilter( "userEmail", $f ); 51 $this->_request->registerFilter( "userName", $f ); 52 $this->_request->registerFilter( "commentTopic", $f ); 53 54 // userUrl is a regular HTML filter, in addition to 55 // being forced to look like a URL 56 $f = new HtmlFilter(); 57 $f->addFilter( new UrlConverter()); 58 $this->_request->registerFilter( "userUrl", $f ); 59 60 $f = new AllowedHtmlFilter(); 61 $f->addFilter( new XhtmlizeFilter()); 62 $this->_request->registerFilter( "commentText", $f ); 63 64 // change the validation mode of the form 65 $this->registerFieldValidator( "articleId", new IntegerValidator()); 66 $this->_form->setFieldErrorMessage( "articleId", $this->_locale->tr("error_incorrect_article_id" )); 67 $this->registerFieldValidator( "blogId", new IntegerValidator()); 68 $this->_form->setFieldErrorMessage( "blogId", $this->_locale->tr("error_incorrect_blog_id" )); 69 $this->registerFieldValidator( "parentId", new IntegerValidator(), true ); 70 $this->_form->setFieldErrorMessage( "parentId", $this->_locale->tr("error_incorrect_article_id" )); 71 $this->registerFieldValidator( "userEmail", new EmailValidator(), true ); 72 $this->_form->setFieldErrorMessage( "userEmail", $this->_locale->tr("error_incorrect_email_address" )); 73 $this->registerFieldValidator( "userName", new StringValidator()); 74 $this->_form->setFieldErrorMessage( "userName", $this->_locale->tr("error_comment_without_name" )); 75 $this->registerFieldValidator( "commentText", new StringValidator()); 76 $this->_form->setFieldErrorMessage( "commentText", $this->_locale->tr("error_comment_without_text")); 77 $this->registerFieldValidator( "userUrl", new HttpUrlValidator(), true ); 78 $this->_form->setFieldErrorMessage( "userUrl", $this->_locale->tr("invalid_url" )); 79 $view = new ErrorView( $this->_blogInfo ); 80 $view->setErrorMessage( "There has been an error validating the data!" ); 81 $this->setValidationErrorView( $view ); 82 83 $this->_fetchFields(); 84 } 85 86 function _fetchFields() 87 { 88 lt_include( PLOG_CLASS_PATH."class/data/textfilter.class.php" ); 89 90 $f = new HtmlFilter( true ); 91 92 $this->_articleId = $this->_request->getValue( "articleId" ); 93 $this->_blogId = $this->_request->getValue( "blogId" ); 94 $this->_opId = $this->_request->getValue( "op" ); 95 $this->_parentId = $this->_request->getValue( "parentId" ); 96 if( $this->_parentId == null || $this->_parentId == "" ) 97 $this->_parentId = 0; 98 $this->_userEmail = $this->_request->getValue( "userEmail" ); 99 $this->_userUrl = $this->_request->getValue( "userUrl" ); 100 $this->_userName = $this->_request->getValue( "userName" ); 101 $this->_commentText = trim($this->_request->getValue( "commentText" )); 102 $this->_commentTopic = $this->_request->getValue( "commentTopic" ); 103 104 // now, if the option is set, we 'beautify' the text typed by users 105 if( $this->_config->getValue( "beautify_comments_text" )) { 106 $tf = new TextFilter(); 107 $this->_commentText = $tf->autop($this->_commentText); 108 } 109 } 110 111 /** 112 * Since this function contains this method, the controller will automatically 113 * call it before calling perform() 114 * 115 * @return True if all fields ok or false otherwise. 116 */ 117 function validate() 118 { 119 // check if comments are enabled 120 $blogSettings = $this->_blogInfo->getSettings(); 121 if( !$blogSettings->getValue( "comments_enabled" )) { 122 $this->_view = new ErrorView( $this->_blogInfo, "error_comments_not_enabled" ); 123 $this->setCommonData(); 124 125 return false; 126 } 127 128 return( parent::validate()); 129 } 130 131 /** 132 * prepare a nicer error message. This method is only going to be executed whenver a validation 133 * error happens (see Action::validate()) 134 * 135 * @see Action::validate() 136 */ 137 function validationErrorProcessing() 138 { 139 $this->_view = new ErrorView( $this->_blogInfo); 140 141 // collect all the errors from all the fields and for those that failed validation, 142 // put them in a nicer string. 143 $results = $this->_form->getFormValidationResults(); 144 $errorMessage = ""; 145 foreach( $results as $field => $result ) { 146 if( !$result ) { 147 $errorMessage .= $this->_form->getFieldErrorMessage( $field )."<br/><br/>"; 148 } 149 } 150 151 $this->_view->setErrorMessage( $errorMessage ); 152 $this->setCommonData(); 153 154 return true; 155 } 156 157 /** 158 * Carries out the action 159 */ 160 function perform() 161 { 162 lt_include( PLOG_CLASS_PATH."class/dao/articlecomments.class.php" ); 163 lt_include( PLOG_CLASS_PATH."class/dao/usercomment.class.php" ); 164 lt_include( PLOG_CLASS_PATH."class/net/client.class.php" ); 165 166 // need to check the ip of the client 167 $clientIp = Client::getIp(); 168 169 // fetch the same article again so that we can have all the comments from 170 // the database, plus this last one 171 $articles = new Articles(); 172 $article = $articles->getBlogArticle( $this->_articleId, $this->_blogInfo->getId()); 173 if(!$article) { 174 $this->_view = new ErrorView( $this->_blogInfo ); 175 $this->_view->setValue( "message", $this->_locale->tr("error_incorrect_article_id")); 176 $this->setCommonData(); 177 return false; 178 } 179 180 $this->notifyEvent( EVENT_POST_LOADED, Array( "article" => &$article )); 181 182 // check if the user wanted to receive comments for this article 183 // or not... 184 if( $article->getCommentsEnabled() == false ) { 185 $this->_view = new ErrorView( $this->_blogInfo ); 186 $this->_view->setValue( "message", "Comments have been disabled for this article." ); 187 $this->setCommonData(); 188 return false; 189 } 190 191 // we have already checked all the data, so we are sure that everything's in place 192 $comments = new ArticleComments(); 193 194 $comment = new UserComment( $this->_articleId, 195 $this->_blogInfo->getId(), 196 $this->_parentId, 197 $this->_commentTopic, 198 $this->_commentText, 199 null, 200 $this->_userName, 201 $this->_userEmail, 202 $this->_userUrl, 203 $clientIp ); 204 205 // check if the comment was being posted by an authenticated user... 206 if( $this->_userInfo ) { 207 // ...and if so, save the user data in the UserComment object 208 $comment->setUser( $this->_userInfo ); 209 } else { 210 $comment->setUserId( 0 ); 211 } 212 213 // fire an event 214 $this->notifyEvent( EVENT_PRE_COMMENT_ADD, Array( "comment" => &$comment )); 215 216 if( !$comments->addComment( $comment )) { 217 // show an error message if problems 218 $this->_view = new ErrorView( $this->_blogInfo ); 219 $this->_view->setValue( "message", "error_adding_comment" ); 220 $this->setCommonData(); 221 return false; 222 } 223 224 // finally, check if there was any user who wanted to be notified of new comments 225 // to this post... 226 $notifier = new ArticleNotifications(); 227 $notifier->notifyUsers( $article->getId(), $this->_blogInfo); 228 229 // fire the post event... 230 $this->notifyEvent( EVENT_POST_COMMENT_ADD, Array( "comment" => &$comment )); 231 232 // 233 // clear caches. This should be done in a more granular way, because right now 234 // we're either removing *all* of them or none. I guess we should only remove the 235 // cache whose identifier corresponds with the blog and article that we just removed, 236 // but let's leave it as it is for the time being... 237 // 238 CacheControl::resetBlogCache( $this->_blogInfo->getId()); 239 240 // clean up the request, there's a parameter called 'userName' also used by 241 // ViewArticleAction but that doesn't have the same meaning so we better remove it 242 // before it's too late! We also need to add a new request commentUserName to replace 243 // original userName, in case developer need it in filter or event plugin. 244 $request = HttpVars::getRequest(); 245 $request["commentUserName"] = $request["userName"]; 246 $request["userName"] = ""; 247 HttpVars::setRequest( $request ); 248 249 // calculate the final URL 250 $rg = $this->_blogInfo->getBlogRequestGenerator(); 251 $rg->setXHTML( false ); 252 $postPermalink = $rg->postPermalink( $article ); 253 254 // and pass it to the redirect view to perform the redirection 255 $this->_view = new RedirectView( $postPermalink ); 256 257 return( true ); 258 } 259 } 260 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
|