home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 July / 07_02.iso / software / mdmx / files / DreamweaverMXInstaller.exe / Disk1 / data1.cab / Configuration_En / Commands / ServerBeh-PHP-SimpRS.js < prev    next >
Encoding:
JavaScript  |  2002-05-01  |  15.7 KB  |  584 lines

  1. // Copyright 2002 Macromedia, Inc. All rights reserved.
  2.  
  3.  
  4. // *************** GLOBALS VARS *****************
  5.  
  6. var helpDoc = MM.HELP_ssSimpleRecordset;
  7.  
  8.  
  9. var RECORDSET_SBOBJ;  // SBRecordset argument to the command.
  10. var CMD_FILENAME_ADV; // Command filename for the advanced recordset dialog.
  11. var SIMPLE_PARAM_NAME = 'colname' ;
  12.  
  13. var _RecordsetName = new TextField("Recordset.htm", "RecordsetName");
  14. var _ConnectionName = new ConnectionMenu("Recordset.htm", "ConnectionName");
  15. var _TableName = new ConnectionTableMenu("Recordset.htm", "TableName");
  16. var _ColumnType = new RadioGroup("Recordset.htm", "ColumnType");
  17. var _ColumnList = new ConnectionColumnMenu("Recordset.htm", "ColumnList", "", false);
  18. var _FilterColumn = new ConnectionColumnMenu("Recordset.htm", "FilterColumn", "", true);
  19. var _FilterOperator = new ListMenu("Recordset.htm", "FilterOperator");
  20. var _FilterParameter = new ListMenu("Recordset.htm", "FilterParameter");
  21. var _FilterParameterValue = new TextField("Recordset.htm", "FilterParameterValue");
  22. var _SortColumn = new ConnectionColumnMenu("Recordset.htm", "SortColumn", "", true);
  23. var _SortDirection = new ListMenu("Recordset.htm", "SortDirection");
  24.  
  25.  
  26. // ******************* API **********************
  27.  
  28. //--------------------------------------------------------------------
  29. // FUNCTION:
  30. //   commandButtons
  31. //
  32. // DESCRIPTION:
  33. //   Returns the list of buttons which should appear on the right hand
  34. //   side of the dialog
  35. //
  36. // ARGUMENTS:
  37. //   none
  38. //
  39. // RETURNS:
  40. //   Array - pairs of button name and function call
  41. //--------------------------------------------------------------------
  42.  
  43. function commandButtons()
  44. {
  45.   return new Array(MM.BTN_OK,       "clickedOK()", 
  46.                    MM.BTN_Cancel,   "clickedCancel()", 
  47.                    MM.BTN_Test,     "clickedTest()", 
  48.                    MM.BTN_Advanced, "clickedAdvanced()", 
  49.                    MM.BTN_Help,     "displayHelp()"); 
  50. }
  51.  
  52.  
  53. //--------------------------------------------------------------------
  54. // FUNCTION:
  55. //   clickedOK
  56. //
  57. // DESCRIPTION:
  58. //   This function is called when the user clicks OK
  59. //
  60. // ARGUMENTS:
  61. //   none
  62. //
  63. // RETURNS:
  64. //   nothing
  65. //--------------------------------------------------------------------
  66.  
  67. function clickedOK()
  68. {
  69.   // Update RECORDSET_SBOBJ from the UI.
  70.   // added a check to make sure that some columns are selected if the radio
  71.   // radio buttion is checked.
  72.   if (_ColumnType.getValue() == "some")
  73.   {
  74.     if (_ColumnList.getValues().length == 0)
  75.     {
  76.       alert(MM.MSG_SelectColumns);
  77.       return;
  78.     }
  79.   }
  80.   updateSBRecordsetObject();
  81.  
  82.   recordsetDialog.onClickOK(window, RECORDSET_SBOBJ);
  83. }
  84. //--------------------------------------------------------------------
  85. // FUNCTION:
  86. //   clickedCancel
  87. //
  88. // DESCRIPTION:
  89. //   This function is called when CANCEL is clicked
  90. //
  91. // ARGUMENTS:
  92. //   none
  93. //
  94. // RETURNS:
  95. //   nothing
  96. //--------------------------------------------------------------------
  97.  
  98. function clickedCancel()
  99. {
  100.   recordsetDialog.onClickCancel(window);
  101. }
  102.  
  103.  
  104. //--------------------------------------------------------------------
  105. // FUNCTION:
  106. //   clickedTest
  107. //
  108. // DESCRIPTION:
  109. //   This function is called when the user clicks the TEST button
  110. //
  111. // ARGUMENTS:
  112. //   none
  113. //
  114. // RETURNS:
  115. //   nothing
  116. //--------------------------------------------------------------------
  117.  
  118. function clickedTest()
  119. {
  120.   // Update RECORDSET_SBOBJ from the UI.
  121.   updateSBRecordsetObject();
  122.   
  123.   recordsetDialog.displayTestDialog(RECORDSET_SBOBJ);
  124. }
  125.  
  126.  
  127. //--------------------------------------------------------------------
  128. // FUNCTION:
  129. //   clickedAdvanced
  130. //
  131. // DESCRIPTION:
  132. //   This function is called when the user clicks the ADVANCED button
  133. //
  134. // ARGUMENTS:
  135. //   none
  136. //
  137. // RETURNS:
  138. //   nothing
  139. //--------------------------------------------------------------------
  140.  
  141. function clickedAdvanced()
  142. {
  143.   // Update RECORDSET_SBOBJ from the UI.
  144.   updateSBRecordsetObject();
  145.  
  146.   recordsetDialog.onClickSwitchUI(window, recordsetDialog.UI_ACTION_SWITCH_ADV, 
  147.                                   RECORDSET_SBOBJ, CMD_FILENAME_ADV);
  148. }
  149.  
  150.  
  151. //--------------------------------------------------------------------
  152. // FUNCTION:
  153. //   displayHelp
  154. //
  155. // DESCRIPTION:
  156. //   This function is called when the user clicks the HELP button
  157. //
  158. // ARGUMENTS:
  159. //   none
  160. //
  161. // RETURNS:
  162. //   nothing
  163. //--------------------------------------------------------------------
  164.  
  165. function displayHelp()
  166. {
  167.   dwscripts.displayDWHelp(helpDoc);
  168. }
  169.  
  170.  
  171. // ***************** LOCAL FUNCTIONS  ******************
  172.  
  173. //--------------------------------------------------------------------
  174. // FUNCTION:
  175. //   initializeUI
  176. //
  177. // DESCRIPTION:
  178. //   This function is called in the onLoad event.  It is responsible
  179. //   for initializing the UI.  If we are inserting a recordset, this
  180. //   is a matter of populating the connection drop down.
  181. //
  182. //   If we are modifying a recordset, this is a matter of inspecting
  183. //   the recordset tag and setting all the form elements.
  184. //
  185. // ARGUMENTS:
  186. //   none
  187. //
  188. // RETURNS:
  189. //   nothing
  190. //--------------------------------------------------------------------
  191.  
  192. function initializeUI()
  193. {
  194.   var sqlObject = null;
  195.  
  196.   var args = dwscripts.getCommandArguments();
  197.   RECORDSET_SBOBJ = args[0];
  198.   CMD_FILENAME_ADV = args[1];
  199.   
  200.   // Get the UI elements
  201.   _RecordsetName.initializeUI();
  202.   _ConnectionName.initializeUI();
  203.   _TableName.initializeUI();
  204.   _ColumnType.initializeUI();
  205.   _ColumnList.initializeUI();
  206.   _FilterColumn.initializeUI();
  207.   _FilterOperator.initializeUI();
  208.   _FilterParameter.initializeUI(dwscripts.getParameterTypeArray(), dwscripts.getParameterTypeArray());
  209.   _FilterParameterValue.initializeUI();
  210.   _SortColumn.initializeUI();
  211.   _SortDirection.initializeUI();
  212.  
  213.   var rsName = RECORDSET_SBOBJ.getRecordsetName();
  214.   if (!rsName)
  215.   {
  216.     rsName = RECORDSET_SBOBJ.getUniqueRecordsetName();
  217.   }
  218.   _RecordsetName.setValue(rsName);
  219.   
  220.   var connectionName = RECORDSET_SBOBJ.getConnectionName();
  221.   if (connectionName)
  222.   {
  223.     _ConnectionName.pickValue(RECORDSET_SBOBJ.getConnectionName());
  224.   }
  225.  
  226.   var sqlParams = new Array();
  227.   var sqlString = RECORDSET_SBOBJ.getDatabaseCall(sqlParams);
  228.   if (sqlString)
  229.   {
  230.     sqlObject = new SQLStatement(sqlString);
  231.   }
  232.   else
  233.   {
  234.     sqlObject = new SQLStatement("");
  235.   }
  236.  
  237.   if (sqlObject.getType() == SQLStatement.STMT_TYPE_SELECT)
  238.   {
  239.     // select the correct table
  240.     var tableNames = sqlObject.getTableNames();
  241.     if (tableNames && tableNames.length > 0)
  242.     {
  243.       _TableName.pickValue(tableNames[0]);
  244.     }
  245.  
  246.     // selet the correct columns
  247.     var columnNames = sqlObject.getColumnNames();
  248.     if (columnNames.length == 1 && columnNames[0] == "*")
  249.     {
  250.       _ColumnType.pickValue("all");
  251.     }
  252.     else
  253.     {
  254.       _ColumnType.pickValue("some");
  255.       _ColumnList.setDisabled(false);
  256.       // multiply select the given columns
  257.       _ColumnList.pickValues(columnNames);
  258.     }
  259.  
  260.     // select the correct filter column
  261.    
  262.     var filterInfo = RECORDSET_SBOBJ.getSimpleWhereInfo(sqlObject);
  263.     if (filterInfo != null)
  264.     {
  265.       if (filterInfo.lval)
  266.       {
  267.         _FilterColumn.pickValue(filterInfo.lval);
  268.       }
  269.       if (filterInfo.operator)
  270.       {
  271.         _FilterOperator.pickValue(filterInfo.operator);
  272.       }
  273.       if (filterInfo.rval)
  274.       {
  275.         var param = new Object();
  276.         // If there is not sql parameter, this is probably an entered parameter
  277.         //   value. Use the filter rval as the runtime code.
  278.         if (sqlParams.length > 0)
  279.         {
  280.           param = sqlParams[0];
  281.         }
  282.         else
  283.         {
  284.           param.runtimeValue = filterInfo.rval; 
  285.         }
  286.  
  287.         // The parameter name is the 'runtime' value expected in dwscripts.getParameterTypeFromCode.
  288.         var paramInfo = dwscripts.getParameterTypeFromCode(param.runtimeValue);
  289.         
  290.         if (filterInfo.isString && paramInfo.paramType == MM.LABEL_PHP_Param_Types[5])
  291.         {
  292.           paramInfo.paramName = "'" + paramInfo.paramName + "'";
  293.         }
  294.         
  295.         _FilterParameter.pickValue(paramInfo.paramType);
  296.         _FilterParameterValue.setValue(paramInfo.paramName);
  297.       }
  298.     }
  299.     else
  300.     {
  301.       updateUI("FilterColumn", "onChange");
  302.     }
  303.  
  304.     var orderByInfo = RECORDSET_SBOBJ.getSimpleOrderByInfo(sqlObject);
  305.     if (orderByInfo != null)
  306.     {
  307.       if (orderByInfo.column)
  308.       {
  309.         _SortColumn.pickValue(orderByInfo.column);
  310.       }
  311.       if (orderByInfo.direction)
  312.       {
  313.         _SortDirection.pickValue(orderByInfo.direction);
  314.       }
  315.     }
  316.     else
  317.     {
  318.       updateUI("SortColumn", "onChange");
  319.     }
  320.   }
  321.   else
  322.   {
  323.     updateUI("ColumnType", "onChange");
  324.     updateUI("FilterColumn", "onChange");
  325.     updateUI("SortColumn", "onChange");
  326.   }
  327.  
  328.   elts = document.forms[0].elements;
  329.   if (elts && elts.length)
  330.   {
  331.     elts[0].focus();
  332.     elts[0].select();
  333.   }
  334. }
  335.  
  336.  
  337. //--------------------------------------------------------------------
  338. // FUNCTION:
  339. //   updateUI
  340. //
  341. // DESCRIPTION:
  342. //   This function is called by the UI controls to handle UI updates
  343. //
  344. // ARGUMENTS:
  345. //   control - string - the name of the control sending the event
  346. //   event - string - the event which is being sent
  347. //
  348. // RETURNS:
  349. //   nothing
  350. //--------------------------------------------------------------------
  351.  
  352. function updateUI(control, event)
  353. {
  354.   if (control == "RecordsetName")
  355.   {
  356.   }
  357.   else if (control == "ConnectionName")
  358.   {
  359.     _TableName.updateUI(_ConnectionName, event);
  360.   }
  361.   else if (control == "Define")
  362.   {
  363.     _ConnectionName.launchConnectionDialog();
  364.   }
  365.   else if (control == "TableName")
  366.   {
  367.     _ColumnList.updateUI(_TableName, event);
  368.     _FilterColumn.updateUI(_TableName, event);
  369.     _SortColumn.updateUI(_TableName, event);
  370.     _ColumnType.pickValue("all");
  371.   }
  372.   else if (control == "ColumnType")
  373.   {
  374.     var value = _ColumnType.getValue();
  375.     if (value == "all")
  376.     {
  377.       _ColumnList.setDisabled(true, true);
  378.     }
  379.     else
  380.     {
  381.       _ColumnList.setDisabled(false);
  382.     }
  383.   }
  384.   else if (control == "FilterColumn")
  385.   {
  386.     if (_FilterColumn.getValue() == "")
  387.     {
  388.       _FilterOperator.setDisabled(true);
  389.       _FilterParameter.setDisabled(true);
  390.       _FilterParameterValue.setValue("");
  391.       _FilterParameterValue.setDisabled(true);
  392.     }
  393.     else
  394.     {
  395.       _FilterOperator.setDisabled(false);
  396.       _FilterParameter.setDisabled(false);
  397.       _FilterParameterValue.setDisabled(false);
  398.       _FilterParameterValue.setValue(_FilterColumn.getValue());
  399.     }
  400.   }
  401.   else if (control == "SortColumn")
  402.   {
  403.     if (_SortColumn.getValue() == "")
  404.     {
  405.       _SortDirection.setDisabled(true);
  406.     }
  407.     else
  408.     {
  409.       _SortDirection.setDisabled(false);
  410.     }
  411.   }
  412.   else
  413.   {
  414.     //alert("updateUI(" + control + ", " + event + ")");
  415.   }
  416. }
  417.  
  418.  
  419. //--------------------------------------------------------------------
  420. // FUNCTION:
  421. //   updateSBRecordsetObject
  422. //
  423. // DESCRIPTION:
  424. //   Collects information from the UI and sets the SBRecordset object
  425. //
  426. // ARGUMENTS:
  427. //   none
  428. //
  429. // RETURNS:
  430. //   boolean - true if successful, false otherwise
  431. //--------------------------------------------------------------------
  432.  
  433. function updateSBRecordsetObject()
  434. {
  435.   RECORDSET_SBOBJ.setRecordsetName(_RecordsetName.getValue());
  436.  
  437.   RECORDSET_SBOBJ.setConnectionName(_ConnectionName.getValue());
  438.  
  439.   var sqlObject = new SQLStatement("");
  440.  
  441.   sqlObject.addFrom(_TableName.getValue());
  442.  
  443.   var colType = _ColumnType.getValue();
  444.   if (colType != "all")
  445.   {
  446.     var columnNames = _ColumnList.getValues();
  447.     for (var i=0; i < columnNames.length; i++)
  448.     {
  449.       sqlObject.addSelect(_TableName.getValue(), columnNames[i], true); // dont include table prefix
  450.     }
  451.   }
  452.  
  453.   var sqlParams = new Array();
  454.  
  455.   if (_FilterColumn.getValue() != "")
  456.   {
  457.     var paramInfo = dwscripts.getParameterCodeFromType(_FilterParameter.getValue(), 
  458.                                                        _FilterParameterValue.getValue(),
  459.                              "");
  460.  
  461.     // If there is no parameter, enter the filter parameter value directly to the
  462.     //   where clause.
  463.     if (paramInfo != null)
  464.     {
  465.       var paramName = dwscripts.getSQLStringForDBColumnType(SIMPLE_PARAM_NAME, _FilterColumn.getType());
  466.  
  467.       RECORDSET_SBOBJ.addSimpleWhere(sqlObject, _FilterColumn.getValue(), _FilterOperator.getValue(), paramName);
  468.  
  469.       var sqlParam = new Object();
  470.       sqlParam.varName = SIMPLE_PARAM_NAME;
  471.       sqlParam.defaultValue = paramInfo.defaultVal;
  472.       sqlParam.runtimeValue = paramInfo.runtimeVal;
  473.       sqlParam.valuePrompt = _FilterParameter.getValue() + ": " + _FilterParameterValue.getValue();
  474.   
  475.       sqlParams.push(sqlParam);
  476.     }
  477.     else
  478.     {
  479.       var paramName = dwscripts.getSQLStringForDBColumnType(_FilterParameterValue.getValue(), _FilterColumn.getType());
  480.       RECORDSET_SBOBJ.addSimpleWhere(sqlObject, _FilterColumn.getValue(), _FilterOperator.getValue(), paramName);      
  481.     }
  482.   }
  483.  
  484.   if (_SortColumn.getValue() != "")
  485.   {
  486.     sqlObject.addOrderBy(_TableName.getValue(), _SortColumn.getValue(), _SortDirection.getValue(), true);  // dont include table prefix
  487.   }
  488.   
  489.   RECORDSET_SBOBJ.setDatabaseCall(sqlObject.getStatement(), sqlParams);
  490. }
  491.  
  492. //--------------------------------------------------------------------
  493. // FUNCTION:
  494. //   canDisplayRecordset
  495. //
  496. // DESCRIPTION:
  497. //   Returns true if the given recordset can be displayed in this
  498. //   recordset dialog. Called by the recordsetDialog to determine which
  499. //   dialog to display.
  500. //
  501. // ARGUMENTS: 
  502. //   sbRecordset - SBRecordset. the recordset to check.
  503. //
  504. // RETURNS:
  505. //   boolean - true if can display the recordset.
  506. //--------------------------------------------------------------------
  507.  
  508. function canDisplayRecordset(sbRecordset) 
  509. {
  510.   var retVal = false;
  511.  
  512.   if (!sbRecordset)
  513.   {
  514.     retVal = true;
  515.   }
  516.  
  517.   if (!retVal)
  518.   {
  519.     var sqlParams = new Array();
  520.     var sqlString = sbRecordset.getDatabaseCall(sqlParams);
  521.  
  522.     if (!sqlString || (sqlString == ""))
  523.     {
  524.       retVal = true;
  525.       return retVal;
  526.     }
  527.     
  528.     var sqlObject = new SQLStatement(sqlString);
  529.  
  530.     // check for select statement
  531.     if (sqlObject.getType() == SQLStatement.STMT_TYPE_SELECT)
  532.     {
  533.       // check that only one table is specified
  534.       if (sqlObject.getTableNames().length == 1)
  535.       {
  536.         // check that the columns are simple values
  537.         var columns = sqlObject.getColumnNames();
  538.         var bSimpleNames = true;
  539.         for (var i=0; i < columns.length; i++)
  540.         { 
  541.           if (!sbRecordset.isSimpleColumnName(columns[i]))
  542.           {
  543.             bSimpleNames = false;
  544.             break;
  545.           }
  546.         }
  547.         if (bSimpleNames)
  548.         {
  549.  
  550.           // check for no filter column, or filter is single param,
  551.           // or filter is single entered value (no parameter).    
  552.           var whereInfo = sbRecordset.getSimpleWhereInfo(sqlObject);
  553.  
  554.           if (   whereInfo != null 
  555.               && (   (!whereInfo.rval && sqlParams.length == 0) 
  556.                   || (sqlParams.length == 0)
  557.                   || (   whereInfo.rval && sqlParams.length == 1 
  558.                           && whereInfo.rval == sqlParams[0].varName
  559.                      )
  560.                  )
  561.              )
  562.           {
  563.             // check for a single order by column
  564.             var orderByInfo = sbRecordset.getSimpleOrderByInfo(sqlObject);
  565.  
  566.             if (orderByInfo != null)
  567.             {
  568.               // now check that the other clauses are empty
  569.               if (!sqlObject.getHaving() && !sqlObject.getGroupBy())
  570.               {
  571.                 retVal = true;
  572.               }
  573.             }
  574.           }
  575.         }
  576.       }
  577.     }
  578.   }
  579.  
  580.   return retVal;
  581. }
  582.  
  583.  
  584.