<?php

function getArrayFromDBTable($DBTableRes) {
    $tmpArr = array();

    if(mysqli_num_rows($DBTableRes) > 0){
        while( $row = mysqli_fetch_assoc($DBTableRes) ){
            $tmpArr[] = $row;
        }
    }

    return $tmpArr;

}

function sendReminderEmail() {

}

$usersList5Sql = "SELECT rmain.statusDate, rmain.statusValue, rmain.userGuid, rdata.userPhone FROM reminders rmain JOIN remindersdata rdata ON rmain.userGuid = rdata.userId WHERE DATE(rmain.statusDate) = DATE_SUB(CURDATE(), INTERVAL 5 DAY)";


//DB Test server section
$dbHost = 'localhost';
$dbUsername = 'h4053_smartuser1';
$dbPassword = 'Denmark92';
$dbName = 'h4053_smartresponsesites';

//Create connection and select DB
$conn = mysqli_connect($dbHost, $dbUsername, $dbPassword, $dbName);

if (mysqli_connect_errno()) {
    die("Unable to connect database: " . mysqli_connect_errno());
}

if (!mysqli_set_charset($conn, "utf8")) {
    printf("Encoding error utf8: %s\n", mysqli_error($conn));
    exit();
}

$to = 'zascia@ukr.net';
$subject = 'Reminder test';
$message = "";
$headers = 'From: zascia@ukr.net' . "rn" .
           'Reply-To: zascia@ukr.net';

$result = mysqli_query($conn, $usersList5Sql);
$resultArray = getArrayFromDBTable($result);

mysqli_close($conn);

if (empty($resultArray)) {
    $message = "No rows affected by the query";
} else {
    // main logic for newrows is here
    foreach ($resultArray as $key => $resultArrayRow) {
        $message .= $key . ": " . $resultArrayRow['userGuid'] . " || " . $resultArrayRow['userPhone'] . "; ";
    }

}

/* mail for test purposes only, later to change by SMS inmobile API */
@mail($to, $subject, $message, $headers);

// transactional email functionality
$named_array = array(
    'fromField' => array(
        'fromFieldId' => 'V'
    ),
    'replyTo' => array(
        'fromFieldId' => 'V'
    ),
    'subject' => 'Order Confirmation - Example Shop',
    'tag' => array(
        'tagId' => 'vBd5'
    ),
    'content' => array(
        'plain' => 'Your order has been confirmed. Thank you for shopping with us!',
        'html' => '<h1>Your order has been confirmed</h1><strong>Thank you for shopping with us!</strong>'
    ),
    'recipients' => array(
        'to' => array(
            'email' => 'john.doe@example.com',
            'name' => 'John Doe',
            'validSince' => '2018-05-02T09:30:43+0200'
        ),
        'cc' => array(
            array(
                'email' => 'john.doe@example.com',
                'name' => 'John Doe'
            )
        )
    ),
    'attachments' => array(
        array(
            'fileName' => 'pixel.png',
            'mimeType' => 'image/png',
            'content' => 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mOUua1dDwAD4AGjnrXmUAAAAABJRU5ErkJggg=='
        )
    )
);

/* EO mail for test purposes only, later to change by SMS inmobile API */

?>