/**
  * Update settings
  *
  * @param  int                   $id
  * @param SettingsRequestManager $settingRequest
  * @return
  * @internal param TempRequest|SettingsRequestManager $request
  */
 public function update($id, SettingsRequestManager $settingRequest)
 {
     $currentUser = auth()->user();
     if ($currentUser->isNotAdmin()) {
         return redirect()->back()->withResponse(['type' => 'warning', 'code' => ['message', ['message' => 'You do not have the correct privileges to view this page.']]]);
     }
     $input = $settingRequest->requestHandler->all();
     $newPublishingType = $input['publishing_type'][0]['publishing'];
     $oldIdentifier = $this->organization->reporting_org[0]['reporting_organization_identifier'];
     $settings = $this->settingsManager->getSettings($this->organization->id);
     $publishingType = $settings->publishing_type;
     $activities = $this->activityManager->getActivities($this->organization->id);
     if ($publishingType != $newPublishingType) {
         $publishedFiles = $this->activityManager->getActivityPublishedFiles(Session::get('org_id'));
         if (count($publishedFiles)) {
             $this->generateNewFiles($newPublishingType, $activities);
         }
     }
     $reportingOrgIdentifier = $input['reporting_organization_info'][0]['reporting_organization_identifier'];
     foreach ($activities as $activity) {
         $status = $activity['published_to_registry'];
         $otherIdentifier = (array) $activity->other_identifier;
         if ($status == 1 && !in_array(["reference" => $oldIdentifier, "type" => "B1", 'owner_org' => []], $otherIdentifier) && $oldIdentifier !== $reportingOrgIdentifier) {
             $otherIdentifier[] = ['reference' => $oldIdentifier, 'type' => 'B1', 'owner_org' => []];
             $this->otherIdentifierManager->update(['other_identifier' => $otherIdentifier], $activity);
         }
     }
     $result = $this->settingsManager->updateSettings($input, $this->organization, $this->settings);
     if (!$result) {
         $response = ['type' => 'danger', 'code' => ['update_failed', ['name' => 'Settings']]];
         return redirect()->back()->withResponse($response);
     }
     $response = ['type' => 'success', 'code' => ['updated', ['name' => 'Settings']]];
     return redirect()->to(config('app.admin_dashboard'))->withResponse($response);
 }
 /**
  * @return bool
  */
 public function publishToRegistry()
 {
     $activityPublishedFiles = $this->activityManager->getActivityPublishedFiles($this->organization_id);
     $settings = $this->settingsManager->getSettings($this->organization_id);
     $api_url = config('filesystems.iati_registry_api_base_url');
     $apiCall = new CkanClient($api_url, $settings['registry_info'][0]['api_id']);
     try {
         foreach ($activityPublishedFiles as $publishedFile) {
             $data = $this->generateJson($publishedFile);
             $this->loggerInterface->info('Payload for publishing', ['payload' => $data, 'by_user' => auth()->user()->name]);
             if ($settings->publishing_type == "segmented") {
                 $filename = explode('-', $publishedFile->filename);
                 $code = str_replace('.xml', '', end($filename));
             }
             if ($publishedFile['published_to_register'] == 0) {
                 $apiCall->package_create($data);
                 $this->activityManager->updatePublishToRegister($publishedFile->id);
             } elseif ($publishedFile['published_to_register'] == 1) {
                 //                    $package = ($settings->publishing_type == "segmented") ? $settings['registry_info'][0]['publisher_id'] . '-' . $code : $settings['registry_info'][0]['publisher_id'] . '-activities';
                 $apiCall->package_update($data);
             }
             $this->loggerInterface->info('Activity file published to registry.', ['payload' => $data, 'by_user' => auth()->user()->name]);
         }
         return true;
     } catch (\Exception $e) {
         $this->loggerInterface->error($e);
         return false;
     }
 }