<?php
/*
 * Name:      Timesheet
 * Directory: timesheet
 * Version:   1.0.1
 * Type:      user
 * UI Name:   Timesheet
 * UI Icon:   timesheet.png
 */

/*
 * Copyright (C) 2007, M2X
 *
 * Authors: Jean-Paul Saman
 *
 * This file is based upon configure.php from HelpDesk dotProject module
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

// Deny all but system admins
if (getDenyEdit('system')) {
    $AppUI->redirect( "m=public&a=access_denied" );
}

@include_once( "./functions/admin_func.php" );

$CONFIG_FILE = "./modules/timesheet/config.php";

$AppUI->savePlace();

// Pull all users
$q = new DBQuery;
$q->addQuery('user_id, contact_first_name, contact_last_name');
$q->addTable('users');
$q->addTable('contacts');
$q->addWhere('user_contact = contact_id');
$q->addOrder('contact_last_name, contact_first_name');
$q->exec();
$users = array();
while ( $row = $q->fetchRow()) {
  $users[$row['user_id']] = $row['contact_last_name'] . ', ' . $row['contact_first_name'];
}

/* All config options, their descriptions and their default values are defined
* here. Add new config options here. Type can be "checkbox", "text", "radio" or
* "select". If the type is "radio," it must include a set of buttons. If it's
* "select" then be sure to include a 'list' entry with the options.  if the key
* starts with headingXXX then it will just display the contents on the value.
* This is used for grouping.
*/
$config_options = array(
    "heading1" => $AppUI->_('Timesheet rights'),
    "items_per_page" => array(
        "description" => $AppUI->_('Approval by'),
        "value" => '',
        'type' => 'select',
        'list' => $users
    )
);

//if this is a submitted page, overwrite the config file.
if(dPgetParam( $_POST, "Save", '' )!='')
{

    if (is_writable($CONFIG_FILE)) {
        if (!$handle = fopen($CONFIG_FILE, 'w')) {
            $AppUI->setMsg( $CONFIG_FILE." ".$AppUI->_('cannot be opened'), UI_MSG_ERROR );
            exit;
        }

        if (fwrite($handle, "<?php //Do not edit this file by hand, it will be overwritten by the configuration utility. \n") === FALSE) {
            $AppUI->setMsg( $CONFIG_FILE." ".$AppUI->_('cannot be written to'), UI_MSG_ERROR );
            exit;
        } else {
            foreach ($config_options as $key=>$value){
              if(substr($key,0,7)=='heading') continue;

                $val="";
                switch($value['type']){
                    case 'checkbox': 
                        $val = isset($_POST[$key])?"1":"0";
                        break;
                    case 'text': 
                        $val = isset($_POST[$key])?$_POST[$key]:"";
                        break;
                    case 'select': 
                        $val = isset($_POST[$key])?$_POST[$key]:"0";
                        break;
                    case 'radio':
                        $val = $_POST[$key];
                        break;
                    default:
                        break;
                }
                fwrite($handle, "\$TIMESHEET_CONFIG['".$key."'] = '".$val."';\n");
            }

            fwrite($handle, "?>\n");
            $AppUI->setMsg( $CONFIG_FILE." ".$AppUI->_('has been successfully updated'), UI_MSG_OK );
            fclose($handle);
            require( $CONFIG_FILE );
        }
    } else {
        $AppUI->setMsg( $CONFIG_FILE." ".$AppUI->_('is not writable'), UI_MSG_ERROR );
    }
} else if(dPgetParam( $_POST, $AppUI->_('cancel'), '' )!=''){
    $AppUI->redirect("m=system&a=viewmods");
}

//$HELPDESK_CONFIG = array();
require_once( $CONFIG_FILE );

//Read the current config values from the config file and update the array.
foreach ($config_options as $key=>$value){
    if(isset($TIMESHEET_CONFIG[$key])){

        $config_options[$key]['value']=$HELPDESK_CONFIG[$key];
    }
}

// setup the title block
$titleBlock = new CTitleBlock( 'Configure Timesheet Module', 'timesheet.png', $m, "$m.$a" );
$titleBlock->addCrumb( "?m=system", "System Admin" );
$titleBlock->addCrumb( "?m=system&a=viewmods", "Modules" );
$titleBlock->show();

?>

<form method="post">
<table class="std">
<?php
foreach ($config_options as $key=>$value){
?>
    <tr>
        <?php
    // the key starts with hr, then just display the value
      if(substr($key,0,7)=='heading'){ ?>
          <th align="center" colspan="2"><?=$value?></th>
        <?php } else { ?>
        <td align="right"><?=$value['description']?></td>
        <td><?php
      switch($value['type']){
        case 'checkbox': ?>
          <input type="checkbox" name="<?=$key?>" <?=$value['value']?"checked=\"checked\"":""?>>
          <?php
          break;
        case 'text': ?>
          <input type="text" name="<?=$key?>" value="<?=$value['value']?>">
          <?php
          break;
        case 'select': 
          print arraySelect( $value["list"], $key, 'class="text" size="1" id="' . $key . '" ' . $value["events"], $value["value"] );
          break;
        case 'radio':
          foreach ($value['buttons'] as $v => $n) {?>
            <label><input type="radio" name="<?php echo $key; ?>" id="<?php echo $key; ?>" value=<?php echo $v; ?> <?php echo (($value['value'] == $v)?"checked":""); ?> <?php echo $value['events']; ?>> <?php echo $n;?></label>
          <?php }
          break;
        default:
          break;
      }
        ?></td>
        <?php
            }
        ?>
    </tr>
<?php
}
?>
    <tr>
        <td colspan="2" align="right"><input type="Submit" name="Cancel" value="<?=$AppUI->_('cancel')?>">
                                  <input type="Submit" name="Save" value="<?=$AppUI->_('save')?>"></td>
    </tr>
</table>
</form>