예제 #1
0
 public static function commentsView($id, $type, $backPage, $open = true)
 {
     $id = (int) $id;
     $type = (string) $type;
     $backPage = (string) $backPage;
     $model = new Comments_Model();
     // comments
     $commentsObj = $model->commentsFor($id, $type);
     $approvedCount = 0;
     // counter of approved comments
     $unapprovedCount = 0;
     // counter of unapproved comments
     $visibleCount = 0;
     // counter of comments visible to user
     // visibilityToken
     if (!Users::isLogged() && isset($_SESSION['wmelon.comments.visibilityToken'])) {
         $visibilityToken = $_SESSION['wmelon.comments.visibilityToken'];
     }
     // composing comments array
     foreach ($commentsObj as $comment) {
         // tools
         $linkEnding = $comment->id . '/' . base64_encode($backPage . '#comment-' . $comment->id);
         $comment->editHref = '%/comments/edit/' . $linkEnding;
         $comment->deleteHref = '%/comments/delete/' . $comment->id . '/' . base64_encode($backPage . '#comments');
         $comment->approveHref = '%/comments/approve/' . $linkEnding;
         $comment->rejectHref = '%/comments/reject/' . $linkEnding;
         // visibility
         // (comment is visible if admin or comment is approved or comment visibility token match user's visibility token)
         $comment->visible = Users::isLogged() || $comment->approved || $comment->visibilityToken == $visibilityToken && !empty($comment->visibilityToken);
         if ($comment->visible) {
             $visibleCount++;
         }
         // additional information (for admin)
         if (Users::isLogged() && $comment->authorID === null) {
             $comment->additionalInfo = $comment->authorEmail . '; IP:' . $comment->authorIP;
         }
         // if commented as logged user
         $authorID = $comment->authorID;
         if ($authorID !== null) {
             // author's user data
             $comment->author = Users::userData(1);
             // CSS class (for admin comments distinction)
             $comment->cssClass = 'adminComment';
         }
         // "awaiting moderation" CSS class
         if (!$comment->approved) {
             $comment->cssClass .= ' awaitingModerationComment';
         }
         // comments counter
         if ($comment->approved) {
             $approvedCount++;
         } else {
             $unapprovedCount++;
         }
         //--
         $comments[] = $comment;
     }
     // form
     $submitPage = 'comments/post/' . $id . '/' . $type . '/' . base64_encode($backPage);
     $form = new Form('wmelon.comments.addComment', $submitPage, $backPage . '#commentForm-link');
     $form->globalMessages = false;
     $form->submitLabel = 'Zapisz';
     // user data inputs (if not logged in)
     if (!Users::isLogged()) {
         // remembered user data
         $name = $_SESSION['wmelon.comments.name'];
         $email = $_SESSION['wmelon.comments.email'];
         $website = $_SESSION['wmelon.comments.website'];
         // inputs args
         $name = array('value' => $name);
         $email = array('value' => $email);
         $website = array('value' => $website, 'labelNote' => '(Opcjonalnie)');
         // adding inputs
         $form->addInput('text', 'name', 'Imię', true, $name);
         $form->addInput('email', 'email', 'Email', true, $email);
         $form->addInput('url', 'website', 'Strona', false, $website);
     }
     // content input
     $form->addInput('textarea', 'content', 'Komentarz');
     // comments counter
     $commentsCount = Users::isLogged() ? $commentsObj->rows : $approvedCount;
     // number of visible (approved) comments - for user and all comments - for admin
     if ($commentsCount > 0) {
         $commentsCountStr = $commentsCount . ' ' . pl_inflect($commentsCount, 'komentarzy', 'komentarz', 'komentarze');
     }
     if (Users::isLogged() && $unapprovedCount > 0) {
         $commentsCountStr .= ' <span class="important">(' . $unapprovedCount . ' do sprawdzenia!)</span>';
     }
     // view
     $view = Loader::view('/comments/comments');
     $view->comments = $comments;
     $view->areComments = $commentsObj->exists;
     $view->commentsCount = $commentsCountStr;
     $view->visibleCount = $visibleCount;
     $view->visibilityToken = $visibilityToken;
     $view->id = $id;
     $view->type = $type;
     $view->backPage = $backPage;
     $view->form = $form->generate();
     $view->open = $open;
     return $view->generate();
 }