<?php

function plugin_deletependingusers_variables()
{
    $variables = array(
        'Plugin Name'   => array(
            'type'          => 'hidden',
            'description'   => '',
            'value'         => 'Delete Pending Users',
        ),
        'Enabled'       => array(
            'type'          => 'yesno',
            'description'   => 'Erases pending users after the amount of days selected without being approved.',
            'value'         => '0',
        ),
        'Amount of days'    => array(
            'type'          => 'text',
            'description'   => 'Set the amount of days before deleting a pending user from the system',
            'value'         => '30',
        ),
        'Run schedule - Minute'  => array(
            'type'          => 'text',
            'description'   => 'Enter number, range, list or steps',
            'value'         => '30',
            'helpid'        => '8',
        ),
        'Run schedule - Hour'  => array(
            'type'          => 'text',
            'description'   => 'Enter number, range, list or steps',
            'value'         => '01',
        ),
        'Run schedule - Day'  => array(
            'type'          => 'text',
            'description'   => 'Enter number, range, list or steps',
            'value'         => '*',
        ),
        'Run schedule - Month'  => array(
            'type'          => 'text',
            'description'   => 'Enter number, range, list or steps',
            'value'         => '*',
        ),
        'Run schedule - Day of the week'  => array(
            'type'          => 'text',
            'description'   => 'Enter number in range 0-7 (0 is Sunday) or a 3 letter shortcut (e.g. sun)',
            'value'         => '*',
        ),
    );

    return $variables;
}

function getUsersWithStatus($status = '')
{
    $query = "SELECT id, UNIX_TIMESTAMP(dateactivated), status FROM users WHERE status =?";
    $result = $GLOBALS['db']->query($query,$status);
    return $result;
}

function secondsToDays($seconds)
{
    return $seconds/(60*60*24);
}

function getUsersToDelete()
{
    global $gSettingsArray;

    $arrayUsersToDelete = array();
    $daysToDeleteUser = $gSettingsArray['plugin_deletependingusers_Amount of days'];
    $result = getUsersWithStatus(0);
    $num_rows = $result->getNumRows();
    if($num_rows > 0){
        $tempActualDate = strtotime(date('Y-m-d'));
        while(list($id, $dateactivated, $status) = $result->fetch()){
            if($status == 0){
                $diffdate = $tempActualDate - $dateactivated;
                $diffdate = secondsToDays($diffdate);
                if($diffdate >= $daysToDeleteUser){
                    $arrayUsersToDelete[] = $id;
                }
            }
        }
    }
    return $arrayUsersToDelete;
}

function plugin_deletependingusers_execute()
{
    require_once 'classes/user.php';
    $arrayUsersToDelete = getUsersToDelete();
    foreach($arrayUsersToDelete as $userid) {
        $objUser =& new User($userid);
        $objUser->delete(false);
    }
    return array(count($arrayUsersToDelete)." user(s) deleted");
}

function plugin_deletependingusers_output()
{
    global $tpl;

    $tpl->define(array('deletependingusers_table' => 'views/admin/deletependingusers_table.tpl'));
    $rowclass="odd";
    $usersToDelete = getUsersToDelete();
    $queueEmpty = true;
	 if($usersToDelete){
	    $query = "SELECT firstname, lastname, organization FROM users WHERE id IN (".implode(",", $usersToDelete).")";
	    $result = $GLOBALS['db']->query($query);
	    
	    while (list($firstname, $lastname, $organization) = $result->fetch()) {
	
	        $tpl->setkey(array(
	            'trClass'                               => $rowclass,
	            'deletependingusers_name'               => $firstname,
	            'deletependingusers_lastname'           => $lastname,
	            'deletependingusers_organization'       => $organization
	        ));
	
	        $queueEmpty = false;
	
	        //alternate row color
	        if ($rowclass=="odd") $rowclass="even";
	        else $rowclass="odd";
	
	        $tpl->parse('trash', '.deletependingusers');
	    }
    }
    if ($queueEmpty) {
        $tpl->setkey('NOUSERS', "<tr><td colspan=\"5\" align=\"center\" style=\"font-style:italic\">".lang('There are no users to be deleted')."</td></tr>");
    } else {
        $tpl->setkey('NOUSERS', '');
    }

    $tpl->parse('output', 'deletependingusers_table');

    return $tpl->fetch('output');
}

function plugin_deletependingusers_dashboard()
{
    $daysToDeleteUser = isset($gSettingsArray['plugin_deletependingusers_Amount of days']) ? $gSettingsArray['plugin_deletependingusers_Amount of days']*24*60*60 : 0;
	$query = "SELECT COUNT(id) FROM users WHERE status = ? AND NOW() - UNIX_TIMESTAMP(dateactivated) >= $daysToDeleteUser";
    $result = $GLOBALS['db']->query($query,0);
	list($numberOfUsersToDelete) = $result->fetch();
    return lang('Pending users to be deleted on next run: %d', $numberOfUsersToDelete);
}

?>
