예제 #1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtSubTableName.TextLength <= 80)
            {
                // First thing we need to do it sync the other tabs data based on the Template
                if (txtSubtableTemplate.Text.Contains("\r\n"))
                {
                    // Uses the first line of the template as the headings
                    var header = txtSubtableTemplate.Text;
                    header = header.Substring(0, header.IndexOf("\r\n", StringComparison.Ordinal));

                    // Renders everything beyond the first line as the table body
                    var body = txtSubtableTemplate.Text;
                    body = body.Substring(body.IndexOf("\r\n", StringComparison.Ordinal) + 2,
                                          body.Length - (body.IndexOf("\r\n", StringComparison.Ordinal) + 2));

                    // Sync the Content tab with the current template
                    txtSubtableContent.Text = ProgSettings.ConvertTemplateToHtml(header, body);

                    // Not technically needed, but render the HTML panel
                    webBrowse.DocumentText = txtSubtableContent.Text;
                }
                var dbDocsTableOutput = new StringBuilder();
                var outputFolder      = Application.ExecutablePath;

                // Strip the Executable name from the path
                outputFolder = outputFolder.Substring(0, outputFolder.LastIndexOf(@"\", StringComparison.Ordinal));

                var selectedTable = lstsubtables.Text;

                // If the output folder doesnt exist, create it
                if (!Directory.Exists(outputFolder + @"\"))
                {
                    Directory.CreateDirectory(outputFolder + @"\");
                }

                if (SubTableId == 0) // New Record
                {
                    if (lstLangs.SelectedIndex != 0)
                    {
                        // If English, connect to main table
                        dbDocsTableOutput.AppendLine("-- WARNING: The default entry should really be in english --");
                    }

                    var newSubtableId = Convert.ToInt32(ProgSettings.GetNewSubTableId());
                    dbDocsTableOutput.AppendLine(
                        "insert  into `dbdocssubtables`(`subtableId`,`languageId`,`subtableName`,`subtablecontent`,`subtableTemplate`) values (" +
                        newSubtableId + "," + lstLangs.SelectedIndex + ",'" + selectedTable + "','" +
                        ProgSettings.PrepareSqlString(txtSubtableContent.Text) + "','" +
                        ProgSettings.PrepareSqlString(txtSubtableTemplate.Text) + "');");

                    // Open the file for append and write the entries to it
                    using (
                        var outfile =
                            new StreamWriter(outputFolder + @"\" + ProgSettings.DbName + "_dbdocssubtables.SQL",
                                             true))
                    {
                        outfile.Write(dbDocsTableOutput.ToString());
                    }

                    // Write the entry out to the Database directly

                    // For an insert, the record is always saved to the primary table, regardless of the language Since the system is English based, it should really have an English
                    // base record.

                    ProgSettings.SubTableInsert(newSubtableId, lstLangs.SelectedIndex, selectedTable,
                                                txtSubtableContent.Text, txtSubtableTemplate.Text);
                    SubTableId      = newSubtableId;
                    blnTextChanged  = false;
                    btnSave.Enabled = false;
                    mnuSave.Enabled = btnSave.Enabled;
                }
                else // Updated Record
                {
                    if (lstLangs.SelectedIndex == 0)
                    {
                        // If English, connect to main table
                        dbDocsTableOutput.AppendLine("delete from `dbdocssubtables` where `subtableId`= " + SubTableId +
                                                     " and languageId=" + lstLangs.SelectedIndex + ";");
                        dbDocsTableOutput.AppendLine(
                            "insert  into `dbdocssubtables`(`subtableId`,`languageId`,`subtableName`,`subtablecontent`,`subtableTemplate`) values (" +
                            SubTableId + "," + lstLangs.SelectedIndex + ",'" + selectedTable + "','" +
                            ProgSettings.PrepareSqlString(txtSubtableContent.Text) + "','" +
                            ProgSettings.PrepareSqlString(txtSubtableTemplate.Text) + "');");

                        // Open the file for append and write the entries to it
                        using (
                            var outfile =
                                new StreamWriter(outputFolder + @"\" + ProgSettings.DbName + "_dbdocssubtables.SQL",
                                                 true))
                        {
                            outfile.Write(dbDocsTableOutput.ToString());
                        }
                    }
                    else
                    {
                        dbDocsTableOutput.AppendLine("delete from `dbdocssubtables_localised` where `subtableId`= " +
                                                     SubTableId + " and languageId=" + lstLangs.SelectedIndex + ";");
                        dbDocsTableOutput.AppendLine(
                            "insert  into `dbdocssubtable_localised`(`subtableId`,`languageId`,`subtablecontent`,`subtableTemplate`) values (" +
                            SubTableId + "," + lstLangs.SelectedIndex + ",'" +
                            ProgSettings.PrepareSqlString(txtSubtableContent.Text) + "','" +
                            ProgSettings.PrepareSqlString(txtSubtableTemplate.Text) + "');");

                        // Open the file for append and write the entries to it
                        using (
                            var outfile =
                                new StreamWriter(
                                    outputFolder + @"\" + ProgSettings.DbName + "_dbdocssubtables_localised.SQL", true))
                        {
                            outfile.Write(dbDocsTableOutput.ToString());
                        }
                    }

                    // Write the entry out to the Database directly For an update the logic to decide which table to update is in the Update function itself

                    ProgSettings.SubTableUpdate(SubTableId, lstLangs.SelectedIndex, selectedTable,
                                                txtSubtableContent.Text,
                                                txtSubtableTemplate.Text);

                    blnTextChanged  = false;
                    btnSave.Enabled = false;
                    mnuSave.Enabled = btnSave.Enabled;
                }
                lblStatus.Text = DateTime.Now + Resources.Save_Complete_for + selectedTable;
            }
            else
            {
                //Report that the comment is too long for the db
                MessageBox.Show(this, Resources.Text_Too_Long, Resources.Error_Saving, MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }