/**
  * Synchronous registration / deregistration for several lists with an array
  * if id_array has more elements than list_array, than is a deregistration defined
  * the difference of id_array and list_array means that these elements shouldn't
  * has subscriptions
  *
  * $subscriptionDataArr = array();
  * $subscriptionDataArr['ez_user_id']
  * $subscriptionDataArr['salutation']
  * $subscriptionDataArr['first_name'] = $http->postVariable( 'Subscription_FirstName' );
  * $subscriptionDataArr['name'] = $http->postVariable( 'Subscription_Name' );
  * $subscriptionDataArr['email'] = $http->postVariable( 'Subscription_Email' );
  *
  * $subscriptionDataArr['id_array'] = $http->postVariable( 'Subscription_IdArray' );
  * $subscriptionDataArr['list_array'] = $http->postVariable( 'Subscription_ListArray' );
  * $subscriptionDataArr['list_output_format_array'] = $http->postVariable( 'Subscription_OutputFormatArray' );
  *
  * @param array $subscriptionDataArr
  * @param $newNewsletterUserStatus status for new created nl users e.g. CjwNewsletterUser::STATUS_PENDING
  * @param $subscribeOnlyMode if true than no subscription will be removed used if subscription is done as ez_user
  * @param $context subscribe | configure | user_edit | datatype_edit | datatype_collect | csvimport from which context the function is called
  * @return array
  */
 static function createSubscriptionByArray($subscriptionDataArr, $newNewsletterUserStatus = CjwNewsletterUser::STATUS_PENDING, $subscribeOnlyMode = false, $context = 'default')
 {
     $resultArray = array();
     $resultArray['list_subscribe'] = array();
     $resultArray['list_remove'] = array();
     $resultArray['errors'] = array();
     $email = $subscriptionDataArr['email'];
     if (isset($subscriptionDataArr['salutation'])) {
         $salutation = $subscriptionDataArr['salutation'];
     } else {
         $salutation = null;
     }
     $firstName = $subscriptionDataArr['first_name'];
     $lastName = $subscriptionDataArr['last_name'];
     $eZUserId = isset($subscriptionDataArr['ez_user_id']) ? (int) $subscriptionDataArr['ez_user_id'] : 0;
     // new value form POST
     $newEzUserId = $eZUserId;
     $newEmail = $email;
     // TODO return here the nl user object for update + status
     $checkResult = CjwNewsletterUser::checkIfUserCanBeUpdated($email, $eZUserId, $updateNewEmail = true);
     switch ($checkResult) {
         // create new user
         case 40:
             break;
             // update user
         // update user
         case 41:
             break;
             // update user with new mail
         // update user with new mail
         case 42:
             break;
         case -20:
         case -1:
             if ($context == 'subscribe') {
                 eZDebug::writeDebug("checkResult[{$checkResult}] - CjwNewsletterSubscription::createSubscriptionByArray return false because email already exists");
                 // break because a newsletter user with email exists
                 return false;
             }
             break;
     }
     $idArray = $subscriptionDataArr['id_array'];
     $listArray = $subscriptionDataArr['list_array'];
     $listOutputFormatArray = $subscriptionDataArr['list_output_format_array'];
     $newsletterUserObject = CjwNewsletterUser::createUpdateNewsletterUser($email, $salutation, $firstName, $lastName, $eZUserId, (int) $newNewsletterUserStatus, $context);
     $resultArray['newsletter_user_object'] = $newsletterUserObject;
     if (is_object($newsletterUserObject) === false) {
         return $resultArray['errors'] = "Can not create new newsletter user with {$email}";
     }
     $newsletterUserId = $newsletterUserObject->attribute('id');
     // list_subscribe
     foreach ($listArray as $listId) {
         $outputFormatArray = $listOutputFormatArray[$listId];
         $status = CjwNewsletterSubscription::STATUS_PENDING;
         $dryRun = false;
         $resultArray['list_subscribe'][$listId] = CjwNewsletterSubscription::createUpdateNewsletterSubscription($listId, $newsletterUserId, $outputFormatArray, $status, $dryRun, $context);
     }
     if ($subscribeOnlyMode === false) {
         $listRemoveArray = array_diff($idArray, $listArray);
         // list_remove by user self
         foreach ($listRemoveArray as $listId) {
             $resultArray['list_remove'][$listId] = CjwNewsletterSubscription::removeSubscriptionByNewsletterUserSelf($listId, $newsletterUserId);
         }
     }
     return $resultArray;
 }
 /**
  * Validates the input and returns true if the input was
  * valid for this datatype.
  */
 function validateObjectAttributeHTTPInput($http, $base, $contentObjectAttribute)
 {
     $classAttribute = $contentObjectAttribute->contentClassAttribute();
     // if datatype is used as informationcollector
     if ($classAttribute->attribute('is_information_collector')) {
     } else {
         $userDataArray = $this->fetchCurrentContentObjectData($contentObjectAttribute);
         if (is_array($userDataArray) == false) {
             $contentObjectAttribute->setValidationError(ezi18n('cjw_newsletter/datatype/cjwnewslettersubcription/validation_error', 'Datatype can not be used here - user_account required.'));
             return eZInputValidator::STATE_INVALID;
         }
         // new value form POST
         $ezUserId = (int) $userDataArray['subscription_data_array']['ez_user_id'];
         $email = strtolower(trim($userDataArray['subscription_data_array']['email']));
         $checkResult = CjwNewsletterUser::checkIfUserCanBeUpdated($email, $ezUserId, $updateNewEmail = false);
         switch ($checkResult) {
             // create new user
             case 40:
                 break;
                 // update user
             // update user
             case 41:
                 break;
                 // update user with new mail
             // update user with new mail
             case 42:
                 break;
             case -20:
                 // the email is already used by an other nl_user with a different user_id
                 // we ignore the check because we think that the email is the unique key
                 // between ezuser and nl_user
                 break;
             case -21:
                 // the email is emty
                 break;
             case -1:
                 $contentObjectAttribute->setValidationError(ezi18n('cjw_newsletter/datatype/cjwnewslettersubcription/validation_error', 'No user account found'));
                 return eZInputValidator::STATE_INVALID;
                 break;
             default:
                 return eZInputValidator::STATE_INVALID;
         }
     }
     return eZInputValidator::STATE_ACCEPTED;
 }