<?php

/* 

Zingtree API PHP wrapper for REST API 
Just pass in a session ID or an API Key, and get JSON data back.

Usage:

// adds an agent to your organization
zt_agent_add($apikey, $agent_name, $agent_login) ;

// sets or updates tags for an agent
zt_agent_tag($apikey, $agent_login, $tags) ;

// removes an agent from your organization
zt_agent_remove($apikey, $agent_login) ;

// return all sessions for a particular agent and date range
// if agent ='*', return for all agents
// $start_date and $end_date are in format YYYY-MM-DD, i.e. '2017-07-12'
zt_agent_sessions($apikey, $agent, $start_date, $end_date) ;

// return all form data collected during a session
zt_get_form_data($session_id) ;

// deletes all form data collected during a session
zt_delete_form_data($session_id) ;

// return all session data, including metadata and click path
zt_get_session_data($session_id) ;

// return all session data, including metadata and click path. Removes "back" and "restart" operations, returning the "pure" path through the tree.
zt_get_session_data_pure($session_id) ;

// returns notes for a session
zt_get_session_notes($session_id) ;

// return all sessions for a particular tree ID and date range
// if tree_id ='*', return for all trees
// $start_date and $end_date are in format YYYY-MM-DD, i.e. '2017-07-12'
zt_tree_sessions($apikey, $tree_id, $start_date, $end_date) ;

// return all tags for every tree in your organization
zt_get_tags($apikey) ;

// return all trees for your oranization
zt_get_trees($apikey) ;

// return all trees and nodes in your organization matching search_text
zt_search_trees($apikey, $search_text) ;

// return all trees that contain ANY of the tags in $taglist, $taglist is case sensitive, and comma delimited.
zt_get_tree_tag_any($apikey, $taglist) ;

// return all trees that contain ALL of the tags in $taglist, $taglist is case sensitive, and comma delimited.
zt_get_tree_tag_all($apikey, $taglist) ;

// get the event log for an organization for a date range
zt_event_log($apikey, $start_date, $end_date) ;

// delete an uploaded file from the Zingtree servers
function zt_delete_file_upload($folder, file) ;

/ return the basic structure of a tree
function zt_get_tree_structure($apikey, $tree_id) ;

// return the variables used or defined in a tree
function zt_get_tree_variables($apikey, $tree_id) ;

*/


// execute a request

function zt_exec($params)
{
    // endpoint    
    $url = "https://zingtree.com/api/rest/zingtree.php";

    $query = http_build_query($params);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $result = curl_exec($ch);
    curl_close($ch);

    return $result;
}


// adds an agent to your organization

function zt_agent_add($apikey, $agent_name, $agent_login)
{
    $params = ['op' => 'agent_add', 'apikey' => $apikey, 'agent_name' => $agent_name, 'agent_login' => $agent_login];
    return (json_decode(zt_exec($params)));
}


// updates agent tags

function zt_agent_tag($apikey, $agent_login, $tags)
{
    $params = ['op' => 'agent_remove', 'apikey' => $apikey, 'agent_login' => $agent_login, 'tags' => $tags];
    return (json_decode(zt_exec($params)));
}

// removes an agent from your organization

function zt_agent_remove($apikey, $agent_login)
{
    $params = ['op' => 'agent_remove', 'apikey' => $apikey, 'agent_login' => $agent_login];
    return (json_decode(zt_exec($params)));
}


// return all sessions for a particular agent and date range
// if agent blank, return for all agents
// $start_date and $end_date are in format YYYY-MM-DD, i.e. '2015-12-30'. If blank, returns sessions for last 30 days.

function zt_agent_sessions($apikey, $agent = '', $start_date = '', $end_date = '')
{
    $params = ['op' => 'agent_sessions', 'apikey' => $apikey, 'agent' => $agent, 'start_date' => $start_date, 'end_date' => $end_date];
    return (json_decode(zt_exec($params)));
}


// returns all form data collected during a session

function zt_get_form_data($session_id)
{
    $params = ['op' => 'get_form_data', 'session_id' => $session_id];
    return (json_decode(zt_exec($params)));
}


// delete an uploaded file from the Zingtree servers

function zt_delete_file_upload($folder, $file)
{
    $params = ['op' => 'delete_file_upload', 'folder' => $folder, 'file' => $file];
    return (json_decode(zt_exec($params)));
}

