public function subscribe($params)
{
$onlyCreateVersion = false;
$className = $this->getClassName();
$object = new $className();
// check for existing e-mail
$existingObject = $className::getByEmail($params["email"], 1);
if ($existingObject) {
// if there's an existing user with this email address, do not overwrite the contents, but create a new
// version which will be published as soon as the contact gets verified (token/email)
$object = $existingObject;
$onlyCreateVersion = true;
//throw new \Exception("email address '" . $params["email"] . "' already exists");
}
if (!array_key_exists("email", $params)) {
throw new \Exception("key 'email' is a mandatory parameter");
}
$object->setValues($params);
if (!$object->getParentId()) {
$object->setParentId(1);
}
$object->setNewsletterActive(true);
$object->setCreationDate(time());
$object->setModificationDate(time());
$object->setUserModification(0);
$object->setUserOwner(0);
$object->setPublished(true);
$object->setKey(\Pimcore\File::getValidFilename($object->getEmail() . "~" . substr(uniqid(), -3)));
if (!$onlyCreateVersion) {
$object->save();
}
// generate token
$token = base64_encode(\Zend_Json::encode(["salt" => md5(microtime()), "email" => $object->getEmail(), "id" => $object->getId()]));
$token = str_replace("=", "~", $token);
// base64 can contain = which isn't safe in URL's
$object->setProperty("token", "text", $token);
if (!$onlyCreateVersion) {
$object->save();
} else {
$object->saveVersion(true, true);
}
$this->addNoteOnObject($object, "subscribe");
return $object;
}