/** * Delete client, display confirmation form * * @return array|\Zend\Http\Response [client, form (Console\Form\DeleteClient)] or redirect response */ public function deleteAction() { $form = $this->_formManager->get('Console\\Form\\DeleteClient'); if ($this->getRequest()->isPost()) { if ($this->params()->fromPost('yes')) { $name = $this->_currentClient['Name']; try { $this->_clientManager->deleteClient($this->_currentClient, (bool) $this->params()->fromPost('DeleteInterfaces')); $this->flashMessenger()->addSuccessMessage(array($this->_("Client '%s' was successfully deleted.") => $name)); } catch (\RuntimeException $e) { $this->flashMessenger()->addErrorMessage(array($this->_("Client '%s' could not be deleted.") => $name)); } return $this->redirectToRoute('client', 'index'); } else { return $this->redirectToRoute('client', 'general', array('id' => $this->_currentClient['Id'])); } } else { return array('client' => $this->_currentClient, 'form' => $form); } }
/** * Merge clients * * This method is used to eliminate duplicates in the database. Based on the * last contact, the newest entry is preserved. All older entries are * deleted. Some information from the older entries can be preserved on the * remaining client. * * @param integer[] $clients IDs of clients to merge * @param bool $mergeCustomFields Preserve custom fields from oldest client * @param bool $mergeGroups Preserve manual group assignments from old clients * @param bool $mergePackages Preserve package assignments from old clients missing on new client * @throws \RuntimeException if an affected client cannot be locked */ public function merge(array $clients, $mergeCustomFields, $mergeGroups, $mergePackages) { // Remove duplicate IDs $clients = array_unique($clients); if (count($clients) < 2) { return; // Nothing to do } $connection = $this->_clients->getAdapter()->getDriver()->getConnection(); $connection->beginTransaction(); try { // Lock all given clients and create a list sorted by LastContactDate. foreach ($clients as $id) { $client = $this->_clientManager->getClient($id); if (!$client->lock()) { throw new \RuntimeException("Cannot lock client {$id}"); } $timestamp = $client['LastContactDate']->getTimestamp(); $list[$timestamp] = $client; } ksort($list); // Now that the list is sorted, renumber the indices $clients = array_values($list); // Newest client will be the only one not to be deleted, remove it from the list $newest = array_pop($clients); if ($mergeCustomFields) { // Overwrite custom fields with values from oldest client $newest->setCustomFields($clients[0]['CustomFields']); } if ($mergeGroups) { // Build list with all manual group assignments from old clients. // If more than 1 old client is to be merged and the clients // have different assignments for the same group, the result is // undefined. $groupList = array(); foreach ($clients as $client) { $groupList += $client->getGroupMemberships(\Model\Client\Client::MEMBERSHIP_MANUAL); } $newest->setGroupMemberships($groupList); } if ($mergePackages) { // Update the client IDs directly. Assignments from all older // clients are merged. Exclude packages that are already assigned. $id = $newest['Id']; $notIn = $this->_clientConfig->getSql()->select(); $notIn->columns(array('ivalue'))->where(array('hardware_id' => $id, 'name' => 'DOWNLOAD')); foreach ($clients as $client) { $this->_clientConfig->update(array('hardware_id' => $id), array('hardware_id' => $client['Id'], new \Zend\Db\Sql\Predicate\Operator('name', '!=', 'DOWNLOAD_SWITCH'), new \Zend\Db\Sql\Predicate\Like('name', 'DOWNLOAD%'), new \Zend\Db\Sql\Predicate\NotIn('ivalue', $notIn))); } } // Delete all older clients foreach ($clients as $client) { $this->_clientManager->deleteClient($client, false); } // Unlock remaining client $newest->unlock(); } catch (\Exception $exception) { $connection->rollback(); throw $exception; } $connection->commit(); }