<?php
  /**
   * Copyright 2011 Zazavi - http://www.zazavi.com
   * Zazavi is free software released under the terms of the GNU General Public License.
   * You should have received a copy of the license.  If not, see <http://www.gnu.org/licenses/>.
   */
  if (!defined("_VALID_PHP"))
      die('Direct access to this location is not allowed.');

  require_once(ZAZAVILITE . "lib/class_forms.php");
  $form = new Forms();
?>
<?php switch($core->action): case "edit": ?>
<?php $row = $core->getRowById("flds", $form->fieldid);?>
<h1>Manage CMS Form Fields &rsaquo; Edit Field</h1>
<p class="info">Here you can update your form fields. Fields marked <?php echo required();?> are required.</p>
<h2>Edit Form Field &rsaquo; <?php echo $row['title'];?></h2>
<form action="" method="post" id="admin_form" name="admin_form">
  <table cellspacing="0" cellpadding="0" class="formtable">
    <tr>
      <td width="150">Field Label: <?php echo required();?></td>
      <td><input name="title" type="text" class="inputbox required" value="<?php echo $row['title'];?>" size="55" title="Please Enter Field Label"/>
        <?php echo tooltip("This will control the display text for the field");?></td>
    </tr>
    <tr>
      <td>Field Tooltip:</td>
      <td><input name="fld_tip" type="text" class="inputbox" value="<?php echo $row['fld_tip'];?>" size="80" /></td>
    </tr>
    <tr>
      <td>Field Required:</td>
      <td><span class="input-out">
        <label for="fld_req-1">Yes</label>
        <input name="fld_req" type="radio" id="fld_req-1" value="1" <?php getChecked($row['fld_req'], 1); ?> />
        <label for="fld_req-2">No</label>
        <input name="fld_req" type="radio" id="fld_req-2" value="0" <?php getChecked($row['fld_req'], 0); ?> />
        </span></td>
    </tr>
    <tr>
      <td>Field Message:</td>
      <td><textarea name="fld_msg"  cols="45" rows="3" class="inputbox"><?php echo $row['fld_msg'];?></textarea>
        <?php echo tooltip("Enter an error message if field is required and not selected");?></td>
    </tr>
    <?php switch($row['fld_type']): case 3: ?>
    <tr>
      <td>Field Value:</td>
      <td><select name="initvalues" size="10" multiple="multiple" class="select" style="width:250px">
          <?php $multiplelist = explode("::",$row['fld_val']);?>
          <?php reset($multiplelist);?>
          <?php foreach ($multiplelist as $mlist): ?>
          <option value="<?php echo $mlist;?>"><?php echo $mlist;?></option>
          <?php endforeach; ?>
          <?php unset($mlist);?>
        </select></td>
    </tr>
    <tr>
      <td>Add Values:</td>
      <td><input name="fld_val" type="text" class="inputbox" value="<?php echo $row['fld_val'];?>" size="60" />
        <?php echo tooltip("Add more values separated by ::");?></td>
    </tr>
    <tr>
      <td>Field Size:</td>
      <td><input name="fld_size" type="text" class="inputbox" value="<?php echo $row['fld_size'];?>" size="5" /></td>
    </tr>
    <?php break;?>
    <?php case 2:?>
    <tr>
      <td>Field Value:</td>
      <td><select name="initvalues" class="select" style="width:250px">
          <?php $selectlist = explode("::",$row['fld_val']);?>
          <?php reset($selectlist);?>
          <?php foreach ($selectlist as $slist): ?>
          <option value="<?php echo $slist;?>"><?php echo $slist;?></option>
          <?php endforeach; ?>
          <?php unset($slist);?>
        </select></td>
    </tr>
    <tr>
      <td>Add Values:</td>
      <td><input name="fld_val" type="text"  class="inputbox" value="<?php echo $row['fld_val'];?>" size="60" />
        <?php echo tooltip("Add more values separated by ::");?></td>
    </tr>
    <?php break;?>
    <?php case 1:?>
    <tr>
      <td>Field Value:</td>
      <td><textarea name="fld_val" cols="40" rows="5" class="inputbox"><?php echo $row['fld_val'];?></textarea>
        <?php echo tooltip("Provide the initial value(s) if any");?></td>
    </tr>
    <tr>
      <td>Field Size:</td>
      <td><input name="fld_size" type="text" class="inputbox" value="<?php echo $row['fld_size'];?>" size="5" /></td>
    </tr>
    <?php break;?>
    <?php case 0:?>
    <tr>
      <td>Field Value:</td>
      <td><input name="fld_val" type="text"  class="inputbox" value="<?php echo $row['fld_val'];?>" size="45" />
        <?php echo tooltip("Provide the initial value(s) if any");?></td>
    </tr>
    <tr>
      <td>Field Size:</td>
      <td><input name="fld_size" type="text" class="inputbox" value="<?php echo $row['fld_size'];?>" size="5" /></td>
    </tr>
    <?php break;?>
    <?php case 4:?>
    <tr>
      <td>Field Value:</td>
      <td><?php $checklist = explode("::",$row['fld_val']);?>
        <?php reset($checklist);?>
        <?php foreach ($checklist as $clist): ?>
        <span class="input-out">
        <label for="<?php echo str_replace(" ","_",$clist);?>"><?php echo $clist;?></label>
        <input name="initvalues" type="checkbox" id="<?php echo str_replace(" ","_",$clist);?>" value="<?php echo $clist;?>" />
        <?php //echo $clist;?>
        </span>
        <?php endforeach; ?>
        <?php unset($clist);?></td>
    </tr>
    <tr>
      <td>Add Values:</td>
      <td><input name="fld_val" type="text"  class="inputbox" value="<?php echo $row['fld_val'];?>" size="60" />
        <?php echo tooltip("Add more values separated by ::");?></td>
    </tr>
    <?php break;?>
    <?php case 5:?>
    <tr>
      <td>Field Value:</td>
      <td><?php $radiolist = explode("::",$row['fld_val']);?>
        <?php reset($radiolist);?>
        <?php foreach ($radiolist as $rlist): ?>
        <span class="input-out">
        <label for="<?php echo str_replace(" ","_",$rlist);?>"><?php echo $rlist;?></label>
        <input name="valuelist" id="<?php echo str_replace(" ","_",$rlist);?>" type="radio" value="<?php echo $rlist;?>" />
        <?php //echo $rlist;?>
        </span>
        <?php endforeach; ?>
        <?php unset($rlist);?></td>
    </tr>
    <tr>
      <td>Add Values:</td>
      <td><input name="fld_val" type="text"  class="inputbox" value="<?php echo $row['fld_val'];?>" size="60" />
        <?php echo tooltip("Add more values separated by ::");?></td>
    </tr>
    <?php break;?>
    <?php case 6:?>
    <tr>
      <td>Label Value:</td>
      <td><textarea name="fld_val" cols="40" rows="5" class="inputbox"><?php echo $row['fld_val'];?></textarea>
        <?php echo tooltip("Enter label value");?></td>
    </tr>
    <?php break;?>
    <?php case 7:?>
    <tr>
      <td>Field Size:</td>
      <td><input name="fld_size" type="text" class="inputbox" value="<?php echo $row['fld_size'];?>" size="5" /></td>
    </tr>
    <?php break;?>
    <?php endswitch;?>
    <tr>
      <td colspan="2"><input type="submit" name="submit" class="button" value="Update Field" /></td>
    </tr>
  </table>
  <input name="fld_type" type="hidden" value="<?php echo $row['fld_type'];?>" />
  <input name="fieldid" type="hidden" value="<?php echo $form->fieldid;?>" />
