From b1408f92a72bad826f09029f745d25e278eafdf6 Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Sun, 30 Oct 2022 09:47:55 +0200 Subject: [PATCH] Wedge plugin beforeSavePetitionAttributes hook --- app/Model/CoPetition.php | 52 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/app/Model/CoPetition.php b/app/Model/CoPetition.php index 447f5364c..e7bf0daf1 100644 --- a/app/Model/CoPetition.php +++ b/app/Model/CoPetition.php @@ -442,6 +442,53 @@ public function attributeOptionalAndEmpty($efAttrID, $data, $efAttrs) { return true; } + + /** + * Petition Attributes Before Save Wedge Plugin Hook + * + * @param Integer $id CO Petition ID + * @param Integer $enrollmentFlowId Enrollment Flow ID + * @param Array $requestData Attributes from submitted Petition + * @param Integer $petitionerId CO Person ID of the petitioner + * + * @since COmanage Registry v4.1.0 + * @return bool + * @throws RuntimeException + */ + + protected function beforeSavePetitionAttributes($id, $enrollmentFlowId, &$requestData, $petitionerId) { + $args = array(); + $args['conditions']['CoEnrollmentFlowWedge.co_enrollment_flow_id'] = $enrollmentFlowId; + $args['conditions']['CoEnrollmentFlowWedge.status'] = SuspendableStatusEnum::Active; + $args['order'] = array('ordr' => 'asc'); + + $wedges = $this->CoEnrollmentFlow->CoEnrollmentFlowWedge->find('all', $args); + + foreach($wedges as $wedge) { + $pluginName = $wedge["CoEnrollmentFlowWedge"]["plugin"]; + // We need to load the dependency for the next call + $this->CoEnrollmentFlow->CoEnrollmentFlowWedge->bindModel( + array('hasMany' => array( + $pluginName => array( + 'className' => $pluginName . '.' . $pluginName + ))) + ); + if(method_exists($this->CoEnrollmentFlow->CoEnrollmentFlowWedge->$pluginName, 'beforeSaveAttributes')) { + try { + $response = $this->CoEnrollmentFlow + ->CoEnrollmentFlowWedge + ->$pluginName + ->beforeSaveAttributes($id, $enrollmentFlowId, $requestData, $petitionerId); + if(!$response) { + throw new RuntimeException(_txt('er.pt.interrupt')); + } + } catch (Exception $e) { + throw new RuntimeException($e->getMessage()); + } + } + + } + } /** * Check the eligibility for a CO Petition. @@ -1983,7 +2030,10 @@ public function saveAttributes($id, $enrollmentFlowId, $requestData, $petitioner if(!$id) { throw new InvalidArgumentException(_txt('er.notprov.id', array(_txt('ct.petitions.1')))); } - + + // Execute any Enroller plugin hooks + $this->beforeSavePetitionAttributes($id, $enrollmentFlowId, $requestData, $petitionerId); + // Start a transaction $dbc = $this->getDataSource(); $dbc->begin();