<?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.');
	  
  if(!$user->getAcl("Users")): print $core->msgAlert(_CG_ONLYADMIN, false); return; endif;
?>
<?php switch($core->action): case "edit": ?>
<?php $row = $core->getRowById("users", $user->userid);?>
<?php $memrow = $member->getMemberships();?>
<h1><img src="images/users-sml.png" alt="" /><?php echo _UR_TITLE1;?></h1>
<p class="info"><span><?php echo $core->langIcon();?></span><?php echo _UR_INFO1. _REQ1. required() . _REQ2;?></p>
<h2><?php echo _UR_SUBTITLE1 . $row['username'];?></h2>
<form action="" method="post" id="admin_form" name="admin_form">
  <table cellspacing="0" cellpadding="0" class="formtable">
    <tr>
      <td width="200"><?php echo _USERNAME;?>: </td>
      <td><input name="username" type="text" disabled="disabled" class="inputbox" value="<?php echo $row['username'];?>" size="55" readonly="readonly" /></td>
    </tr>
    <tr>
      <td><?php echo _PASSWORD;?>:</td>
      <td><input name="password" type="text" class="inputbox" size="55" />
        &nbsp;&nbsp; <?php echo tooltip(_UR_PASS_T);?></td>
    </tr>
    <tr>
      <td><?php echo _UR_EMAIL;?>: <?php echo required();?></td>
      <td><input name="email" type="text" class="inputbox required" value="<?php echo $row['email'];?>" size="55" title="<?php echo _UR_EMAIL_R;?>"/></td>
    </tr>
    <tr>
      <td><?php echo _UR_FNAME;?>: <?php echo required();?></td>
      <td><input name="fname" type="text" class="inputbox required" value="<?php echo $row['fname'];?>"  title="<?php echo _UR_FNAME_R;?>" size="55" /></td>
    </tr>
    <tr>
      <td><?php echo _UR_LNAME;?>: <?php echo required();?></td>
      <td><input name="lname" type="text" class="inputbox required" value="<?php echo $row['lname'];?>"  title="<?php echo _UR_LNAME_R;?>" size="55" /></td>
    </tr>
    <tr>
      <td><?php echo _MEMBERSHIP;?>:</td>
      <td><select name="membership_id" class="select" style="width:200px">
          <option value="0"><?php echo _UR_NOMEMBERSHIP;?></option>
          <?php if($memrow):?>
          <?php foreach ($memrow as $mlist):?>
          <?php $selected = ($row['membership_id'] == $mlist['id']) ? " selected=\"selected\"" : "";?>
          <option value="<?php echo $mlist['id'];?>"<?php echo $selected;?>><?php echo $mlist['title'];?></option>
          <?php endforeach;?>
          <?php unset($mlist);?>
          <?php endif;?>
        </select></td>
    </tr>
    <tr>
      <td><?php echo _UR_LEVEL;?>:</td>
      <td><span class="input-out">
        <label for="userlevel-1"><?php echo _UR_SADMIN;?></label>
        <input name="userlevel" type="radio" id="userlevel-1" value="9" <?php getChecked($row['userlevel'], 9); ?> />
        <label for="userlevel-2"><?php echo _UR_ADMIN;?></label>
        <input name="userlevel" type="radio" id="userlevel-2" value="8" <?php getChecked($row['userlevel'], 8); ?> />
        <label for="userlevel-3"><?php echo _USER;?></label>
        <input name="userlevel" type="radio" id="userlevel-3" value="1" <?php getChecked($row['userlevel'], 1); ?> />
        <?php echo tooltip(_UR_ADMIN_T);?></span></td>
    </tr>
    <tr>
      <td><?php echo _UR_PERM;?>:</td>
      <td><?php echo $user->getPermissionList($row['access']);?></td>
    </tr>
    <tr>
      <td><?php echo _UR_STATUS;?>:</td>
      <td><span class="input-out">
        <label for="active-1"><?php echo _USER_A;?></label>
        <input name="active" type="radio" id="active-1" value="y" <?php getChecked($row['active'], "y"); ?> />
        <label for="active-2"><?php echo _USER_I;?></label>
        <input name="active" type="radio" id="active-2" value="n" <?php getChecked($row['active'], "n"); ?> />
        <label for="active-3"><?php echo _USER_B;?></label>
        <input name="active" type="radio" id="active-3" value="b" <?php getChecked($row['active'], "b"); ?> />
        <label for="active-4"><?php echo _USER_P;?></label>
        <input name="active" type="radio" id="active-4" value="t" <?php getChecked($row['active'], "t"); ?> />
        </span></td>
    </tr>
    <tr>
      <td><?php echo _UR_IS_NEWSLETTER;?>:</td>
      <td><span class="input-out">
        <label for="newsletter-1"><?php echo _YES;?></label>
        <input name="newsletter" type="radio" id="newsletter-1" value="1" <?php getChecked($row['active'], 1); ?>/>
        <label for="newsletter-2"><?php echo _NO;?></label>
        <input name="newsletter" type="radio" id="newsletter-2" value="0" <?php getChecked($row['active'], 0); ?> />
        </span></td>
    </tr>
    <tr>
      <td><?php echo _UR_DATE_REGGED;?>:</td>
      <td><span class="input-out"><?php echo $row['created'];?></span></td>
    </tr>
    <tr>
      <td><?php echo _UR_LASTLOGIN;?>:</td>
      <td><span class="input-out"><?php echo $row['lastlogin'];?></span></td>
    </tr>
    <tr>
      <td><?php echo _UR_LASTLOGIN_IP;?>:</td>
      <td><span class="input-out"><?php echo $row['lastip'];?></span></td>
    </tr>
    <tr>
      <td><input type="submit" name="submit" class="button" value="<?php echo _UR_UPDATE;?>" /></td>
      <td><a href="loader.php?do=users" class="button-alt"><?php echo _CANCEL;?></a></td>
    </tr>
  </table>
  <input name="username" type="hidden" value="<?php echo $row['username'];?>" />
  <input name="userid" type="hidden" value="<?php echo $user->userid;?>" />
