* Date: 20.09.14 23:15 * You must not use this file without permission. */ namespace Workflow\Plugins\Mailattachments; use RedooReports\Report; use Workflow\VtUtils; class RedooReports extends \Workflow\Attachment { public function isActive($moduleName) { return vtlib_isModuleActive('RedooReports'); } public function getConfigurations($moduleName) { $sql = 'SELECT * FROM vtiger_redooreport WHERE mainmodule != "" AND title != "" ORDER BY title'; $result = VtUtils::query($sql); $reports = array(); $options = array(); while($row = VtUtils::fetchByAssoc($result)) { $type = ucfirst(strtolower($row['type'])); $options[] = ''; } $configuration = array( 'html' => 'Attach Report from Flex Reports
', 'script' => " jQuery('#flexreportid').on('change', function(e) { if(jQuery('option:selected', e.currentTarget).data('type') == 'grid' || jQuery('option:selected', e.currentTarget).data('type') == 'sql') { jQuery('#flexreportfiletype option[value=\"xls\"]').prop('disabled', false); jQuery('#flexreportfiletype option[value=\"png\"]').prop('disabled', true); } else { jQuery('#flexreportfiletype option[value=\"xls\"]').prop('disabled', true); jQuery('#flexreportfiletype option[value=\"png\"]').prop('disabled', false); } }); Attachments.registerCallback('redooreport', function() { jQuery('#redooreportAttachmentContainer').hide(); var reportID = jQuery('select[data-name=\"reportid\"]').val(); var ReportLabel = jQuery('select[data-name=\"reportid\"] option:selected').data('title'); var filetype = jQuery('select[data-name=\"filetype\"]').val(); var filename = jQuery('#filename').val(); return [ { 'id' : 's#redooreports#' + reportID + '#' + filetype, 'label' : filetype.toUpperCase() + ' ' + ReportLabel, 'filename' : '', 'options' : { 'filename' : filename, 'reportid' : reportID, 'filetype' : filetype } } ]; });"); $return = array($configuration); return $return; } /** * @param $key * @param $value * @param $context \Workflow\VTEntity * @return array|void */ public function generateAttachments($key, $value, $context) { global $current_user; $adb = \PearDatabase::getInstance(); $config = $value[2]; $reportId = $config['reportid']; require_once(vglobal('root_directory').'/modules/RedooReports/autoload_wf.php'); @mkdir(vglobal('root_directory') . DIRECTORY_SEPARATOR . 'test' . DIRECTORY_SEPARATOR . 'Workflow' ); $tmpfile = vglobal('root_directory') . DIRECTORY_SEPARATOR . 'test' . DIRECTORY_SEPARATOR . 'Workflow' . DIRECTORY_SEPARATOR . ''.date('Y-m-d-H-i-s').'.'.$config['filetype']; $reportObj = new Report($reportId); $type = $reportObj->getType(); if($type == 'grid' || $type == 'sql') { if($config['filetype'] == 'pdf') { $tmpfile = $reportObj->getParser()->generatePDF(); } elseif($config['filetype'] == 'xls') { $reportObj->getParser()->generateXLS($tmpfile); } } else { $reportObj->getParser()->generateStatic($tmpfile, $config['filetype']); } $filename = $value[2]['filename']; $filename = \Workflow\VTTemplate::parse($filename, $context); if(empty($filename)) { $filename = 'report-'.$reportId; } if(strpos($filename, '.'.$config['filetype']) === false) { $filename .= '.'.$config['filetype']; } $filename = preg_replace('/[^A-Za-z0-9-_.]/', '_', $filename); if($this->_mode === self::MODE_NOT_ADD_NEW_ATTACHMENTS) { $this->addAttachmentRecord('PATH', $tmpfile, $filename); return; } $upload_file_path = decideFilePath(); $next_id = $adb->getUniqueID("vtiger_crmentity"); rename($tmpfile, $upload_file_path . $next_id . "_" . $filename); $filesize = filesize($upload_file_path . $next_id . "_" . $filename); $filetype = "application/octet-stream"; $sql1 = "insert into vtiger_crmentity (crmid,smcreatorid,smownerid,setype,description,createdtime,modifiedtime) values(?, ?, ?, ?, ?, ?, ?)"; $params1 = array($next_id, $current_user->id, $current_user->id, "Documents Attachment",'Documents Attachment', date("Y-m-d H:i:s"), date("Y-m-d H:i:s")); $adb->pquery($sql1, $params1); $sql2 = "insert into vtiger_attachments(attachmentsid, name, description, type, path) values(?, ?, ?, ?, ?)"; $params2 = array($next_id, $filename,'', $filetype, $upload_file_path); $adb->pquery($sql2, $params2); $this->addAttachmentRecord('ID', $next_id); } } \Workflow\Attachment::register('redooreports', '\Workflow\Plugins\Mailattachments\RedooReports');