</form>
<?php echo $core->doForm("processField");?>
<?php break;?>
<?php case"add": ?>
<h1>Manage CMS Form Fields &rsaquo; Add Field</h1>
<p class="info">Here you can add your form fields. Fields marked <?php echo required();?> are required.</p>
<h2>Add Form Fields For &rsaquo; <?php echo getValue("name", "forms", "id='".$form->formid."'");?></h2>
<form action="" method="post" id="admin_form" name="admin_form">
  <table cellspacing="0" cellpadding="0" class="formtable">
    <tr>
      <td width="150">Field Label: <?php echo required();?></td>
      <td><input name="title" type="text" class="inputbox required" size="55" title="Please Enter Field Label"/>
        <?php echo tooltip("This will control the display text for the field");?></td>
    </tr>
    <tr>
      <td>Field Tooltip:</td>
      <td><input name="fld_tip" type="text" class="inputbox" size="80" /></td>
    </tr>
    <tr>
      <td>Field Required:</td>
      <td><span class="input-out">
        <label for="fld_req-1">Yes</label>
        <input name="fld_req" type="radio" id="fld_req-1" value="1" checked="checked" />
        <label for="fld_req-2">No</label>
        <input name="fld_req" type="radio" id="fld_req-2" value="0"  />
        </span></td>
    </tr>
    <tr>
      <td>Field Message:</td>
      <td><textarea name="fld_msg"  cols="45" rows="3" class="inputbox"></textarea>
        <?php echo tooltip("Enter an error message if field is required and not selected");?></td>
    </tr>
    <tr>
      <td>Field Type:</td>
      <td><span class="inputwrap">
        <select name="fld_type" class="select" style="width:250px" id="field">
          <?php echo $form->getFieldList();?>
        </select>
        </span></td>
    </tr>
    <tr>
      <td>Field Value:</td>
      <td><input name="fld_val" type="text" class="inputbox"  size="80" value="<?php echo post('fld_val');?>" />
        <?php echo tooltip("Provide the initial value(s) if any");?> <br />
        In case of multiple list, select list, checkboxes or radio buttons provide the values separated by double-colon ::</td>
    </tr>
    <tr>
      <td>Field Size:</td>
      <td><input name="fld_size" type="text" class="inputbox" size="5" />
        <br />
        For Multiple Lists, Text Areas and Text Fields you can enter field size.</td>
    </tr>
    <tr>
      <td colspan="2"><input type="submit" name="submit" class="button" value="Add Field" /></td>
    </tr>
  </table>
  <input name="formid" type="hidden" value="<?php echo $form->formid;?>" />
