From c2cf6a4f315953767d99ff5a6c4141bfc8da7da5 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/Lib/lang.php | 2 ++ app/Model/CoPetition.php | 52 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/app/Lib/lang.php b/app/Lib/lang.php index 8f00b8382..bfca300cf 100644 --- a/app/Lib/lang.php +++ b/app/Lib/lang.php @@ -1256,8 +1256,10 @@ 'er.prov.plugin' => 'Provisioning failed for %1$s: %2$s', 'er.pt.conf.exp' => 'Viewed expired confirmation', 'er.pt.dupe.cou' => 'The target CO Person already has a Role in the specified COU', + 'er.pt.dupe.cou-a' => 'The target CO Person already has a Role in the "%1$s" COU', 'er.pt.duplicate' => 'The identifier "%1$s" is already attached to an identity enrolled in this CO. This petition has been flagged as a duplicate.', 'er.pt.mail' => 'No email address found for %1$s to use for confirmation', + 'er.pt.interrupt' => 'Petition has been interrupted', 'er.pt.readonly' => 'Petition is now read only (status=%1$s)', 'er.pt.relink.org' => 'Could not automatically relink Org Identity due to another existing CO Person record', 'er.pt.relink.role.c' => 'Could not automatically relink CO Person Role due to another existing CO Person record', 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();