Developer guide     >     Developing Extensions     >     Examples

Commenting

This example demonstrates the usage of comments. The included Template renders comments of a Page and allows Users to add comments (depending on your commenting settings anonymous posting might be allowed). 
  1. <?php
  2.     namespace com\yg;
  3.    
  4.     class ExampleCommenting extends \PageExtension {
  5.  
  6.         public $info = array(
  7.             "NAME" => "Comments!",
  8.             "DEVELOPERNAME" => "yeager GmbH",
  9.             "VERSION" => "1.0",
  10.             "API" => "1.0",
  11.             "DESCRIPTION" => "Add, edit and delete Comments",
  12.             "URL" => "http://www.yeager.cm/",
  13.             "TYPE" => EXTENSION_PAGE,
  14.             "ASSIGNMENT" => EXTENSION_ASSIGNMENT_EXT_CONTROLLED
  15.         );
  16.  
  17.         public function install () {
  18.             if ( parent::install() ) {
  19.                 return parent::setInstalled();
  20.             } else {
  21.                 return false;
  22.             }
  23.         }
  24.  
  25.         public function onRender ($args = NULL) {
  26.  
  27.             $page = $this->getPage();
  28.             $pageinfo = $page->get();
  29.             $action = sApp()->request->parameters['ACTION'];
  30.            
  31.             // check if commenting user is authenticated
  32.             $anonymousUserID = sUserMgr()->getAnonymousID();
  33.             if ((int)sUserMgr()->getCurrentUserID() == $anonymousUserID) {
  34.                 $isLoggedIn = false;
  35.             } else {
  36.                 $isLoggedIn = true;
  37.             }
  38.  
  39.             // get comment infos
  40.             $commentError = NULL;
  41.             $commentObject = $page->comments;
  42.             $commentSettings = $commentObject->getSettings();
  43.             $commentStatus = $commentObject->getStatus($pageinfo['ID']);
  44.             $commentPermission = $page->permissions->check(sUserMgr()->getCurrentUserID(), $pageinfo['ID'], 'RCOMMENT');
  45.  
  46.             // add comment
  47.             if($action == 'ADD') {
  48.                 $commentUser = sApp()->request->parameters['USER'];
  49.                 $commentEmail = sApp()->request->parameters['EMAIL'];
  50.                 $commentText = sApp()->request->parameters['TEXT'];
  51.  
  52.                 $result = $commentObject->add($commentText, $pageinfo['ID'], $commentUser, $commentEmail);
  53.                 if ($result !== ERROR_NONE) {
  54.                     switch($result) {
  55.                         case ERROR_COMMENTS_MINIMUM_POST_INTERVAL_EXCEEDED:
  56.                             $commentError = 'Minimum post interval was exceeded.';
  57.                             break;
  58.                         case ERROR_COMMENTS_BLACKLISTED_WORD:
  59.                             $commentError = 'You have used a blacklisted word.';
  60.                             break;
  61.                         case ERROR_COMMENTS_AUTHENTICATION_NEEDED:
  62.                             $commentError = 'Authentication is needed.';
  63.                             break;
  64.                         case ERROR_COMMENTS_COMMENTING_IS_CLOSED:
  65.                             $commentError = 'Commenting is closed.';
  66.                             break;
  67.                         case ERROR_COMMENTS_NO_COMMENT_RIGHTS:
  68.                             $commentError = 'No permissions to comment.';
  69.                             break;
  70.                         default:
  71.                             $commentError = 'An unknown error has occured.';
  72.                             break;
  73.                     }
  74.                 }
  75.             }
  76.  
  77.             // get comments - has to be after add functionaly in order for new comments to show up
  78.             $commentCount = $commentObject->getCommentsCount();
  79.             $comments = $commentObject->getComments($pageinfo['ID'], NULL);
  80.  
  81.             sSmarty()->assign("isLoggedIn", $isLoggedIn);
  82.             sSmarty()->assign("commentError", $commentError);
  83.             sSmarty()->assign("commentCount", $commentCount);
  84.             sSmarty()->assign("commentPermission", $commentPermission);
  85.             sSmarty()->assign("commentSettings", $commentSettings);
  86.             sSmarty()->assign("commentStatus", $commentStatus);
  87.             sSmarty()->assign("comments", $comments);
  88.  
  89.         }
  90.  
  91.     }
  92.  
  93. ?>