请求跟踪器REST API:用于创建新故障单的Web界面

时间:2015-12-18 21:44:40

标签: ajax wordpress rt

我以前有一个自定义表单,用户可以填写下订单,一旦他们点击提交,它将创建一个包含所有信息的新票证,并将其添加到其中一个RT队列中。

我们之前使用Mason来做这件事,但现在我们已经转移到WordPress,并希望使用PHP以更干净的方式重做它。

我阅读了API文档并审核了this thread以及Stack Overflow上发布的许多其他文档。我知道如何连接到RT并通过命令行和cURL创建新票证,但我似乎无法在提交时使用Web界面来弄清楚如何这样做。如果有人能给我一些关于从哪里开始的建议,我真的很感激。

由于

修改 感谢您的答复。下面是我创建的表单,它与我们的SQL数据库交互以提取一些信息,我需要它来创建一个包含所有提交信息的新票证。我应该创建一个类似于[this] [2]的新php文件并将其作为表单操作包含吗?

<form action="<?php echo $_SELF; ?>";

      method="post"

          id="woForm"

        name="woForm"

     enctype="multipart/form-data"
     >
      <input type="hidden" name="session_id" value="<?php echo session_id(); ?>">

      <input type="hidden" name="Queue"  value="<?php echo $queue; ?>">

      <input type="hidden" name="id"     value="new">

      <input type="hidden" name="Status" value="new">

      <input type="hidden" name="Owner"  value="10">



      <table width="450" align="center" border="0" cellpadding="0" cellspacing="5">


      <tr><td align="left" colspan="2">

             <h2><?php echo $name; ?></h2>

             <p>Please note that all fields except for <b>Ext:</b>, <b>CC:</b> and <b>Attachments:</b> are <span class="required">required</span>.

               You cannot submit a request for assistance using this form unless all the required

               fields have been completed.</p>

             <h2 style="color:red;">Please enter information for the INDIVIDUAL needing assistance</h2>

          </td>

      </tr>

      <?php
      // Get all of the customFields
      $query1 = "select * from CustomFields where disabled='0' and sortOrder != 0 order by sortOrder ASC;";
      $result1 = mysql_query($query1) or die ("dead3: ".mysql_error());

      // Go through each custom field
      while($row1 = mysql_fetch_array($result1)) {

         // Get the information about that field
         $count = 0;
         $fieldId = $row1['id'];
         $name = $row1['Name'];

         // $postname is in a very specific format, and will become the name of the field in the form 
         // where the data for this custom field is entered. In order to submit a ticket into rt, the  
         // name of the field MUST be in this format.
         $postName = "Object-RT::Ticket--CustomField-".$fieldId."-Values";
      ?>

         <!-- Create a row in the table for this custom field -->
         <tr>
              <!-- Create a column with the name of the custom field -->
              <td align="right" class="requestformlabel"><label class="required"><?php echo $name; ?>:</label></td>
      <!-- Create a column for the input field -->
      <td class = "requestformtd">

      <?php   

      // If the custom field is department or building, we need a pull-down menu
      if($name=="Department" || $name=="Building") { ?>

      <!-- start of the pull-down menu -->
      <select name="<?php echo $postName; ?>">

       <?php   

         // Get all of the possible values for the customField from the database
         // Added option to exclude sort order 9999.  See ticket #40665 for more info.
         $query3 = "SELECT * FROM CustomFieldValues WHERE CustomField='$fieldId' AND SortOrder != '9999' ORDER BY SortOrder ASC";
         $result3 = mysql_query($query3) or die ("dead4: ".mysql_error());

         // Go through each possible value for the custom field
         while($row3 = mysql_fetch_array($result3)) {      

           // Get the information on the custom field value from the database
           $tmp = $row3['Name'];
           $description = $row3['Description'];

           // If the custom field value was already selected
           if($tmp == $_POST["$postName"]) {

             // Insert the option into the pull-down menu and mark it as selected in the form
             echo "<option value='$tmp' selected='selected'>$description</option>";

           // otherwise
           } else {

             // Only insert it as an option in the pull-down menu
             echo "<option value='$tmp'>$description</option>";
           } 
        } 
      ?> 

      </td></tr> 
      <?php 

      // If the name of the custom field is operating system, we want radio buttons
      } else if ($name == "Operating System") {   

         // Get all the possible values for this field form the database
         $query4 = "select * from CustomFieldValues where CustomField='$fieldId' order by sortorder asc";
         $result4 = mysql_query($query4) or die ("dead5: ".mysql_error());

         // For each customfield value
         while($row4 = mysql_fetch_array($result4)) {      

            // Get the description of the customfieldvalue from the database
            $osName = $row4['Description']; 

            // If the customfieldvalue has already been selected
            if ($osName == $_POST["$postName"]) {

                // Put the radio button into the form and mark it as checked
                echo "<input type='radio' name='$postName' value='$osName' checked='checked'>$osName";

            // Otherwise
            } else {

                // Put the radio button into the form
                echo "<input type='radio' name='$postName' value='$osName'>$osName";
            }
         } ?> 

         </td></tr>

      <?php 

      // If the name of the custom field is ip adress, we want a disbaled text box. This is because while we want the user to see their ip adress, we do not want them to be able to change it.
      } else if ($name == "IP_Address"){ 

      ?>

              <input name="<?php echo $postName; ?>" size="40" value='<?php 
      echo $_SERVER['REMOTE_ADDR']; ?>' readonly></td></tr>         

      <?php  

      // If it's the hostname variable
      } else if ($name == "Host_Name"){

      ?>

              <input name="<?php echo $postName; ?>" size="40" value='<?php echo gethostbyaddr($_SERVER['REMOTE_ADDR']); ?>' readonly></td></tr>

      <?php  

      // Otherwise, create a text box for the custom field.
      } else { 

      ?>

              <input name="<?php echo $postName; ?>" size="40" value='<?php echo $_POST["$postName"]; ?>'></td></tr>

      <?php  } // end else statement

      } // end while loop

      ?>



      <tr>

          <td class="requestformlabel" align="right"><label class="required">Your E-mail Address:</label></td>

            <td  align="left" class="requestformtd"><input name="Requestors" size=40 value="<?php echo $_POST['Requestors']; ?>"></td>



      </tr>



      <tr>

          <td class="requestformlabel" align="right"><label class="required">Confirm Your E-mail Address:</label></td>

            <td align="left" class="requestformtd"><input name="Requestors_2" size=40 value="<?php echo $_POST['Requestors_2']; ?>"></td>

      </tr>



      <tr>

          <td class="requestformlabel" align="right"><label class="fields">Cc:</label></td>

            <td  align="left" class="requestformtd"><input name="Cc" size=40 value="<?php echo $_POST['Cc']; ?>"></td>

         </tr>



      <tr>

             <td align="right"><p>&nbsp;<br/>&nbsp;</p></td>

      <td align="right"><span class="ccnote">(Separate multiple email addresses with commas.)<br/>&nbsp;</span></td>

      </tr>



      <tr>

          <td class="requestformlabel" align="right"><label class="required">Short Problem Summary:</label></td>

            <td align="left" class="requestformtd"><input name="Subject" size=40 maxsize=100  value="<?php echo $_POST['Subject']; ?>"></td></tr>



      <tr>

          <td class="requestformlabel" align="right"><label class="required">Decribe the issue below:</label></td>

         <td  align="left" class="requestformtd"><textarea

           class="messagebox" cols=35 rows=15 wrap="hard" name="Content"><?php echo $_POST['Content']; ?></textarea>

         </td>



      </tr>



      <?php 



      //if session has attachments

      if($_SESSION['attach'] != '') {

      ?>

      <!-- row for existing attahcments -->

      <tr>
          <!-- column that states these are the current attachments, and tells the user what to do if 
      they wish to remove an attachment. -->

          <td class="requestformlabel" align="right">Current Attachments:<br/>

              <span class="ccnote">(Check box to delete)</span>

          </td>

          <!-- coulmn that lists the attachments -->

          <td class="requestformtd" align="right">

              <?php

                 // Go through each file in $_SESSION['attach']

                 while (list($key, $val) = each($_SESSION['attach'])) {

                    // Get the name of the file

                    $attName = $val['name'];

                    // Create a checkbox to mark the file as needing to be removed from the list

                    echo "<input type='checkbox' name='DeleteAttach-$attName' value='1'>$attName<br/>";


                 } // end while loop

              ?>

          </td>

      </tr>



      <?php // end if for attachments

      }

      ?>



      <tr>

          <td class="requestformlabel" align="right"><label class="fields">Attachments:</label></br>

              <span class="ccnote">Max. attachment size: 50MB.</span></td>

          <td align="right" colspan="2"  class="requestformtd">

               <input type="file" name="Attach">

               <br/>

               <input type="submit" name="AddMoreAttach" value="Add More Files">

          </td>

      </tr>



      <tr>

          <td align="left"><input type="submit" name="submit" value="Submit Request"></td>

          <td>&nbsp;</td>

      </tr>



      </table>

