home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 86 / PIWD86.iso / pc / contents / dreamweaver / software / dwmx2004.exe / Disk1 / data1.cab / Configuration_En / Commands / ServerBeh-PHP-SimpRS.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  15.8 KB  |  585 lines

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