</form>
<?php echo $core->doForm("processUser");?>
<?php break;?>
<?php case"add": ?>
<?php $memrow = $member->getMemberships();?>
<h1><img src="images/users-sml.png" alt="" /><?php echo _UR_TITLE2;?></h1>
<p class="info"><span><?php echo $core->langIcon();?></span><?php echo _UR_INFO2. _REQ1. required() . _REQ2;?></p>
<h2><?php echo _UR_SUBTITLE2;?></h2>
<form action="" method="post" id="admin_form" name="admin_form">
  <table cellspacing="0" cellpadding="0" class="formtable">
    <tr>
      <td width="200"><?php echo _USERNAME;?>: <?php echo required();?></td>
      <td><span id="getusername">
        <input name="username" type="text" class="inputbox required"  id="username" size="55" title="<?php echo _UR_USERNAME_R;?>"/>
        <img src="images/yes.png" alt="" id="yes" style="display:none" title="<?php echo _UR_USERAVAIL;?>" /> <img src="images/delete.png" alt="" id="no" style="display:none" title="<?php echo _UR_USERNOAVAIL;?>" /></span></td>
    </tr>
    <tr>
      <td><?php echo _PASSWORD;?>: <?php echo required();?></td>
      <td><input name="password" type="text" class="inputbox required" size="55" title="<?php echo _UR_PASSWORD_R;?>"/></td>
    </tr>
    <tr>
      <td><?php echo _UR_EMAIL;?>: <?php echo required();?></td>
      <td><input name="email" type="text" class="inputbox required" size="55" title="<?php echo _UR_EMAIL_R;?>"/></td>
    </tr>
    <tr>
      <td><?php echo _UR_FNAME;?>: <?php echo required();?></td>
      <td><input name="fname" type="text" class="inputbox required" title="<?php echo _UR_FNAME_R;?>" size="55" /></td>
    </tr>
    <tr>
      <td><?php echo _UR_LNAME;?>: <?php echo required();?></td>
      <td><input name="lname" type="text" class="inputbox required" title="<?php echo _UR_LNAME_R;?>" size="55" /></td>
    </tr>
    <tr>
      <td><?php echo _MEMBERSHIP;?>:</td>
      <td><select name="membership_id" class="select" style="width:200px">
          <option value="0"><?php echo _UR_NOMEMBERSHIP;?></option>
          <?php if($memrow):?>
          <?php foreach ($memrow as $mlist):?>
          <option value="<?php echo $mlist['id'];?>"><?php echo $mlist['title'];?></option>
          <?php endforeach;?>
          <?php unset($mlist);?>
          <?php endif;?>
        </select></td>
    </tr>
    <tr>
      <td><?php echo _UR_LEVEL;?>:</td>
      <td><span class="input-out">
        <label for="userlevel-1"><?php echo _UR_SADMIN;?></label>
        <input name="userlevel" type="radio" id="userlevel-1" value="9" />
        <label for="userlevel-2"><?php echo _UR_ADMIN;?></label>
        <input name="userlevel" type="radio" id="userlevel-2" value="8" />
        <label for="userlevel-3"><?php echo _USER;?></label>
        <input name="userlevel" type="radio" id="userlevel-3" value="1" checked="checked" />
        <?php echo tooltip(_UR_ADMIN_T);?></span></td>
    </tr>
    <tr>
      <td><?php echo _UR_PERM;?>:</td>
      <td><?php echo $user->getPermissionList();?></td>
    </tr>
    <tr>
      <td><?php echo _UR_STATUS;?>:</td>
      <td><span class="input-out">
        <label for="active-1"><?php echo _USER_A;?></label>
        <input name="active" type="radio" id="active-1" value="y" checked="checked" />
        <label for="active-2"><?php echo _USER_I;?></label>
        <input name="active" type="radio" id="active-2" value="n" />
        <label for="active-3"><?php echo _USER_B;?></label>
        <input name="active" type="radio" id="active-3" value="b" />
        <label for="active-4"><?php echo _USER_P;?></label>
        <input name="active" type="radio" id="active-4" value="t" />
        </span></td>
    </tr>
    <tr>
      <td><?php echo _UR_IS_NEWSLETTER;?>:</td>
      <td><span class="input-out">
        <label for="newsletter-1"><?php echo _YES;?></label>
        <input name="newsletter" type="radio" id="newsletter-1" value="1" />
        <label for="newsletter-2"><?php echo _NO;?></label>
        <input name="newsletter" type="radio" id="newsletter-2" value="0" checked="checked" />
        </span></td>
    </tr>
    <tr>
      <td><?php echo _UR_NOTIFY;?>:</td>
      <td><span class="input-out">
        <input type="checkbox" name="notify" value="1" />
        <?php echo tooltip(_UR_NOTIFY_T);?></span></td>
    </tr>
    <tr>
      <td><input type="submit" name="submit" class="button" value="<?php echo _UR_ADD;?>" /></td>
      <td><a href="loader.php?do=users" class="button-alt"><?php echo _CANCEL;?></a></td>
    </tr>
  </table>
