Ejemplo n.º 1
0
 public function index_action()
 {
     $page = (int) $this->params->page;
     // page number
     $page = $page < 1 ? 1 : $page;
     // page=1, if specified page<1
     $postsObj = $this->model->posts($page);
     $posts = array();
     // if page is not 1, and there are no posts (page is invalid), redirect to 0 page
     if ($page != 1 && $postsObj->empty) {
         SiteRedirect('blog');
     }
     // adding data to posts
     foreach ($postsObj as $i => $post) {
         // omiting 11th post
         if ($i == 10) {
             continue;
         }
         // edit/delete links
         $post->editHref = '%/blog/edit/' . $post->id . '?backTo=site';
         $post->deleteHref = '%/blog/trash/' . $post->id . '/' . base64_encode('#/');
         // URL to post
         $post->url = '$/' . date('Y/m', $post->published) . '/' . $post->name;
         // is draft
         $post->draft = $post->status == 'draft';
         // post creation human-readable date
         $post->published_human = HumanDate($post->published, true, true);
         // comments counter - visible (approved) comments for users, all comments - for admin
         if (!Users::isLogged()) {
             $approvedComments = $post->approvedCommentsCount;
             if ($approvedComments > 0) {
                 $post->comments = ', ' . $approvedComments;
                 $post->comments .= ' ' . pl_inflect($approvedComments, 'komentarzy', 'komentarz', 'komentarze');
             }
         } else {
             // counters
             $approvedComments = $post->approvedCommentsCount;
             $allComments = $post->commentsCount;
             $unapprovedComments = $allComments - $approvedComments;
             // all comments counter
             if ($allComments > 0) {
                 $post->comments = ', ' . $allComments . ' ' . pl_inflect($allComments, 'komentarzy', 'komentarz', 'komentarze');
             }
             // unapproved comments counter
             if ($unapprovedComments > 0) {
                 $post->comments .= ' <a href="' . $post->url . '#comments" class="important">(' . $unapprovedComments . ' do sprawdzenia!)</a>';
             }
         }
         // content
         if ($post->summary === null) {
             $post->content = Textile::textile($post->content);
         } else {
             $post->summary = Textile::textile($post->summary . ' <em>[...]</em>');
         }
         //--
         $posts[] = $post;
     }
     // displaying
     $this->noHeader = true;
     $view = View('posts');
     $view->posts = $posts;
     $view->page = $page;
     $view->anotherPage = $postsObj->rows == 11;
     // whether there is another page
     $view->previousPage = $page == 2 ? '$/blog' : '$/blog?page=' . ($page - 1);
     // URL for previous page
     $view->nextPage = '$/blog?page=' . ($page + 1);
     // URL for next page
     $view->display();
 }
Ejemplo n.º 2
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();
 }
Ejemplo n.º 3
0
function HumanDate($timestamp, $dateOnly = false, $html = false)
{
    $timestamp = (int) $timestamp;
    list($day, $month, $year, $hour, $minute) = explode('.', date('j.n.Y.G.i', $timestamp));
    // timestamp
    list($dayN, $monthN, $yearN) = explode('.', date('d.m.Y', time()));
    // now
    $date = date('d.m.Y, G:i', $timestamp);
    // function returning HTML if $html == true, and appending ', hour:minute' if $time == true and $dateOnly == false
    $humanDate = function ($humanDate, $time = false) use($html, $dateOnly, $date, $hour, $minute) {
        // appending hour:minute
        if ($time && !$dateOnly) {
            $humanDate .= ', ' . $hour . ':' . $minute;
        }
        // returning
        if ($html) {
            return '<span title="' . $date . '">' . $humanDate . '</span>';
        } else {
            return $humanDate;
        }
    };
    // more precise time, but only if $dateOnly == false
    if (!$dateOnly) {
        // less than a minute ago
        if ($timestamp + 60 > time()) {
            return $humanDate('przed chwilą');
        }
        // less than an hour ago
        if ($timestamp + 3600 > time()) {
            $minutesAgo = (int) ((time() - $timestamp) / 60);
            return $humanDate($minutesAgo . ' ' . pl_inflect($minutesAgo, 'minut', 'minutę', 'minuty') . ' temu');
        }
    }
    // today, but more than an hour ago
    if ($day == $dayN && $month == $monthN && $year == $yearN) {
        return $humanDate('dziś', true);
    }
    // yesterday
    if ($day == $dayN - 1 && $month == $monthN && $year == $yearN) {
        return $humanDate('wczoraj', true);
    }
    // day before yesterday
    if ($day == $dayN - 2 and $month == $monthN and $year == $yearN) {
        return $humanDate('przedwczoraj', true);
    }
    // more than two days ago
    switch ($month) {
        case 1:
            $month = 'stycznia';
            break;
        case 2:
            $month = 'lutego';
            break;
        case 3:
            $month = 'marca';
            break;
        case 4:
            $month = 'kwietnia';
            break;
        case 5:
            $month = 'maja';
            break;
        case 6:
            $month = 'czerwca';
            break;
        case 7:
            $month = 'lipca';
            break;
        case 8:
            $month = 'sierpnia';
            break;
        case 9:
            $month = 'września';
            break;
        case 10:
            $month = 'października';
            break;
        case 11:
            $month = 'listopada';
            break;
        case 12:
            $month = 'grudnia';
            break;
    }
    // omitting year, if the same
    if ($year == $yearN) {
        return $humanDate($day . ' ' . $month, true);
    } else {
        return $humanDate($day . ' ' . $month . ' ' . $year, true);
    }
}