/**
  * Statistic data about sending process
  *
  * @return array
  */
 function getSendItemsStatistic()
 {
     $editionSendId = $this->attribute('id');
     $itemsCount = CjwNewsletterEditionSendItem::fetchListBySendIdAndStatusCount($editionSendId, false);
     $itemsNotSend = CjwNewsletterEditionSendItem::fetchListBySendIdAndStatusCount($editionSendId, CjwNewsletterEditionSendItem::STATUS_NEW);
     $itemsSend = CjwNewsletterEditionSendItem::fetchListBySendIdAndStatusCount($editionSendId, CjwNewsletterEditionSendItem::STATUS_SEND);
     $itemsBounced = CjwNewsletterEditionSendItem::fetchBounceCountByEditionSendId($editionSendId);
     $itemsSendInPersent = 0;
     // catch division by zero
     if ($itemsCount > 0) {
         $itemsSendInPersent = round($itemsSend / $itemsCount * 100, 1);
     }
     return array('items_count' => $itemsCount, 'items_not_send' => $itemsNotSend, 'items_send' => $itemsSend, 'items_send_in_percent' => $itemsSendInPersent, 'items_bounced' => $itemsBounced);
 }
 /**
  * saved parsed infos from ezcMailObject into database
  *
  * @param $parsedResult
  */
 private function saveParsedInfos($parsedResult)
 {
     $this->setAttribute('email_from', $parsedResult['from']);
     $this->setAttribute('email_to', $parsedResult['to']);
     $this->setAttribute('email_subject', $parsedResult['subject']);
     $this->setAttribute('bounce_code', $parsedResult['error_code']);
     //$this->setAttribute( 'final_recipient', $parsedResult[ 'final_recipient' ] );
     $this->setAttribute('email_send_date', $this->convertEmailSendDateToTimestamp($parsedResult['email_send_date']));
     // if x-cjwnl-senditem hash was set in bounce mail than fetch some ez data
     if (isset($parsedResult['x-cjwnl-senditem'])) {
         $sendItemHash = $parsedResult['x-cjwnl-senditem'];
         // try to fetch edition send item object
         $sendItemObject = CjwNewsletterEditionSendItem::fetchByHash($sendItemHash, true);
         if (is_object($sendItemObject)) {
             $newsletterUserId = $sendItemObject->attribute('newsletter_user_id');
             $editionSendId = $sendItemObject->attribute('edition_send_id');
             $editionSendItemId = $sendItemObject->attribute('id');
             $this->setAttribute('newsletter_user_id', $newsletterUserId);
             $this->setAttribute('edition_send_id', $editionSendId);
             $this->setAttribute('edition_send_item_id', $editionSendItemId);
             if ($this->isBounce()) {
                 $sendItemObject->setBounced();
                 $newsletterUser = $sendItemObject->attribute('newsletter_user_object');
                 if (is_object($newsletterUser)) {
                     // bounce nl user
                     $isHardBounce = false;
                     $newsletterUser->setBounced($isHardBounce);
                 }
             }
         }
     } elseif (isset($parsedResult['x-cjwnl-user'])) {
         $newsletterUser = CjwNewsletterUser::fetchByHash($sendItemHash, true);
         if (is_object($sendItemObject)) {
             $newsletterUserId = $newsletterUser->attribute('id');
             $this->setAttribute('newsletter_user_id', $newsletterUserId);
             if ($this->isBounce()) {
                 // bounce nl user
                 $isHardBounce = false;
                 $newsletterUser->setBounced($isHardBounce);
             }
         }
     }
     CjwNewsletterLog::writeDebug('parse_result CjwNewsletterMailboxItem::saveParsedInfos', 'mailbox_item', $this->attribute('id'), $parsedResult);
     // item is parsed
     $this->setAttribute('processed', time());
     $this->store();
 }
 /**
  *
  * @param integer $newsletterUserId
  * @return array
  */
 static function fetchEditonSendItemListCount($newsletterUserId)
 {
     $count = CjwNewsletterEditionSendItem::fetchListByNewsletterIdCount($newsletterUserId);
     return array('result' => $count);
 }
 /**
  * This should be called if a user is bounced or blacklisted
  * all related subscriptions and active senditems will be updated
  *
  * @param $status
  * @return unknown_type
  */
 private function setAllNewsletterUserRelatedItemsToStatus($status)
 {
     $newsletterUserId = $this->attribute('id');
     $updateSubcriptions = false;
     switch ($status) {
         case CjwNewsletterSubscription::STATUS_BOUNCED_SOFT:
         case CjwNewsletterSubscription::STATUS_BOUNCED_HARD:
             $bounceCount = CjwNewsletterEditionSendItem::setAllActiveItemsToStatusAbortAndBouncedByNewsletterUserId($newsletterUserId);
             $updateSubcriptions = true;
             break;
         case CjwNewsletterSubscription::STATUS_BLACKLISTED:
             // update active senditems
             $abortCount = CjwNewsletterEditionSendItem::setAllActiveItemsToStatusAbortByNewsletterUserId($newsletterUserId);
             $updateSubcriptions = true;
             break;
     }
     if ($updateSubcriptions === true) {
         // update active subscriptions
         $activeSubscriptionList = CjwNewsletterSubscription::fetchListNotRemovedOrBlacklistedByNewsletterUserId($newsletterUserId, true);
         foreach ($activeSubscriptionList as $subscription) {
             if ($subscription->attribute('status') == $status) {
                 CjwNewsletterLog::writeDebug('skip - already set this status - CjwNewsletterUser::setAllNewsletterUserRelatedItemsToStatus', 'subscription', 'status', array('status' => $status, 'subscription_id' => $subscription->attribute('id'), 'nl_user' => $newsletterUserId));
             } else {
                 $subscription->setAttribute('status', $status);
                 $subscription->store();
                 /*    CjwNewsletterLog::writeDebug(
                        'set CjwNewsletterUser::setAllNewsletterUserRelatedItemsToStatus',
                       'subscription',
                       'status',
                       array( 'status' => $status,
                       'subscription_id' => $subscription->attribute('id'),
                       'nl_user' => $newsletterUserId ) );
                       */
             }
         }
     }
 }
 /**
  * fetcht edition_send_item object from hash code
  *
  * @param string $hashCode
  * @return object / boolean
  */
 public static function fetchByHash($hashCode, $asObject)
 {
     return eZPersistentObject::fetchObject(CjwNewsletterEditionSendItem::definition(), null, array('hash' => $hashCode), $asObject);
 }
         $outputFormatStringArray[$outputFormatId] = CjwNewsletterEdition::prepareImageInclude($outputFormatNewsletterContentArray);
     }
 }
 // embed images ends
 $emailSender = $sendObject->attribute('email_sender');
 $emailSenderName = $sendObject->attribute('email_sender_name');
 $limit = 50;
 $offset = 0;
 $itemCounter = 1;
 $progressMonitor = new ezcConsoleProgressMonitor($output, $itemsNotSend);
 $cjwMail = new CjwNewsletterMail();
 $cjwMail->setTransportMethodCronjobFromIni();
 // process every send_item of current sendobject
 for ($i = 0; $i < $itemsNotSend; $i += $limit) {
     //  $progressBar->advance();
     $sendItemList = CjwNewsletterEditionSendItem::fetchListSendIdAndStatus($editionSendId, CjwNewsletterEditionSendItem::STATUS_NEW, $limit, $offset);
     $count = count($sendItemList);
     foreach ($sendItemList as $sendItem) {
         $id = $sendItem->attribute('id');
         $outputFormatId = $sendItem->attribute('output_format_id');
         // ### get newsletter user data through send_item_object
         $newsletterUserObject = $sendItem->attribute('newsletter_user_object');
         $emailReceiver = $newsletterUserObject->attribute('email');
         $emailReceiverName = $newsletterUserObject->attribute('email_name');
         // fetch html & text content of parsed outputxml from senmdobject
         // data of outputformate
         $outputStringArray = $outputFormatStringArray[$outputFormatId]['body'];
         $emailSubject = $outputFormatStringArray[$outputFormatId]['subject'];
         $outputStringArrayNew = array('html' => '', 'text' => '');
         foreach ($outputStringArray as $index => $string) {
             $outputStringArrayNew[$index] = CjwNewsletterUtils::replaceNewsletterMarkers($string, $sendObject, $newsletterUserObject);
 $message = "++ Find SubscriptionObjects with STATUS_APPROVED: " . count($subscriptionObjectList);
 $cli->output($message);
 $counter = 0;
 foreach ($subscriptionObjectList as $subscriptionObject) {
     $subscriptionId = $subscriptionObject->attribute('id');
     $editionContentObjectId = $subscriptionObject->attribute('edition_contentobject_id');
     $newsletterUserId = $subscriptionObject->attribute('newsletter_user_id');
     $subscriptionOutputFormatArray = $subscriptionObject->attribute('output_format_array');
     $counter++;
     // status == STATUS_WAIT_FOR_PROCESS || != ABORT ?
     $newsletterEdtionSendObject->sync();
     if ($newsletterEdtionSendObject->attribute('status') == CjwNewsletterEditionSend::STATUS_WAIT_FOR_PROCESS) {
         // every subscription can have multiple outputformats
         // create for every outputformat one send_item
         foreach ($subscriptionOutputFormatArray as $outputFormatId => $outputFormatName) {
             $newSendItemResult = CjwNewsletterEditionSendItem::create($sendId, $newsletterUserId, $outputFormatId, $subscriptionId);
             if (is_object($newSendItemResult)) {
                 // create edtion_send_item
                 $message = "++ [SEND_ITEM][{$counter}] create new sendItem id: " . $newSendItemResult->attribute('id');
                 $cli->output($message);
             } else {
                 // create edtion_send_item
                 $message = "++ [Error][SEND_ITEM][{$counter}] sendItem already exist do nothing with it!";
                 $cli->output($message);
             }
         }
     } else {
         $message = "++ [ABBORT][{$counter}] Abborting EditionSendObject has not Status STATUS_WAIT_FOR_PROCESS or : " . $sendId;
         $cli->output($message);
     }
 }