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 / ServerObject-UpdRecPHP.js < prev    next >
Encoding:
JavaScript  |  2003-09-05  |  15.3 KB  |  533 lines

  1. // Copyright 2000, 2001, 2002, 2003 Macromedia, Inc. All rights reserved.
  2.  
  3.     
  4. //**********************GLOBAL VARS********************
  5.  
  6. var helpDoc = MM.HELP_objUpdateRecord;
  7.  
  8. var rsName = "";
  9.  
  10. // stores separate file dom needed for dynamic UI
  11. var domUIPieces = dw.getDocumentDOM("ServerObject-FormEditHTML.htm"); 
  12.  
  13.  
  14. // global form widget variables
  15. var _DisplayAs, _SubmitAs, _ElemUp, _ElemDown, _ElemAdd, _ElemDel;
  16. var _GoToURL, _ElementLabel;
  17.  
  18. var _ConnectionName = new ConnectionMenu("ServerObject-UpdRecPHP.htm", "ConnectionName"); 
  19. var _TableName = new ConnectionTableMenu("ServerObject-UpdRecPHP.htm", "TableName");
  20. var _RecordsetName  = new RecordsetMenu("", "RecordsetName", "");
  21. var _ColumnNames = new TreeControlWithNavControls("ColumnNames");
  22. var _UniqueKeyColumn = new ListControl("UniqueKeyColumn");
  23. var _IsNumeric = new CheckBox("", "IsNumeric"); 
  24.  
  25.  
  26. var ColumnsToAdd = new Array();  // the list of columns available for adding
  27. var ColumnTypes = new Array();
  28.  
  29. // stores the unique key column's SubmitAs
  30. // TODO: the submitAs types are platform specific
  31. var SUBMIT_AS_NUMERIC_KEY = "none,0,null";
  32. var SUBMIT_AS_TEXT_KEY = "',none,none";   
  33.  
  34. //********************API FUNCTIONS******************************    
  35. //--------------------------------------------------------------------
  36. // FUNCTION:
  37. //   commandButtons
  38. //
  39. // DESCRIPTION:
  40. //   Returns the list of buttons which should appear on the right hand
  41. //   side of the dialog
  42. //
  43. // ARGUMENTS:
  44. //   none
  45. //
  46. // RETURNS:
  47. //   Array - pairs of button name and function call
  48. //--------------------------------------------------------------------
  49. function commandButtons()
  50. {
  51.   return new Array(MM.BTN_OK,     "clickedOK()", 
  52.                    MM.BTN_Cancel, "clickedCancel()", 
  53.                    MM.BTN_Help,   "displayHelp()"); 
  54. }
  55.  
  56.  
  57. //--------------------------------------------------------------------
  58. // FUNCTION:
  59. //   canInsertObject
  60. //
  61. // DESCRIPTION:
  62. //   This function is called to determine if this object can be inserted
  63. //   into the current document.  It displays the relevant error messages,
  64. //   and then returns a boolean to indicate if insertion is possible.
  65. //
  66. //   NOTE: this function is called before initializeUI, so it should
  67. //         not rely on internal state.
  68. //
  69. // ARGUMENTS:
  70. //   none
  71. //
  72. // RETURNS:
  73. //   boolean
  74. //--------------------------------------------------------------------
  75. function canInsertObject()
  76. {
  77.   var retVal = true;
  78.  
  79.   var errMsgStr = "";
  80.   if (dwscripts.getRecordsetNames().length == 0) 
  81.   { 
  82.     errMsgStr = MM.MSG_NeedRecordsetForObject;
  83.   }
  84.  
  85.   if (errMsgStr)
  86.   {
  87.     alert (errMsgStr);
  88.     retVal = false;
  89.   }
  90.  
  91.   return retVal;
  92. }
  93.  
  94.  
  95.  
  96. //********************LOCAL FUNCTIONS******************************    
  97. //--------------------------------------------------------------------
  98. // FUNCTION:
  99. //   clickedOK
  100. //
  101. // DESCRIPTION:
  102. //   This function is called when the user clicks OK
  103. //
  104. // ARGUMENTS:
  105. //   none
  106. //
  107. // RETURNS:
  108. //   nothing
  109. //--------------------------------------------------------------------
  110. function clickedOK()
  111. {
  112.   var conn = _ConnectionName.getValue();
  113.   var table = _TableName.getValue();
  114.   var redirectURL   = (_GoToURL.value)?_GoToURL.value:"";
  115.   var nRows, i, currRowInfoObj, fieldInfoObj;  
  116.   var sqlObj = new SQLStatement("");
  117.   var columnInfoList = new Array(), columnInfoNode, ElementStrArr = new Array();
  118.   var hiddenFieldNamesArr = new Array(), hiddenFieldValuesArr = new Array();
  119.   
  120.   rs  = _RecordsetName.getValue();
  121.   col = _UniqueKeyColumn.getValue();
  122.   rowInfoArr = _ColumnNames.valueList;
  123.   nRows = rowInfoArr.length;
  124.     
  125.   // check for error conditions
  126.   var errMsgStr = "";
  127.   if (!conn) 
  128.   {
  129.     errMsgStr = MM.MSG_NoConnection;
  130.   } 
  131.   else if (!table) 
  132.   {
  133.     errMsgStr = MM.MSG_NoTables;
  134.   }
  135.   else if (!nRows)
  136.   {
  137.     errMsgStr = MM.Msg_NoColumnsInTable;
  138.   } 
  139.   else if (!rs)
  140.   {
  141.     errMsgStr = MM.MSG_NoRecordset;
  142.   }
  143.   else if (!col) 
  144.   {
  145.     errMsgStr = MM.MSG_NoColumn;
  146.   }
  147.   
  148.   if (!errMsgStr) 
  149.   {
  150.     var colList = dwscripts.getFieldNames(rs);
  151.     // check that the selected column exists in the recordset
  152.     var j, found=true;
  153.     for (j=0, found=false; !found && j < colList.length; j++)
  154.     {
  155.       found = (colList[j].toLowerCase() == col.toLowerCase());
  156.     }  
  157.     if (!found) errMsgStr = MM.MSG_NoColumnInRS;
  158.   }
  159.   
  160.   // if error condition, alert it and return
  161.   if (errMsgStr)
  162.   {
  163.     alert (errMsgStr);
  164.     return;
  165.   }
  166.   
  167.   // if no error conditions, build the edits to apply to the document
  168.   MM.setBusyCursor();
  169.   
  170.   // create parameter object used to provide variables for this edit op
  171.   // server behavior
  172.   var paramObj = new Object(); 
  173.  
  174.   paramObj.TableAlign   = "center";
  175.   // Get a unique form name...
  176.   var formName = dwscripts.getUniqueNameForTag("FORM","form");
  177.   paramObj.FormName = formName; 
  178.   
  179.   var addedPrimary = false;
  180.   
  181.   for (i = 0; i < nRows; i++)
  182.   {
  183.     currRowInfoObj = rowInfoArr[i];
  184.     fieldInfoObj = currRowInfoObj.displayAs; 
  185.     fieldInfoObj.fieldName = currRowInfoObj.fieldName;
  186.     var columnName = currRowInfoObj.column;
  187.     
  188.     if (columnName)
  189.     {
  190.       columnInfoNode = dwscripts.getColumnValueNode(); // get platform specific ColumnValueNode
  191.       columnInfoNode.setColumnName(columnName);    
  192.       columnInfoNode.setColumnType(fieldInfoObj.type);      
  193.       columnInfoNode.setVariableName(getVarNameFromFormField(fieldInfoObj.fieldName));                  
  194.    
  195.       // Check if this field is the unique primary key field, if so
  196.       // push another node for the primary key...
  197.       if (columnName == col)
  198.       {
  199.         addedPrimary = true;
  200.         columnInfoNode.setIsPrimaryKey(true);
  201.         // Select between submit as strings for a text primary key or numeric primary key.
  202.         columnInfoNode.setSubmitAs(_IsNumeric.getCheckedState() ? SUBMIT_AS_NUMERIC_KEY 
  203.                                                                 : SUBMIT_AS_TEXT_KEY);
  204.       }
  205.       else
  206.       {      
  207.         columnInfoNode.setSubmitAs(currRowInfoObj.submitAs);                  
  208.       }    
  209.       columnInfoList.push(columnInfoNode);          
  210.     }
  211.             
  212.     // handle the hidden fields
  213.     if (fieldInfoObj.type == "hiddenField")
  214.     {
  215.       hiddenFieldNamesArr.push(fieldInfoObj.fieldName);
  216.       hiddenFieldValuesArr.push(fieldInfoObj.value);
  217.     }  
  218.   }  
  219.   
  220.   // Also push the Update and Primary Key column hidden fields into the array...
  221.   hiddenFieldNamesArr.push("MM_update");
  222.   hiddenFieldValuesArr.push(formName);
  223.   
  224.   // TODO: the following code is platform specific, how can we remove this?
  225.   hiddenFieldNamesArr.push(col);
  226.   var primaryKeyValue = "<?php echo $row_" + rs + "['" + col + "']; ?>";
  227.   hiddenFieldValuesArr.push(primaryKeyValue);
  228.   
  229.   if (!addedPrimary)
  230.   {
  231.     // add primary key columnInfoNode
  232.     columnInfoNode = dwscripts.getColumnValueNode(); // get platform specific ColumnValueNode
  233.     columnInfoNode.setColumnName(col);
  234.     columnInfoNode.setColumnType("");
  235.     columnInfoNode.setVariableName(getVarNameFromFormField(col));                  
  236.     columnInfoNode.setIsPrimaryKey(true);
  237.     // Select between submit as strings for a text primary key or numeric primary key.
  238.     columnInfoNode.setSubmitAs(_IsNumeric.getCheckedState() ? SUBMIT_AS_NUMERIC_KEY 
  239.                                                             : SUBMIT_AS_TEXT_KEY);
  240.     columnInfoList.push(columnInfoNode);
  241.   }
  242.   
  243.   paramObj.HiddenFieldName = hiddenFieldNamesArr;
  244.   paramObj.HiddenFieldValue = hiddenFieldValuesArr;
  245.     
  246.   // this will go through all the form elements and returns a string array...
  247.   ElementStrArr = createFormElementStrings(rowInfoArr);
  248.   
  249.   paramObj.ElementString = ElementStrArr;
  250.   paramObj.ButtonText = MM.BTN_UpdateRecord;
  251.   
  252.   paramObj.Redirect_url = redirectURL;
  253.   paramObj.ConnectionName = conn;
  254.   paramObj.COnnectionPath = dwscripts.getConnectionURL(conn);
  255.   
  256.   paramObj.RecordsetName = rs;
  257.   paramObj.PrimaryKeyColumn = col; 
  258.  
  259.   var sqlVarList = sqlObj.createUpdateStatement(table, columnInfoList);    
  260.   var sqlString = sqlObj.getStatement(true);  // strip line breaks
  261.   paramObj.SQLStatement = sqlString;
  262.   paramObj.SQLVariableList = sqlVarList.join(",\n                       ");
  263.   
  264.   // correct the selection
  265.   checkThatCursorIsNotInsideOfAForm();
  266.   dwscripts.setCursorOutsideParagraph();
  267.   dwscripts.fixUpSelection(dw.getDocumentDOM(), false, true);
  268.   
  269.   dwscripts.applyGroup("RecordUpdateForm", paramObj);
  270.  
  271.   MM.clearBusyCursor();
  272.   window.close();
  273. }
  274.  
  275. //--------------------------------------------------------------------
  276. // FUNCTION:
  277. //   clickedCancel
  278. //
  279. // DESCRIPTION:
  280. //   This function is called when CANCEL is clicked
  281. //
  282. // ARGUMENTS:
  283. //   none
  284. //
  285. // RETURNS:
  286. //   nothing
  287. //--------------------------------------------------------------------
  288. function clickedCancel(){
  289.   MM.commandReturnValue = "";
  290.   window.close();
  291. }
  292.  
  293. //--------------------------------------------------------------------
  294. // FUNCTION:
  295. //   displayHelp
  296. //
  297. // DESCRIPTION:
  298. //   Displays the built-in Dreamweaver help.
  299. //
  300. // ARGUMENTS:
  301. //   none
  302. //
  303. // RETURNS:
  304. //   nothing
  305. //--------------------------------------------------------------------
  306. function displayHelp()
  307. {
  308.   // Replace the following call if you are modifying this file for your own use.
  309.   dwscripts.displayDWHelp(helpDoc);
  310. }
  311.  
  312. //--------------------------------------------------------------------
  313. // FUNCTION:
  314. //   initializeUI
  315. //
  316. // DESCRIPTION:
  317. //   This function is called in the onLoad event.  It is responsible
  318. //   for initializing the UI.
  319. //
  320. // ARGUMENTS:
  321. //   none
  322. //
  323. // RETURNS:
  324. //   nothing
  325. //--------------------------------------------------------------------
  326. function initializeUI()
  327.   _DisplayAs = new ListControl("DisplayAs");
  328.   _SubmitAs = new ListControl("SubmitAs");
  329.   _GoToURL  = dwscripts.findDOMObject("GoToURL");
  330.   _ElementLabel    = dwscripts.findDOMObject("ElementLabel");
  331.   _ElemUp = dwscripts.findDOMObject("elemUp");
  332.   _ElemDown = dwscripts.findDOMObject("elemDown");
  333.   _ElemAdd = dwscripts.findDOMObject("elemAdd");
  334.   _ElemDel = dwscripts.findDOMObject("elemDel");
  335.      
  336.   // initialize the form elements
  337.   _ConnectionName.initializeUI();
  338.   
  339.   _IsNumeric.initializeUI(); 
  340.  
  341.    // populate menus
  342.    var displayAsArr = new Array(TEXTFIELD,TEXTAREA,MENU,HIDDENFIELD,CHECKBOX,RADIOGROUP,PASSWORDFIELD,STATICTEXT);
  343.    _DisplayAs.setAll(displayAsArr,displayAsArr);
  344.    
  345.    _SubmitAs.init();  // get the values from the HTML
  346.    
  347.   _RecordsetName.initializeUI();
  348.   _TableName.initializeUI();   
  349.  
  350.   rsName = _RecordsetName.getValue();
  351.   
  352.   _ColumnNames.setColumnNames(MM.LABEL_ColGrid);
  353.   
  354.   //EnableDisableUpDownBtns();
  355.   //EnableDisableAddDelBtns();    
  356.  
  357.   elts = document.forms[0].elements;
  358.   if (elts && elts.length)
  359.   {
  360.     elts[0].focus();
  361.   }
  362. }
  363.  
  364. //--------------------------------------------------------------------
  365. // FUNCTION:
  366. //   updateUI
  367. //
  368. // DESCRIPTION:
  369. //   This function is called by the UI controls to handle UI updates
  370. //
  371. // ARGUMENTS:
  372. //   control - string - the name of the control sending the event
  373. //   event - string - the event which is being sent
  374. //
  375. // RETURNS:
  376. //   nothing
  377. //--------------------------------------------------------------------
  378. function updateUI(control, event)
  379. {
  380.   if (control == "ConnectionName")
  381.   {
  382.    _TableName.updateUI(_ConnectionName, event); // this will trigger a recursive call to this function
  383.   }
  384.   else if (control == "TableName")
  385.   {
  386.     var i, colArr = new Array(), EMPTY_LIST = new Array();
  387.     // clear additional column list
  388.     // it lists columns that don't get populated in the grid, and needs to be cleared
  389.     updateAdditionalColumnList('clear'); 
  390.  
  391.     // populate grid
  392.     populateColumnGrid();
  393.     
  394.     checkForUnsupportedColumnTypes(true);
  395.     
  396.     // clear global field names array (used to check for dupe field names)
  397.     STR_ELEMENT_NAMES = STR_DIVIDER;
  398.     
  399.     // populate UI according to first grid item
  400.     displayGridFieldValues();
  401.  
  402.     // populate uniqueID menu
  403.     for (i in ColumnTypes)
  404.     {
  405.       colArr.push(i);
  406.     }
  407.     
  408.     if (colArr.length > 0)
  409.       _UniqueKeyColumn.setAll(colArr, colArr);
  410.     else
  411.       _UniqueKeyColumn.setAll(new Array(MM.LABEL_NoColumns), EMPTY_LIST); 
  412.     updateUI("UniqueKeyColumn");
  413.   }
  414.   else if (control == "RecordsetName")
  415.   {
  416.     if(event == 'onChange')
  417.     {
  418.       if (rsName != "")
  419.       {
  420.         // populate grid
  421.         populateColumnGrid(); 
  422.         checkForUnsupportedColumnTypes(false);         
  423.         updateDefaultFormFieldValues(rsName, _RecordsetName.getValue());
  424.         displayGridFieldValues();
  425.         rsName = _RecordsetName.getValue();
  426.       }
  427.     }
  428.   }
  429.   else if (control == "UniqueKeyColumn")
  430.   {        
  431.     // check for checking numeric box here
  432.     var column = _UniqueKeyColumn.get();
  433.     
  434.     // populate the column grid again after the unique key column is selected
  435.     populateColumnGrid();
  436.     
  437.     checkForUnsupportedColumnTypes(false);
  438.     
  439.     // refresh the grid field values
  440.     displayGridFieldValues();
  441.         
  442.     if (ColumnTypes[column] != null) 
  443.     {
  444.       if (dwscripts.isNumericDBColumnType(ColumnTypes[column])) {
  445.         _IsNumeric.setCheckedState(true);
  446.       } else {
  447.         _IsNumeric.setCheckedState(false);
  448.       }
  449.     }
  450.   }
  451.   else if (control == "AddRow")
  452.   {
  453.     addGridRow();
  454.     //EnableDisableUpDownBtns();
  455.     //EnableDisableAddDelBtns();    
  456.   }
  457.   else if (control == "DeleteRow")
  458.   {
  459.     deleteGridRow();
  460.     //EnableDisableUpDownBtns();
  461.     //EnableDisableAddDelBtns();    
  462.   }
  463.   else if (control == "MoveRowUp")
  464.   {
  465.     _ColumnNames.moveRowUp();
  466.     //EnableDisableUpDownBtns();
  467.   }
  468.   else if (control == "MoveRowDown")
  469.   {
  470.     _ColumnNames.moveRowDown();
  471.     //EnableDisableUpDownBtns();
  472.   }
  473.   else if (control == "ColumnNames")
  474.   {
  475.     displayGridFieldValues();
  476.   }
  477.   else if (control == "ElementLabel")
  478.   {
  479.     updateGridRow('label');
  480.   }
  481.   else if (control == "SubmitAs")
  482.   {
  483.     updateGridRow('submitAs');
  484.   }
  485.   else if (control == "DisplayAs")
  486.   {
  487.     showDifferentParams(true);
  488.  
  489.     var newSubmitType = getSubmitTypeBasedOnElementType
  490.       (_SubmitAs.getValue(),_DisplayAs.get(),ColumnTypes[ _ColumnNames.getRowValue().column ]);
  491.     _SubmitAs.pickValue(newSubmitType);
  492.  
  493.     updateGridRow('displayAs')
  494.   }  
  495.   else if (control == "GoToURL")
  496.   {
  497.     var theRedirect_url = dw.browseForFileURL("select", MM.LABEL_FileRedirect,0,0); 
  498.     
  499.     if (theRedirect_url.length > 0)
  500.     {
  501.       // convert any script blocks to concat values
  502.       theRedirect_url = theRedirect_url.replace(/<\?php\s+(?:echo\s+)?/gi, "\" . ");
  503.       theRedirect_url = theRedirect_url.replace(/;?\s*\?>/gi, " . \"");
  504.       
  505.       _GoToURL.value = theRedirect_url;
  506.     }
  507.   }
  508. }
  509.  
  510. //--------------------------------------------------------------------
  511. // FUNCTION:
  512. //   getVarNameFromFormField
  513. //
  514. // DESCRIPTION:
  515. //   Prepare the php columnValueNode variable name based on form field name.
  516. //
  517. // ARGUMENTS:
  518. //   value - formFieldName
  519. //
  520. // RETURNS:
  521. //   string - php columnValueNode variable name
  522. //--------------------------------------------------------------------
  523. function getVarNameFromFormField(value)
  524. {
  525.   var varName = "";
  526.   if (value)
  527.   {
  528.     var paramInfo = dwscripts.getParameterCodeFromType(MM.LABEL_PHP_Param_Types[1], value);
  529.     varName = ((paramInfo) ? paramInfo.nameVal : "");
  530.   }
  531.   return varName;
  532. }