/** * create / update subscription * return newsletter_user_object * * @param integer $listContentObjectId * @param integer $newsletterUserId * @param array $outputFormatArray * @param integer $status * @param integer $dryRun if true changes will be not stored to db usefull for test runs @see user_edit * @return object */ static function createUpdateNewsletterSubscription($listContentObjectId, $newsletterUserId, $outputFormatArray, $status = CjwNewsletterSubscription::STATUS_PENDING, $dryRun = false, $context = 'default') { $existingSubscriptionObject = CjwNewsletterSubscription::fetchByListIdAndNewsletterUserId($listContentObjectId, $newsletterUserId); $newsletterUser = CjwNewsletterUser::fetch($newsletterUserId); // if nl user status is confirmed set all nl subscription with status pending to confirmed if (is_object($newsletterUser) && (int) $newsletterUser->attribute('status') == CjwNewsletterUser::STATUS_CONFIRMED && $status == CjwNewsletterSubscription::STATUS_PENDING) { $status = CjwNewsletterSubscription::STATUS_CONFIRMED; } // update existing if (is_object($existingSubscriptionObject)) { $existingSubscriptionObject->setAttribute('output_format_array_string', CjwNewsletterSubscription::arrayToString($outputFormatArray)); if ($context == 'configure') { // if nl list autoapprove is disabled + admin has approved the nl subscription // + the nl subscription should be get status approved when update confirmstatus, if ($existingSubscriptionObject->attribute('status') == CjwNewsletterSubscription::STATUS_APPROVED) { // set confirmed timestamp if emty - could be possible if admin has approved subscription before user has confirm his email address if ($existingSubscriptionObject->attribute('confirmed') == 0) { $existingSubscriptionObject->setAttribute('confirmed', time()); } // else nothing } else { $existingSubscriptionObject->setAttribute('status', $status); } } else { $existingSubscriptionObject->setAttribute('status', $status); } if ($dryRun === false) { $existingSubscriptionObject->sync(); } return $existingSubscriptionObject; } else { $object = CjwNewsletterSubscription::create($listContentObjectId, $newsletterUserId, $outputFormatArray, $status, $context); if ($dryRun === false) { $object->store(); } return $object; } }