action) { return $this->doTree(); } $header_template = 'header_datatables.twig'; $footer_template = 'footer.twig'; ob_start(); switch ($this->action) { case 'save_create': if (isset($_POST['cancel'])) { $this->doDefault(); } else { $this->doSaveCreate(); } break; case 'create': $header_template = 'header_select2.twig'; $this->doCreate(); break; case 'drop': if (isset($_POST['drop'])) { $this->doDrop(false); } else { $this->doDefault(); } break; case 'confirm_drop': $this->doDrop(true); break; case 'save_edit': if (isset($_POST['cancel'])) { $this->doDefault(); } else { $this->doSaveEdit(); } break; case 'edit': $header_template = 'header_sqledit.twig'; $footer_template = 'footer_sqledit.twig'; $this->doEdit(); break; case 'properties': $header_template = 'header_highlight.twig'; $this->doProperties(); break; case 'show': if (isset($_GET['function'], $_GET['function_oid'])) { $header_template = 'header_highlight.twig'; $this->showDefinition(); } else { $this->doDefault(); } break; default: $this->doDefault(); break; } $output = ob_get_clean(); $this->printHeader($this->headerTitle(), null, true, $header_template); $this->printBody(); echo $output; $this->printFooter(true, $footer_template); } /** * Show default list of functions in the database. * * @param mixed $msg */ public function doDefault($msg = '') { $data = $this->misc->getDatabaseAccessor(); $this->printTrail('schema'); $this->printTabs('schema', 'functions'); $this->printMsg($msg); $funcs = $data->getFunctions(); $columns = [ 'function' => [ 'title' => $this->lang['strfunction'], 'field' => Decorator::field('proproto'), 'url' => \SUBFOLDER."/redirect/function?action=properties&{$this->misc->href}&", 'vars' => ['function' => 'proproto', 'function_oid' => 'prooid'], ], 'returns' => [ 'title' => $this->lang['strreturns'], 'field' => Decorator::field('proreturns'), ], 'owner' => [ 'title' => $this->lang['strowner'], 'field' => Decorator::field('proowner'), ], 'proglanguage' => [ 'title' => $this->lang['strproglanguage'], 'field' => Decorator::field('prolanguage'), ], 'actions' => [ 'title' => $this->lang['stractions'], ], 'comment' => [ 'title' => $this->lang['strcomment'], 'field' => Decorator::field('procomment'), ], ]; $actions = [ 'multiactions' => [ 'keycols' => ['function' => 'proproto', 'function_oid' => 'prooid'], 'url' => 'functions', ], 'alter' => [ 'content' => $this->lang['stralter'], 'attr' => [ 'href' => [ 'url' => 'functions', 'urlvars' => [ 'action' => 'edit', 'function' => Decorator::field('proproto'), 'function_oid' => Decorator::field('prooid'), ], ], ], ], 'drop' => [ 'multiaction' => 'confirm_drop', 'content' => $this->lang['strdrop'], 'attr' => [ 'href' => [ 'url' => 'functions', 'urlvars' => [ 'action' => 'confirm_drop', 'function' => Decorator::field('proproto'), 'function_oid' => Decorator::field('prooid'), ], ], ], ], 'privileges' => [ 'content' => $this->lang['strprivileges'], 'attr' => [ 'href' => [ 'url' => 'privileges', 'urlvars' => [ 'subject' => 'function', 'function' => Decorator::field('proproto'), 'function_oid' => Decorator::field('prooid'), ], ], ], ], ]; echo $this->printTable($funcs, $columns, $actions, $this->table_place, $this->lang['strnofunctions']); $this->_printNavLinks('functions-functions'); } private function _printNavLinks($place, $func_full = '') { if ($place === 'functions-properties') { $navlinks = [ 'showall' => [ 'attr' => [ 'href' => [ 'url' => 'functions', 'urlvars' => [ 'server' => $_REQUEST['server'], 'database' => $_REQUEST['database'], 'schema' => $_REQUEST['schema'], ], ], ], 'content' => $this->lang['strshowallfunctions'], ], 'alter' => [ 'attr' => [ 'href' => [ 'url' => 'functions', 'urlvars' => [ 'action' => 'edit', 'server' => $_REQUEST['server'], 'database' => $_REQUEST['database'], 'schema' => $_REQUEST['schema'], 'function' => $_REQUEST['function'], 'function_oid' => $_REQUEST['function_oid'], ], ], ], 'content' => $this->lang['stralter'], ], 'drop' => [ 'attr' => [ 'href' => [ 'url' => 'functions', 'urlvars' => [ 'action' => 'confirm_drop', 'server' => $_REQUEST['server'], 'database' => $_REQUEST['database'], 'schema' => $_REQUEST['schema'], 'function' => $func_full, 'function_oid' => $_REQUEST['function_oid'], ], ], ], 'content' => $this->lang['strdrop'], ], ]; } elseif ($place === 'functions-functions') { $navlinks = [ 'createpl' => [ 'attr' => [ 'href' => [ 'url' => 'functions', 'urlvars' => [ 'action' => 'create', 'server' => $_REQUEST['server'], 'database' => $_REQUEST['database'], 'schema' => $_REQUEST['schema'], ], ], ], 'content' => $this->lang['strcreateplfunction'], ], 'createinternal' => [ 'attr' => [ 'href' => [ 'url' => 'functions', 'urlvars' => [ 'action' => 'create', 'language' => 'internal', 'server' => $_REQUEST['server'], 'database' => $_REQUEST['database'], 'schema' => $_REQUEST['schema'], ], ], ], 'content' => $this->lang['strcreateinternalfunction'], ], 'createc' => [ 'attr' => [ 'href' => [ 'url' => 'functions', 'urlvars' => [ 'action' => 'create', 'language' => 'C', 'server' => $_REQUEST['server'], 'database' => $_REQUEST['database'], 'schema' => $_REQUEST['schema'], ], ], ], 'content' => $this->lang['strcreatecfunction'], ], ]; } else { return; } $this->printNavLinks($navlinks, $place, get_defined_vars()); } /** * Generate XML for the browser tree. */ public function doTree() { $data = $this->misc->getDatabaseAccessor(); $funcs = $data->getFunctions(); $proto = Decorator::concat(Decorator::field('proname'), ' (', Decorator::field('proarguments'), ')'); $reqvars = $this->misc->getRequestVars('function'); $attrs = [ 'text' => $proto, 'icon' => 'Function', 'toolTip' => Decorator::field('procomment'), 'action' => Decorator::redirecturl( 'redirect', $reqvars, [ 'action' => 'properties', 'function' => $proto, 'function_oid' => Decorator::field('prooid'), ] ), ]; return $this->printTree($funcs, $attrs, 'functions'); } /** * Function to save after editing a function. */ public function doSaveEdit() { $data = $this->misc->getDatabaseAccessor(); $fnlang = strtolower($_POST['original_lang']); if ('c' == $fnlang) { $def = [$_POST['formObjectFile'], $_POST['formLinkSymbol']]; } elseif ('internal' == $fnlang) { $def = $_POST['formLinkSymbol']; } else { $def = $_POST['formDefinition']; } if (!$data->hasFunctionAlterSchema()) { $_POST['formFuncSchema'] = ''; } $status = $data->setFunction( $_POST['original_function'], $_POST['formFunction'], $_POST['original_arguments'], $_POST['original_returns'], $def, $_POST['original_lang'], $_POST['formProperties'], isset($_POST['original_setof']), $_POST['original_owner'], $_POST['formFuncOwn'], $_POST['original_schema'], $_POST['formFuncSchema'], isset($_POST['formCost']) ? $_POST['formCost'] : null, isset($_POST['formRows']) ? $_POST['formRows'] : 0, $_POST['formComment'] ); if (0 == $status) { // If function has had schema altered, need to change to the new schema // and reload the browser frame. if (!empty($_POST['formFuncSchema']) && ($_POST['formFuncSchema'] != $_POST['original_schema'])) { // Jump them to the new function schema $this->misc->setCurrentSchema($_POST['formFuncSchema']); // Force a browser reload $this->misc->setReloadBrowser(true); } $this->doProperties($this->lang['strfunctionupdated']); } else { $this->doEdit($this->lang['strfunctionupdatedbad']); } } private function _getNamedParamsArgs($data, $fndata) { if (isset($fndata->fields['proallarguments'])) { $args_arr = $data->phpArray($fndata->fields['proallarguments']); } else { $args_arr = explode(', ', $fndata->fields['proarguments']); } $names_arr = $data->phpArray($fndata->fields['proargnames']); $modes_arr = $data->phpArray($fndata->fields['proargmodes']); $args = ''; $args_arr_size = sizeof($args_arr); for ($i = 0; $i < $args_arr_size; ++$i) { if (0 != $i) { $args .= ', '; } if (isset($modes_arr[$i])) { switch ($modes_arr[$i]) { case 'i': $args .= ' IN '; break; case 'o': $args .= ' OUT '; break; case 'b': $args .= ' INOUT '; break; case 'v': $args .= ' VARIADIC '; break; case 't': $args .= ' TABLE '; break; } } if (isset($names_arr[$i]) && '' != $names_arr[$i]) { $data->fieldClean($names_arr[$i]); $args .= '"'.$names_arr[$i].'" '; } $args .= $args_arr[$i]; } return $args; } /** * Function to allow editing of a Function. * * @param mixed $msg */ public function doEdit($msg = '') { $data = $this->misc->getDatabaseAccessor(); $this->printTrail('function'); $this->printTabs('function', 'definition'); $this->printTitle($this->lang['stralter'], 'pg.function.alter'); $this->printMsg($msg); $fndata = $data->getFunction($_REQUEST['function_oid']); if ($fndata->recordCount() <= 0) { echo "

{$this->lang['strnodata']}

".PHP_EOL; return; } $fndata->fields['proretset'] = $data->phpBool($fndata->fields['proretset']); // Initialise variables $_POST['formDefinition'] = $this->getPostParam('formDefinition', $fndata->fields['prosrc']); $_POST['formProperties'] = $this->getPostParam('formProperties', $data->getFunctionProperties($fndata->fields)); $_POST['formFunction'] = $this->getPostParam('formFunction', $fndata->fields['proname']); $_POST['formComment'] = $this->getPostParam('formComment', $fndata->fields['procomment']); $_POST['formObjectFile'] = $this->getPostParam('formObjectFile', $fndata->fields['probin']); $_POST['formLinkSymbol'] = $this->getPostParam('formLinkSymbol', $fndata->fields['prosrc']); $_POST['formFuncOwn'] = $this->getPostParam('formFuncOwn', $fndata->fields['proowner']); $_POST['formFuncSchema'] = $this->getPostParam('formFuncSchema', $fndata->fields['proschema']); if ($data->hasFunctionCosting()) { $_POST['formCost'] = $this->getPostParam('formCost', $fndata->fields['procost']); $_POST['formRows'] = $this->getPostParam('formRows', $fndata->fields['prorows']); } // Deal with named parameters if ($data->hasNamedParams()) { $args = $this->_getNamedParamsArgs($data, $fndata); } else { $args = $fndata->fields['proarguments']; } echo '
'.PHP_EOL; echo ''.PHP_EOL; echo ''.PHP_EOL; echo "".PHP_EOL; echo "".PHP_EOL; echo "".PHP_EOL; echo "".PHP_EOL; echo "".PHP_EOL; echo ''.PHP_EOL; echo ''.PHP_EOL; echo ''.PHP_EOL; echo ''.PHP_EOL; echo ''.PHP_EOL; echo ''.PHP_EOL; echo ''.PHP_EOL; echo ''.PHP_EOL; $fnlang = strtolower($fndata->fields['prolanguage']); if ('c' == $fnlang) { echo "".PHP_EOL; echo "".PHP_EOL; echo ''.PHP_EOL; echo ''.PHP_EOL; } elseif ('internal' == $fnlang) { echo "".PHP_EOL; echo ''.PHP_EOL; } else { echo "".PHP_EOL; echo ''.PHP_EOL; } // Display function comment echo "".PHP_EOL; echo ''.PHP_EOL; // Display function cost options if ($data->hasFunctionCosting()) { echo "".PHP_EOL; echo "'; echo "'; } // Display function properties if (is_array($data->funcprops) && sizeof($data->funcprops) > 0) { echo "".PHP_EOL; echo ''.PHP_EOL; } // function owner if ($data->hasFunctionAlterOwner()) { $users = $data->getUsers(); echo "'.PHP_EOL; } echo '
{$this->lang['strschema']}{$this->lang['strfunction']}{$this->lang['strarguments']}{$this->lang['strreturns']}{$this->lang['strproglanguage']}
'; echo ''.PHP_EOL; if ($data->hasFunctionAlterSchema()) { $schemas = $data->getSchemas(); echo ''.PHP_EOL; } else { echo $fndata->fields['proschema']; } echo ''; echo ''.PHP_EOL; echo "_maxNameLen}\" value=\"", htmlspecialchars($_POST['formFunction']), '" />'; echo '', $this->misc->printVal($args), PHP_EOL; echo ''.PHP_EOL; echo ''; if ($fndata->fields['proretset']) { echo 'setof '; } echo $this->misc->printVal($fndata->fields['proresult']), PHP_EOL; echo ''.PHP_EOL; if ($fndata->fields['proretset']) { echo ''.PHP_EOL; } echo '', $this->misc->printVal($fndata->fields['prolanguage']), PHP_EOL; echo ''.PHP_EOL; echo '
{$this->lang['strobjectfile']}{$this->lang['strlinksymbol']}
{$this->lang['strlinksymbol']}
{$this->lang['strdefinition']}
'; $textarea_id = ($fnlang === 'sql' || $fnlang === 'plpgsql') ? 'query' : 'formDefinition'; echo '
{$this->lang['strcomment']}
'; echo '
{$this->lang['strfunctioncosting']}
{$this->lang['strexecutioncost']}: {$this->lang['strresultrows']}: fields['proretset']) ? 'disabled' : '', '/>
{$this->lang['strproperties']}
'.PHP_EOL; $i = 0; foreach ($data->funcprops as $k => $v) { echo "
'.PHP_EOL; ++$i; } echo '
{$this->lang['strowner']}: '.PHP_EOL; echo ''.PHP_EOL; echo '
'.PHP_EOL; echo '

'.PHP_EOL; echo ''.PHP_EOL; echo ''.PHP_EOL; echo $this->misc->form; echo "lang['stralter']}\" />".PHP_EOL; echo "lang['strcancel']}\" />

".PHP_EOL; echo '
'.PHP_EOL; } /** * Show the creation sentence for this function. * * @param string $fname The function name * @param int $function_oid The function oid * * @return string the navlinks to print at the bottom */ public function showDefinition($fname, $function_oid) { $data = $this->misc->getDatabaseAccessor(); $this->printTrail('function'); $this->printTabs('function', 'export'); $this->printTitle($this->lang['strproperties'], 'pg.function'); $fname = str_replace(' ', '', $f); $funcdata = $data->getFunctionDef($function_oid); $func_full = ''; if ($funcdata->recordCount() <= 0) { echo "

{$this->lang['strnodata']}

".PHP_EOL; return $this->_printNavLinks('functions-properties', $func_full); } echo ''.PHP_EOL; $fnlang = strtolower($funcdata->fields['prolanguage']); echo ''.PHP_EOL; echo '
'; echo sprintf('
%s', PHP_EOL);

        echo sprintf('%s--%s', PHP_EOL, PHP_EOL);
        echo sprintf('-- Name: %s; Type: FUNCTION; Schema: %s; Owner: %s', $fname, $funcdata->fields['nspname'], $funcdata->fields['relowner']);
        echo sprintf('%s--%s%s', PHP_EOL, PHP_EOL, PHP_EOL);

        echo sprintf('%s;', $funcdata->fields['pg_get_functiondef']);

        echo sprintf('%s%sALTER FUNCTION %s OWNER TO %s;%s', PHP_EOL, PHP_EOL, $fname, $funcdata->fields['relowner'], PHP_EOL);

        // Show comment if any
        if (null !== $funcdata->fields['relcomment']) {
            echo sprintf('%s--%s', PHP_EOL, PHP_EOL);
            echo sprintf('-- Name: %s; Type: COMMENT; Schema: %s; Owner: %s', $fname, $funcdata->fields['nspname'], $funcdata->fields['relowner']);
            echo sprintf('%s--%s%s', PHP_EOL, PHP_EOL, PHP_EOL);
            echo sprintf("%sCOMMENT ON FUNCTION %s.%s IS '%s';%s", PHP_EOL, $funcdata->fields['nspname'], $fname, $funcdata->fields['relcomment'], PHP_EOL);
            //echo '

', $this->misc->printVal($funcdata->fields['relcomment']), '

' . PHP_EOL; } echo sprintf('%s
', PHP_EOL); echo '
'.PHP_EOL; return $this->_printNavLinks('functions-properties', $func_full); } /** * Show read only properties of a function. * * @param mixed $msg */ public function doProperties($msg = '') { $data = $this->misc->getDatabaseAccessor(); $this->printTrail('function'); $this->printTabs('function', 'definition'); $this->printTitle($this->lang['strproperties'], 'pg.function'); $this->printMsg($msg); $funcdata = $data->getFunction($_REQUEST['function_oid']); $func_full = ''; if ($funcdata->recordCount() <= 0) { echo "

{$this->lang['strnodata']}

".PHP_EOL; return $this->_printNavLinks('functions-properties', $func_full); } // Deal with named parameters $args = $this->_getPropertiesArgs($funcdata); // Show comment if any if (null !== $funcdata->fields['procomment']) { echo '

', $this->misc->printVal($funcdata->fields['procomment']), '

'.PHP_EOL; } $funcdata->fields['proretset'] = $data->phpBool($funcdata->fields['proretset']); $func_full = $funcdata->fields['proname'].'('.$funcdata->fields['proarguments'].')'; echo ''.PHP_EOL; echo sprintf('%s', $this->lang['strfunction'], PHP_EOL); echo sprintf('%s', $this->lang['strarguments'], PHP_EOL); echo sprintf('%s', $this->lang['strreturns'], PHP_EOL); echo sprintf('%s', $this->lang['strproglanguage'], PHP_EOL); echo ''.PHP_EOL; echo ''.PHP_EOL; echo ''.PHP_EOL; echo ''.PHP_EOL; $fnlang = strtolower($funcdata->fields['prolanguage']); if ('c' == $fnlang) { echo "".PHP_EOL; echo "".PHP_EOL; echo ''.PHP_EOL; echo ''.PHP_EOL; } elseif ('internal' == $fnlang) { echo "".PHP_EOL; echo ''.PHP_EOL; } else { echo ''.PHP_EOL; } // Display function cost options if ($data->hasFunctionCosting()) { echo "".PHP_EOL; echo "'; echo "'; } // Show flags if (is_array($data->funcprops) && sizeof($data->funcprops) > 0) { // Fetch an array of the function properties $funcprops = $data->getFunctionProperties($funcdata->fields); echo "".PHP_EOL; echo ''.PHP_EOL; } echo "'.PHP_EOL; echo '
%s%s%s%s
', $this->misc->printVal($funcdata->fields['proname']), '', $this->misc->printVal($args), ''; if ($funcdata->fields['proretset']) { echo 'setof '; } echo $this->misc->printVal($funcdata->fields['proresult']), '', $this->misc->printVal($funcdata->fields['prolanguage']), '
{$this->lang['strobjectfile']}{$this->lang['strlinksymbol']}
', $this->misc->printVal($funcdata->fields['probin']), '', $this->misc->printVal($funcdata->fields['prosrc']), '
{$this->lang['strlinksymbol']}
', $this->misc->printVal($funcdata->fields['prosrc']), '
'; echo sprintf('
%s
', $fnlang, $funcdata->fields['prosrc']); echo '
{$this->lang['strfunctioncosting']}
{$this->lang['strexecutioncost']}: ", $this->misc->printVal($funcdata->fields['procost']), ' {$this->lang['strresultrows']}: ", $this->misc->printVal($funcdata->fields['prorows']), '
{$this->lang['strproperties']}
'.PHP_EOL; foreach ($funcprops as $v) { echo $this->misc->printVal($v), '
'.PHP_EOL; } echo '
{$this->lang['strowner']}: ", htmlspecialchars($funcdata->fields['proowner']), PHP_EOL; echo '
'.PHP_EOL; return $this->_printNavLinks('functions-properties', $func_full); } /** * Show confirmation of drop and perform actual drop. * * @param mixed $confirm */ public function doDrop($confirm) { $data = $this->misc->getDatabaseAccessor(); if (empty($_REQUEST['function']) && empty($_REQUEST['ma'])) { return $this->doDefault($this->lang['strspecifyfunctiontodrop']); } if ($confirm) { $this->printTrail('function'); $this->printTabs('function', 'definition'); $this->printTitle($this->lang['strdrop'], 'pg.function.drop'); echo '
'.PHP_EOL; //If multi drop if (isset($_REQUEST['ma'])) { foreach ($_REQUEST['ma'] as $v) { $a = unserialize(htmlspecialchars_decode($v, ENT_QUOTES)); echo '

', sprintf($this->lang['strconfdropfunction'], $this->misc->printVal($a['function'])), '

'.PHP_EOL; echo ''.PHP_EOL; echo ''.PHP_EOL; } } else { echo '

', sprintf($this->lang['strconfdropfunction'], $this->misc->printVal($_REQUEST['function'])), '

'.PHP_EOL; echo ''.PHP_EOL; echo ''.PHP_EOL; } echo ''.PHP_EOL; echo $this->misc->form; echo "

".PHP_EOL; echo "lang['strdrop']}\" />".PHP_EOL; echo "lang['strcancel']}\" />".PHP_EOL; echo '
'.PHP_EOL; } else { if (is_array($_POST['function_oid'])) { $msg = ''; $status = $data->beginTransaction(); if (0 == $status) { foreach ($_POST['function_oid'] as $k => $s) { $status = $data->dropFunction($s, isset($_POST['cascade'])); if (0 == $status) { $msg .= sprintf('%s: %s
', htmlentities($_POST['function'][$k], ENT_QUOTES, 'UTF-8'), $this->lang['strfunctiondropped']); } else { $data->endTransaction(); $this->doDefault(sprintf('%s%s: %s
', $msg, htmlentities($_POST['function'][$k], ENT_QUOTES, 'UTF-8'), $this->lang['strfunctiondroppedbad'])); return; } } } if (0 == $data->endTransaction()) { // Everything went fine, back to the Default page.... $this->misc->setReloadBrowser(true); $this->doDefault($msg); } else { $this->doDefault($this->lang['strfunctiondroppedbad']); } } else { $status = $data->dropFunction($_POST['function_oid'], isset($_POST['cascade'])); if (0 == $status) { $this->misc->setReloadBrowser(true); $this->doDefault($this->lang['strfunctiondropped']); } else { $this->doDefault($this->lang['strfunctiondroppedbad']); } } } } /** * Displays a screen where they can enter a new function. * * @param string $msg message to display * @param mixed $szJS */ public function doCreate($msg = '', $szJS = '') { $data = $this->misc->getDatabaseAccessor(); $this->printTrail('schema'); $_POST['formFunction'] = $this->getPostParam('formFunction', ''); $_POST['formArguments'] = $this->getPostParam('formArguments', ''); $_POST['formReturns'] = $this->getPostParam('formReturns', ''); $this->coalesceArr($_POST, 'formLanguage', isset($_REQUEST['language']) ? $_REQUEST['language'] : 'sql'); $_POST['formDefinition'] = $this->getPostParam('formDefinition', ''); $_POST['formObjectFile'] = $this->getPostParam('formObjectFile', ''); $_POST['formLinkSymbol'] = $this->getPostParam('formLinkSymbol', ''); $_POST['formProperties'] = $this->getPostParam('formProperties', $data->defaultprops); $_POST['formSetOf'] = $this->getPostParam('formSetOf', ''); $_POST['formArray'] = $this->getPostParam('formArray', ''); $_POST['formCost'] = $this->getPostParam('formCost', ''); $_POST['formRows'] = $this->getPostParam('formRows', ''); $_POST['formComment'] = $this->getPostParam('formComment', ''); $types = $data->getTypes(true, true, true); $langs = $data->getLanguages(true); $fnlang = strtolower($_POST['formLanguage']); switch ($fnlang) { case 'c': $this->printTitle($this->lang['strcreatecfunction'], 'pg.function.create.c'); break; case 'internal': $this->printTitle($this->lang['strcreateinternalfunction'], 'pg.function.create.internal'); break; default: $this->printTitle($this->lang['strcreateplfunction'], 'pg.function.create.pl'); break; } $this->printMsg($msg); // Create string for return type list $szTypes = ''; while (!$types->EOF) { $szSelected = ''; if ($types->fields['typname'] == $_POST['formReturns']) { $szSelected = ' selected="selected"'; } // this variable is include in the JS code bellow, so we need to ENT_QUOTES $szTypes .= ''; $types->moveNext(); } $szFunctionName = "_maxNameLen}\" value=\"". htmlspecialchars($_POST['formFunction']).'" />'; $szArguments = ''; $szSetOfSelected = ''; $szNotSetOfSelected = ''; if ('' == $_POST['formSetOf']) { $szNotSetOfSelected = ' selected="selected"'; } elseif ('SETOF' == $_POST['formSetOf']) { $szSetOfSelected = ' selected="selected"'; } $szReturns = ''; $szReturns .= ''; $szReturns .= ''; // Create string array type selector $szArraySelected = ''; $szNotArraySelected = ''; if ('' == $_POST['formArray']) { $szNotArraySelected = ' selected="selected"'; } elseif ('[]' == $_POST['formArray']) { $szArraySelected = ' selected="selected"'; } $szReturns .= '\n"; // Create string for language $szLanguage = ''; if ('c' == $fnlang || 'internal' == $fnlang) { $szLanguage .= $_POST['formLanguage'].PHP_EOL; $szLanguage .= "".PHP_EOL; } else { $szLanguage .= ''.PHP_EOL; } $szLanguage .= ''; $szJSArguments = "{$this->lang['strarguments']}"; $arrayModes = ['IN', 'OUT', 'INOUT']; $szModes = ''; $szArgReturns = ''; $subfolder = \SUBFOLDER; if (!empty($this->conf['theme'])) { $szImgPath = \SUBFOLDER."/assets/images/themes/{$this->conf['theme']}"; } else { $szImgPath = \SUBFOLDER.'/assets/images/themes/default'; } if (empty($msg)) { // $this->prtrace($subfolder); $szJSTRArg = "".PHP_EOL; } else { $szJSTRArg = ''; } $szJSAddTR = "".PHP_EOL; $szJSAddTR .= '"; $szJSAddTR .= "
'; $szJSAddTR .= "\"Add{$this->lang['strargadd']}
\n".PHP_EOL; echo ' "; echo '
'.PHP_EOL; echo ''.PHP_EOL; echo "".PHP_EOL; echo "".PHP_EOL; echo "".PHP_EOL; echo ''.PHP_EOL; echo "{$szFunctionName}\n"; echo "{$szReturns}\n"; echo "{$szLanguage}\n"; echo ''.PHP_EOL; echo "{$szJSArguments}\n"; echo ''.PHP_EOL; echo "".PHP_EOL; echo "".PHP_EOL; echo "".PHP_EOL; echo ''.PHP_EOL; echo "{$szJSAddTR}\n"; if ('c' == $fnlang) { echo "".PHP_EOL; echo "".PHP_EOL; echo ''.PHP_EOL; echo ''.PHP_EOL; } elseif ('internal' == $fnlang) { echo "".PHP_EOL; echo ''.PHP_EOL; } else { echo "".PHP_EOL; echo ''.PHP_EOL; } // Display function comment echo "".PHP_EOL; echo ''.PHP_EOL; // Display function cost options if ($data->hasFunctionCosting()) { echo "".PHP_EOL; echo "'; echo "'; } // Display function properties if (is_array($data->funcprops) && sizeof($data->funcprops) > 0) { echo "".PHP_EOL; echo ''.PHP_EOL; } echo '
{$this->lang['strname']}{$this->lang['strreturns']}{$this->lang['strproglanguage']}
{$this->lang['strargmode']}{$this->lang['strname']}{$this->lang['strargtype']}
{$this->lang['strobjectfile']}{$this->lang['strlinksymbol']}
{$this->lang['strlinksymbol']}
{$this->lang['strdefinition']}
'; echo '
{$this->lang['strcomment']}
{$this->lang['strfunctioncosting']}
{$this->lang['strexecutioncost']}: {$this->lang['strresultrows']}:
{$this->lang['strproperties']}
'.PHP_EOL; $i = 0; foreach ($data->funcprops as $k => $v) { echo "
'.PHP_EOL; ++$i; } echo '
'.PHP_EOL; echo $szJSTRArg; echo '

'.PHP_EOL; echo $this->misc->form; echo "lang['strcreate']}\" />".PHP_EOL; echo "lang['strcancel']}\" />

".PHP_EOL; echo '
'.PHP_EOL; echo $szJS; } /** * Actually creates the new function in the database. */ public function doSaveCreate() { $data = $this->misc->getDatabaseAccessor(); $fnlang = strtolower($_POST['formLanguage']); if ('c' == $fnlang) { $def = [$_POST['formObjectFile'], $_POST['formLinkSymbol']]; } elseif ('internal' == $fnlang) { $def = $_POST['formLinkSymbol']; } else { $def = $_POST['formDefinition']; } $szJS = ''; echo ''; echo ''; if (!empty($_POST['formArgName'])) { $szJS = $this->_buildJSRows($this->_buildFunctionArguments($_POST)); } else { $subfolder = \SUBFOLDER; // $this->prtrace($subfolder); $szJS = ''; } $cost = (isset($_POST['formCost'])) ? $_POST['formCost'] : null; if ('' == $cost || !is_numeric($cost) || $cost != (int) $cost || $cost < 0) { $cost = null; } $rows = (isset($_POST['formRows'])) ? $_POST['formRows'] : null; if ('' == $rows || !is_numeric($rows) || $rows != (int) $rows) { $rows = null; } // Check that they've given a name and a definition if ('' == $_POST['formFunction']) { $this->doCreate($this->lang['strfunctionneedsname'], $szJS); } elseif ('internal' != $fnlang && !$def) { $this->doCreate($this->lang['strfunctionneedsdef'], $szJS); } else { // Append array symbol to type if chosen $status = $data->createFunction( $_POST['formFunction'], empty($_POST['nojs']) ? $this->_buildFunctionArguments($_POST) : $_POST['formArguments'], $_POST['formReturns'].$_POST['formArray'], $def, $_POST['formLanguage'], $_POST['formProperties'], 'SETOF' == $_POST['formSetOf'], $cost, $rows, $_POST['formComment'], false ); if (0 == $status) { $this->doDefault($this->lang['strfunctioncreated']); } else { $this->doCreate($this->lang['strfunctioncreatedbad'], $szJS); } } } /** * Build out the function arguments string. * * @param array $arrayVars * * @return string the imploded array vars */ private function _buildFunctionArguments($arrayVars) { if (isset($_POST['formArgName'])) { $arrayArgs = []; foreach ($arrayVars['formArgName'] as $pK => $pV) { $arrayArgs[] = $arrayVars['formArgModes'][$pK].' '.trim($pV).' '.trim($arrayVars['formArgType'][$pK]).$arrayVars['formArgArray'][$pK]; } return implode(',', $arrayArgs); } return ''; } /** * Build out JS to re-create table rows for arguments. * * @param string $szArgs args to parse */ private function _buildJSRows($szArgs) { $arrayModes = ['IN', 'OUT', 'INOUT']; $arrayArgs = explode(',', $szArgs); $arrayProperArgs = []; $nC = 0; $szReturn = ''; $szMode = []; foreach ($arrayArgs as $pV) { $arrayWords = explode(' ', $pV); if (true === in_array($arrayWords[0], $arrayModes, true)) { $szMode = $arrayWords[0]; array_shift($arrayWords); } $szArgName = array_shift($arrayWords); if (false === strpos($arrayWords[count($arrayWords) - 1], '[]')) { $szArgType = implode(' ', $arrayWords); $bArgIsArray = 'false'; } else { $szArgType = str_replace('[]', '', implode(' ', $arrayWords)); $bArgIsArray = 'true'; } $arrayProperArgs[] = [$szMode, $szArgName, $szArgType, $bArgIsArray]; $subfolder = \SUBFOLDER; // $this->prtrace($subfolder); $szReturn .= ';'; ++$nC; } return $szReturn; } private function _buildJSData() { $data = $this->misc->getDatabaseAccessor(); $arrayModes = ['IN', 'OUT', 'INOUT']; $arrayTypes = $data->getTypes(true, true, true); $arrayPTypes = []; $arrayPModes = []; while (!$arrayTypes->EOF) { $arrayPTypes[] = "'".$arrayTypes->fields['typname']."'"; $arrayTypes->moveNext(); } foreach ($arrayModes as $pV) { $arrayPModes[] = "'{$pV}'"; } $szTypes = 'g_main_types = new Array('.implode(',', $arrayPTypes).');'; $szModes = 'g_main_modes = new Array('.implode(',', $arrayPModes).');'; return $szTypes.$szModes; } /** * Get the concatenated arguments for a function. * * @param \PHPPgAdmin\ADORecordSet $funcdata The funcdata record * * @return string The arguments of the function */ private function _getPropertiesArgs($funcdata) { $data = $this->misc->getDatabaseAccessor(); if ($data->hasNamedParams()) { if (isset($funcdata->fields['proallarguments'])) { $args_arr = $data->phpArray($funcdata->fields['proallarguments']); } else { $args_arr = explode(', ', $funcdata->fields['proarguments']); } $names_arr = $data->phpArray($funcdata->fields['proargnames']); $modes_arr = $data->phpArray($funcdata->fields['proargmodes']); $args = ''; $args_arr_size = sizeof($args_arr); for ($i = 0; $i < $args_arr_size; ++$i) { if (0 != $i) { $args .= ', '; } if (isset($modes_arr[$i])) { switch ($modes_arr[$i]) { case 'i': $args .= ' IN '; break; case 'o': $args .= ' OUT '; break; case 'b': $args .= ' INOUT '; break; case 'v': $args .= ' VARIADIC '; break; case 't': $args .= ' TABLE '; break; } } if (isset($names_arr[$i]) && '' != $names_arr[$i]) { $data->fieldClean($names_arr[$i]); $args .= '"'.$names_arr[$i].'" '; } $args .= $args_arr[$i]; } } else { $args = $funcdata->fields['proarguments']; } return $args; } }