public function editAction()

in pp3/module/Application/src/Application/Controller/PluginController.php [240:358]


    public function editAction() {
        $pId = $this->params()->fromQuery('id');
        $plugin = $this->_pluginRepository->find($pId);        
        if (!$plugin || empty($pId) || !$plugin->isOwnedBy($this->getAuthenticatedUserId())) {
            return $this->redirect()->toRoute('plugin', array(
                'action' => 'list'
            ));
        }
        $req = $this->request;
        if ($req->isPost()) {
            $queryString = "";
            if ($this->params()->fromPost('addUserByEMail')) {
                $queryString='&activeTab=authors';
                $users = $this->_userRepository->findByEmail($this->params()->fromPost('addUserByEMail'));
                if (count($users) > 0) {
                    $added = 0;
                    foreach ($users as $user) {
                        if($plugin->addAuthor($user)) {
                            $added++;
                        }
                    }
                    if ($added > 0) {
                        $this->flashMessenger()->setNamespace('success')->addMessage('Authors updated.');
                        $this->_pluginRepository->persist($plugin);
                    } else {
                        $this->flashMessenger()->setNamespace('warning')->addMessage('Cannot add the same user twice.');
                    }
                } else {
                    $this->flashMessenger()->setNamespace('error')->addMessage('No user found with specified address.');
                }
            } else if ($this->params()->fromPost('removeAuthor')) {
                $queryString='&activeTab=authors';
                $user = $this->_userRepository->find($this->params()->fromPost('removeAuthor'));
                if(! $user) {
                    $this->flashMessenger()->setNamespace('error')->addMessage('No user found with specified id.');
                } else {
                    $plugin->removeAuthor($user);
                    if (count($plugin->getAuthors()) > 0) {
                        $this->_pluginRepository->persist($plugin);
                        $this->flashMessenger()->setNamespace('success')->addMessage('Author was removed.');
                    } else {
                        $this->flashMessenger()->setNamespace('error')->addMessage('Last author can not be removed.');
                    }
                }
            } else {
                $validatedData = $this->_validateAndCleanPluginData(
                        $this->params()->fromPost('name'),
                        $this->params()->fromPost('license'),
                        $this->params()->fromPost('description'),
                        $this->params()->fromPost('short_description'),
                        $this->params()->fromPost('category'),
                        $this->params()->fromPost('homepage'),
                        array_key_exists('image-file', $_FILES) ? $_FILES['image-file'] : false
                );
                if ($validatedData) {
                    $plugin->setName($validatedData['name']);
                    $plugin->setLicense($validatedData['license']);
                    $plugin->setDescription($validatedData['description']);
                    $plugin->setShortDescription($validatedData['short_description']);
                    $plugin->setHomepage($validatedData['homepage']);
                    $plugin->setLastUpdatedAt(new \DateTime('now'));

                    $imageDir = $this->_config['pp3']['catalogSavepath'] . '/plugins/' . $plugin->getId();

                    $oldImage = $plugin->getImage();

                    // save image
                    $im = $this->handleImgUpload($validatedData, $imageDir);
                    if ($im) {
                        $plugin->setImage($im);
                    }

                    if($this->params()->fromPost('image-file-delete') == 'true') {
                        $plugin->setImage(null);
                    }

                    if($oldImage && $oldImage != $plugin->getImage()) {
                        unlink($imageDir . '/' . $oldImage);
                    }

                    // categ
                    $plugin->removeCategories();
                    $this->_pluginRepository->persist($plugin);
                    $cat = $this->_categoryRepository->find($validatedData['category']);
                    if ($cat) {
                        $plugin->addCategory($cat);
                    }
                    $cat2 = $this->params()->fromPost('category2');
                    if ($cat2 && (!$cat || ($cat2 != $cat->getId()))) {
                        $cat2 = $this->_categoryRepository->find($this->params()->fromPost('category2'));
                        if ($cat2) {
                            $plugin->addCategory($cat2);
                        }
                    }

                    $this->_pluginRepository->persist($plugin);

                    $this->flashMessenger()->setNamespace('success')->addMessage('Plugin updated.');
                } else {
                    $this->flashMessenger()->setNamespace('error')->addMessage('Missing required data.');
                }
            }
            return $this->redirect()->toUrl('./edit?id='.$plugin->getId() . $queryString);
        }

        $activeTab = $this->params()->fromQuery("activeTab");

        if(!($activeTab == 'settings' || $activeTab == 'authors')) {
            $activeTab = 'settings';
        }

        return new ViewModel(array(
            'plugin' => $plugin,
            'categories' => $this->_categoryRepository->getAllCategoriesSortByName(),
            'activeTab' => $activeTab,
            'imageTypes' => $this->_config['imageTypes'],
            'editMode' => true
        ));
    }