Newscoop\Services\AuthorService::reorderAuthors PHP Метод

reorderAuthors() публичный Метод

Reorder Article Authors
public reorderAuthors ( EntityManager $em, array $articleAuthors, array $order = [] ) : boolean
$em Doctrine\ORM\EntityManager
$articleAuthors array
$order array
Результат boolean
    public function reorderAuthors($em, $articleAuthors, $order = array())
    {
        // clear current order
        foreach ($articleAuthors as $articleAuthor) {
            $articleAuthor->setOrder(null);
        }
        $em->flush();
        if (count($order) > 1) {
            $counter = 0;
            foreach ($order as $item) {
                list($authorId, $authorTypeId) = explode("-", $item);
                foreach ($articleAuthors as $articleAuthor) {
                    if ($articleAuthor->getAuthor()->getId() == $authorId && $articleAuthor->getType()->getId() == $authorTypeId) {
                        $articleAuthor->setOrder($counter + 1);
                        $counter++;
                    }
                }
            }
        } else {
            $counter = 1;
            foreach ($articleAuthors as $articleAuthor) {
                $articleAuthor->setOrder($counter);
                $counter++;
            }
        }
        $em->flush();
        return true;
    }