/** {@inheritdoc} */ public function dispatch(\Zend\Stdlib\RequestInterface $request, \Zend\Stdlib\ResponseInterface $response = null) { // Fetch client with given ID for actions referring to a particular client $action = $this->getEvent()->getRouteMatch()->getParam('action'); if ($action != 'index' and $action != 'search' and $action != 'import') { try { $this->_currentClient = $this->_clientManager->getClient($request->getQuery('id')); } catch (\RuntimeException $e) { // Client does not exist - may happen when URL has become stale. $this->flashMessenger()->addErrorMessage('The requested client does not exist.'); return $this->redirectToRoute('client', 'index'); } } return parent::dispatch($request, $response); }
/** * 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(); }