function save_phpcode($filename) { global $wb; // Set project variables foreach ($wb->project_array as $var) { ${$var} = $wb->form[$wb->currentform]->{$var}; if (!$var) { $var = constant("DEFAULT_" . strtoupper($var)); } } // Set up some useful variables $form = $wb->form[$wb->currentform]; $valueid = $wb->form[$wb->currentform]->startval; $ctrlid = $wb->form[$wb->currentform]->startctrlval; $ncontrols = $form->numcontrols; $tabnumber = 0; // Empty table of IDs make_unique_id(null); // Start PHP code if ($filename) { $phpcode = "<?php\r\n\r\n" . PHP_HEADER; } else { $phpcode = ""; } //--------------------- Code to generate identifiers for all controls $phpcode .= "// Control identifiers\r\n\r\n"; $invalid = array(); $defined = array(); // Loop for all controls for ($nctrl = 0; $nctrl < $ncontrols; $nctrl++) { $ct = $wb->form[$wb->currentform]->ct[$nctrl]; $define_this = false; if (!$ct->id) { // Null ID } else { if (preg_match("/^-?[0-9]+\$/", $ct->id)) { // Numeric ID } else { if (preg_match('/^[a-z][a-z0-9_]*$/i', $ct->id)) { if (in_array($ct->id, $wb->presetids)) { continue; } // Valid string identifier if (!in_array($ct->id, $defined)) { $defined[] = $ct->id; if (preg_match("/^" . $wb->form[$wb->currentform]->prefix . "[a-z]+([0-9]+)\$/i", $ct->id, $matches)) { $valueid = $matches[1]; } else { $valueid++; } $define_this = true; } } else { // Invalid ID: fix it $inv = $ct->id; $wb->form[$wb->currentform]->ct[$nctrl]->id = make_valid_id($ct->id, 'MAKEID_'); if (!in_array($ct->id, $defined)) { $defined[] = $ct->id; $ct = $wb->form[$wb->currentform]->ct[$nctrl]; $valueid++; $valueid = make_unique_id($valueid); $invalid[] = "{$inv} (changed to {$ct->id})"; $define_this = true; } } } } // Create a define() call if ($define_this) { $valueid = make_unique_id($valueid); $phpcode .= "if(!defined('{$ct->id}')) define('{$ct->id}', {$valueid});\r\n"; } } // Create code to generate the form (optional) if (!$wb->form[$wb->currentform]->istabpage) { $phpcode .= "\r\n// Create window\r\n\r\n"; $phpcode .= "{$formvar} = wb_create_window({$parent}, " . ($form->cclass == "TabPage" ? "AppWindow" : $form->cclass) . ', ' . ($wb->form[$wb->currentform]->localize && $filename ? make_valid_id(str_replace('.prj', '', basename($filename))) : "'{$form->caption}'") . ", WBC_CENTER, WBC_CENTER, {$form->width}, {$form->height}, " . sprintf("0x%08X", $form->style) . ", {$form->value});\r\n"; } else { $tabnumber = $wb->form[$wb->currentform]->tabnumber; } //--------------------- Code to generate the controls $phpcode .= "\r\n// Insert controls\r\n\r\n"; $last_tab = null; for ($i = 0; $i < $ncontrols; $i++) { $ct = $wb->form[$wb->currentform]->ct[$i]; $cid = !$ct->id ? '0' : $ct->id; $ctrlparent = $formvar; $ctrlcode = ''; $xoff = 0; $yoff = 0; //--------------------- Adds optional code for tabs if ($wb->form[$wb->currentform]->insertontabs) { // If a control is over a tab control, force its parent to be the tab static $xtaboffset = 0; static $ytaboffset = 0; if ($ct->cclass == 'TabControl') { $ctrlcode = '$tab = '; $ctrlparent = $formvar; $tabctrl = $ct; $xtaboffset = $tabctrl->left; $ytaboffset = $tabctrl->top + 22; // Height of a tab, should be read from API } else { $ctrlcode = ''; // Is the control inside the tab? if (isset($tabctrl) && ($ct->left >= $tabctrl->left && $ct->left <= $tabctrl->left + $tabctrl->width && $ct->top >= $tabctrl->top && $ct->top <= $tabctrl->top + $tabctrl->height)) { $ctrlparent = '$tab'; $xoff = $xtaboffset; $yoff = $ytaboffset; } else { $ctrlparent = $formvar; $xoff = 0; $yoff = 0; } } } //--------------------- Adds optional code (before control creation) if ($wb->form[$wb->currentform]->filllists) { switch ($ct->cclass) { case 'ComboBox': case 'ListBox': $ctrlcode .= '$ctrl = '; break; } } if ($wb->form[$wb->currentform]->fillheaders) { switch ($ct->cclass) { case 'ListView': case 'ImageButton': $ctrlcode .= '$ctrl = '; break; } } if ($wb->form[$wb->currentform]->imagenames) { switch ($ct->cclass) { case 'ImageButton': $ctrlcode .= '$ctrl = '; break; } } if ($wb->form[$wb->currentform]->applyvalues) { switch ($ct->cclass) { case 'ScrollBar': case 'Slider': case 'Gauge': $ctrlcode .= '$ctrl = '; break; } } //--------------------- Add variable name (if any) $var = $wb->form[$wb->currentform]->ctrlvar; if ($var) { if ($var[0] != '$') { $var = '$' . $var; } $ctrlcode .= sprintf($var, $ctrlid) . ' = '; $ctrlid++; } //--------------------- Add code to create control $phpcode .= $ctrlcode . "wb_create_control({$ctrlparent}, {$ct->cclass}, " . ($wb->form[$wb->currentform]->localize && $filename && $ct->caption ? make_valid_id($ct->caption) : "'{$ct->caption}'") . ", " . ($ct->left - $xoff) . ", " . ($ct->top - $yoff) . ", {$ct->width}, {$ct->height}, {$cid}, " . sprintf("0x%08X", $ct->style) . ", {$ct->value}, {$tabnumber});\r\n"; //--------------------- Adds optional code (after control creation) switch ($ct->cclass) { case 'ImageButton': if ($wb->form[$wb->currentform]->imagenames) { if (substr($ct->caption, -4) == '.bmp' && file_exists($ct->caption)) { $imgfile = "'{$ct->caption}'"; } else { $imgfile = "'resources\\symb_imagebutton.bmp'"; } $phpcode .= "\$img = wb_load_image({$imgfile});\n" . " wb_set_image(\$ctrl, \$img, NOCOLOR, 0, {$ct->value});\n" . " wb_destroy_image(\$img);"; } break; case 'ListView': if ($wb->form[$wb->currentform]->fillheaders) { $phpcode .= "wb_set_text(\$ctrl, '{$ct->caption}');\n"; } break; case 'ComboBox': case 'ListBox': if ($wb->form[$wb->currentform]->filllists) { $phpcode .= "wb_set_text(\$ctrl, explode(',', '{$ct->caption}'));\n"; } break; case 'ScrollBar': case 'Slider': case 'Gauge': if ($wb->form[$wb->currentform]->applyvalues) { $phpcode .= "wb_set_value(\$ctrl, {$ct->value});\n"; } break; } } // end control loop if (!$filename) { $phpcode = str_replace('$this->', '$', $phpcode); } // Prevents errors in PHP 5 -- Stefano $phpcode .= "\r\n// End controls\r\n"; // Let the user know that invalid IDs were corrected if (!empty($invalid)) { update_control_data(); // Update ID field on main screen if necessary wb_message_box($wb->mainwin, "The following invalid IDs were automatically corrected:\n\n" . implode("\n", $invalid), APPNAME, WBC_INFO); } //--------------------- Add ending PHP code and save file if ($filename) { $phpcode .= "\r\n?>"; $filename = str_replace(".prj", ".form.php", $filename); file_put_contents($filename, $phpcode); // Generate localization file if ($wb->form[$wb->currentform]->localize) { $phpcode = "<?php\r\n\r\n" . PHP_HEADER; $phpcode .= "// Identifiers for localization\r\n\r\n"; // Generate line for dialog box title if (!$wb->form[$wb->currentform]->istabpage) { $id = make_valid_id(str_replace('.form.php', '', basename($filename))); echo "******" . "{$id}\n"; $phpcode .= "define('{$id}'," . str_repeat(' ', 26 - strlen($id)) . "'{$form->caption}');\n\n"; } // Create a define() call for each localization identifier for ($i = 0; $i < $ncontrols; $i++) { $ct = $wb->form[$wb->currentform]->ct[$i]; $cid = !$ct->id ? '0' : $ct->id; if ($ct->caption) { $id = make_valid_id($ct->caption); if (strlen($id) < 26) { $phpcode .= "define('{$id}'," . str_repeat(' ', 26 - strlen($id)) . "'{$ct->caption}');\n"; } else { $phpcode .= "define('{$id}', '{$ct->caption}');\n"; } } } $phpcode .= "\r\n?>"; $filename = str_replace(".form", ".lang", $filename); file_put_contents($filename, $phpcode); } } else { return $phpcode; } }
function test_make_unique_id() { $seen = array(); for ($i = 100000; $i < 100010; $i++) { $s = make_unique_id($i); $this->assertTrue(strlen($s) == 10); $this->assertTrue(!isset($seen[$s])); $seen[$s] = 1; } }