submitticket.php

Amarjeet Kaur, 01/09/2013 03:26 am

Download (5.8 kB)

 
1
<?php
2
/**
3
 * =======================================
4
 * ###################################
5
 * WHMCS Integration
6
 *
7
 * @package        WHMCS Integration
8
 * @author                Ruchi Kothari
9
 * @copyright        Copyright (c) 2001-2012, Kayako Infotech Ltd.
10
 * @license                http://www.kayako.com/license
11
 * @link                http://www.kayako.com
12
 * @filesource
13
 * ###################################
14
 * =======================================
15
 */
16
17
/**
18
 * Common file to submit a ticket
19
 *
20
 * @author Ruchi Kothari
21
 */
22
23
//Include config file
24
require_once 'config.php';
25
26
//Include all necessary classes and helper methods
27
require_once 'API/kyIncludes.php';
28
29
//Include common functions
30
require_once 'functions.php';
31
32
//Include constants file
33
require_once 'constants.php';
34
35
//Initialize the client
36
kyConfig::set(new kyConfig(API_URL, API_KEY, SECRET_KEY));
37
38
if (!isset($_REQUEST['step'])) {
39
40
        $_allDepartments = kyDepartment::getAll()->filterByModule(kyDepartment::MODULE_TICKETS)->filterByType(kyDepartment::TYPE_PUBLIC);
41
        $_topDepartments = $_allDepartments->filterByParentDepartmentId(null)->orderByDisplayOrder();
42
43
        $_departmentMap = array();
44
        foreach ($_topDepartments as $_topDepartment) {
45
                $_department = array();
46
                $_department['departmentid']= $_topDepartment->getId();
47
                $_department['title']= $_topDepartment->getTitle();
48
                $_department['departmenttype']= $_topDepartment->getType();
49
50
                $_departmentMap[$_topDepartment->getId()] = $_department;
51
52
                $_childDepartments = $_allDepartments->filterByParentDepartmentId($_topDepartment->getId())->orderByDisplayOrder();
53
54
                foreach ($_childDepartments as $_childDepartment) {
55
                        $_department = array();
56
                        $_department['departmentid']= $_childDepartment->getId();
57
                        $_department['title']= $_childDepartment->getTitle();
58
                        $_department['departmenttype']= $_childDepartment->getType();
59
                        $_departmentMap[$_topDepartment->getId()]['subdepartments'][] = $_department;
60
                }
61
        }
62
63
        $smarty->assign('_departments', $_departmentMap);
64
        $smarty->assign('_departmentFormURL', WHMCS_URL . 'submitticket.php?step=2');
65
66
        $templatefile = "selectdepartment";
67
68
} else if ($_REQUEST['step'] == 2) {
69
        if (!empty($_POST)) {
70
71
                // get priorities of 'public' type only
72
                $_ticketPriorityContainer = kyTicketPriority::getAll()->filterByType(kyTicketPriority::TYPE_PUBLIC)->orderByDisplayOrder();
73
74
                $_ticketPriorities = array();
75
                foreach ($_ticketPriorityContainer as $_ticketPriorityObject) {
76
                        $_ticketPriority['priorityid'] = $_ticketPriorityObject->getId();
77
                        $_ticketPriority['title'] = $_ticketPriorityObject->getTitle();
78
                        $_ticketPriorities[] = $_ticketPriority;
79
                }
80
81
                $smarty->assign('_ticketPriorities', $_ticketPriorities);
82
83
                //Get Ticket Custom fields
84
                $_customFieldObjectContainer = kyCustomFieldDefinition::getAll();
85
86
                $_customFields = array();
87
                foreach ($_customFieldObjectContainer as $_customFieldObject) {
88
                        $_customFields[$_customFieldObject->getGroupId()][] = RenderCustomField($_customFieldObject, MODE_INSERT);
89
                }
90
91
                // Custom field groups
92
                $_customFieldGroupContainer = array();
93
94
                // fetch custom field groups based on selected department
95
                $_customFieldGroupObjectContainer = kyCustomFieldGroup::getAll(array('departmentid' => $_POST['departmentid']))->filterByGroupType(array(kyCustomFieldGroup::GROUP_STAFFTICKET,
96
                        kyCustomFieldGroup::GROUP_STAFFUSERTICKET, kyCustomFieldGroup::GROUP_USERTICKET));
97
98
                foreach ($_customFieldGroupObjectContainer as $_customFieldGroupObject) {
99
                        if (isset($_customFields[$_customFieldGroupObject->getId()])) {
100
                                $_customFieldGroup = array();
101
                                $_customFieldGroup['title'] = $_customFieldGroupObject->getTitle();
102
                                $_customFieldGroup['_fields'] = $_customFields[$_customFieldGroupObject->getId()];
103
104
                                $_customFieldGroupContainer[$_customFieldGroupObject->getId()] = $_customFieldGroup;
105
                        }
106
                }
107
108
                $smarty->assign('_customFieldGroupContainer', $_customFieldGroupContainer);
109
                $smarty->assign('_ticketFormURL', WHMCS_URL . 'submitticket.php?step=3');
110
                $smarty->assign('_departmentID', $_POST['departmentid']);
111
                $smarty->assign('_templateURL', getcwd() . '/templates/kayako');
112
                $smarty->assign('_imageURL', WHMCS_URL . 'templates/kayako/images');
113
114
                $templatefile = 'ticketform';
115
116
        } else {
117
                header('Location: ' . WHMCS_URL . 'submitticket.php');
118
        }
119
} else if ($_REQUEST['step'] == 3){
120
        if (!empty($_POST)) {
121
                //Set Defaults for a new ticket
122
                $_defaultStatusID = kyTicketStatus::getAll()->filterByTitle("Open")->first()->getId();
123
                $_defaultPriorityID = kyTicketPriority::getAll()->filterByTitle("Normal")->first()->getId();
124
                $_defaultTypeID = kyTicketType::getAll()->filterByTitle("Issue")->first()->getId();
125
                kyTicket::setDefaults($_defaultStatusID, $_defaultPriorityID, $_defaultTypeID);
126
127
                //Create ticket
128
                $_department = kyDepartment::get($_POST['departmentid']);
129
                $_priority = kyTicketPriority::get($_POST['ticketpriorityid']);
130
                $_ticket = kyTicket::createNewAuto(
131
                                $_department,
132
                                $clientsdetails['firstname'] . ' ' . $clientsdetails['lastname'],
133
                                $clientsdetails['email'],
134
                                $_POST['ticketmessage'],
135
                                $_POST['ticketsubject'])
136
                        ->setPriority($_priority)
137
                        ->create();
138
139
                $_ticketPosts = $_ticket->getPosts();
140
141
                //Save ticket attachments
142
                foreach ($_FILES['ticketattachments']['tmp_name'] as $_key => $_ticketAttachment) {
143
                        kyTicketAttachment::createNewFromFile($_ticketPosts[0], $_ticketAttachment, $_FILES['ticketattachments']['name'][$_key])
144
                                ->create();
145
                }
146
147
                //Save custom fields
148
                $_ticket->setCustomFieldValuesFromPOST();
149
                $_ticket->updateCustomFields();
150
151
                $smarty->assign('_ticketDisplayID', $_ticket->getDisplayId());
152
                $smarty->assign('_ticketSubject', $_POST['ticketsubject']);
153
                $smarty->assign('_ticketMessage', $_POST['ticketmessage']);
154
155
                $templatefile = 'ticketconfirmation';
156
157
        } else {
158
                header('Location: ' . WHMCS_URL . 'submitticket.php');
159
        }
160
161
} else {
162
        header('Location: ' . WHMCS_URL . 'submitticket.php');
163
}
164
165
$smarty->assign('_jscssURL',  'templates/kayako');
166
167
?>