Maslosoft Mangan API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan\Traits\Model;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\EntityManager;
use Maslosoft\Mangan\Events\Event;
use Maslosoft\Mangan\Events\ModelEvent;
use Maslosoft\Mangan\Helpers\RawFinder;
use Maslosoft\Mangan\Interfaces\SimpleTreeInterface;
use Maslosoft\Mangan\Interfaces\TrashInterface;
use MongoId;
/**
* Related Tree Trait
* @see SimpleTreeInterface
* @author Piotr
*/
trait SimpleTreeTrait
{
use WithParentTrait;
/**
* @RelatedArray(join = {'_id' = 'parentId'}, updatable = true)
* @RelatedOrdering('order')
* @var AnnotatedInterface[]
*/
public $children = [];
/**
* @Label('Manual sort')
* @var int
*/
public $order = 0;
/**
* NOTE: This must be called by class using this trait
* TODO Move event initializer to some other global event init, as events now handle traits too.
* @Ignored
*/
public function initTree()
{
if ($this instanceof TrashInterface)
{
// Trash related events
$onBeforeTrash = function(ModelEvent $event)
{
$event->isValid = true;
};
Event::on($this, TrashInterface::EventBeforeTrash, $onBeforeTrash);
$onAfterTrash = function(ModelEvent $event)
{
foreach ($event->sender->children as $child)
{
$child->trash();
}
};
Event::on($this, TrashInterface::EventAfterTrash, $onAfterTrash);
$onAfterRestore = function(ModelEvent $event)
{
// Root nodes does not have parentId
if ($this->parentId)
{
// Put node to root if parent does not exists
// RawFinder is used for performance reasons here
// and to skip filters
/**
* TODO Similar mechanism should be used to detect orphaned tree items.
* TODO Use exists here instead of raw finder.
*
*/
if (!(new RawFinder($this))->findByPk(new MongoId($this->parentId)))
{
$this->parentId = null;
(new EntityManager($this))->update(['parentId']);
}
}
};
$onAfterRestore->bindTo($this);
Event::on($this, TrashInterface::EventAfterRestore, $onAfterRestore);
}
}
/**
* Move to a new parent
* @param string|MongoId $parentId
* @param string[]|MongoId[] $order
* @Ignored
*/
public function moveTo($parentId, $order = [])
{
$this->parentId = $parentId;
(new EntityManager($this))->update(['parentId']);
$i = 0;
$node = new static;
$em = new EntityManager($node);
foreach ((array) $order as $id)
{
assert(property_exists($node, '_id'), sprintf('Property `_id` is required to use with `%s`', SimpleTreeTrait::class));
$node->_id = $id;
$node->order = $i++;
$em->update(['order']);
}
}
}
API documentation generated by ApiGen