Init(); global $log; $this->log = $log; $this->PDFMakerModuleModel = Vtiger_Module_Model::getInstance('PDFMaker'); $this->PDFMakerModel = new PDFMaker_PDFMaker_Model(); foreach ($this->PDFAttributes as $atr) { $this->set($atr, ''); } $this->set('generate_type', 'attachment'); $this->set('onlyname', false); } /** * @param string $value * @return self */ public static function getInstance($value = 'PDFMaker') { return new self(); } public function setAvailablePassword($set_password) { $this->set_password = $set_password; } public function setPrint($isPrint = true) { $this->print = $isPrint; } /** * @param Vtiger_Request $request * @return array|void * @throws Exception */ public function generate(Vtiger_Request $request) { $this->retrieveAttributes($request); $this->retrieveRecords($request); $this->retrieveTemplateIds($request); $this->retrieveFocus(); $type = $this->get('type'); $mode = $this->get('mode'); if ('content' === $mode) { $this->generateContent($request); } elseif (in_array($type, ['doc', 'rtf'])) { $this->generateDocRtf($request); } else { $templateIds = $this->get('templateIds'); $preContent = $this->getPreContent($request); $language = $this->getLanguage(); $name = $this->PDFMakerModel->GetPreparedMPDF($this->mpdf, $this->records, $templateIds, $this->relModule, $language, $preContent, $this->set_password); $this->set('export_file_name', $name); if ('true' === $this->get('is_portal')) { return $this->getPortalInfo($request); } if (true === $this->get('onlyname')) { return $this->getFileInfo($request); } $this->generatePreview($request); } } /** * @throws Exception */ public function getPreContent(Vtiger_Request $request) { $preContent = array(); $contentTypes = array('header', 'body', 'footer'); $mode = $this->get('mode'); if (isset($mode) && 'edit' === $mode) { $templateIds = $this->get('templateIds'); $contentData = $request->getAll(); foreach ($templateIds as $templateId) { foreach ($contentTypes as $contentType) { $preContent[$contentType . $templateId] = $this->updatePreContent($contentData, $contentType, $templateId); } } } return $preContent; } /** * @param array $contentData * @param string $contentType * @param string $templateId * @return string * @throws Exception */ public function updatePreContent($contentData, $contentType, $templateId) { $content = $contentData[$contentType . $templateId]; return $this->replacePageBreaks($content, $templateId); } /** * @throws Exception */ public function replacePageBreaks($content, $templateId) { $pdfContent = $this->getPDFContent($templateId); if(!empty($content)) { $pageBreak = PDFMaker_PageBreak_Model::getInstance($content); $pageBreak->setPageBreak($pdfContent->getPageBreak()); $pageBreak->updateContent(); return $pageBreak->getContent(); } return $content; } /** * @throws Exception */ public function getPDFContent($templateId) { if (empty($this->PDFContentModels[$templateId])) { $this->setPDFContent($templateId, PDFMaker_PDFContent_Model::getInstance($templateId, $this->relModule, $this->focus, $this->getLanguage())); } return $this->PDFContentModels[$templateId]; } public function setPDFContent($templateId, $value) { $this->PDFContentModels[$templateId] = $value; } /** * @throws Exception */ public function retrieveAttributes(Vtiger_Request $request) { foreach ($this->PDFAttributes as $atr) { if ($request->has($atr) && !$request->isEmpty($atr)) { $this->set($atr, $request->get($atr)); } } if ($request->has('relmodule') && !$request->isEmpty('relmodule')) { $this->relModule = $request->get('relmodule'); $this->set('source_module', $this->relModule); } else { $this->relModule = $this->get('source_module'); } } /** * @throws Exception */ public function retrieveRecords(Vtiger_Request $request) { $forView = $this->get('forview'); $this->records = array(); if ('List' === $forView) { $this->records = $this->PDFMakerModuleModel->getRecordsListFromRequest($request); } else { $idsList = $this->get('idslist'); $record = $this->get('record'); if (!empty($idsList)) { //generating from listview $this->records = explode(';', rtrim($idsList, ';')); } elseif (!empty($record)) { $this->records = array($record); } } if (empty($this->relModule) && isset($this->records[0])) { $this->relModule = getSalesEntityType($this->records[0]); $request->set('relmodule', $this->relModule); } } public function retrieveTemplateIds(Vtiger_Request $request) { $pdfTemplateId = ''; if ($request->has('commontemplateid') && !$request->isEmpty('commontemplateid')) { $pdfTemplateId = $request->get('commontemplateid'); } elseif ($request->has('pdftemplateid') && !$request->isEmpty('pdftemplateid')) { $pdfTemplateId = $request->get('pdftemplateid'); } if (!empty($pdfTemplateId)) { $commonTemplateIds = trim($pdfTemplateId, ';'); $templateIds = explode(';', $commonTemplateIds); $this->set('templateIds', $templateIds); } else { $templateIds = $this->get('templateIds'); if (empty($templateIds)) { $templateIds = $this->PDFMakerModuleModel->getRequestTemplatesIds($request); $this->set('templateIds', $templateIds); } } } public function retrieveFocus() { $this->focus = CRMEntity::getInstance($this->relModule); } /** * @throws Exception * @return string */ public function getLanguage() { return !empty($this->get('language')) ? $this->get('language') : Vtiger_Language_Handler::getLanguage(); } /** * @throws Exception * @var Vtiger_Request $request */ public function generateContent(Vtiger_Request $request) { if (!Users_Privileges_Model::isPermitted($this->relModule, 'EditView')) { throw new AppException(vtranslate('LBL_PERMISSION_DENIED', $this->relModule)); } $language = $this->getLanguage(); $PDFContents = array(); foreach ($this->records as $record) { $this->focus->retrieve_entity_info($record, $this->relModule); $this->focus->id = $record; foreach ($this->templateIds as $templateId) { $PDFContent = $this->PDFMakerModel->GetPDFContentRef($templateId, $this->relModule, $this->focus, $language); $pdf_content = $PDFContent->getContent(); $body_html = $pdf_content['body']; $body_html = str_replace('#LISTVIEWBLOCK_START#', '', $body_html); $body_html = str_replace('#LISTVIEWBLOCK_END#', '', $body_html); $PDFContents[$templateId]['header'] = $pdf_content['header']; $PDFContents[$templateId]['body'] = $body_html; $PDFContents[$templateId]['footer'] = $pdf_content['footer']; } } include_once 'modules/PDFMaker/EditPDF.php'; showEditPDFForm($PDFContents); } /** * @throws Exception * @var Vtiger_Request $request */ public function generateDocRtf(Vtiger_Request $request) { if (!$this->PDFMakerModuleModel->CheckPermissions('EXPORT_RTF')) { $this->PDFMakerModuleModel->DieDuePermission(); } $language = $this->getLanguage(); $requestData = $request->getAll(); $siteUrl = vglobal('site_URL'); $Section = array(); $i = 1; foreach ($this->records as $record) { $this->focus->retrieve_entity_info($record, $this->relModule); $this->focus->id = $record; foreach ($this->templateIds as $templateId) { $PDFContent = $this->PDFMakerModel->GetPDFContentRef($templateId, $this->relModule, $this->focus, $language); $PDFContent->retrievePageBreak(); $PDFSettings = $PDFContent->getSettings(); if (empty($name)) { $name = $PDFContent->getFilename(); } if (isset($mode) && 'edit' === $mode) { $header_html = $requestData['header' . $templateId]; $body_html = $requestData['body' . $templateId]; $footer_html = $requestData['footer' . $templateId]; } else { $pdf_content = $PDFContent->getContent(); $header_html = $pdf_content['header']; $body_html = $pdf_content['body']; $footer_html = $pdf_content['footer']; } if (!empty($header_html) || !empty($footer_html)) { $headerFooterUrl = sprintf('cache/pdfmaker/%s_headerfooter_%s_%s.html', $record, $templateId, $i); $header_html = str_replace('{PAGENO}', "1", $header_html); $footer_html = str_replace('{PAGENO}', "1", $footer_html); $header_html = str_replace('{nb}', "1", $header_html); $footer_html = str_replace('{nb}', "1", $footer_html); $headerFooter = ''; } else { $headerFooterUrl = ''; $headerFooter = ''; } $ListViewBlocks = array(); if (strpos($body_html, '#LISTVIEWBLOCK_START#') !== false && strpos($body_html, '#LISTVIEWBLOCK_END#') !== false) { preg_match_all('|#LISTVIEWBLOCK_START#(.*)#LISTVIEWBLOCK_END#|sU', $body_html, $ListViewBlocks, PREG_PATTERN_ORDER); } if (PDFMaker_Utils_Helper::count($ListViewBlocks) > 0) { $TemplateContent[$templateId] = $pdf_content; $TemplateSettings[$templateId] = $PDFSettings; $num_listview_blocks = PDFMaker_Utils_Helper::count($ListViewBlocks[0]); for ($idx = 0; $idx < $num_listview_blocks; $idx++) { $ListViewBlock[$templateId][$idx] = $ListViewBlocks[0][$idx]; $ListViewBlockContent[$templateId][$idx][$record][] = $ListViewBlocks[1][$idx]; } } else { $content = '