</form>
<script type="text/javascript">
// <![CDATA[
$(document).ready(function() {
	$('#username').keyup(username_check);
});
function username_check() {
	var username = $('#username').val();
	if (username == "" || username.length < 4) {
		$('#yes').hide();
	} else {
		$.ajax({
			type: "POST",
			url: "ajax.php",
			data: 'checkUsername=' + username,
			cache: false,
			success: function(response) {
				if (response == 1) {
					$('#yes').hide();
					$('#no').fadeIn();
				} else {
					$('#no').hide();
					$('#yes').fadeIn();
				}

			}
		});
	}
}
// ]]>
</script> 
<?php echo $core->doForm("processUser");?>
<?php break;?>
<?php default:?>
<?php   
  $sort = (isset($_GET['sort'])) ? str_replace("-", " ", $_GET['sort']) : false;
  $userrow = $user->getUsers($sort);
?>
<h1><img src="images/users-sml.png" alt="" /><?php echo _UR_TITLE3;?></h1>
<p class="info"><span><?php echo $core->langIcon();?></span><?php echo _UR_INFO3;?></p>
<h2><span><a href="loader.php?do=users&amp;action=add" class="button-sml"><?php echo _UR_ADD;?></a></span><?php echo _UR_SUBTITLE3;?></h2>
<div class="box">
  <table cellpadding="0" cellspacing="0" class="formtable">
    <tr style="background-color:transparent">
      <td style="position:relative"><input name="search" type="text" class="inputbox" id="search-input" size="40" style="width:240px" onclick="disAutoComplete(this);"/>
        <div id="suggestions"></div></td>
      <td align="center"><form action="" method="post" id="dForm">
          <strong> <?php echo _UR_SHOW_FROM;?></strong>
          <input name="fromdate" type="text" style="margin-right:3px" class="inputbox" size="10" id="fromdate" />
          <strong> <?php echo _UR_SHOW_TO;?></strong>
          <input name="enddate" type="text" class="inputbox" size="10" id="enddate" />
          <input name="find" type="submit" class="button-sml" value="<?php echo _UR_FIND;?>" />
        </form></td>
      <td align="right"><form action="" method="get" name="filter_browse" id="filter_browse">
          <strong><?php echo _UR_USR_FILTER;?>:</strong>&nbsp;&nbsp;
          <select name="sort" onchange="if(this.value!='NA') window.location='loader.php?do=users&amp;sort='+this[this.selectedIndex].value; else window.location='loader.php?do=users';" style="width:180px">
            <option value="NA"><?php echo _UR_RESET_FILTER;?></option>
            <?php echo $user->getUserFilter();?>
          </select>
        </form></td>
    </tr>
    <tr style="background-color:transparent">
      <td colspan="2"><img src="images/u_active.png" alt="" title="<?php echo _USER_A;?>"/> <?php echo _USER_A;?> <img src="images/u_inactive.png" alt="" title="<?php echo _USER_I;?>"/> <?php echo _USER_I;?> <img src="images/u_pending.png" alt="" title="<?php echo _USER_P;?>"/> <?php echo _USER_P;?> <img src="images/u_banned.png" alt="" title="<?php echo _USER_B;?>"/> <?php echo _USER_B;?></td>
      <td align="right"><?php echo $pager->items_per_page();?> &nbsp;&nbsp;
        <?php if($pager->num_pages >= 1) echo $pager->jump_menu();?></td>
    </tr>
  </table>
