<?php

/*
 *
 * ZENDESK - UPLOAD PHOTOS
 * IMPORT PROFILE PHOTO FOR ALL USERS OF A SAME ORGANIZATION
 *
 */
 
// Utility functions
require_once("../shared/functions.php");

// Zendesk Connect
require("config.Zendesk.inc.php");

$updated = $error = $ignored = 0;

if(isset($_POST['organizationid']) && $_POST['organizationid'] > 0) { $organizationid = $_POST['organizationid']; }
else $organizationid = 0; // 6690389009


if(isset($_GET['debug']) && $_GET['debug'] > 0) { $debug = $_GET['debug']; }
else $debug = 0;

if(isset($organizationid) && (int) $organizationid > 0 && isset($_FILES) && $_FILES['inputfile']['error'] == 0) {

    // GET USER FROM ORGANIZATION
    $queryParameters = array();

    if($organizationid == 4549269429) {
        // If HiPay, do pagination
        $maxpage = 20;
    }
    else {
        $maxpage = 1;
    }

    for($page = 1; $page < $maxpage; $page++) {


        // Create cURL options array
        $options = array(
                CURLOPT_URL => $resource."organizations/$organizationid/users.json?page=$page",
                CURLOPT_USERPWD => $username."/token:$token",
                CURLOPT_RETURNTRANSFER =>true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "GET",
        );

        // Zendesk Curl
        require("curl.Zendesk.inc.php");

        $result = json_decode($result);
        krumo($result);
        if(isset($result->users) && isset($result->count) && $result->count > 0) {

            // Process uploaded Photo
            $dir = "/home/jabella/public_html/tools/zendesk/photos";
            $ext = end((explode(".", $_FILES['inputfile']['name'])));
            move_uploaded_file($_FILES['inputfile']['tmp_name'], "$dir/$organizationid.$ext" ); //Move the file to desired directory.
            $photo = "$dir/$organizationid.$ext";

            // Store user list
            $users = array();
            $users = $result->users;

        }
        else echo "No users found";


        // UPDATE PHOTOS ON EACH USER FOUND
        if(isset($users) && is_array($users)) {

            foreach($users as $key => $user) {

                $cmd='curl -v -u '.$username.'/token:'.$token.' -X PUT -F "user[photo][uploaded_data]=@'.$photo.'" "'.$resource.'users/'.$user->id.'.json" -0' ;
                    exec($cmd,$curlresult);

                    if(isset($debug) && $debug) { echo "<h3>Response:</h3>"; krumo($curlresult); }

                    if(isset($curlresult) && is_array($curlresult) && isset($curlresult[0])) {
                        $result = json_decode($curlresult[0]);

                        if(isset($result->user->id) && $result->user->id > 0) {
                            echo "<p>".$user->name."</p>";
                            $updated++;
                        }
                        else $error++;
                    }
                    else $error++;
            }
            //else $ignored++;
        }

    }

    echo "<p>updated = $updated - error = $error - ignored = $ignored</p>";
}

?>
<html lang="en">
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <meta charset="utf-8">
        <title>User Profile Photos | Zendesk Import</title>
        
        <script src="//code.jquery.com/jquery-3.1.0.min.js" integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s=" crossorigin="anonymous"></script>
    
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

        <!-- Optional theme -->
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

        <!-- Latest compiled and minified JavaScript -->
        <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    </head>
    <body>

        <div class="container">

        <h2>Upload a photo to Zendesk profiles</h2>
        <form method="post" action="" enctype="multipart/form-data">
            <p><label for="organizationid">Zendesk Organization ID</label>
            <input type="text" id="organizationid" name="organizationid" style="width:200px;" value="" placeholder="4549269429" /><small>HiPay ID is 4549269429</small></small></p>
            <p><label for="inputfile">Photo to insert</label>
            <input type="file" id="inputfile" name="inputfile" style="width:300px;"/></p>
            <p><input type="submit" value="Upload" />
            <input type="hidden" name="action" value="1" /></p>
        </form>

        </div>
        
    </body>
</html>