From 7c631452f557854c6605446b07de584d742fb0d7 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Wed, 25 Jul 2012 11:58:57 -0400
Subject: [PATCH] Add ajax suppor to client interface - needed to fetch
 multifile config.

---
 ajax.php                | 34 +++++++++++++++++++++++++
 include/ajax.config.php | 17 +++++++++++--
 js/osticket.js          | 56 ++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 104 insertions(+), 3 deletions(-)
 create mode 100644 ajax.php

diff --git a/ajax.php b/ajax.php
new file mode 100644
index 000000000..71f9494de
--- /dev/null
+++ b/ajax.php
@@ -0,0 +1,34 @@
+<?php
+/*********************************************************************
+    ajax.php
+
+    Ajax utils for client interface.
+
+    Peter Rotich <peter@osticket.com>
+    Copyright (c)  2006-2012 osTicket
+    http://www.osticket.com
+
+    Released under the GNU General Public License WITHOUT ANY WARRANTY.
+    See LICENSE.TXT for details.
+
+    vim: expandtab sw=4 ts=4 sts=4:
+**********************************************************************/
+
+function clientLoginPage($msg='Unauthorized') {
+    Http::response(403,'Must login: '.Format::htmlchars($msg));
+    exit;
+}
+
+require('client.inc.php');
+
+if(!defined('INCLUDE_DIR'))	Http::response(500,'config error');
+require_once INCLUDE_DIR.'/class.dispatcher.php';
+require_once INCLUDE_DIR.'/class.ajax.php';
+
+$dispatcher = patterns('',
+    url('^/config/', patterns('ajax.config.php:ConfigAjaxAPI',
+        url_get('^client', 'client')
+    ))
+);
+print $dispatcher->resolve($_SERVER['PATH_INFO']);
+?>
diff --git a/include/ajax.config.php b/include/ajax.config.php
index 5a60bc459..feb1eb4b0 100644
--- a/include/ajax.config.php
+++ b/include/ajax.config.php
@@ -20,14 +20,27 @@ class ConfigAjaxAPI extends AjaxController {
 
     //config info UI might need.
     function scp() {
-        global $thisstaff, $cfg;
+        global $cfg;
 
-        $config=array('lock_time'       => ($cfg->getLockTime()*3600),
+        $config=array(
+                      'lock_time'       => ($cfg->getLockTime()*3600),
                       'file_types'      => $cfg->getAllowedFileTypes(),
                       'max_file_size'   => (int) $cfg->getMaxFileSize(),
                       'max_file_uploads'=> (int) $cfg->getStaffMaxFileUploads()
                       );
         return $this->json_encode($config);
     }
+
+    function client() {
+        global $cfg;
+
+        $config=array(
+                      'file_types'      => $cfg->getAllowedFileTypes(),
+                      'max_file_size'   => (int) $cfg->getMaxFileSize(),
+                      'max_file_uploads'=> (int) $cfg->getClientMaxFileUploads()
+                      );
+
+        return $this->json_encode($config);
+    }
 }
 ?>
diff --git a/js/osticket.js b/js/osticket.js
index a809e6f2f..4057b04b8 100644
--- a/js/osticket.js
+++ b/js/osticket.js
@@ -1 +1,55 @@
-//Nothing for now...
+/* 
+   osticket.js
+   Copyright (c) osTicket.com
+ */
+
+$(document).ready(function(){
+
+    $("input:not(.dp):visible:enabled:first").focus();
+    $('table.list tbody tr:odd').addClass('odd');
+
+    $("form#save :input").change(function() {
+        var fObj = $(this).closest('form');
+        if(!fObj.data('changed')){
+            fObj.data('changed', true);
+            $('input[type=submit]', fObj).css('color', 'red');
+            $(window).bind('beforeunload', function(e) {
+                return 'Are you sure you want to leave? Any changes or info you\'ve entered will be discarded!';
+             });
+        }
+       });
+
+    $("form#save :input[type=reset]").click(function() {
+        var fObj = $(this).closest('form');
+        if(fObj.data('changed')){
+            $('input[type=submit]', fObj).removeAttr('style');
+            $('label', fObj).removeAttr('style');
+            $('label', fObj).removeClass('strike');
+            fObj.data('changed', false);
+            $(window).unbind('beforeunload');
+        }
+       });
+
+    $('form#save').submit(function() {
+        $(window).unbind('beforeunload');
+        return true;
+       });
+
+    /* Get config settings from the backend */
+    var $config = null;
+    $.ajax({
+        url: "ajax.php/config/client",
+        dataType: 'json',
+        async: false,
+        success: function (config) {
+            $config = config;
+            }
+        });
+     
+    /* Multifile uploads */
+     $('.multifile').multifile({
+        container:   '.uploads',
+        max_uploads: ($config && $config.max_file_uploads)?$config.max_file_uploads:1,
+        file_types:  ($config && $config.file_types)?$config.file_types:".*"
+       });
+});
-- 
GitLab