Skip to content

Commit

Permalink
Added anonymous update contact controller action
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Croker committed Nov 7, 2018
1 parent b861583 commit d79e7f3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/controllers/ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
64 changes: 60 additions & 4 deletions src/controllers/TrackerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
// =========================================================================
Expand Down Expand Up @@ -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
*/
Expand All @@ -397,7 +453,7 @@ private function _getContact()
}

/**
* Gets sendout by SID in query param
* Gets sendout by SID in param
*
* @return SendoutElement|null
*/
Expand All @@ -413,7 +469,7 @@ private function _getSendout()
}

/**
* Gets link by LID in query param
* Gets link by LID in param
*
* @return LinkRecord|null
*/
Expand Down
2 changes: 1 addition & 1 deletion src/services/ContactsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function getContactByCid(string $cid)
}

$contact = ContactElement::find()
->where(['cid' => $cid])
->cid($cid)
->status(null)
->one();

Expand Down
2 changes: 1 addition & 1 deletion src/templates/settings/mailinglisttypes/_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

{% block content %}

<input type="hidden" name="action" value="campaign/mailing-list-types/save-mailing-list-type">
<input type="hidden" name="action" value="campaign/mailing-list-types/save-mailing-list-type" />

{{ redirectInput('campaign/settings/mailinglisttypes') }}

Expand Down

0 comments on commit d79e7f3

Please sign in to comment.