// deletes all form data collected during a session

function zt_delete_form_data($session_id)
{
    $params = ['op' => 'delete_form_data', 'session_id' => $session_id];
    return (json_decode(zt_exec($params)));
}


// returns all session data, including metadata and click path

function zt_get_session_data($session_id)
{
    $params = ['op' => 'get_session_data', 'session_id' => $session_id];
    return (json_decode(zt_exec($params)));
}


// returns all session data, including metadata and click path
// returns a "pure" linear path throug hthe tree, eliminating BACK and RESTART operations

function zt_get_session_data_pure($session_id)
{
    $params = ['op' => 'get_session_data_pure', 'session_id' => $session_id];
    return (json_decode(zt_exec($params)));
}

// returns all session data, including metadata and click path

function zt_get_session_notes($session_id)
{
    $params = ['op' => 'get_session_notes', 'session_id' => $session_id];
    return (json_decode(zt_exec($params)));
}


// returns all trees in your organization

function zt_get_trees($apikey)
{
    $params = ['op' => 'get_trees', 'apikey' => $apikey];
    return (json_decode(zt_exec($params)));
}


// returns all tags used by trees in your organization

function zt_get_tags($apikey)
{
    $params = ['op' => 'get_tags', 'apikey' => $apikey];
    return (json_decode(zt_exec($params)));
}


// return all trees and nodes in your organization matching search_text

function zt_search_trees($apikey, $search_text)
{
    $params = ['op' => 'search_trees', 'apikey' => $apikey, 'search' => $search_text];
    return (json_decode(zt_exec($params)));
}


// return all trees that have ANY tag in $taglist. $taglist is case sensitive, and separated by commas.

function zt_get_tree_tag_any($apikey, $taglist)
{
    $params = ['op' => 'get_tree_tag_any', 'apikey' => $apikey, 'tags' => $taglist];
    return (json_decode(zt_exec($params)));
}


// return all trees that have ANY tag in $taglist. $taglist is case sensitive, and separated by commas.

function zt_get_tree_tag_all($apikey, $taglist)
{
    $params = ['op' => 'get_tree_tag_all', 'apikey' => $apikey, 'tags' => $taglist];
    return (json_decode(zt_exec($params)));
}


// return all sessions for a particular tree ID and date range
// if tree_id is blank, return for all trees
// $start_date and $end_date are in format YYYY-MM-DD, i.e. '2015-12-30'. If blank, returns sessions for last 30 days.

function zt_tree_sessions($apikey, $tree_id = '', $start_date = '', $end_date = '')
{
    $params = ['op' => 'tree_sessions', 'apikey' => $apikey, 'tree_id' => $tree_id, 'start_date' => $start_date, 'end_date' => $end_date];
    return (json_decode(zt_exec($params)));
}

// return all sessions for a particular tree ID and date range (for last click)
// if tree_id is blank, return for all trees
// $start_date and $end_date are in format YYYY-MM-DD, i.e. '2015-12-30'. If blank, returns sessions for last 30 days.

function zt_tree_sessions_last_clicked($apikey, $tree_id = '', $start_date = '', $end_date = '')
{
    $params = ['op' => 'tree_sessions_last_clicked', 'apikey' => $apikey, 'tree_id' => $tree_id, 'start_date' => $start_date, 'end_date' => $end_date];
    return (json_decode(zt_exec($params)));
}


// get the event log for an organization for a date range

function zt_event_log($apikey, $start_date, $end_date)
{
    $params = ['op' => 'event_log', 'apikey' => $apikey, 'start_date' => $start_date, 'end_date' => $end_date];
    return (json_decode(zt_exec($params)));
}


// return the basic structure of a tree

function zt_get_tree_structure($apikey, $tree_id)
{
    $params = ['op' => 'get_tree_structure', 'apikey' => $apikey, 'tree_id' => $tree_id];
    return (json_decode(zt_exec($params)));
}


// return the variables used or defined in a tree

function zt_get_tree_variables($apikey, $tree_id)
{
    $params = ['op' => 'get_tree_variables', 'apikey' => $apikey, 'tree_id' => $tree_id];
    return (json_decode(zt_exec($params)));
}


?>