<?php
/**
 *  Copyright (C) 2022, 2024  Double Bastion LLC
 *
 *  This file is part of Roundpin, which is licensed under the
 *  GNU Affero General Public License Version 3.0. The license terms
 *  are detailed in the "LICENSE.txt" file located in the root directory.
 */

session_start();

if (isset($_POST['s_ajax_call']) && ($_POST['s_ajax_call'] == $_SESSION['validate_s_access'])) {

    define('ACCESSCONST', TRUE);

    require('db-connect.php');

    $username = $_POST['username'];
    $csvDelimiter = $_POST['csvdelimiter'];
    $selectedGroups = $_POST['selectedgroups'];
    $selectedGroupsArr = explode("|", $selectedGroups);
    array_shift($selectedGroupsArr);
    array_pop($selectedGroupsArr);   

    // Get the id of the current user from the 'app_users' table
    $queryid = $mysqli->prepare("SELECT id, username FROM app_users WHERE BINARY username = ?");
    $queryid->bind_param("s", $username);
    $queryid->execute();
    $queryres = $queryid->get_result()->fetch_assoc();
    $userID = $queryres['id'];

    // Get the contacts for the current user
    $querycont = $mysqli->prepare("SELECT id, user_id, contact_name, contact_desc, contact_long_desc, address_street, address_zip, address_town, address_country, address_state, 
                                   extension_number, contact_mobile, contact_num1, contact_num2, contact_fax, contact_email, profile_picture_c, groups, date_added, date_modified 
                                   FROM contacts WHERE user_id = ?");
    $querycont->bind_param("i", $userID);
    $querycont->execute();
    $queryres = $querycont->get_result();

    $contactsarr = [];
    while ($querycontres = $queryres->fetch_assoc()) {

           $contactGroupsArr = explode("|", $querycontres['groups']);
           array_shift($contactGroupsArr);
           array_pop($contactGroupsArr);

           $gck = 0;
           foreach ($contactGroupsArr as $key => $arrval) {
                 if (in_array($arrval, $selectedGroupsArr)) { $gck++; }
           }

           if ($gck > 0) {

               $contactsarr[] = ['contact_name' => $querycontres['contact_name'], 'contact_desc' => $querycontres['contact_desc'],
                                 'contact_long_desc' => $querycontres['contact_long_desc'], 'address_street' => $querycontres['address_street'], 
                                 'address_zip' => $querycontres['address_zip'], 'address_town' => $querycontres['address_town'], 'address_country' => $querycontres['address_country'],
                                 'address_state' => $querycontres['address_state'], 'extension_number' => $querycontres['extension_number'],
                                 'contact_mobile' => $querycontres['contact_mobile'], 'contact_num1' => $querycontres['contact_num1'], 'contact_num2' => $querycontres['contact_num2'],
                                 'contact_fax' => $querycontres['contact_fax'], 'contact_email' => $querycontres['contact_email'], 
                                 'profile_picture_c' => $querycontres['profile_picture_c'], 'groups' => $querycontres['groups'], 'date_added' => $querycontres['date_added'], 
                                 'date_modified' => $querycontres['date_modified']];

               $messagetosend = 'success';

               $crntRow = implode("", $querycontres);
               if (strpos($crntRow, $csvDelimiter) !== false) { $messagetosend = 'delimiterfound'; break; }
           }

    }

    $contactsfromdb = ['contactsdata' => $contactsarr, 'messagetoconvey' => $messagetosend];

    echo json_encode($contactsfromdb);

} else {
        header("Location: ../login.php");
}

?>
