<?php
/* $Id$ */
/*
	freeswitch_public_includes_edit.php
	Copyright (C) 2008 Mark J Crane
	All rights reserved.

	FreeSWITCH (TM)
	http://www.freeswitch.org/

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

require("guiconfig.inc");
require("/usr/local/pkg/freeswitch.inc");



//freeswitchpublicincludes
  //publicincludeid
  //extensionname
  //context
      //default
  //enabled
  //descr

//



$a_public_includes          = &$config['installedpackages']['freeswitchpublicincludes']['config'];
$a_public_include_details 	= &$config['installedpackages']['freeswitchpublicincludedetails']['config'];

$id = $_GET['id'];
if (isset($_POST['id'])) {
	$id = $_POST['id'];
}
$parentid = $id;



if (isset($id) && $a_public_includes[$id]) {
	$pconfig['publicincludeid'] = $a_public_includes[$id]['publicincludeid'];	
	$publicincludeid = $a_public_includes[$id]['publicincludeid'];	
	$pconfig['extensionname'] = $a_public_includes[$id]['extensionname'];
	$pconfig['order'] = $a_public_includes[$id]['order'];
	$pconfig['context'] = $a_public_includes[$id]['context'];
	$pconfig['enabled'] = $a_public_includes[$id]['enabled'];    	
	$pconfig['descr'] = $a_public_includes[$id]['descr'];	
	$pconfig['opt1name'] = $a_public_includes[$id]['opt1name'];
	$pconfig['opt1value'] = $a_public_includes[$id]['opt1value'];
}

if ($_POST) {

	unset($input_errors);
	$pconfig = $_POST;

	if ($_GET['act'] == "del") {
    if ($_GET['type'] == 'publicincludedetails') {
        if ($a_public_include_details[$_GET['id']]) {
            unset($a_public_include_details[$_GET['id']]);
            write_config();
            sync_package_freeswitch_public_includes();
            header("Location: freeswitch_public_include_edit.php?id=".$_GET['id']);
            exit;
        }
    }
  }


	if (!$input_errors) {

		$ent = array();
		if (strlen($_POST['publicincludeid']) > 0) {
		    //update
  			$ent['publicincludeid'] = $_POST['publicincludeid'];    		
		}
		else {
			//add
      		$ent['publicincludeid'] = guid();
    	}
		$ent['extensionname'] = $_POST['extensionname'];
		$ent['order'] = $_POST['order'];
		//$ent['context'] = $_POST['context'];
		$ent['context'] = 'default';
		$ent['enabled'] = $_POST['enabled'];			
		$ent['descr'] = $_POST['descr'];		
    	$ent['opt1name'] = $_POST['opt1name'];
		$ent['opt1value'] = $_POST['opt1value'];
		


		if (isset($id) && $a_public_includes[$id]) {
        	$a_public_includes = $config['installedpackages']['freeswitchpublicincludes']['config'];
		    if (count($a_public_includes) > 0) {
		        foreach($a_public_includes as $rowhelper) {
		
		          //$rowhelper['publicincludeid'];
		          //$rowhelper['extensionname'];
		          //$rowhelper['context'];
		          //$rowhelper['enabled'];
		
		          $filenamechanged = false;
		          if ($rowhelper['publicincludeid'] == $_POST['publicincludeid']) {
		
		              if ($rowhelper['extensionname'] != $_POST['extensionname']) {
		                  //if the extension name has changed then remove the current public xml file
		                    //to prepare for the new file
		                  $filenamechanged = true;
		              }
		              if ($rowhelper['order'] != $_POST['order']) {
		                  //if the order has changed then remove the current public xml file
		                    //to prepare for the new file
		                  $filenamechanged = true;
		              }
		              if ($_POST['enabled'] == "false") {
		                  //if the extension name is disabled then remove the public xml file
		                  $filenamechanged = true;
		              }
		              if ($filenamechanged){
		                $publicincludefilename = $rowhelper['order']."_".$rowhelper['extensionname'].".xml";
		                if (file_exists("/usr/local/freeswitch/conf/dialplan/public/".$publicincludefilename)) {
		                  unlink("/usr/local/freeswitch/conf/dialplan/public/".$publicincludefilename);
		                }
		                unset($publicincludefilename);
		              }
		
		          }
		          unset($filenamechanged);
		
		        } //end foreach
	        } //end if count
	
	  		  //update the config
	        $a_public_includes[$id] = $ent;
		}
		else {
  		  //add to the config
  			$a_public_includes[] = $ent;
		}
		
		
		write_config();
		sync_package_freeswitch_public_includes();

		header("Location: freeswitch_public_includes.php");
		exit;
	}
}

include("head.inc");

?>

<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
<?php include("fbegin.inc"); ?>
<p class="pgtitle">FreeSWITCH: Public: Edit</p>
<?php if ($input_errors) print_input_errors($input_errors); ?>


<div id="mainlevel">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td class="tabnavtbl">
<?php

display_top_tabs(build_menu());
 	
?>
</td></tr>
</table>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
   <tr>
     <td class="tabcont" >

      	<table width="100%" border="0" cellpadding="6" cellspacing="0">
          <tr>
            <td><p><span class="vexpl"><span class="red"><strong>Public:<br>
                </strong></span>
                Public Include general settings.
                </span></p></td>
          </tr>
        </table>
        <br />
  		
        <form action="freeswitch_public_includes_edit.php" method="post" name="iform" id="iform">
            <table width="100%" border="0" cellpadding="6" cellspacing="0">
            <tr>
              <td width="22%" valign="top" class="vncellreq">Extension Name</td>
              <td width="78%" class="vtable">
                <input name="extensionname" type="text" class="formfld" id="extensionname" size="40" value="<?=htmlspecialchars($pconfig['extensionname']);?>">
                <br />
                Supported characters are 'a-z', 'A-Z', '0-9', underscore '_', and period '.'.
              </td>
            </tr>
            <!--
            <tr>
              <td width="22%" valign="top" class="vncellreq">Context</td>
              <td width="78%" class="vtable">
                <input name="context" type="text" class="formfld" id="context" size="40" value="<?=htmlspecialchars($pconfig['context']);?>">
                <br />
                e.g. default
              </td>
            </tr>
            -->

            <tr>
              <td width="22%" valign="top" class="vncellreq">Enabled</td>
              <td width="78%" class="vtable">
                <?php
                echo "              <select name='enabled' class='formfld'>\n";
                echo "                <option></option>\n";
                switch (htmlspecialchars($pconfig['enabled'])) {
                case "true":
                    echo "              <option value='true' selected='yes'>true</option>\n";
                    echo "              <option value='false'>false</option>\n";
                    break;
                case "false":
                    echo "              <option value='true'>true</option>\n";
                    echo "              <option value='false' selected='yes'>false</option>\n";

                    break;
                default:
                    echo "              <option value='true' selected='yes'>true</option>\n";
                    echo "              <option value='false'>false</option>\n";
                }
                echo "              </select>\n";
                ?>
              </td>
            </tr>

            <tr>
              <td width="22%" valign="top" class="vncellreq">Order</td>
              <td width="78%" class="vtable">
                <?php

                echo "              <select name='order' class='formfld'>\n";
                echo "              <option></option>\n";
                if (strlen(htmlspecialchars($pconfig['order']))> 0) {
                    echo "              <option selected='yes' value='".htmlspecialchars($pconfig['order'])."'>".htmlspecialchars($pconfig['order'])."</option>\n";
                }
                $i=0;
                while($i<=999) {
                  if (strlen($i) == 1) {
                    echo "              <option value='00$i'>00$i</option>\n";
                  }
                  if (strlen($i) == 2) {
                    echo "              <option value='0$i'>0$i</option>\n";
                  }
                  if (strlen($i) == 3) {
                    echo "              <option value='$i'>$i</option>\n";
                  }

                  $i++;
                }
                echo "              </select>\n";
                ?>
                <br />
                Processing of each public include is determined by this order.
              </td>
            </tr>

            <tr>
              <td width="22%" valign="top" class="vncell">Description</td>
              <td width="78%" class="vtable">
                <input name="descr" type="text" class="formfld" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>">
                <br> <span class="vexpl">You may enter a description here
                for your reference (not parsed).</span></td>
            </tr>
            <tr>
              <td width="22%" valign="top">&nbsp;</td>
              <td width="78%">
                <input name="publicincludeid" type="hidden" value="<?=htmlspecialchars($pconfig['publicincludeid']);?>">
                <?php
                if (strlen($id) > 0 && $a_public_includes[$id]) {
                    echo "\n";
                    echo "                <input name=\"id\" type=\"hidden\" value=\"$id\">\n";
                    echo "                <input name=\"opt1name\" type=\"hidden\" value=\"".htmlspecialchars($pconfig['opt1name'])."\">\n";
                    echo "                <input name=\"opt1value\" type=\"hidden\" value=\"".htmlspecialchars($pconfig['opt1value'])."\">\n";
                }
                ?>
                <input name="Submit" type="submit" class="formbtn" value="Save"> <input class="formbtn" type="button" value="Cancel" onclick="history.back()">
              </td>
            </tr>
            </table>
        </form>

      <br>
      <br>

      <form action="freeswitch_public_includes_edit.php" method="post" name="iform2" id="iform2">
      <?php

		//echo "<pre>";
		//print_r ($a_public_includes);
		//echo "</pre>";

		//if ($savemsg) print_info_box($savemsg);
		//if (file_exists($d_hostsdirty_path)): echo"<p>";
		//print_info_box_np("The FreeSWITCH recordings have been changed.<br>You must apply the changes in order for them to take effect.");
		//echo"<br />";
		//endif;

		//create a temporary id for the array
		$i = 0;
		if (count($a_public_include_details) > 0) {
			foreach ($a_public_include_details as $ent) {
				$a_public_include_details[$i]['id'] = $i;
				$i++;
			}
		}

		//order the array
		function cmp_number_public_details($a, $b) {
			if ($a["fieldorder"] > $b["fieldorder"]) {
				return 1;
			}
			else {
				return 0;
			}
		}
		if (count($a_public_include_details) > 0) { usort($a_public_include_details, "cmp_number_public_details"); }
	
      ?>


  	<table width="100%" border="0" cellpadding="6" cellspacing="0">
      <tr>
        <td><p><span class="vexpl"><span class="red"><strong>Conditions and Actions<br />
            </strong></span>
            The following conditions, actions and anti-actions are used in the public to direct call flow. Each is processed in order until you reach the action tag which tells FreeSWITCH what action to perform. You are not limited to only one condition or action tag for a given extension.
            </span></p></td>
      </tr>
    </table>
    <br />

    <table width="100%" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="40" class="listhdrr">Tag</td>
      <td width="40" class="listhdrr">Type</td>
      <td width="50%" class="listhdrr">Data</td>
	  <td width="40" class="listhdrr">Order</td>
      <td width="40" class="list">
        <table border="0" cellspacing="0" cellpadding="1">
          <tr>
            <td width="17"></td>
            <td valign="middle"><a href="freeswitch_public_includes_details_edit.php?parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>&a=action"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" width="17" height="17" border="0"></a></td>
          </tr>
        </table>
      </td>
		</tr>

	<?php

      $i = 0;
      if (count($a_public_include_details) > 0) {

        foreach ($a_public_include_details as $ent) {
          if ($ent['tag'] == "condition" && $publicincludeid == $ent['publicincludeid']) {
    ?>
            <tr>
              <td class="listlr" ondblclick="document.location='freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>'">
                <?=$ent['tag']?>
              </td>
              <td class="listr" ondblclick="document.location='freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>';">
                <?=$ent['fieldtype'];?>&nbsp;
              </td>
              <td class="listr" ondblclick="document.location='freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>';">
                <?=$ent['fielddata'];?>&nbsp;
              </td>
              <td class="listr" ondblclick="document.location='freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>';">
                <?=$ent['fieldorder'];?>&nbsp;
              </td>
              <td valign="middle" nowrap class="list">
                <table border="0" cellspacing="0" cellpadding="1">
                  <tr>
                    <td valign="middle"><a href="freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_e.gif" width="17" height="17" border="0"></a></td>
                    <td><a href="freeswitch_public_includes_details.php?type=publicincludedetails&act=del&id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>" onclick="return confirm('Do you really want to delete this?')"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" border="0"></a></td>
                  </tr>
               </table>
              </td>
            </tr>
		<?php
		      }
          $i++;
        }
      }

      $i = 0;
      if (count($a_public_include_details) > 0) {

        foreach ($a_public_include_details as $ent) {
          if ($ent['tag'] == "action" && $publicincludeid == $ent['publicincludeid']) {
    ?>
            <tr>
              <td class="listlr" ondblclick="document.location='freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>'">
                <?=$ent['tag']?>
              </td>
              <td class="listr" ondblclick="document.location='freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>';">
                <?=$ent['fieldtype'];?>&nbsp;
              </td>
              <td class="listr" ondblclick="document.location='freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>';">
                <?=$ent['fielddata'];?>&nbsp;
              </td>
              <td class="listr" ondblclick="document.location='freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>';">
                <?=$ent['fieldorder'];?>&nbsp;
              </td>
              <td valign="middle" nowrap class="list">
                <table border="0" cellspacing="0" cellpadding="1">
                  <tr>
                    <td valign="middle"><a href="freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_e.gif" width="17" height="17" border="0"></a></td>
                    <td><a href="freeswitch_public_includes_details.php?type=publicincludedetails&act=del&id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>" onclick="return confirm('Do you really want to delete this?')"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" border="0"></a></td>
                  </tr>
               </table>
              </td>
            </tr>
		<?php
		      }
          $i++;
        }
      }

      $i = 0;
      if (count($a_public_include_details) > 0) {

        foreach ($a_public_include_details as $ent) {
          if ($ent['tag'] == "anti-action" && $publicincludeid == $ent['publicincludeid']) {
    ?>
            <tr>
              <td class="listlr" ondblclick="document.location='freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>'">
                <?=$ent['tag']?>
              </td>
              <td class="listr" ondblclick="document.location='freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>';">
                <?=$ent['fieldtype'];?>&nbsp;
              </td>
              <td class="listr" ondblclick="document.location='freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>';">
                <?=$ent['fielddata'];?>&nbsp;
              </td>
              <td class="listr" ondblclick="document.location='freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>';">
                <?=$ent['fieldorder'];?>&nbsp;
              </td>
              <td valign="middle" nowrap class="list">
                <table border="0" cellspacing="0" cellpadding="1">
                  <tr>
                    <td valign="middle"><a href="freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_e.gif" width="17" height="17" border="0"></a></td>
                    <td><a href="freeswitch_public_includes_details.php?type=publicincludedetails&act=del&id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>" onclick="return confirm('Do you really want to delete this?')"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" border="0"></a></td>
                  </tr>
               </table>
              </td>
            </tr>
		<?php
		      }
          $i++;
        }
      }

      $i = 0;
      if (count($a_public_include_details) > 0) {

        foreach ($a_public_include_details as $ent) {
          if ($ent['tag'] == "param" && $publicincludeid == $ent['publicincludeid']) {
    ?>
            <tr>
              <td class="listlr" ondblclick="document.location='freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>'">
                <?=$ent['tag']?>
              </td>
              <td class="listr" ondblclick="document.location='freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>';">
                <?=$ent['fieldtype'];?>&nbsp;
              </td>
              <td class="listr" ondblclick="document.location='freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>';">
                <?=$ent['fielddata'];?>&nbsp;
              </td>
              <td class="listr" ondblclick="document.location='freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>';">
                <?=$ent['fieldorder'];?>&nbsp;
              </td>
              <td valign="middle" nowrap class="list">
                <table border="0" cellspacing="0" cellpadding="1">
                  <tr>
                    <td valign="middle"><a href="freeswitch_public_includes_details_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_e.gif" width="17" height="17" border="0"></a></td>
                    <td><a href="freeswitch_public_includes_details.php?type=publicincludedetails&act=del&id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>" onclick="return confirm('Do you really want to delete this?')"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" border="0"></a></td>
                  </tr>
               </table>
              </td>
            </tr>
		<?php
		      }
          $i++;
        }
      }
    ?>
    <tr>
      <td class="list" colspan="4"></td>
      <td class="list">
        <table border="0" cellspacing="0" cellpadding="1">
          <tr>
            <td width="17"></td>
            <td valign="middle"><a href="freeswitch_public_includes_details_edit.php?parentid=<?=$parentid;?>&publicincludeid=<?=$publicincludeid;?>&a=action"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" width="17" height="17" border="0"></a></td>
          </tr>
        </table>
		  </td>
     </tr>


     <tr>
       <td class="list" colspan="4"></td>
       <td class="list"></td>
     </tr>
     </table>

      </form>


      <br>
      <br>

     </td>
   </tr>
</table>

</div>

<?php include("fend.inc"); ?>
</body>
</html>