</div>
<table cellpadding="0" cellspacing="0" class="display">
  <thead>
    <tr>
      <th width="20" class="left">#</th>
      <th class="left"><?php echo _USERNAME;?></th>
      <th class="left"><?php echo _UR_NAME;?></th>
      <th><?php echo _UR_STATUS;?></th>
      <th><?php echo _MEMBERSHIP;?></th>
      <th><?php echo _UR_LEVEL;?></th>
      <th><?php echo _UR_EDIT;?></th>
      <th><?php echo _DELETE;?></th>
    </tr>
  </thead>
  <tbody>
    <?php if($userrow == 0):?>
    <tr>
      <td colspan="8"><?php echo $core->msgAlert(_UR_NOUSER,false);?></td>
    </tr>
    <?php else:?>
    <?php foreach ($userrow as $row):?>
    <tr>
      <td><?php echo $row['id'];?>.</td>
      <td><a href="loader.php?do=newsletter&amp;emailid=<?php echo urlencode($row['email']);?>"><?php echo $row['username'];?></a></td>
      <td><?php echo $row['name'];?></td>
      <td align="center"><?php echo userStatus($row['active']);?></td>
      <td align="center"><?php if($row['membership_id'] == 0):?>
        --/--
        <?php else:?>
        <a href="loader.php?do=memberships&amp;action=edit&amp;id=<?php echo $row['mid'];?>"><?php echo $row['title'];?></a>
        <?php endif;?></td>
      <td align="center"><?php echo isAdmin($row['userlevel']);?></td>
      <td align="center"><a href="loader.php?do=users&amp;action=edit&amp;userid=<?php echo $row['id'];?>"><img src="images/edit.png" class="tooltip"  alt="" title="<?php echo _UR_EDIT;?>"/></a></td>
      <td align="center"><?php if($row['id'] == 1):?>
        <img src="images/delete.png" class="tooltip"  alt="" title="<?php echo _DELETE;?>"/>
        <?php else:?>
        <a href="javascript:void(0);" class="delete" rel="<?php echo $row['username'];?>" id="item_<?php echo $row['id'];?>"><img src="images/delete.png" class="tooltip"  alt="" title="<?php echo _DELETE;?>"/></a>
        <?php endif;?></td>
    </tr>
    <?php endforeach;?>
    <?php unset($row);?>
    <?php if($pager->items_total >= $pager->items_per_page):?>
    <tr style="background-color:transparent">
      <td colspan="8" style="padding:10px;"><div class="pagination"><span class="inner"><?php echo $pager->display_pages();?></span></div></td>
    </tr>
    <?php endif;?>
    <?php endif;?>
  </tbody>
</table>
<div id="dialog-confirm" style="display:none;" title="<?php echo _DELETE.' '._USER;?>">
  <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span><?php echo _DEL_CONFIRM;?></p>
</div>
<script type="text/javascript"> 
// <![CDATA[
$(document).ready(function () {
    $("#search-input").watermark("<?php echo _UR_FIND_UNAME;?>");
    $("#search-input").keyup(function () {
        var srch_string = $(this).val();
        var data_string = 'userSearch=' + srch_string;
        if (srch_string.length > 3) {
            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: data_string,
                beforeSend: function () {
                    $('#search-input').addClass('loading');
                },
                success: function (res) {
                    $('#suggestions').html(res).show();
                    $("input").blur(function () {
                        $('#suggestions').customFadeOut();
                    });
                    if ($('#search-input').hasClass("loading")) {
                        $("#search-input").removeClass("loading");
                    }
                }
            });
        }
        return false;
    });
    $('a.delete').live('click', function () {
        var id = $(this).attr('id').replace('item_', '')
        var parent = $(this).parent().parent();
        var title = $(this).attr('rel');
        $("#dialog-confirm").data({
            'delid': id,
            'parent': parent,
            'title': title
        }).dialog('open');
        return false;
    });
    $("#dialog-confirm").dialog({
        resizable: false,
        bgiframe: true,
        autoOpen: false,
        width: 400,
        height: "auto",
        zindex: 9998,
        modal: false,
        buttons: {
            '<?php echo _DELETE;?>': function () {
                var parent = $(this).data('parent');
                var id = $(this).data('delid');
                var title = $(this).data('title');
                $.ajax({
                    type: 'post',
                    url: "ajax.php",
                    data: 'deleteUser=' + id + '&username=' + title,
                    beforeSend: function () {
                        parent.animate({
                            'backgroundColor': '#FFBFBF'
                        }, 400);
                    },
                    success: function (msg) {
                        parent.fadeOut(400, function () {
                            parent.remove();
                        });
                        $("html, body").animate({
                            scrollTop: 0
                        }, 600);
                        $("#msgholder").html(msg);
                    }
                });
                $(this).dialog('close');
            },
            '<?php echo _CANCEL;?>': function () {
                $(this).dialog('close');
            }
        }
    });
});
$(function () {
    var dates = $('#fromdate, #enddate').datepicker({
        defaultDate: "+1w",
        changeMonth: false,
        numberOfMonths: 2,
        dateFormat: 'yy-mm-dd',
        onSelect: function (selectedDate) {
            var option = this.id == "fromdate" ? "minDate" : "maxDate";
            var instance = $(this).data("datepicker");
            var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
            dates.not(this).datepicker("option", option, date);
        }
    });
});
// ]]>
</script>
<?php break;?>
<?php endswitch;?>