/**
  * @param                      $id
  * @param Request              $request
  * @param BudgetRequestManager $budgetRequestManager
  * @return mixed
  */
 public function update($id, Request $request, BudgetRequestManager $budgetRequestManager)
 {
     $activityData = $this->activityManager->getActivityData($id);
     if (Gate::denies('ownership', $activityData)) {
         return redirect()->back()->withResponse($this->getNoPrivilegesMessage());
     }
     $this->authorizeByRequestType($activityData, 'budget');
     $budget = $request->all();
     if ($this->budgetManager->update($budget, $activityData)) {
         $this->activityManager->resetActivityWorkflow($id);
         $response = ['type' => 'success', 'code' => ['updated', ['name' => 'Budget']]];
         return redirect()->to(sprintf('/activity/%s', $id))->withResponse($response);
     }
     $response = ['type' => 'danger', 'code' => ['update_failed', ['name' => 'Budget']]];
     return redirect()->back()->withInput()->withResponse($response);
 }
 /**
  * updates activity title
  * @param                     $id
  * @param Request             $request
  * @param TitleRequestManager $titleRequestManager
  * @return \Illuminate\Http\RedirectResponse
  */
 public function update($id, Request $request, TitleRequestManager $titleRequestManager)
 {
     if (!$this->currentUserIsAuthorizedForActivity($id)) {
         return redirect()->back()->withResponse($this->getNoPrivilegesMessage());
     }
     $activityData = $this->titleManager->getActivityData($id);
     $this->authorizeByRequestType($activityData, 'title');
     $activityTitle = $request->all();
     if ($this->titleManager->update($activityTitle, $activityData)) {
         $this->activityManager->resetActivityWorkflow($id);
         $response = ['type' => 'success', 'code' => ['updated', ['name' => 'Activity Title']]];
         return redirect()->to(sprintf('/activity/%s', $id))->withResponse($response);
     }
     $response = ['type' => 'danger', 'code' => ['update_failed', ['name' => 'Activity Title']]];
     return redirect()->back()->withInput()->withResponse($response);
 }
 /**
  * updates activity recipient region
  * @param                               $id
  * @param Request                       $request
  * @param RecipientRegionRequestManager $recipientRegionRequestManager
  * @return \Illuminate\Http\RedirectResponse
  */
 public function update($id, Request $request, RecipientRegionRequestManager $recipientRegionRequestManager)
 {
     $activityData = $this->activityManager->getActivityData($id);
     if (Gate::denies('ownership', $activityData)) {
         return redirect()->back()->withResponse($this->getNoPrivilegesMessage());
     }
     $this->authorizeByRequestType($activityData, 'recipient_region');
     $activityTransactions = $this->transactionManager->getTransactions($id);
     $count = 0;
     if ($activityTransactions) {
         foreach ($activityTransactions as $transactions) {
             $transactionDetail = $transactions->transaction;
             removeEmptyValues($transactionDetail);
             if (!empty($transactionDetail['recipient_country']) || !empty($transactionDetail['recipient_region'])) {
                 $count++;
             }
         }
     }
     if ($count > 0) {
         $response = ['type' => 'warning', 'code' => ['message', ['message' => 'You cannot save Recipient Region in activity level because you have already saved recipient country or region in transaction level.']]];
         return redirect()->back()->withInput()->withResponse($response);
     }
     $recipientRegions = $request->all();
     foreach ($recipientRegions['recipient_region'] as &$recipientRegion) {
         $recipientRegion['region_vocabulary'] != '' ?: ($recipientRegion['region_vocabulary'] = '1');
     }
     if ($this->recipientRegionManager->update($recipientRegions, $activityData)) {
         $this->activityManager->resetActivityWorkflow($id);
         $response = ['type' => 'success', 'code' => ['updated', ['name' => 'Recipient Region']]];
         return redirect()->to(sprintf('/activity/%s', $id))->withResponse($response);
     }
     $response = ['type' => 'danger', 'code' => ['update_failed', ['name' => 'Recipient Region']]];
     return redirect()->back()->withInput()->withResponse($response);
 }
 public function update($id, Request $request, ConditionRequestManager $conditionRequestManager)
 {
     $activityData = $this->activityManager->getActivityData($id);
     if (Gate::denies('ownership', $activityData)) {
         return redirect()->back()->withResponse($this->getNoPrivilegesMessage());
     }
     $this->authorizeByRequestType($activityData, 'conditions');
     $condition = $request->except(['_token', '_method']);
     if ($this->conditionManager->update($condition, $activityData)) {
         $this->activityManager->resetActivityWorkflow($id);
         $response = ['type' => 'success', 'code' => ['updated', ['name' => 'Conditions']]];
         return redirect()->to(sprintf('/activity/%s', $id))->withResponse($response);
     }
     $response = ['type' => 'danger', 'code' => ['update_failed', ['name' => 'Related Activity']]];
     return redirect()->back()->withInput()->withResponse($response);
 }
 /**
  * updates activity sector
  * @param                      $id
  * @param Request              $request
  * @param SectorRequestManager $sectorRequestManager
  * @return \Illuminate\Http\RedirectResponse
  */
 public function update($id, Request $request, SectorRequestManager $sectorRequestManager)
 {
     $activityData = $this->activityManager->getActivityData($id);
     if (Gate::denies('ownership', $activityData)) {
         return redirect()->back()->withResponse($this->getNoPrivilegesMessage());
     }
     $this->authorizeByRequestType($activityData, 'sector');
     $sectors = $request->all();
     foreach ($sectors['sector'] as &$sector) {
         if ($sector['sector_vocabulary'] == 1 || $sector['sector_vocabulary'] == '') {
             $sector['sector_vocabulary'] = 1;
             $sector['sector_category_code'] = '';
             $sector['sector_text'] = '';
         } elseif ($sector['sector_vocabulary'] == 2) {
             $sector['sector_code'] = '';
             $sector['sector_text'] = '';
         } else {
             $sector['sector_code'] = '';
             $sector['sector_category_code'] = '';
         }
     }
     if ($this->sectorManager->update($sectors, $activityData)) {
         $this->activityManager->resetActivityWorkflow($id);
         $response = ['type' => 'success', 'code' => ['updated', ['name' => 'Sector']]];
         return redirect()->to(sprintf('/activity/%s', $id))->withResponse($response);
     }
     $response = ['type' => 'danger', 'code' => ['update_failed', ['name' => 'Sector']]];
     return redirect()->back()->withInput()->withResponse($response);
 }
 /**
  * updates activity participating organization
  * @param                                         $id
  * @param Request                                 $request
  * @param ParticipatingOrganizationRequestManager $participatingOrganizationRequestManager
  * @return \Illuminate\Http\RedirectResponse
  */
 public function update($id, Request $request, ParticipatingOrganizationRequestManager $participatingOrganizationRequestManager)
 {
     $activityData = $this->activityManager->getActivityData($id);
     if (Gate::denies('ownership', $activityData)) {
         return redirect()->back()->withResponse($this->getNoPrivilegesMessage());
     }
     $this->authorizeByRequestType($activityData, 'participating_organization');
     $participatingOrganization = $request->all();
     if (!$this->validateData($request->get('participating_organization'))) {
         $response = ['type' => 'warning', 'code' => ['participating_org', ['name' => 'participating organization']]];
         return redirect()->back()->withInput()->withResponse($response);
     }
     if ($this->participatingOrganizationManager->update($participatingOrganization, $activityData)) {
         $this->activityManager->resetActivityWorkflow($id);
         $response = ['type' => 'success', 'code' => ['updated', ['name' => 'Activity Participating Organization']]];
         return redirect()->to(sprintf('/activity/%s', $id))->withResponse($response);
     }
     $response = ['type' => 'danger', 'code' => ['update_failed', ['name' => 'Activity Participating Organization']]];
     return redirect()->back()->withInput()->withResponse($response);
 }
 /**
  * deletes data block from transaction
  * @param $id
  * @param $transactionId
  * @param $jsonPath
  */
 public function deleteBlock($id, $transactionId, $jsonPath)
 {
     $result = $this->transactionManager->deleteBlock($transactionId, $jsonPath);
     if ($result) {
         $this->activityManager->resetActivityWorkflow($id);
         $response = ['type' => 'success', 'code' => ['transaction_block_removed', ['element' => 'activity']]];
     } else {
         $response = ['type' => 'danger', 'code' => ['transaction_block_not_removed', ['element' => 'activity']]];
     }
     return redirect()->back()->withResponse($response);
 }
 /**
  * update country budget item
  * @param                                 $id
  * @param Request                         $request
  * @param CountryBudgetItemRequestManager $countryBudgetItemRequestManager
  * @return mixed
  */
 public function update($id, Request $request, CountryBudgetItemRequestManager $countryBudgetItemRequestManager)
 {
     $activityData = $this->activityManager->getActivityData($id);
     if (Gate::denies('ownership', $activityData)) {
         return redirect()->back()->withResponse($this->getNoPrivilegesMessage());
     }
     $this->authorizeByRequestType($activityData, 'country_budget_items');
     $countryBudgetItems = $request->all();
     foreach ($countryBudgetItems['country_budget_item'] as &$countryBudgetItem) {
         $code = $countryBudgetItem['vocabulary'] == 1 ? 'code_text' : 'code';
         $countryBudgetItem['budget_item'][0][$code] = '';
     }
     if ($this->countryBudgetItemManager->update($countryBudgetItems, $activityData)) {
         $this->activityManager->resetActivityWorkflow($id);
         $response = ['type' => 'success', 'code' => ['updated', ['name' => 'Country Budget Item']]];
         return redirect()->to(sprintf('/activity/%s', $id))->withResponse($response);
     }
     $response = ['type' => 'danger', 'code' => ['update_failed', ['name' => 'Country Budget Item']]];
     return redirect()->back()->withInput()->withResponse($response);
 }
 /**
  * deletes activity element
  * @param $id
  * @param $element
  */
 public function deleteElement($id, $element)
 {
     $activity = $this->activityManager->getActivityData($id);
     if (Gate::denies('ownership', $activity)) {
         return redirect()->back()->withResponse($this->getNoPrivilegesMessage());
     }
     $result = $this->activityManager->deleteElement($activity, $element);
     if ($result) {
         $this->activityManager->resetActivityWorkflow($id);
         $response = ['type' => 'success', 'code' => ['activity_element_removed', ['element' => 'activity']]];
     } else {
         $response = ['type' => 'danger', 'code' => ['activity_element_not_removed', ['element' => 'activity']]];
     }
     return redirect()->back()->withResponse($response);
 }
 /**
  * store the uploaded transaction
  * @param Request                  $request
  * @param                          $id
  * @param UploadTransactionRequest $uploadTransactionRequest
  * @param CsvImportValidator       $csvImportValidator
  * @return $this
  */
 public function store(Request $request, $id, UploadTransactionRequest $uploadTransactionRequest, CsvImportValidator $csvImportValidator)
 {
     $activity = $this->activityManager->getActivityData($id);
     if (Gate::denies('ownership', $activity)) {
         return redirect()->back()->withResponse($this->getNoPrivilegesMessage());
     }
     $this->authorize('add_activity', $activity);
     $file = $request->file('transaction');
     if ($this->uploadTransactionManager->isEmptyCsv($file)) {
         return redirect()->back()->withResponse(['type' => 'danger', 'code' => ['empty_template', ['name' => 'Transaction']]]);
     }
     $validator = $this->validatorForCurrentCsvType($file, $csvImportValidator);
     if (null === $validator) {
         return redirect()->back()->withResponse(['type' => 'danger', 'code' => ['header_mismatch', ['name' => 'Transaction']]]);
     }
     if (null !== $validator && $validator->fails()) {
         $response = ['type' => 'danger', 'messages' => $validator->errors()->all()];
         return redirect()->back()->withInput()->withResponse($response);
     }
     $this->uploadTransactionManager->save($file, $activity);
     $this->activityManager->resetActivityWorkflow($id);
     $response = ['type' => 'success', 'code' => ['updated', ['name' => 'Transactions']]];
     return redirect()->to(sprintf('/activity/%s/transaction', $id))->withResponse($response);
 }
 /**
  * Remove result from storage.
  * @param  int $id
  * @param      $resultId
  * @return \Illuminate\Http\Response
  */
 public function destroy($id, $resultId)
 {
     $activityData = $this->activityManager->getActivityData($id);
     if (Gate::denies('ownership', $activityData)) {
         return redirect()->back()->withResponse($this->getNoPrivilegesMessage());
     }
     $this->authorize('delete_activity', $activityData);
     $activityResult = $this->resultManager->getResult($resultId, $id);
     if ($this->resultManager->deleteResult($activityResult)) {
         $this->activityManager->resetActivityWorkflow($id);
         $response = ['type' => 'success', 'code' => ['deleted', ['name' => 'Result']]];
     } else {
         $response = ['type' => 'danger', 'code' => ['delete_failed', ['name' => 'result']]];
     }
     return redirect()->back()->withResponse($response);
 }
 /**
  * @param                              $activityId
  * @param IatiIdentifierRequestManager $iatiIdentifierRequestManager
  * @param Request                      $request
  * @return \Illuminate\Http\RedirectResponse
  */
 public function update($activityId, IatiIdentifierRequestManager $iatiIdentifierRequestManager, Request $request)
 {
     $activityData = $this->activityManager->getActivityData($activityId);
     if (Gate::denies('ownership', $activityData)) {
         return redirect()->back()->withResponse($this->getNoPrivilegesMessage());
     }
     $iatiIdentifierData = $this->iatiIdentifierManager->getActivityData($activityId);
     $this->authorizeByRequestType($iatiIdentifierData, 'identifier');
     $input = $request->all();
     if ($this->iatiIdentifierManager->update($input, $iatiIdentifierData)) {
         $this->activityManager->resetActivityWorkflow($activityId);
         $response = ['type' => 'success', 'code' => ['updated', ['name' => 'Activity Iati Identifier']]];
         return redirect()->to(sprintf('/activity/%s', $activityId))->withResponse($response);
     }
     $response = ['type' => 'danger', 'code' => ['update_failed', ['name' => 'Activity Iati Identifier']]];
     return redirect()->route('activity.iati-identifier.index', $activityId)->withInput()->withResponse($response);
 }