</form>

编辑2: 谢谢。使用this repo中的文档和代码,我创建了一个名为new_ticket.php的新文件,其中包含以下内容:

<?php

if($_POST['action'] == 'call_this') {
    require_once 'RequestTracker.php';
    $url = "www.test.com/rt/REST/1.0/";
    $user = "user";
    $pass = "password";

    $rt = new RequestTracker($url, $user, $pass);

    $content = array(
        'Queue'=>'9',
        'Requestor'=>'test@example.com',
        'Subject'=>'Lorem Ipsum',
        'Text'=>'dolor sit amet'
    );
$response = $rt->createTicket($content);
print_r($response);
}
?>

我还从同一个Github回购邮件中复制了RequestTracker.php。

在表单所在的文件中,我添加了以下脚本,并将create_ticket()作为操作添加到提交按钮的onclick属性中。但这似乎并没有起作用。我尝试将某些内容记录到控制台以查看代码获取的距离,正确调用create_ticket()函数,但是$ .ajax之后出现的任何内容({...上面的内容不会出现在控制台中。我也试过放一些控制台登录我的new_ticket.php文件,但也没有记录任何内容,所以我做错了什么?

<script>
 function create_ticket() {
      $.ajax({
        url:"new_ticket.php", //the page containing php script
        type: "POST", //request type
        data:{action:'call_this'},
        success:function(result){
         alert(result);
       }
     });
 }
</script>

PS:我使用的是ajax,因为我需要在onclick上运行PHP代码,这不能像在Javascript中那样直接完成。

0 个答案:

没有答案