Exemplo n.º 1
0
 function create_script_from_template($id)
 {
     global $_CAMILA;
     $this->menuitems_script = '';
     $this->formulas = 'Array(';
     $this->queries = 'Array(';
     $resultTable = $this->db->Execute('select * from ' . CAMILA_TABLE_WORKT . ' where id=' . $this->db->qstr($id));
     if ($resultTable === false) {
         camila_error_page(camila_get_translation('camila.sqlerror') . ' ' . $this->db->ErrorMsg());
     }
     $result = $this->db->Execute('select * from ' . CAMILA_TABLE_WORKC . ' where (wt_id=' . $this->db->qstr($id) . ' and is_deleted<>' . $this->db->qstr('y') . ') order by sequence');
     if ($result === false) {
         camila_error_page(camila_get_translation('camila.sqlerror') . ' ' . $this->db->ErrorMsg());
     }
     require_once CAMILA_LIB_DIR . 'minitemplator/MiniTemplator.class.php';
     $t = new MiniTemplator();
     $t->readTemplateFromFile(CAMILA_DIR . 'templates/worktable.inc.php');
     $report_fields = 'id,';
     if (CAMILA_WORKTABLE_SPECIAL_ICON_ENABLED) {
         $report_fields .= 'cf_bool_is_special,';
     }
     if (CAMILA_WORKTABLE_SELECTED_ICON_ENABLED) {
         $report_fields .= 'cf_bool_is_selected,';
     }
     $default_fields = 'cf_bool_is_special,cf_bool_is_selected,';
     $mapping = $this->get_translation('camila.worktable.sysfields.mapping') . '#';
     $mappingAbbrev = $this->get_translation('camila.worktable.sysfields.mapping') . '#';
     $order_field = $resultTable->fields['order_field'];
     $order_dir = $resultTable->fields['order_dir'];
     $canupdate = $resultTable->fields['canupdate'] == 'y' ? 'true' : 'false';
     $caninsert = $resultTable->fields['caninsert'] == 'y' ? 'true' : 'false';
     $candelete = $resultTable->fields['candelete'] == 'y' ? 'true' : 'false';
     $rcount = 0;
     $vcount = 0;
     $fcount = 0;
     $qcount = 0;
     while (!$result->EOF) {
         if ($vcount > 0) {
             $default_fields .= ',';
         }
         if ($rcount > 0) {
             $report_fields .= ',';
             $mapping .= '#';
             $mappingAbbrev .= '#';
         } else {
             if ($order_field == '') {
                 $order_field = $result->fields['col_name'];
             }
         }
         if ($result->fields['type'] != 'formula' && $result->fields['type'] != 'query') {
             $report_fields .= $result->fields['col_name'];
         } else {
             if ($result->fields['type'] == 'query') {
                 $report_fields .= $result->fields['col_name'] . ' as cf_query_' . $result->fields['col_name'];
                 if ($qcount > 0) {
                     $this->queries .= ',';
                 }
                 $this->queries .= '\'' . 'cf_query_' . $result->fields['col_name'] . '\'=>\'' . $result->fields['field_options'] . '\'';
                 $qcount++;
             } else {
                 $report_fields .= $result->fields['col_name'] . ' as cf_formula_' . $result->fields['col_name'];
                 if ($fcount > 0) {
                     $this->formulas .= ',';
                 }
                 $this->formulas .= '\'' . 'cf_formula_' . $result->fields['col_name'] . '\'=>\'' . $result->fields['field_options'] . '\'';
                 $fcount++;
             }
         }
         if ($result->fields['visible'] == 'y') {
             if ($result->fields['type'] != 'formula' && $result->fields['type'] != 'query') {
                 $default_fields .= $result->fields['col_name'];
             } else {
                 if ($result->fields['type'] == 'query') {
                     $default_fields .= $result->fields['col_name'] . ' as cf_query_' . $result->fields['col_name'];
                 } else {
                     $default_fields .= $result->fields['col_name'] . ' as cf_formula_' . $result->fields['col_name'];
                 }
             }
             $vcount++;
         }
         if ($result->fields['type'] != 'formula' && $result->fields['type'] != 'query') {
             $mappingAbbrev .= $result->fields['col_name'] . '=' . $this->escape($result->fields['name_abbrev']);
             $mapping .= $result->fields['col_name'] . '=' . $this->escape($result->fields['name']);
         } else {
             if ($result->fields['type'] == 'query') {
                 $mappingAbbrev .= 'cf_query_' . $result->fields['col_name'] . '=' . $this->escape($result->fields['name_abbrev']);
                 $mappingAbbrev .= '#' . $result->fields['col_name'] . ' as cf_query_' . $result->fields['col_name'] . '=' . $this->escape($result->fields['name_abbrev']);
                 $mapping .= 'cf_formula_' . $result->fields['col_name'] . '=' . $this->escape($result->fields['name']);
                 $mapping .= '#' . $result->fields['col_name'] . 'as cf_query_' . $result->fields['col_name'] . '=' . $this->escape($result->fields['name']);
             } else {
                 $mappingAbbrev .= 'cf_formula_' . $result->fields['col_name'] . '=' . $this->escape($result->fields['name_abbrev']);
                 $mappingAbbrev .= '#' . $result->fields['col_name'] . ' as cf_formula_' . $result->fields['col_name'] . '=' . $this->escape($result->fields['name_abbrev']);
                 $mapping .= 'cf_formula_' . $result->fields['col_name'] . '=' . $this->escape($result->fields['name']);
                 $mapping .= '#' . $result->fields['col_name'] . 'as cf_formula_' . $result->fields['col_name'] . '=' . $this->escape($result->fields['name']);
             }
         }
         $rcount++;
         $t->setVariable('form_element', $this->get_form_element($result->fields));
         $t->addBlock('element');
         $result->MoveNext();
     }
     $report_fields .= ',created,created_by,created_by_surname,created_by_name,last_upd,last_upd_by,last_upd_by_surname,last_upd_by_name,mod_num';
     foreach ($this->requires as $value) {
         $t->setVariable('form_require', $value);
         $t->addBlock('require');
     }
     $result = $this->db->Execute('select distinct autosuggest_wt_name from ' . CAMILA_TABLE_WORKC . ' where (wt_id=' . $this->db->qstr($id) . ' and is_deleted<>' . $this->db->qstr('y') . ')');
     if ($result === false) {
         camila_error_page(camila_get_translation('camila.sqlerror') . ' ' . $this->db->ErrorMsg());
     }
     while (!$result->EOF) {
         $tablename = $result->fields['autosuggest_wt_name'];
         if ($tablename != '') {
             $result2 = $this->db->Execute('select id, tablename from ' . CAMILA_TABLE_WORKT . ' where short_title=' . $this->db->qstr($tablename));
             if ($result2 === false) {
                 camila_error_page(camila_get_translation('camila.sqlerror') . ' ' . $this->db->ErrorMsg());
             }
             $extid = $result2->fields['id'];
             $table = $result2->fields['tablename'];
             $exttable = false;
             if (substr($tablename, 0, 1) == '[') {
                 $exttable = true;
                 $table = substr($tablename, 1, -1);
             }
             $result2 = $this->db->Execute('select sequence,col_name, autosuggest_wt_colname from ' . CAMILA_TABLE_WORKC . ' where (autosuggest_wt_name=' . $this->db->qstr($tablename) . ' and wt_id=' . $this->db->qstr($id) . ' and is_deleted<>' . $this->db->qstr('y') . ')');
             if ($result2 === false) {
                 camila_error_page(camila_get_translation('camila.sqlerror') . ' ' . $this->db->ErrorMsg());
             }
             while (!$result2->EOF) {
                 $field = $result2->fields['col_name'];
                 $sequence = $result2->fields['sequence'];
                 $suggcolname = $result2->fields['autosuggest_wt_colname'];
                 //search for col_names
                 $result3 = $this->db->Execute('select col_name from ' . CAMILA_TABLE_WORKC . ' where (name=' . $this->db->qstr($suggcolname) . ' and wt_id=' . $this->db->qstr($extid) . ' and is_deleted<>' . $this->db->qstr('y') . ')');
                 if ($result3 === false) {
                     camila_error_page(camila_get_translation('camila.sqlerror') . ' ' . $this->db->ErrorMsg());
                 }
                 $col_name = $result3->fields['col_name'];
                 if ($exttable) {
                     $col_name = $suggcolname;
                 }
                 $suggfield = $col_name;
                 $infofields = '';
                 $destfields = '';
                 $query1 = 'select col_name, autosuggest_wt_colname from ' . CAMILA_TABLE_WORKC . ' where (autosuggest_wt_name=' . $this->db->qstr($tablename) . ' and autosuggest_wt_colname <>' . $this->db->qstr($suggcolname) . ' and wt_id=' . $this->db->qstr($id) . ' and is_deleted<>' . $this->db->qstr('y') . ' and sequence >= ' . $sequence . ' ) order by sequence';
                 $query2 = 'select col_name, autosuggest_wt_colname from ' . CAMILA_TABLE_WORKC . ' where (autosuggest_wt_name=' . $this->db->qstr($tablename) . ' and autosuggest_wt_colname <>' . $this->db->qstr($suggcolname) . ' and wt_id=' . $this->db->qstr($id) . ' and is_deleted<>' . $this->db->qstr('y') . ' and sequence < ' . $sequence . ' ) order by sequence';
                 $result4 = $this->db->Execute($query1);
                 if ($result4 === false) {
                     camila_error_page(camila_get_translation('camila.sqlerror') . ' ' . $this->db->ErrorMsg());
                 }
                 while (!$result4->EOF) {
                     $suggcolname = $result4->fields['autosuggest_wt_colname'];
                     $result3 = $this->db->Execute('select col_name from ' . CAMILA_TABLE_WORKC . ' where (name=' . $this->db->qstr($suggcolname) . ' and wt_id=' . $this->db->qstr($extid) . ' and (is_deleted<>' . $this->db->qstr('y') . ' or is_deleted is null))');
                     if ($result3 === false) {
                         camila_error_page(camila_get_translation('camila.sqlerror') . ' ' . $this->db->ErrorMsg());
                     }
                     $col_name = $result3->fields['col_name'];
                     $dest_col_name = $result4->fields['col_name'];
                     if ($dest_col_name != '') {
                         $destfields .= ',' . $dest_col_name;
                     }
                     if ($col_name != '') {
                         $infofields .= ',' . $col_name;
                     }
                     if ($exttable) {
                         $infofields .= ',' . $suggcolname;
                     }
                     $result4->MoveNext();
                 }
                 $result4 = $this->db->Execute($query2);
                 if ($result4 === false) {
                     camila_error_page(camila_get_translation('camila.sqlerror') . ' ' . $this->db->ErrorMsg());
                 }
                 while (!$result4->EOF) {
                     $suggcolname = $result4->fields['autosuggest_wt_colname'];
                     $result3 = $this->db->Execute('select col_name from ' . CAMILA_TABLE_WORKC . ' where (name=' . $this->db->qstr($suggcolname) . ' and wt_id=' . $this->db->qstr($extid) . ' and (is_deleted<>' . $this->db->qstr('y') . ' or is_deleted is null))');
                     if ($result3 === false) {
                         camila_error_page(camila_get_translation('camila.sqlerror') . ' ' . $this->db->ErrorMsg());
                     }
                     $col_name = $result3->fields['col_name'];
                     $dest_col_name = $result4->fields['col_name'];
                     if ($dest_col_name != '') {
                         $destfields .= ',' . $dest_col_name;
                     }
                     if ($col_name != '') {
                         $infofields .= ',' . $col_name;
                     }
                     if ($exttable) {
                         $infofields .= ',' . $suggcolname;
                     }
                     $result4->MoveNext();
                 }
                 $script .= "if (is_object(\$form->fields['{$field}']))\n";
                 $script .= "{\n";
                 $script .= "\$form->fields['{$field}']->autosuggest_table = '" . $table . "';\n";
                 $script .= "\$form->fields['{$field}']->autosuggest_field = '" . $suggfield . "';\n";
                 $script .= "\$form->fields['{$field}']->autosuggest_idfield = '" . id . "';\n";
                 $script .= "\$form->fields['{$field}']->autosuggest_infofields = '" . substr($infofields, 1) . "';\n";
                 $script .= "\$form->fields['{$field}']->autosuggest_pickfields = '" . substr($infofields, 1) . "';\n";
                 $script .= "\$form->fields['{$field}']->autosuggest_destfields = '" . substr($destfields, 1) . "';\n";
                 $script .= "}\n";
                 $result2->MoveNext();
             }
         }
         $result->MoveNext();
     }
     $this->formulas .= ');';
     $this->queries .= ');';
     $t->setVariable('menuitems_script', $this->menuitems_script);
     $t->setVariable('formulas', $this->formulas);
     $t->setVariable('queries', $this->queries);
     $t->setVariable('autosuggest_script', $script);
     $t->setVariable('table', CAMILA_TABLE_WORKP . $id);
     $t->setVariable('report_fields', $report_fields);
     $t->setVariable('default_fields', $default_fields);
     $t->setVariable('mapping', $mapping);
     $t->setVariable('mapping_abbrev', $mappingAbbrev);
     $t->setVariable('order_field', $order_field);
     $t->setVariable('order_dir', $order_dir);
     $t->setVariable('canupdate', $canupdate);
     $t->setVariable('caninsert', $caninsert);
     $t->setVariable('candelete', $candelete);
     $t->generateOutputToString($output);
     //$t->generateOutputToFile(CAMILA_WORKTABLES_DIR . '/' . CAMILA_TABLE_WORKP . $id . '.inc.php');
     $trans = new Latin1UTF8();
     $fh = fopen(CAMILA_WORKTABLES_DIR . '/' . CAMILA_TABLE_WORKP . $id . '.inc.php', 'wb');
     fwrite($fh, $trans->mixed_to_utf8($output));
     fclose($fh);
     $record = array();
     $record['visible'] = 'yes';
     $record['active'] = 'yes';
     $success3 = true;
     $updateSQL = $this->db->AutoExecute(CAMILA_TABLE_PAGES, $record, 'UPDATE', 'url=' . $this->db->qstr($resultTable->fields['scriptname']));
     if (!$updateSQL) {
         camila_information_text(camila_get_translation('camila.worktable.db.error'));
         $success3 = false;
     }
     return $success3;
 }
Exemplo n.º 2
0
/**
 * Generates a PDF file from the string of html provided. If a filename is supplied, it will be written to the file; otherwise it will be returned from the function
 * @param unknown_type $html
 * @param unknown_type $output_filename
 */
function generatePDF($html, $output_filename = null, $charset = DEFAULT_CHARSET)
{
    global $APPLICATION_PATH;
    $cv = new Latin1UTF8();
    $html = $cv->mixed_to_latin1($html);
    //and just in case there's still anything left...
    $html = preg_replace('/[^(\\x20-\\x7F)]*/', '', $html);
    @set_time_limit(0);
    if (is_array($APPLICATION_PATH) && isset($APPLICATION_PATH["htmldoc"]) && @is_executable($APPLICATION_PATH["htmldoc"])) {
        //This used to have every option separated by a backslash and newline. In testing it was discovered that there was a magical limit of 4 backslashes -- beyond which it would barf.
        $exec_command = $APPLICATION_PATH["htmldoc"] . " --format pdf14 --charset " . $charset . " --size Letter --pagemode document --no-duplex --encryption --owner-password " . PDF_PASSWORD . " --compression=6 --permissions print,modify --header ... --footer ... --headfootsize 0 --browserwidth 800 --top 1cm --bottom 1cm --left 2cm --right 2cm --embedfonts --bodyfont Times --headfootsize 8 --headfootfont Times --headingfont Times --firstpage p1 --quiet --book --color --no-toc --no-title --no-links --textfont Times - ";
        if ($output_filename) {
            @exec($exec_command);
            @exec("chmod 644 " . $output_filename);
        } else {
            /**
             * This section needs a little explanation.
             *
             * exec and shell_exec were not used because they cannot receive standard input.
             * proc_open allows the specification of pipes (or files) for standard input/output/error
             * hence the descriptorsepc array specifiying pipes for all three
             * and writing to pipe[0] for standard input
             * and reading the stream from pipe[1] for standard output.
             */
            $descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
            $proc = proc_open($exec_command, $descriptorspec, $pipes);
            fwrite($pipes[0], $html);
            fclose($pipes[0]);
            $pdf_string = stream_get_contents($pipes[1]);
            fclose($pipes[1]);
            fclose($pipes[2]);
            //just close we're not interested in the error info
            $return_val = proc_close($proc);
            return $pdf_string;
        }
    }
}