public function handleTask(&$context)
 {
     /* Insert here source code to execute the task */
     $fileid = $this->get('fileid', $context);
     $filestore = $context->getTempFiles($fileid);
     \Workflow\FileAction::doActions($this->get('resultaction'), $filestore['path'], $filestore['name'], $context, array(), $this->getWorkflow());
     return "yes";
 }
 public function handleTask(&$context)
 {
     $adb = \PearDatabase::getInstance();
     /* Insert here source code to execute the task */
     $format = $this->get('fileformat');
     $fields = $this->get('fields');
     $tmpfile = tempnam(sys_get_temp_dir(), 'CSV');
     @unlink($tmpfile);
     $source = $this->get('source');
     if ($source == -1 || $source == 'envid') {
         $env = $context->getEnvironment($this->get('recordlistid'));
     } elseif ($source == 'customview') {
         $search_module = $this->get("search_module");
         $parts = explode("#~#", $search_module);
         $searchModuleName = $parts[0];
         $queryGenerator = new \QueryGenerator($searchModuleName, \Users::getActiveAdminUser());
         $queryGenerator->initForCustomViewById($this->get('customviewsource'));
         $query = $queryGenerator->getQuery();
         $parts = preg_split('/FROM/i', $query);
         $sqlQuery = 'SELECT vtiger_crmentity.crmid as id_col FROM ' . $parts[1];
         $result = $adb->query($sqlQuery, true);
         $this->addStat("num Rows: " . $adb->num_rows($result));
         while ($row = $adb->fetchByAssoc($result)) {
             $recordids[] = $row["id_col"];
         }
         $env = array('moduleName' => $searchModuleName, 'ids' => $recordids);
     }
     $moduleName = $env['moduleName'];
     $ids = $env['ids'];
     $filename = $this->get('filename', $context);
     switch ($format) {
         case 'csv':
             $file = fopen($tmpfile, 'w');
             $headline = $this->get('insertheadline');
             if ($headline == '1') {
                 $headline = array();
                 foreach ($fields as $field) {
                     $headline[] = $field['label'];
                 }
                 fputcsv($file, $headline, ';');
             }
             foreach ($ids as $id) {
                 $record = \Workflow\VTEntity::getForId($id, $moduleName);
                 $tmp = array();
                 foreach ($fields as $field) {
                     $tmp[] = \Workflow\VTTemplate::parse($field['field'], $record);
                 }
                 fputcsv($file, $tmp, ';');
             }
             fclose($file);
             break;
         case 'excel':
             require_once $this->getAdditionalPath('phpexcel') . 'PHPExcel.php';
             // Create new PHPExcel object
             $objPHPExcel = new PHPExcel();
             // Set document properties
             $objPHPExcel->getProperties()->setCreator("Workflow Designer")->setLastModifiedBy("Workflow Designer")->setTitle("Workflow Designer Export")->setSubject("Workflow Designer Export");
             $headline = $this->get('insertheadline');
             $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
             $currentROW = 1;
             if ($headline == '1') {
                 $headline = array();
                 foreach ($fields as $field) {
                     $headline[] = $field['label'];
                 }
                 $objPHPExcel->setActiveSheetIndex(0)->fromArray(array($headline), NULL, 'A1');
                 $currentROW++;
             }
             foreach ($ids as $id) {
                 $record = \Workflow\VTEntity::getForId($id, $moduleName);
                 $tmp = array();
                 foreach ($fields as $field) {
                     $tmp[] = \Workflow\VTTemplate::parse($field['value'], $record);
                 }
                 $objPHPExcel->getActiveSheet()->fromArray($tmp, '', 'A' . $currentROW);
                 $currentROW++;
             }
             $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
             $objWriter->save($tmpfile);
             break;
     }
     \Workflow\FileAction::doActions($this->get('resultaction'), $tmpfile, $filename, $context, $context->getId(), $this->getWorkflow());
     @unlink($tmpfile);
     return "yes";
 }
Beispiel #3
0
 public function handleTask(&$context)
 {
     $adb = \PearDatabase::getInstance();
     if (!getTabid('PDFMaker') || !vtlib_isModuleActive('PDFMaker')) {
         throw new \Exception('PDFMaker Extension not found!');
     }
     /* Insert here source code to execute the task */
     $recordChooser = $this->get('recordChooser');
     if (empty($recordChooser) || $recordChooser == -1) {
         throw new \Exception('You need to configure the PDFMaker Integraion.');
     }
     $recordIds = array();
     if ($recordChooser === 'current') {
         $recordIds = array($context->getId());
         $moduleName = $this->getModuleName();
     } else {
         $currentTime = microtime(true);
         $benchmark = array();
         $parts = explode("#~#", $this->get("search_module"));
         $related_module = VtUtils::getModuleName($parts[1]);
         $moduleName = $related_module;
         $logger = new \Workflow\ConditionLogger();
         $objMySQL = new \Workflow\ConditionMysql($related_module, $context);
         $objMySQL->setLogger($logger);
         $main_module = \CRMEntity::getInstance($related_module);
         $sqlCondition = $objMySQL->parse($this->get("condition"));
         $newTime = microtime(true);
         $benchmark[] = round($newTime - $currentTime, 3);
         $currentTime = $newTime;
         $sqlTables = $objMySQL->generateTables();
         if (strlen($sqlCondition) > 3) {
             $sqlCondition .= " AND vtiger_crmentity.deleted = 0";
         } else {
             $sqlCondition .= " vtiger_crmentity.deleted = 0";
         }
         $logs = $logger->getLogs();
         $this->setStat($logs);
         $sqlCondition .= " GROUP BY vtiger_crmentity.crmid ";
         $idColumn = $main_module->table_name . "." . $main_module->table_index;
         $sqlQuery = "SELECT {$idColumn} as `idCol` " . $sqlTables . " WHERE " . (strlen($sqlCondition) > 3 ? $sqlCondition : "");
         $sortField = $this->get("sort_field");
         if (!empty($sortField) && $sortField != -1) {
             $sortField = VtUtils::getColumnName($sortField);
             $sortDirection = $this->get("sortDirection");
             $sqlQuery .= " ORDER BY " . $sortField . " " . $sortDirection;
         }
         $this->addStat("MySQL Query: " . $sqlQuery);
         $result = $adb->query($sqlQuery, true);
         $newTime = microtime(true);
         $benchmark[] = round($newTime - $currentTime, 3);
         $this->addStat("num Rows: " . $adb->num_rows($result));
         # If no records are found, fo other way
         if ($adb->num_rows($result) == 0) {
             return "yes";
         }
         $this->addStat("Benchmark: " . implode("/", $benchmark));
         while ($row = $adb->fetchByAssoc($result)) {
             $recordIds[] = $row['idcol'];
         }
     }
     $context->save();
     $useUser = Users::getActiveAdminUser();
     $oldUser = vglobal('current_user');
     vglobal('current_user', $useUser);
     $PDFMaker = new PDFMaker_PDFMaker_Model();
     $dl = Vtiger_Language_Handler::getLanguage();
     $mpdf = "";
     $copies = $this->get('copies');
     if ($copies == -1 || empty($copies)) {
         $copies = 1;
     }
     $templateids = array();
     for ($i = 0; $i < $copies; $i++) {
         $templateids = array_merge($templateids, $this->get("template"));
     }
     $filename = $PDFMaker->GetPreparedMPDF($mpdf, $recordIds, $templateids, $moduleName, $dl, '');
     if (strpos($filename, '.pdf') === false) {
         $filename .= '.pdf';
     }
     //        $filename = $PDFMaker->generate_cool_uri($filename);
     $tmpfile = tempnam(sys_get_temp_dir(), 'WfTmp');
     @unlink($tmpfile);
     $mpdf->Output($tmpfile);
     $overwriteFilename = $this->get("filename", $context);
     if ($overwriteFilename != -1 && !empty($overwriteFilename)) {
         $filename = $overwriteFilename;
     }
     \Workflow\FileAction::doActions($this->get('resultaction'), $tmpfile, $filename, $context, $recordIds, $this->getWorkflow());
     vglobal('current_user', $oldUser);
     return "yes";
 }