<?php
/**
 * Created by JetBrains PhpStorm.
 * User: jrotering
 * Date: 7/5/13
 * Time: 6:19 PM
 * To change this template use File | Settings | File Templates.
 */
//dirname(__FILE__)
require_once(SCRIPT_DIR . 'db_connect.php');
require_once(SCRIPT_DIR . 'utils.php');

function getAccounts($tpl = null) {
    $results = getRows("SELECT * FROM `accounts` WHERE `active`=1;");
    if (!$tpl) {
        return $results;
    }
    else {
        $map = array('fields' => array(
            array('attr'=>'value',
                'display'=>'value="__field__" ',
                'field'=>'id'),
            array('attr'=>'text',
                'display'=>'__field__',
                'field'=>'name')
            )
        );
        return tplResults($results['results'], $tpl, $map);
    }
}
function getAccount($id) {
$results = getRow("SELECT * FROM `accounts` WHERE `id`=$id;");
        return $results;
}
function getAccountByName($name) {
    $results = getRow("SELECT * FROM `accounts` WHERE `name`='$name';");
    return $results;
}
function getAppointment($email) {
    $q = "SELECT * FROM `newcustomers` WHERE `email`='$email';";
    $results = getRow($q);
    $results['q'] = $q;
    return $results;
}
function getLocation($id, $acct = null) {
   // $results = getRow("SELECT * FROM `locations` WHERE `id`=$id AND `account_id`=$acct;");
    $results = getRow("SELECT * FROM `locations` WHERE `id`=$id;");
    return $results;
}
function getLocations($acct, $location = null, $tpl = null, $eltype = 'select') {
    if ($acct) {
        $results =  getRows("SELECT * FROM `locations` WHERE `account_id` = $acct;");
        if (!$tpl) {
            return $results;
        }
        else {
            $map = array('fields' => array(
                array('attr'=>'value',
                    'display'=>'value="__field__" ',
                    'field'=>'id'),
                array('attr'=>'text',
                    'display'=>'__field__',
                    'field'=>'name')
            )
            );
            if ($eltype) {
                $map['element'] = $eltype;
            }
            return tplResults($results['results'], $tpl, $map, $location);
        }
    }
    else {
        return '<!--account not set-->';
    }
}
function getPromotion($id, $acct) {
    $results = getRow("SELECT * FROM `promotions` WHERE `id`=$id AND `account_id`=$acct;");
    return $results;
}
function getPromotions($acct, $tpl = null, $eltype = 'select') {
    if ($acct) {
        $results =  getRows("SELECT * FROM `promotions` WHERE `account_id` = $acct;");
        if (!$tpl) {
            return $results;
        }
        else {
            $map = array('fields' => array(
                array('attr'=>'value',
                    'display'=>'value="__field__" ',
                    'field'=>'id'),
                array('attr'=>'text',
                    'display'=>'__field__',
                    'field'=>'name')
            )
            );
            if ($eltype) {
                $map['element'] = $eltype;
            }
            return tplResults($results['results'], $tpl, $map);
        }
    }
    else {
        return '<!--account not set-->';
    }
}
function getServices($acct, $service = null, $tpl = null, $eltype = 'select') {
    $q = "SELECT * FROM `services` WHERE `account_id` = $acct;";
    $results =  getRows($q);
    if (!$tpl) {
        return $results;
    }
    else {
        $map = array('fields' => array(
            array('attr'=>'value',
                'display'=>'value="__field__" ',
                'field'=>'id'),
            array('attr'=>'text',
                'display'=>'__field__',
                'field'=>'name')
        )
        );
        if ($eltype) {
            $map['element'] = $eltype;
        }
        return tplResults($results['results'], $tpl, $map, $service);
    }
}

function getServiceName($id) {
    $results = getRow("SELECT * FROM `services` WHERE `id`=$id;");
    if (isset($results['results'])) {
        return $results['results']['name'];
    }
    else {
        if ($results['error']) {
            return $results['error'];
        }
    }
}
function getStylistName($id) {
    $results = getRow("SELECT * FROM `staff` WHERE `id`=$id;");
    if (isset($results['results'])) {
        return $results['results']['name'];
    }
    else {
        if ($results['error']) {
            return $results['error'];
        }
    }
}

function getStaff($location, $staff = null, $tpl = null, $eltype = 'select') {
    if ($location) {
        $q = "SELECT * FROM `staff` WHERE `location_id` = $location;";
        $results =  getRows($q);
        if (!$tpl) {
            return $results;
        }
        else {
            $map = array('fields' => array(
                array('attr'=>'value',
                    'display'=>'value="__field__" ',
                    'field'=>'id'),
                array('attr'=>'text',
                    'display'=>'__field__',
                    'field'=>'name')
            )
            );
            if ($eltype) {
                $map['element'] = $eltype;
            }
            return tplResults($results['results'], $tpl, $map, $staff);
        }
    }


}


function getRows($query) {
    //echo $query;
    global $link;
    $return = array();
    $return['query'] = $query;
    if ($res = mysql_query($query, $link)) {
        $return['results'] = array();
        while ($row = mysql_fetch_assoc($res)) {
            $return['results'][] = $row;
        }
    }
    else {
        $return['error'] = "Query Error: " . mysql_error();
    }
    return $return;
}
function getRow($query) {
    global $link;
    $return = array();
    $return['query'] = $query;
    if ($res = mysql_query($query, $link)) {
        $return['results'] = array();
        $count = mysql_num_rows($res);
        if ( $count == 1) {
            $return['results'] =  mysql_fetch_assoc($res);
        }
        else {
            $return['error'] = "getRow Query Error: expected one result, $count results found.";
        }
    }
    else {
        $return['error'] = "getRow Query Error: " . mysql_error();
    }
    return $return;
}