Custom related dropdown field with Admin Interface - Sandeep Bodh
Follow this link: http://www.tmib.net/agencies-dropdown-and-admin-ui-osticket-17st#comment-form(http://www.tmib.net/agencies-dropdown-and-admin-ui-osticket-17st#comment-form)
Steps:
Suppose if you want to create a custom Area field and State field, follow these steps:
1. Create a table in database with the following query:
CREATE TABLE IF NOT EXISTS `ost_areas` (
`area_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`state_id` tinyint(3) unsigned NOT NULL DEFAULT '0',
`area` varchar(255) NOT NULL,
`addDate` datetime NOT NULL,
`isenabled` tinyint(1) unsigned NOT NULL,
`editor` varchar(25) DEFAULT NULL,
`upDate` datetime DEFAULT NULL,
`notes` varchar(255) NOT NULL,
PRIMARY KEY (`area_id`),
FULLTEXT KEY `area` (`area`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC AUTO_INCREMENT=11 ;
And Insert record in it:
INSERT INTO `ost_areas` (`area_id`, `state_id`, `area`, `addDate`, `isenabled`, `editor`, `upDate`, `notes`) VALUES
(10, 8, 'Area 2', '2013-08-06 09', 1, NULL, '2013-08-06 09', ''),
(9, 10, 'Area 1', '2013-08-06 09', 1, NULL, '2013-08-06 09', '');
Next we need to add a field to the ost_ticket table that will hold the area_id.
ALTER TABLE `DATABASENAME`.`ost_ticket` ADD COLUMN `area_id` INTEGER UNSIGNED NOT NULL DEFAULT 0 AFTER `updated`;
CREATE TABLE IF NOT EXISTS `ost_states` (
`state_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`state` varchar(255) NOT NULL,
`addDate` datetime NOT NULL,
`isenabled` tinyint(1) unsigned NOT NULL,
`editor` varchar(25) DEFAULT NULL,
`upDate` datetime DEFAULT NULL,
`notes` varchar(255) NOT NULL,
PRIMARY KEY (`state_id`),
FULLTEXT KEY `state` (`state`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC AUTO_INCREMENT=11 ;
INSERT INTO `ost_states` (`state_id`, `state`, `addDate`, `isenabled`, `editor`, `upDate`, `notes`) VALUES
(8, 'Mumbai', '2013-08-06 09', 1, NULL, '2013-08-06 09', ''),
(10, 'Delhi', '2013-08-06 09', 0, NULL, '2013-08-06 09', '');
2. Modify the files:
a. Edit /include/client/open.inc.php
State:
Select State
<?
$states= db_query('SELECT state_id,state FROM '.TABLE_PREFIX.'states ORDER BY state');
while (list($stateId,$state) = db_fetch_row($states)){
$ck=($info==$stateId)?'selected':''; ?>
" <?=$ck?>><?=$state?>
<?
}?>
* <?=$errors?>
Area:
Select Area
<?
if ($info=='' || is_null($info)) { $info='0'; }
$areas= db_query('SELECT area_id,area FROM '.TABLE_PREFIX.'areas WHERE isenabled=1 AND state_id = '.$info.' ORDER BY area');
while (list($areaId,$area) = db_fetch_row($areas)){
$ck=($info==$areaId)?'selected':''; ?>
" <?=$ck?>><?=$area?>
<?
}?>
* <?=$errors?>
b. edit /include/class.ticket.php
Search
.' LEFT JOIN '.DEPT_TABLE.' dept ON (ticket.dept_id=dept.dept_id) '
Add after this
.' LEFT JOIN ost_areas area ON (ticket.area_id=area.area_id) '
.' LEFT JOIN ost_states state ON (ticket.state_id=state.state_id) '
Search
function getNumber() {
return $this->number;
}
Add after this
function getArea() {
return $this->ht;
}
function getState() {
return $this->ht;
}
Search
function getUpdateInfo() {
$info=array('name' => $this->getName(),
Add after this
'area' => $this->getArea(),
'state' => $this->getState(),
Search
$fields = array('type'=>'email', 'required'=>1, 'error'=>'Valid email required');
Add after this
$fields = array('type'=>'string', 'required'=>1, 'error'=>'Area Name required');
$fields = array('type'=>'string', 'required'=>1, 'error'=>'State Name required');
Search
$sql='UPDATE '.TICKET_TABLE.' SET updated=NOW() '
Add after this
.' ,area_id='.db_input($vars)
.' ,state_id='.db_input($vars); // (remove ; if not adding in last)
Search
$id=0;
$fields=array();
Add after this
$fields = array('type'=>'int', 'required'=>1, 'error'=>'Area Name required');
$fields = array('type'=>'int', 'required'=>1, 'error'=>'State Name required');
Search
$sql='INSERT INTO '.TICKET_TABLE.' SET created=NOW() '
Add after this
.' ,area_id='.db_input($vars)
.' ,state_id='.db_input($vars); // (check for ;)
c. edit /include/client/view.inc.php
Add this code where you want to display the field.
State:
<?
$states= db_query('SELECT state FROM '.TABLE_PREFIX.'states WHERE state_id='.$ticket->getState().'');
$state = db_fetch_row($states);
?>
<?=Format:($state);?>
Area:
<?
$areas= db_query('SELECT area FROM '.TABLE_PREFIX.'areas WHERE area_id='.$ticket->getArea().'');
$area = db_fetch_row($areas);
?>
<?=Format:($area);?>
d. edit /include/staff/ticket-open.inc.php
Add this code where you display the field in a ticket form.
State:
Select State
<?
$states= db_query('SELECT state_id,state FROM '.TABLE_PREFIX.'states ORDER BY state');
while (list($stateId,$state) = db_fetch_row($states)){
$ck=($info==$stateId)?'selected':''; ?>
" <?=$ck?>><?=$state?>
<?
}?>
* <?=$errors?>
Area:
Select Area
<?
if ($info=='' || is_null($info)) { $info='0'; }
$areas= db_query('SELECT area_id,area FROM '.TABLE_PREFIX.'areas WHERE isenabled=1 AND state_id = '.$info.' ORDER BY area');
while (list($areaId,$area) = db_fetch_row($areas)){
$ck=($info==$areaId)?'selected':''; ?>
" <?=$ck?>><?=$area?>
<?
}?>
* <?=$errors?>
Search
Add this code after this:
function getXMLHTTP() { //fuction to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getArea(strURL)
{
var req = getXMLHTTP(); // fuction to get xmlhttp object
if (req)
{
req.onreadystatechange = function()
{
if (req.readyState == 4) { //data is retrieved from server
if (req.status == 200) { // which reprents ok status
document.getElementById('areadiv').innerHTML=req.responseText;
}
else
{
alert("There was a problem while using XMLHTTP:\n");
}
}
}
req.open("GET", strURL, true); //open url using get method
req.send(null);
}
}
e. edit /include/staff/ticket-edit.inc.php
Repeat same step -d in this file.
f. edit /include/staff/ticket-view.inc.php
State:
<?
$states= db_query('SELECT state FROM '.TABLE_PREFIX.'states WHERE state_id='.$ticket->getState().'');
$state = db_fetch_row($states); ?>
<?php echo Format:($state);?>
Area:
<?
$areas= db_query('SELECT area FROM '.TABLE_PREFIX.'areas WHERE area_id='.$ticket->getArea().'');
$area = db_fetch_row($areas); ?>
<?php echo Format:($area);?>
For Admin Interface :
g. edit /include/class.nav.php
Search case 'manage':
Add after this $subnav=array('desc'=>'Areas','href'=>'areas.php','iconclass'=>'alert-settings');
$subnav=array('desc'=>'States','href'=>'states.php','iconclass'=>'alert-settings');
h. edit /upload/scp.php
change 'alerts' => 'Alerts and Notices Settings'); to 'alerts' => 'Alerts and Notices Settings',
Add next line
'states' => 'States',
'areas' => 'Areas');
To be continued................
See below mentioned steps to follow after this: