Pagekit\Site\Model\NodeModelTrait::saving PHP Method

saving() public static method

public static saving ( $event, Pagekit\Site\Model\Node $node )
$node Pagekit\Site\Model\Node
    public static function saving($event, Node $node)
    {
        $db = self::getConnection();
        $i = 2;
        $id = $node->id;
        if (!$node->slug) {
            $node->slug = $node->title;
        }
        // A node cannot have itself as a parent
        if ($node->parent_id === $node->id) {
            $node->parent_id = 0;
        }
        // Ensure unique slug
        while (self::where(['slug = ?', 'parent_id= ?'], [$node->slug, $node->parent_id])->where(function ($query) use($id) {
            if ($id) {
                $query->where('id <> ?', [$id]);
            }
        })->first()) {
            $node->slug = preg_replace('/-\\d+$/', '', $node->slug) . '-' . $i++;
        }
        // Update own path
        $path = '/' . $node->slug;
        if ($node->parent_id && ($parent = Node::find($node->parent_id)) and $parent->menu == $node->menu) {
            $path = $parent->path . $path;
        } else {
            // set Parent to 0, if old parent is not found
            $node->parent_id = 0;
        }
        // Update children's paths
        if ($id && $path != $node->path) {
            $db->executeUpdate('UPDATE ' . self::getMetadata()->getTable() . ' SET path = REPLACE (' . $db->getDatabasePlatform()->getConcatExpression($db->quote('//'), 'path') . ", {$db->quote('//' . $node->path)}, {$db->quote($path)})" . ' WHERE path LIKE ' . $db->quote($node->path . '//%'));
        }
        $node->path = $path;
        // Set priority
        if (!$id) {
            $node->priority = 1 + $db->createQueryBuilder()->select($db->getDatabasePlatform()->getMaxExpression('priority'))->from('@system_node')->where(['parent_id' => $node->parent_id])->execute()->fetchColumn();
        }
    }