diff --git a/src/controllers/ContactsController.php b/src/controllers/ContactsController.php index 8e6953ba..72ef3f88 100644 --- a/src/controllers/ContactsController.php +++ b/src/controllers/ContactsController.php @@ -204,7 +204,7 @@ public function actionSaveContact() // Set the field layout ID $contact->fieldLayoutId = Campaign::$plugin->getSettings()->contactFieldLayoutId; - // Set the field locations + // Set the field values using the fields location $fieldsLocation = $request->getParam('fieldsLocation', 'fields'); $contact->setFieldValuesFromRequest($fieldsLocation); diff --git a/src/controllers/TrackerController.php b/src/controllers/TrackerController.php index 957b4b2e..b7bbde84 100644 --- a/src/controllers/TrackerController.php +++ b/src/controllers/TrackerController.php @@ -43,7 +43,7 @@ class TrackerController extends Controller /** * @inheritdoc */ - protected $allowAnonymous = ['open', 'click', 'subscribe', 'unsubscribe', 'verify-email']; + protected $allowAnonymous = ['open', 'click', 'subscribe', 'unsubscribe', 'verify-email', 'update-contact']; // Public Methods // ========================================================================= @@ -377,11 +377,67 @@ public function actionVerifyEmail(): Response ]); } + /** + * Update Contact + * + * @return Response|null + */ + public function actionUpdateContact() + { + $this->requirePostRequest(); + + $request = Craft::$app->getRequest(); + + // Get contact + $contact = $this->_getContact(); + + if ($contact === null) { + throw new NotFoundHttpException(Craft::t('campaign', 'Contact not found.')); + } + + $uid = $request->getBodyParam('uid'); + + // Verify UID + if ($uid === null || $contact->uid !== $uid) { + throw new NotFoundHttpException(Craft::t('campaign', 'Contact could not be verified.')); + } + + // Set the field values using the fields location + $fieldsLocation = $request->getParam('fieldsLocation', 'fields'); + $contact->setFieldValuesFromRequest($fieldsLocation); + + // Save it + if (!Craft::$app->getElements()->saveElement($contact)) { + if ($request->getAcceptsJson()) { + return $this->asJson([ + 'errors' => $contact->getErrors(), + ]); + } + + Craft::$app->getSession()->setError(Craft::t('campaign', 'Couldn’t save contact.')); + + // Send the contact back to the template + Craft::$app->getUrlManager()->setRouteParams([ + 'contact' => $contact + ]); + + return null; + } + + if ($request->getAcceptsJson()) { + return $this->asJson(['success' => true]); + } + + Craft::$app->getSession()->setNotice(Craft::t('campaign', 'Contact saved.')); + + return $this->redirectToPostedUrl($contact); + } + // Private Methods // ========================================================================= /** - * Gets contact by CID in query param + * Gets contact by CID in param * * @return ContactElement|null */ @@ -397,7 +453,7 @@ private function _getContact() } /** - * Gets sendout by SID in query param + * Gets sendout by SID in param * * @return SendoutElement|null */ @@ -413,7 +469,7 @@ private function _getSendout() } /** - * Gets link by LID in query param + * Gets link by LID in param * * @return LinkRecord|null */ diff --git a/src/services/ContactsService.php b/src/services/ContactsService.php index 17e3930d..4283aadc 100644 --- a/src/services/ContactsService.php +++ b/src/services/ContactsService.php @@ -68,7 +68,7 @@ public function getContactByCid(string $cid) } $contact = ContactElement::find() - ->where(['cid' => $cid]) + ->cid($cid) ->status(null) ->one(); diff --git a/src/templates/settings/mailinglisttypes/_edit.html b/src/templates/settings/mailinglisttypes/_edit.html index 31585729..32c5cdbe 100644 --- a/src/templates/settings/mailinglisttypes/_edit.html +++ b/src/templates/settings/mailinglisttypes/_edit.html @@ -12,7 +12,7 @@ {% block content %} - + {{ redirectInput('campaign/settings/mailinglisttypes') }}