private function renderSubscriber($iSbsId, $iSbsNum = 0)
 {
     $sResult = "";
     $oSubscriber = umiObjectsCollection::getInstance()->getObject($iSbsId);
     $oSubscriber = new umiSubscriber($iSbsId);
     $params = array();
     if ($oSubscriber instanceof umiSubscriber) {
         $params['sbs_name'] = $oSubscriber->getValue('lname') . " " . $oSubscriber->getValue('fname') . " " . $oSubscriber->getValue('father_name') . " (" . $oSubscriber->getName() . ")";
         if ($iSbsNum > 0) {
             $params['sbs_num'] = $iSbsNum;
         }
         $arrDispatches = $oSubscriber->getValue('subscriber_dispatches');
         $sDispNames = "";
         if (is_array($arrDispatches) && count($arrDispatches)) {
             for ($iI = 0; $iI < count($arrDispatches); $iI++) {
                 $oNextDisp = umiObjectsCollection::getInstance()->getObject($arrDispatches[$iI]);
                 $sDispNames .= "<a href=\"{$this->pre_lang}/admin/dispatches/dispatch_edit/{$arrDispatches[$iI]}/\">" . $oNextDisp->getName() . "</a>";
                 if ($iI < count($arrDispatches) - 1) {
                     $sDispNames .= ", ";
                 }
             }
         } else {
             $sDispNames = "<span style='color:red;'>нет</span>";
         }
         $params['sbs_dispatches'] = $sDispNames;
         $params['sbs_status'] = $oSubscriber->getValue('uid') && umiObjectsCollection::getInstance()->getObject($oSubscriber->getValue('uid')) ? "зарегистрированный пользователь" : "Гость";
         $oSbsDate = $oSubscriber->getValue('subscribe_date');
         $sSbsDate = "";
         if ($oSbsDate instanceof umiDate && $oSbsDate->timestamp) {
             $sSbsDate = $oSbsDate->getFormattedDate("d.m.Y H:m");
         }
         $params['sbs_date'] = $sSbsDate;
         $params['sbs_id'] = $oSubscriber->getId();
         $sResult = $this->parse_form("subscribers_list_row", $params);
     }
     return $sResult;
 }
    public function release_send()
    {
        $buffer = outputBuffer::current('HTTPOutputBuffer');
        $buffer->charset('utf-8');
        $buffer->contentType('text/xml');
        $buffer->push('<?xml version="1.0" encoding="utf-8"?>');
        $iDispId = (int) getRequest('param0');
        $iReleaseId = $this->getNewReleaseInstanceId($iDispId);
        $arrPostData = getRequest("data_values");
        $objectsColl = umiObjectsCollection::getinstance();
        $controller = cmsController::getInstance();
        $oDispatch = $objectsColl->getObject($iDispId);
        $oRelease = $objectsColl->getObject($iReleaseId);
        if (!$oDispatch instanceof umiObject || !$oRelease instanceof umiObject) {
            $buffer->push("<error>Не указан идентификатор рассылки</error>");
            $buffer->end();
        }
        if ($oRelease->getValue('status')) {
            $buffer->push("<error>Этот выпуск уже был отправлен</error>");
            $buffer->end();
        }
        $arrRecipients = array();
        if (!getSession('umi_send_list_' . $iReleaseId)) {
            $sel = new selector('objects');
            $sel->types("object-type")->name("dispatches", "subscriber");
            $sel->where("subscriber_dispatches")->equals($iDispId);
            $sel->option('return')->value('id');
            $sel->group("name");
            foreach ($sel->result() as $recipient) {
                $arrRecipients[] = $recipient['id'];
            }
            $_SESSION['umi_send_list_' . $iReleaseId] = $arrRecipients;
            $_SESSION['umi_send_list_' . $iReleaseId . '_count'] = count($arrRecipients);
        } else {
            $arrRecipients = getSession('umi_send_list_' . $iReleaseId);
        }
        $delay = getSession('umi_send_list_' . $iReleaseId . '_delay');
        $iTotal = (int) getSession('umi_send_list_' . $iReleaseId . '_count');
        if ($delay and time() < $delay) {
            $iSended = $iTotal - count($arrRecipients);
            $sResult = <<<END
<release dispatch="{$iDispId}">
\t<total>{$iTotal}</total>
\t<sended>{$iSended}</sended>
</release>
END;
            $buffer->push($sResult);
            $buffer->end();
        }
        $sHost = $controller->getCurrentDomain()->getHost();
        $oMailer = new umiMail();
        $arrMailBlocks = array();
        $arrMailBlocks['header'] = $oDispatch->getName();
        $arrMailBlocks['messages'] = "";
        list($sReleaseFrm, $sMessageFrm) = def_module::loadTemplatesForMail("dispatches/release", "release_body", "release_message");
        $sel = new selector('objects');
        $sel->types("object-type")->name("dispatches", "message");
        $sel->where("release_reference")->equals($iReleaseId);
        if ($sel->length()) {
            foreach ($sel->result() as $oNextMsg) {
                if ($oNextMsg instanceof umiObject) {
                    $arrMsgBlocks = array();
                    $arrMsgBlocks['body'] = $oNextMsg->getValue('body');
                    $arrMsgBlocks['header'] = $oNextMsg->getValue('header');
                    $arrMailBlocks['messages'] .= def_module::parseTemplateForMail($sMessageFrm, $arrMsgBlocks, false, $oNextMsg->getId());
                    $oNextAttach = $oNextMsg->getValue('attach_file');
                    if ($oNextAttach instanceof umiFile && !$oNextAttach->getIsBroken()) {
                        $oMailer->attachFile($oNextAttach);
                    }
                }
            }
        } else {
            unset($_SESSION[$iDispId . '_new_templater']);
            $buffer->push("<error>В выпуске нет сообщений</error>");
            $buffer->end();
        }
        $oMailer->setFrom(regedit::getInstance()->getVal("//settings/email_from"), regedit::getInstance()->getVal("//settings/fio_from"));
        $oMailer->setSubject($arrMailBlocks['header']);
        $delay = 0;
        $max_messages = (int) mainConfiguration::getinstance()->get('modules', 'dispatches.max_messages_in_hour');
        if ($max_messages && $iTotal >= $max_messages) {
            $delay = floor(3600 / $max_messages);
        }
        $aSended = array();
        $iPacketSize = 5;
        //TODO: add to settings
        foreach ($arrRecipients as $recipient_id) {
            --$iPacketSize;
            $oNextMailer = clone $oMailer;
            $oNextSbs = new umiSubscriber($recipient_id);
            $sRecipientName = $oNextSbs->getValue('lname') . " " . $oNextSbs->getValue('fname') . " " . $oNextSbs->getValue('father_name');
            $mail = $oNextSbs->getValue('email');
            if (!strlen($mail)) {
                $mail = $oNextSbs->getName();
            }
            $arrMailBlocks['unsubscribe_link'] = "http://" . $sHost . "/dispatches/unsubscribe/" . $oNextSbs->getId() . '/?email=' . $mail;
            $sMailBody = def_module::parseTemplateForMail($sReleaseFrm, $arrMailBlocks, false, $oNextSbs->getId());
            $oNextMailer->setContent($sMailBody);
            $oNextMailer->addRecipient($mail, $sRecipientName);
            if (!(defined('CURRENT_VERSION_LINE') && CURRENT_VERSION_LINE == 'demo')) {
                $oNextMailer->commit();
                $oNextMailer->send();
            }
            $aSended[] = $recipient_id;
            //Unload temporary objects
            unset($oNextMailer);
            if ($iPacketSize === 0) {
                break;
            }
            if ($delay) {
                $_SESSION['umi_send_list_' . $iReleaseId . '_delay'] = $delay + time();
                $_SESSION['umi_send_list_' . $iReleaseId] = array_diff($arrRecipients, $aSended);
                $iTotal = (int) getSession('umi_send_list_' . $iReleaseId . '_count');
                $iSended = $iTotal - (count($arrRecipients) - count($aSended));
                $sResult = <<<END
<release dispatch="{$iDispId}">
\t<total>{$iTotal}</total>
\t<sended>{$iSended}</sended>
</release>
END;
                $buffer->push($sResult);
                $buffer->end();
            }
        }
        umiMail::clearFilesCache();
        $_SESSION['umi_send_list_' . $iReleaseId] = array_diff($arrRecipients, $aSended);
        if (!count(getSession('umi_send_list_' . $iReleaseId))) {
            $oRelease->setValue('status', true);
            $oDate = new umiDate(time());
            $oRelease->setValue('date', $oDate);
            $oRelease->setName($oDate->getFormattedDate('d-m-Y H:i'));
            $oRelease->commit();
            $oDispatch->setValue('disp_last_release', $oDate);
            $oDispatch->commit();
        }
        $iTotal = (int) getSession('umi_send_list_' . $iReleaseId . '_count');
        $iSended = $iTotal - (count($arrRecipients) - count($aSended));
        usleep(500000);
        $sResult = <<<END
<release dispatch="{$iDispId}">
\t<total>{$iTotal}</total>
\t<sended>{$iSended}</sended>
</release>
END;
        unset($_SESSION[$iDispId . '_new_templater']);
        $buffer->push($sResult);
        $buffer->end();
    }