/** * Get the validation rules that apply to the request. * @return array */ public function rules() { $activityIdentifiers = []; $activityId = $this->get('id'); $identifiers = $activityId ? $this->iatiIdentifierRepository->getActivityIdentifiersForOrganizationExcept($activityId) : $this->iatiIdentifierRepository->getIdentifiersForOrganization(); foreach ($identifiers as $identifier) { $activityIdentifiers[] = $identifier->activity_identifier; } $activityIdentifier = implode(',', $activityIdentifiers); $rules = []; $rules['activity_identifier'] = 'required|exclude_operators|not_in:' . $activityIdentifier; $rules['iati_identifier_text'] = 'required'; return $rules; }
/** * Stores the activity by uploading csv * @param Request $request * @param UploadActivityRequest $uploadActivityRequest * @param CsvImportValidator $csvImportValidator * @param IatiIdentifierRepository $iatiIdentifierRepository * @return $this */ public function store(Request $request, UploadActivityRequest $uploadActivityRequest, CsvImportValidator $csvImportValidator, IatiIdentifierRepository $iatiIdentifierRepository) { $organization = $this->organizationManager->getOrganization($this->organizationId); $this->authorize('add_activity', $organization); $settings = $this->settingsManager->getSettings($this->organizationId); $defaultFieldValues = $settings->default_field_values; $defaultFieldGroups = $settings->default_field_groups; if (!isset($organization->reporting_org[0])) { $response = ['type' => 'warning', 'code' => ['settings', ['name' => 'activity']]]; return redirect('/settings')->withResponse($response); } $identifiers = []; $activityIdentifiers = $iatiIdentifierRepository->getIdentifiersForOrganization(); foreach ($activityIdentifiers as $identifier) { $identifiers[] = $identifier->identifier['activity_identifier']; } $file = $request->file('activity'); $uploadedActivityArray = $this->uploadActivityManager->getVersion()->getExcel()->load($file)->toArray(); $geopolitical = $defaultFieldGroups[0]['Geopolitical Information']; foreach ($uploadedActivityArray as $activity) { $recipientRegionExist = array_key_exists('recipient_region', $geopolitical); if (!$recipientRegionExist && !is_null($activity['recipient_region'])) { return redirect()->back()->withResponse(['type' => 'danger', 'code' => ['recipient_region_unselected_in_settings', ['name' => 'Activity']]]); } } if ($this->uploadActivityManager->isEmptyCsv($file)) { return redirect()->back()->withResponse(['type' => 'danger', 'code' => ['empty_template', ['name' => 'Activity']]]); } $validator = $csvImportValidator->validator->isValidActivityCsv($file, $identifiers); if (!$validator) { $response = ['type' => 'danger', 'code' => ['csv_template_error', ['name' => '']]]; return redirect()->back()->withResponse($response); } elseif ($validator->fails()) { $failedRows = $validator->failures(); $uploadedActivities = $this->uploadActivityManager->getVersion()->getExcel()->load($file)->toArray(); $validActivities = array_keys(array_diff_key($uploadedActivities, $failedRows)); $validActivityIndices = []; foreach ($validActivities as $validActivity) { $validActivityIndices[] = $validActivity + 1; } $filename = 'temporary-' . $this->organizationId . 'activity'; $this->temporarilyStoreCsvFor($validActivities, $filename); $validCsvFilePath = storage_path() . '/exports/' . $filename . '.csv'; if (!$this->saveValidatedActivities($validCsvFilePath, $defaultFieldValues)) { return redirect()->back()->withResponse(['type' => 'warning', 'code' => ['save_failed', ['name' => 'Activity']]]); } return $this->invalidActivities($validator, $validActivityIndices, $failedRows); } $check = $this->uploadActivityManager->save($file, $organization, $defaultFieldValues); if (is_a($check, 'Illuminate\\View\\View')) { return $check; } $response = $check ? ['type' => 'success', 'code' => ['updated', ['name' => 'Activities']]] : ['type' => 'danger', 'code' => ['update_failed', ['name' => 'Activities']]]; return redirect()->to(sprintf('/activity'))->withResponse($response); }