</form>
<?php echo $core->doForm("processField");?>
<?php break;?>
<?php default: ?>
<?php
  $fieldsrow = $form->getFields();
  $core->setActiveInactive("flds", "loader.php?do=fields&formid=".$form->formid);
?>
<h1>Manage Form Fields</h1>
<p class="info">Here you can manage your form fields. Click on Form Fields to add, or Form Layout Field name to edit.<br />
  You can rearrange form fields by dragging <img src="images/handle-white.png" alt="" /> icon.</p>
<h2>Viewing Form Fields For  &rsaquo; <?php echo getValue("name", "forms", "id='".$form->formid."'");?></h2>
<table cellpadding="0" cellspacing="0" class="display">
  <thead>
    <tr>
      <th width="200" class="left">Form Fields</th>
      <th class="left">Form Layout</th>
    </tr>
  </thead>
  <tr style="background-color:inherit">
    <td valign="top" class="left"><?php echo $form->getDefaultFields();?></td>
    <td valign="top" class="left"><?php if($fieldsrow == 0):?>
      <div class="msgAlert"><span>Alert!</span>You don't have any fields yet...</div>
      <?php else:?>
      <ul id="used-fields">
        <?php foreach ($fieldsrow as $row):?>
        <?php $status = ($row['active'] == 0) ? 1:0;?>
        <li id="fid_<?php echo $row['id'];?>"><a href="loader.php?do=fields&amp;formid=<?php echo $form->formid;?>&amp;publish=<?php echo $status;?>&amp;id=<?php echo $row['id'];?>" class="status"><?php echo isActive($row['active']);?></a><img src="images/handle-white.png" alt="" class="smallHandle" /> <a href="loader.php?do=fields&amp;action=edit&amp;fieldid=<?php echo $row['id'];?>&amp;formid=<?php echo $form->formid;?>"><?php echo '<small>'.$form->getFieldLabels($row['fld_type']).'</small> - '.$row['title'];?></a></li>
        <?php endforeach;?>
      </ul>
      <?php unset($row);?>
      <?php endif;?></td>
  </tr>
</table>
<script type="text/javascript"> 
// <![CDATA[
$(document).ready(function () {
    $("ul#used-fields").sortable({
        handle: '.smallHandle',
        opacity: 0.6,
        helper: 'helper',
        update: function() {
            var order = $('ul#used-fields').sortable('serialize');
            $.post("ajax.php?sortfields=1&" + order, function(theResponse) {
                $("#msgDisplay").html(theResponse);
            });
        }

    });
});
// ]]>
</script>
<?php break;?>
<?php endswitch;?>