phowie74
10/11/2018 - 8:18 AM

Blog / News Listing C5 v8

Updated date archiving of posts for C5 v8 Triggers the Handler script when post published, approved or moved

// file saved in /application/src/Concrete/BlogPublish/Handler.php

<?php
namespace Application\Concrete\BlogPublish;

use Concrete\Core\Page\Page;
use Concrete\Core\Page\Template;
use Concrete\Core\Page\Type\Type;

class Handler {

  public function placePost(Page $page) {

    $date = $page->getCollectionDatePublic();
    $y = date('Y', strtotime($date));
    $m = date('m', strtotime($date));
    $mm = date('F', strtotime($date));

    $handle = $page->getCollectionHandle();
    if (!$handle) { $handle = $page->getCollectionID(); }

    $posts = \Page::getByPath('/blog');
    $yearPage = \Page::getByPath('/blog/' . $y);

    $pageType = \PageType::getByHandle('list_handle');
    $template = \PageTemplate::getByHandle('default');

    if (!is_object($yearPage) || $yearPage->isError()) {
      $year = $posts->add($pageType, array(
        'cName' => "$y",
        'cDescription' => "Posts from $y",
        'cDatePublic' => $y.'-01-01 13:05:05', // Y-m-d H:i:s e.g. 2016-01-28 13:05:05
        'cHandle ' => $y
      ), $template);
      $year->setCanonicalPagePath('/blog/' . $y);
      $yearPage = \Page::getByPath('/blog/'.$y);
    }

    $monthPage = \Page::getByPath('/blog/' . $y . '/' . $m);

    if (!is_object($monthPage) || $monthPage->isError()) {

    $descMonth = date('F', strtotime($date));

      $month = $yearPage->add($pageType, array(
        'cName' => "$descMonth $y",
        'cDescription' => "Posts from $descMonth $y",
        'cDatePublic' => $y.'-'.$m.'-01 13:05:05', // Y-m-d H:i:s e.g. 2016-01-28 13:05:05
        'cHandle ' => $m
      ), $template);
      $month->setCanonicalPagePath('/blog/'.$y.'/'.$m);
      $monthPage = \Page::getByPath('/blog/'.$y.'/'.$m);

    }
    $page->move($monthPage);
  }

  public function cleanPostParent(Page $parent) {

    if ($parent->getPageTypeHandle() == 'list_handle') {
      $children = $parent->getCollectionChildren();
      if (count($children) == 0) {
        $year = Page::getByID($parent->getCollectionParentID());
        $parent->moveToTrash();
        if (is_object($year) && !$year->isError() && $year->getPageTypeHandle() == 'list_handle') {
          $children2 = $year->getCollectionChildren();
          if (count($children2) == 0) { $year->moveToTrash(); }
        }
      }
    }

  }

}
// file saved at /application/bootstrap/app.php

<?php

use Application\Concrete\BlogPublish\Handler as PageHandler;
//use Application\Src\Concrete\BlogPublish\Handler as PageHandler;

$handler = new PageHandler();
\Events::addListener('on_page_type_publish', function($event) use ($handler) {
  $page = $event->getPageObject();
  if ($page->getPageTypeHandle() == 'post_handle') {
    $handler->placePost($page);
  }
});
\Events::addListener('on_page_version_approve', function($event) use ($handler) {
    $page = $event->getPageObject();
    if ($page->getPageTypeHandle() == 'post_handle' && $page->isPageDraft()) {
        $handler->placePost($page);
    }
});
\Events::addListener('on_page_move', function($event) use ($handler) {
    $page = $event->getPageObject();
    if ($page->getPageTypeHandle() == 'post_handle') {
        $parent = $event->getOldParentPageObject();
        $handler->cleanPostParent($parent);
    }
});