forked from sulu/SuluArticleBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added route-behavior to article-page (sulu#131)
- Loading branch information
1 parent
9a8a5e4
commit 1c368b8
Showing
12 changed files
with
644 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ArticleBundle\Document\Behavior; | ||
|
||
use Sulu\Component\DocumentManager\Behavior\Mapping\ParentBehavior; | ||
use Sulu\Component\DocumentManager\Behavior\Mapping\UuidBehavior; | ||
|
||
/** | ||
* This behavior has to be attached to documents which should have a page-number property. | ||
*/ | ||
interface PageBehavior extends ParentBehavior, UuidBehavior | ||
{ | ||
/** | ||
* Returns page-number. | ||
* | ||
* @return int | ||
*/ | ||
public function getPageNumber(); | ||
|
||
/** | ||
* Set page-number. | ||
* | ||
* @param int $pageNumber | ||
* | ||
* @return self | ||
*/ | ||
public function setPageNumber($pageNumber); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ArticleBundle\Document\Subscriber; | ||
|
||
use Sulu\Bundle\ArticleBundle\Document\Behavior\PageBehavior; | ||
use Sulu\Component\DocumentManager\DocumentInspector; | ||
use Sulu\Component\DocumentManager\Event\MetadataLoadEvent; | ||
use Sulu\Component\DocumentManager\Event\PersistEvent; | ||
use Sulu\Component\DocumentManager\Event\RemoveEvent; | ||
use Sulu\Component\DocumentManager\Events; | ||
use Sulu\Component\DocumentManager\PropertyEncoder; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* Handles document-manager events to set and update page-numbers. | ||
*/ | ||
class PageSubscriber implements EventSubscriberInterface | ||
{ | ||
const FIELD = 'pageNumber'; | ||
|
||
/** | ||
* @var DocumentInspector | ||
*/ | ||
private $documentInspector; | ||
|
||
/** | ||
* @var PropertyEncoder | ||
*/ | ||
private $propertyEncoder; | ||
|
||
/** | ||
* @param DocumentInspector $documentInspector | ||
* @param PropertyEncoder $propertyEncoder | ||
*/ | ||
public function __construct(DocumentInspector $documentInspector, PropertyEncoder $propertyEncoder) | ||
{ | ||
$this->documentInspector = $documentInspector; | ||
$this->propertyEncoder = $propertyEncoder; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
Events::PERSIST => [['handlePersist', -1024]], | ||
Events::REMOVE => [['handleRemove', 5]], | ||
Events::METADATA_LOAD => 'handleMetadataLoad', | ||
]; | ||
} | ||
|
||
/** | ||
* Add page-number to metadata. | ||
* | ||
* @param MetadataLoadEvent $event | ||
*/ | ||
public function handleMetadataLoad(MetadataLoadEvent $event) | ||
{ | ||
$metadata = $event->getMetadata(); | ||
|
||
if (false === $metadata->getReflectionClass()->isSubclassOf(PageBehavior::class)) { | ||
return; | ||
} | ||
|
||
$metadata->addFieldMapping( | ||
'pageNumber', | ||
[ | ||
'encoding' => 'system', | ||
'property' => self::FIELD, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Set the page-number to new pages. | ||
* | ||
* @param PersistEvent $event | ||
*/ | ||
public function handlePersist(PersistEvent $event) | ||
{ | ||
$document = $event->getDocument(); | ||
if (!$document instanceof PageBehavior || $document->getPageNumber()) { | ||
return; | ||
} | ||
|
||
$parentDocument = $document->getParent(); | ||
|
||
$page = 1; | ||
foreach ($parentDocument->getChildren() as $child) { | ||
if (!$child instanceof PageBehavior) { | ||
continue; | ||
} | ||
|
||
++$page; | ||
} | ||
|
||
$childNode = $this->documentInspector->getNode($document); | ||
$childNode->setProperty($this->propertyEncoder->systemName(static::FIELD), $page); | ||
} | ||
|
||
/** | ||
* Adjust the page-numbers of siblings when removing a page. | ||
* | ||
* @param RemoveEvent $event | ||
*/ | ||
public function handleRemove(RemoveEvent $event) | ||
{ | ||
$document = $event->getDocument(); | ||
if (!$document instanceof PageBehavior) { | ||
return; | ||
} | ||
|
||
$parentDocument = $document->getParent(); | ||
|
||
$page = 1; | ||
foreach ($parentDocument->getChildren() as $child) { | ||
if (!$child instanceof PageBehavior || $child->getUuid() === $document->getUuid()) { | ||
continue; | ||
} | ||
|
||
$childNode = $this->documentInspector->getNode($child); | ||
$childNode->setProperty($this->propertyEncoder->systemName(static::FIELD), $page++); | ||
} | ||
} | ||
} |
Oops, something went wrong.