<?php
/* $Id$ */
/*

  v_hunt_group_edit.php
  Copyright (C) 2008 Mark J Crane
  All rights reserved.

	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/v_config.inc");;


$a_hunt_group          = &$config['installedpackages']['freeswitchhuntgroup']['config'];
$a_hunt_group_destinations 	= &$config['installedpackages']['freeswitchhuntgroupdestinations']['config'];


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



if (isset($id) && $a_hunt_group[$id]) {
	$pconfig['huntgroupid'] = $a_hunt_group[$id]['huntgroupid'];
	$huntgroupid = $a_hunt_group[$id]['huntgroupid'];
	$pconfig['huntgroupextension'] = $a_hunt_group[$id]['huntgroupextension'];
	$pconfig['huntgroupname'] = $a_hunt_group[$id]['huntgroupname'];
	$pconfig['huntgrouptype'] = $a_hunt_group[$id]['huntgrouptype'];
	$pconfig['huntgroupcontext'] = $a_hunt_group[$id]['huntgroupcontext'];
	$pconfig['huntgrouptimeout'] = $a_hunt_group[$id]['huntgrouptimeout'];
	$pconfig['huntgrouptimeoutdestination'] = $a_hunt_group[$id]['huntgrouptimeoutdestination'];
	$pconfig['huntgrouptimeouttype'] = $a_hunt_group[$id]['huntgrouptimeouttype'];
	$pconfig['huntgroupringback'] = $a_hunt_group[$id]['huntgroupringback'];	
	$pconfig['huntgroupcidnameprefix'] = $a_hunt_group[$id]['huntgroupcidnameprefix'];	
	$pconfig['huntgrouppin'] = $a_hunt_group[$id]['huntgrouppin'];
	$pconfig['huntgroupcallerannounce'] = $a_hunt_group[$id]['huntgroupcallerannounce'];	
	$pconfig['huntgroupdescr'] = $a_hunt_group[$id]['huntgroupdescr'];
}

if ($_POST) {

	unset($input_errors);
	$pconfig = $_POST;
	
	if ($_GET['act'] == "del") {
    if ($_GET['type'] == 'destinations') {
        if ($a_hunt_group_destinations[$_GET['optionid']]) {
            unset($a_hunt_group_destinations[$_GET['optionid']]);
            write_config();
            sync_package_v_hunt_group();
            header("Location: v_hunt_group_edit.php?id=".$_GET['id']);
            exit;
        }
    }
  }


	if (!$input_errors) {
  	
		$huntgroupent = array();
		if (strlen($_POST['huntgroupid']) > 0) {
			$huntgroupent['huntgroupid'] = $_POST['huntgroupid'];
		}
		else {
      		$huntgroupent['huntgroupid'] = guid();
    	}
		$huntgroupent['huntgroupextension'] = $_POST['huntgroupextension'];
		$huntgroupent['huntgroupname'] = $_POST['huntgroupname'];
		$huntgroupent['huntgrouptype'] = $_POST['huntgrouptype'];
		$huntgroupent['huntgroupcontext'] = $_POST['huntgroupcontext'];
		$huntgroupent['huntgrouptimeout'] = $_POST['huntgrouptimeout'];
		$huntgroupent['huntgrouptimeoutdestination'] = $_POST['huntgrouptimeoutdestination'];
		$huntgroupent['huntgrouptimeouttype'] = $_POST['huntgrouptimeouttype'];
		$huntgroupent['huntgroupringback'] = $_POST['huntgroupringback'];
		$huntgroupent['huntgroupcidnameprefix'] = $_POST['huntgroupcidnameprefix'];
		$huntgroupent['huntgrouppin'] = $_POST['huntgrouppin'];
		$huntgroupent['huntgroupcallerannounce'] = $_POST['huntgroupcallerannounce'];
		$huntgroupent['huntgroupdescr'] = $_POST['huntgroupdescr'];

		if (isset($id) && $a_hunt_group[$id]) {
		  	//update
      		$a_hunt_group[$id] = $huntgroupent;
		}
		else {
		  	//add
			$a_hunt_group[] = $huntgroupent;
		}
		
		//touch($d_hostsdirty_path);		
		write_config();
		sync_package_v_hunt_group();
		
		header("Location: v_hunt_group.php");
		exit;
	}
}

include("head.inc");

?>

<body link="#0000CC" vlink="#0000CC" alink="#0000CC">

<?php
include("fbegin.inc");
if ($v_label_show) {
	echo "<p class=\"pgtitle\">$v_label: Hunt Group: Edit</p>\n";
}
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

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>General Settings:<br>
                </strong></span>
                Hunt Group general settings.
                </span></p></td>
          </tr>
        </table>
        <br />
    		
        <form action="v_hunt_group_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</td>
              <td width="78%" class="vtable">
                <input name="huntgroupextension" type="text" class="formfld" id="huntgroupextension" size="40" value="<?=htmlspecialchars($pconfig['huntgroupextension']);?>">
                <br> <span class="vexpl">e.g. <em>7002</em></span></td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncellreq">Hunt Group Name</td>
              <td width="78%" class="vtable">
                <input name="huntgroupname" type="text" class="formfld" id="huntgroupname" size="40" value="<?=htmlspecialchars($pconfig['huntgroupname']);?>">
              </td>
            </tr>
			
			<tr>
              <td width="22%" valign="top" class="vncellreq">Type</td>
              <td width="78%" class="vtable">
                <?php
                echo "                <select name='huntgrouptype' class='formfld'>\n";
                echo "                <option></option>\n";
                if (htmlspecialchars($pconfig['huntgrouptype']) == "simultaneous") {
                    echo "                <option selected='yes'>simultaneous</option>\n";
                }
                else {
                    echo "                <option>simultaneous</option>\n";
                }
                if (htmlspecialchars($pconfig['huntgrouptype']) == "sequentially") {
                    echo "                <option selected='yes'>sequentially</option>\n";
                }
                else {
                    echo "                <option>sequentially</option>\n";
                }
                echo "                </select>\n";
                ?>
              </td>
            </tr>

            <?php
            if (strlen($pconfig['huntgrouptimeout']) == 0) {
                $pconfig['huntgrouptimeout'] = 30; //set a default timeout
            }
            ?>
            <tr>
              <td width="22%" valign="top" class="vncell">Context</td>
              <td width="78%" class="vtable">
                <input name="huntgroupcontext" type="text" class="formfld" id="huntgroupextension" size="40" value="<?=htmlspecialchars($pconfig['huntgroupcontext']);?>">
                <br> <span class="vexpl">e.g. <em>default</em></span></td>
            </tr>			
            <tr>
              <td width="22%" valign="top" class="vncell">Timeout</td>
              <td width="78%" class="vtable">
                <input name="huntgrouptimeout" type="text" class="formfld" id="huntgrouptimeout" size="40" value="<?=htmlspecialchars($pconfig['huntgrouptimeout']);?>">
                <br>
				<span class="vexpl">
				The timeout sets the time in seconds to continue to call before timing out.
				</span>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncell">Timeout Type</td>
              <td width="78%" class="vtable">
                <?php
                echo "                <select name='huntgrouptimeouttype' class='formfld'>\n";
                echo "                <option></option>\n";
                if (htmlspecialchars($pconfig['huntgrouptimeouttype']) == "extension") {
                    echo "                <option selected='yes'>extension</option>\n";
                }
                else {
                    echo "                <option>extension</option>\n";
                }
                if (htmlspecialchars($pconfig['huntgrouptimeouttype']) == "voicemail") {
                    echo "                <option selected='yes'>voicemail</option>\n";
                }
                else {
                    echo "                <option>voicemail</option>\n";
                }
                if (htmlspecialchars($pconfig['huntgrouptimeouttype']) == "sip uri") {
                    echo "                <option selected='yes'>sip uri</option>\n";
                }
                else {
                    echo "                <option>sip uri</option>\n";
                }
                echo "                </select>\n";
                ?>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncell">Timeout Destination</td>
              <td width="78%" class="vtable">
                <input name="huntgrouptimeoutdestination" type="text" class="formfld" id="huntgrouptimeoutdestination" size="40" value="<?=htmlspecialchars($pconfig['huntgrouptimeoutdestination']);?>">
                <br> <span class="vexpl">Destination<br>
                e.g. <em>1001</em></span></td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncell">Ring Back</td>
              <td width="78%" class="vtable">
                <?php
                echo "                <select name='huntgroupringback' class='formfld'>\n";
                echo "                <option></option>\n";
                if (htmlspecialchars($pconfig['huntgroupringback']) == "ring") {
                    echo "                <option selected='yes'>ring</option>\n";
                }
                else {
                    echo "                <option>ring</option>\n";
                }
                if (htmlspecialchars($pconfig['huntgroupringback']) == "music") {
                    echo "                <option selected='yes'>music</option>\n";
                }
                else {
                    echo "                <option>music</option>\n";
                }
                echo "                </select>\n";
                ?>
				<br>
				<span class="vexpl">
				Defines what the caller will hear while destination is being called. The choices are music 
				(music on hold) ring (ring tone.) default: music 
				</span>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncell">CID Prefix</td>
              <td width="78%" class="vtable">
                <input name="huntgroupcidnameprefix" type="text" class="formfld" id="huntgroupcidnameprefix" size="40" value="<?=htmlspecialchars($pconfig['huntgroupcidnameprefix']);?>">
                <br>
				<span class="vexpl">
				Set a prefix on the caller ID name. (optional)
				</span>
              </td>
            </tr>
            <tr>
              <td width="22%" valign="top" class="vncell">PIN</td>
              <td width="78%" class="vtable">
                <input name="huntgrouppin" type="text" class="formfld" id="huntgrouppin" size="40" value="<?=htmlspecialchars($pconfig['huntgrouppin']);?>">
                <br>
				<span class="vexpl">
				If this is provided then the caller will be required to enter the PIN number. (optional)
				</span>
              </td>
            </tr>
			
			<tr>
              <td width="22%" valign="top" class="vncell">Caller Announce</td>
              <td width="78%" class="vtable">
                <?php
                echo "                <select name='huntgroupcallerannounce' class='formfld'>\n";
                echo "                <option></option>\n";
                if (htmlspecialchars($pconfig['huntgroupcallerannounce']) == "true") {
                    echo "                <option selected='yes'>true</option>\n";
                }
                else {
                    echo "                <option>true</option>\n";
                }
                if (htmlspecialchars($pconfig['huntgroupcallerannounce']) == "false") {
                    echo "                <option selected='yes'>false</option>\n";
                }
                else {
                    echo "                <option>false</option>\n";
                }
                echo "                </select>\n";
                ?>
              </td>
            </tr>
			
            <tr>
              <td width="22%" valign="top" class="vncell">Description</td>
              <td width="78%" class="vtable">
                <input name="huntgroupdescr" type="text" class="formfld" id="descr" size="40" value="<?=htmlspecialchars($pconfig['huntgroupdescr']);?>">
                <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="Submit" type="submit" class="formbtn" value="Save"> <input class="formbtn" type="button" value="Cancel" onclick="history.back()">
                  <input name="huntgroupid" type="hidden" value="<?=htmlspecialchars($pconfig['huntgroupid']);?>">
                <?php if (isset($id) && $a_hunt_group[$id]): ?>
                  <input name="id" type="hidden" value="<?=$id;?>">
                <?php endif; ?>
              </td>
            </tr>
            </table>
        </form>

      <br>
      <br>

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


      //echo "<pre>";
      //print_r ($a_hunt_group);
      //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;

      ?>

  	<table width="100%" border="0" cellpadding="6" cellspacing="0">
      <tr>
        <td><p><span class="vexpl"><span class="red"><strong>Destinations<br />
            </strong></span>
            The following destinations will be called.
            </span></p></td>
      </tr>
    </table>
    <br />

    <table width="100%" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="40%" class="listhdrr">Destination</td>
      <td width="40" class="listhdrr">Type</td>
	  <td width="40" class="listhdrr">Profile</td>
	  <td width="40" class="listhdrr">Order</td>
      <td width="45%" class="listhdr">Description</td>
      <td width="30" class="list">
        <table border="0" cellspacing="0" cellpadding="1">
          <tr>
            <td width="17"></td>
            <td valign="middle">
			<?php if (strlen($huntgroupid) > 1) { ?>
				<a href="v_hunt_group_destinations_edit.php?parentid=<?=$parentid;?>&huntgroupid=<?=$huntgroupid;?>&a=action"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" width="17" height="17" border="0"></a></td>
			<?php } ?>
		  </tr>
        </table>
      </td>
	</tr>

	<?php
	//create a temporary id for the array
	$i = 0;
	if (count($a_hunt_group_destinations) > 0) {
		foreach ($a_hunt_group_destinations as $ent) {
			$a_hunt_group_destinations[$i]['id'] = $i;
			$i++;
		}
	}
	
	//order the array
	function cmp_number($a, $b) {
		if ($a["destinationorder"] > $b["destinationorder"]) {
			return 1;
		}
		else {
			return 0;
		}
	}
	if (count($a_hunt_group_destinations) > 0) { usort($a_hunt_group_destinations, "cmp_number"); }
	
    $i = 0;
    if (count($a_hunt_group_destinations) > 0) {
		foreach ($a_hunt_group_destinations as $ent) {
			if ($huntgroupid == $ent['huntgroupid']) {
	?>
				<tr>
				  <td class="listlr" ondblclick="document.location='v_hunt_group_destinations_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&huntgroupid=<?=$huntgroupid;?>'">
					<?=$ent['destinationnumber']?>
				  </td>
				  <td class="listr" ondblclick="document.location='v_hunt_group_destinations_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&huntgroupid=<?=$huntgroupid;?>';">
					<?=$ent['destinationtype'];?>&nbsp;
				  </td>
				  <td class="listr" ondblclick="document.location='v_hunt_group_destinations_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&huntgroupid=<?=$huntgroupid;?>';">
					<?=$ent['destinationprofile'];?>&nbsp;
				  </td>
				  <td class="listr" ondblclick="document.location='v_hunt_group_destinations_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&huntgroupid=<?=$huntgroupid;?>';">
					<?=$ent['destinationorder'];?>&nbsp;
				  </td>				  
				  <td class="listbg" ondblclick="document.location='v_hunt_group_destinations_edit.php?id=<?=$ent['id'];?>&parentid=<?=$parentid;?>&huntgroupid=<?=$huntgroupid;?>';">
					<font color="#FFFFFF"><?=htmlspecialchars($ent['destinationdescr']);?>&nbsp;
				  </td>
				  <td valign="middle" nowrap class="list">
				  <?php 
				  if (strlen($huntgroupid) > 1) {
					echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\">\n";
					echo "  <tr>\n";
					echo "	<td valign=\"middle\"><a href=\"v_hunt_group_destinations_edit.php?id=".$ent['id']."&parentid=".$parentid."&huntgroupid=".$huntgroupid."\"><img src=\"/themes/".$g['theme']."/images/icons/icon_e.gif\" width=\"17\" height=\"17\" border=\"0\"></a></td>\n";
					echo "	<td><a href=\"v_hunt_group_destinations.php?type=huntgroupdestinations&act=del&id=".$ent['id']."&parentid=".$parentid."&huntgroupid=".$huntgroupid."\" onclick=\"return confirm('Do you really want to delete this recording?')\"><img src=\"/themes/".$g['theme']."/images/icons/icon_x.gif\" width=\"17\" height=\"17\" border=\"0\"></a></td>\n";
					echo "  </tr>\n";
				    echo "</table>\n";
				  }
				  ?>
				  </td>
				</tr>
	<?php
			}
			$i++;
		}
    }
    ?>
    <tr>
      <td class="list" colspan="5"></td>
      <td class="list">
        <table border="0" cellspacing="0" cellpadding="1">
          <tr>
            <td width="17"></td>
            <td valign="middle">
			<?php if (strlen($huntgroupid) > 1) { ?>
				<a href="v_hunt_group_destinations_edit.php?parentid=<?=$parentid;?>&huntgroupid=<?=$huntgroupid;?>&a=action"><img src="/themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" width="17" height="17" border="0"></a>
			<?php } ?>
			</td>
          </tr>
        </table>
		  </td>
     </tr>


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

      </form>




     </td>
     </tr>


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



      <br>
      <br>

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

</div>

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