From cf374158a89680031f567f5959859cc7ac3e1631 Mon Sep 17 00:00:00 2001
From: Dmitriy Simushev <simushevds@ossg.ru>
Date: Wed, 15 Feb 2012 13:56:55 +0000
Subject: [PATCH] Added groups weight

---
 src/messenger/webim/install/dbinfo.php    |  3 ++-
 src/messenger/webim/install/dbperform.php |  4 ++++
 src/messenger/webim/libs/operator.php     |  4 ++--
 src/messenger/webim/locales/en/properties |  3 +++
 src/messenger/webim/locales/ru/properties |  3 +++
 src/messenger/webim/operator/group.php    | 20 ++++++++++++++------
 src/messenger/webim/view/group.php        | 11 ++++++++++-
 src/messenger/webim/view/groups.php       |  7 ++++++-
 8 files changed, 44 insertions(+), 11 deletions(-)

diff --git a/src/messenger/webim/install/dbinfo.php b/src/messenger/webim/install/dbinfo.php
index f10803d1..95d57ef8 100644
--- a/src/messenger/webim/install/dbinfo.php
+++ b/src/messenger/webim/install/dbinfo.php
@@ -27,6 +27,7 @@ $dbtables = array(
 		"vccommonname" => "varchar(64) NOT NULL",
 		"vclocaldescription" => "varchar(1024) NOT NULL",
 		"vccommondescription" => "varchar(1024) NOT NULL",
+		"iweight" => "int NOT NULL DEFAULT 0",
 	),
 
 	"${mysqlprefix}chatthread" => array(
@@ -163,7 +164,7 @@ $dbtables_can_update = array(
 	"${mysqlprefix}chatmessage" => array("agentId"),
 	"${mysqlprefix}chatoperator" => array("vcavatar", "vcjabbername", "iperm", "istatus", "idisabled", "vcemail", "dtmrestore", "vcrestoretoken"),
 	"${mysqlprefix}chatban" => array(),
-	"${mysqlprefix}chatgroup" => array("vcemail"),
+	"${mysqlprefix}chatgroup" => array("vcemail", "iweight"),
 	"${mysqlprefix}chatgroupoperator" => array(),
 	"${mysqlprefix}chatresponses" => array("vctitle"),
 	"${mysqlprefix}chatsitevisitor" => array(),
diff --git a/src/messenger/webim/install/dbperform.php b/src/messenger/webim/install/dbperform.php
index 915a6eea..b84222e1 100644
--- a/src/messenger/webim/install/dbperform.php
+++ b/src/messenger/webim/install/dbperform.php
@@ -167,6 +167,10 @@ if ($act == "silentcreateall") {
 			runsql("ALTER TABLE ${mysqlprefix}chatgroup ADD vcemail varchar(64)", $link);
 		}
 
+		if (in_array("${mysqlprefix}chatgroup.iweight", $absent)) {
+			runsql("ALTER TABLE ${mysqlprefix}chatgroup ADD iweight int DEFAULT 0", $link);
+		}
+
 		$res = mysql_query("select null from information_schema.statistics where table_schema = '$mysqldb' and table_name = '${mysqlprefix}chatmessage' and index_name = 'idx_agentid'", $link);
 		if ($res && mysql_num_rows($res) == 0) {
 			runsql("ALTER TABLE ${mysqlprefix}chatmessage ADD INDEX idx_agentid (agentid)", $link);
diff --git a/src/messenger/webim/libs/operator.php b/src/messenger/webim/libs/operator.php
index 14ee6416..470ba9ab 100755
--- a/src/messenger/webim/libs/operator.php
+++ b/src/messenger/webim/libs/operator.php
@@ -397,7 +397,7 @@ function get_all_groups($link)
 function get_groups($link, $checkaway)
 {
 	global $mysqlprefix;
-	$query = "select ${mysqlprefix}chatgroup.groupid as groupid, vclocalname, vclocaldescription" .
+	$query = "select ${mysqlprefix}chatgroup.groupid as groupid, vclocalname, vclocaldescription, iweight" .
 			 ", (SELECT count(*) from ${mysqlprefix}chatgroupoperator where ${mysqlprefix}chatgroup.groupid = " .
 			 "${mysqlprefix}chatgroupoperator.groupid) as inumofagents" .
 			 ", (SELECT min(unix_timestamp(CURRENT_TIMESTAMP)-unix_timestamp(dtmlastvisited)) as time " .
@@ -411,7 +411,7 @@ function get_groups($link, $checkaway)
 					   "and ${mysqlprefix}chatgroupoperator.operatorid = ${mysqlprefix}chatoperator.operatorid) as ilastseenaway"
 					 : ""
 			 ) .
-			 " from ${mysqlprefix}chatgroup order by vclocalname";
+			 " from ${mysqlprefix}chatgroup order by iweight, vclocalname";
 	return select_multi_assoc($query, $link);
 }
 
diff --git a/src/messenger/webim/locales/en/properties b/src/messenger/webim/locales/en/properties
index 98a92734..fad50795 100644
--- a/src/messenger/webim/locales/en/properties
+++ b/src/messenger/webim/locales/en/properties
@@ -154,6 +154,8 @@ form.field.groupdesc=Description
 form.field.groupemail.description=Group email for notifications. Leave empty to use the default address.
 form.field.groupname.description=Name to identify the group.
 form.field.groupname=Name
+form.field.groupweight=Weight
+form.field.groupweight.description=Groups with lower weight display higher in groups list. Group weight is a positive integer value.
 form.field.login.description=Login can consist of small Latin letters and underscore.
 form.field.login=Login
 form.field.mail.description=For notifications and password retrieval.
@@ -297,6 +299,7 @@ page.groups.isaway=Away
 page.groups.isonline=Online
 page.groups.new=Create new group
 page.groups.title=Groups
+page.groups.weight=Weight
 page.preview.agentchat=Chat window (operator-mode)
 page.preview.agentrochat=View Chat window (operator in read-only mode)
 page.preview.chatsimple=Simple chat window. Refresh to post messages (IE 5, Opera 7)
diff --git a/src/messenger/webim/locales/ru/properties b/src/messenger/webim/locales/ru/properties
index c5cc4cf2..184029e5 100644
--- a/src/messenger/webim/locales/ru/properties
+++ b/src/messenger/webim/locales/ru/properties
@@ -152,6 +152,8 @@ form.field.groupdesc=
 form.field.groupemail.description=����� ��� ���������. �������� ������, ����� ������������ ���������� �����.
 form.field.groupname.description=����� ���� ��������� ������ � ����� ��������.
 form.field.groupname=�������� ������
+form.field.groupweight=��� ������
+form.field.groupweight.description=������ � ������� ����� ������������ ���� � ������ �����. ��� - ��� ����� ������������� �����.
 form.field.login.description=����� ����� �������� �� ��������� ��������� ���� � ����� �������������.
 form.field.login=�����
 form.field.mail.description=��� ����������� � �������������� ������.
@@ -295,6 +297,7 @@ page.groups.isaway=Away
 page.groups.isonline=��������
 page.groups.new=�������� ������...
 page.groups.title=������
+page.groups.weight=���
 page.preview.agentchat=���� ���� (�� ������� ���������)
 page.preview.agentrochat=���� ��������� ���� (��� ���������)
 page.preview.chatsimple=Simple chat window, refresh to post messages (IE 5, Opera 7)
diff --git a/src/messenger/webim/operator/group.php b/src/messenger/webim/operator/group.php
index 98397971..fd90ade9 100644
--- a/src/messenger/webim/operator/group.php
+++ b/src/messenger/webim/operator/group.php
@@ -39,16 +39,17 @@ function group_by_name($name)
 	return $group;
 }
 
-function create_group($name, $descr, $commonname, $commondescr, $email)
+function create_group($name, $descr, $commonname, $commondescr, $weight ,$email)
 {
 	global $mysqlprefix;
 	$link = connect();
 	$query = sprintf(
-		"insert into ${mysqlprefix}chatgroup (vclocalname,vclocaldescription,vccommonname,vccommondescription,vcemail) values ('%s','%s','%s','%s','%s')",
+		"insert into ${mysqlprefix}chatgroup (vclocalname,vclocaldescription,vccommonname,vccommondescription,iweight,vcemail) values ('%s','%s','%s','%s',%u,'%s')",
 		db_escape_string($name),
 		db_escape_string($descr),
 		db_escape_string($commonname),
 		db_escape_string($commondescr),
+		$weight,
 		db_escape_string($email));
 
 	perform_query($query, $link);
@@ -59,16 +60,17 @@ function create_group($name, $descr, $commonname, $commondescr, $email)
 	return $newdep;
 }
 
-function update_group($groupid, $name, $descr, $commonname, $commondescr, $email)
+function update_group($groupid, $name, $descr, $commonname, $commondescr, $weight, $email)
 {
 	global $mysqlprefix;
 	$link = connect();
 	$query = sprintf(
-		"update ${mysqlprefix}chatgroup set vclocalname = '%s', vclocaldescription = '%s', vccommonname = '%s', vccommondescription = '%s', vcemail = '%s' where groupid = %s",
+		"update ${mysqlprefix}chatgroup set vclocalname = '%s', vclocaldescription = '%s', vccommonname = '%s', vccommondescription = '%s', iweight = %u, vcemail = '%s' where groupid = %s",
 		db_escape_string($name),
 		db_escape_string($descr),
 		db_escape_string($commonname),
 		db_escape_string($commondescr),
+		$weight,
 		db_escape_string($email),
 		$groupid);
 
@@ -83,11 +85,15 @@ if (isset($_POST['name'])) {
 	$description = getparam('description');
 	$commonname = getparam('commonname');
 	$commondescription = getparam('commondescription');
+	$weight = getparam('weight');
 	$email = getparam('email');
 
 	if (!$name)
 		$errors[] = no_field("form.field.groupname");
 
+	if (! preg_match("/^\d{1,9}$/", $weight))
+		$errors[] = wrong_field("form.field.groupweight");
+
 	if ($email != '' && !is_valid_email($email))
 		$errors[] = wrong_field("form.field.mail");
 
@@ -98,11 +104,11 @@ if (isset($_POST['name'])) {
 
 	if (count($errors) == 0) {
 		if (!$groupid) {
-			$newdep = create_group($name, $description, $commonname, $commondescription, $email);
+			$newdep = create_group($name, $description, $commonname, $commondescription, $weight, $email);
 			header("Location: $webimroot/operator/groupmembers.php?gid=" . $newdep['groupid']);
 			exit;
 		} else {
-			update_group($groupid, $name, $description, $commonname, $commondescription, $email);
+			update_group($groupid, $name, $description, $commonname, $commondescription, $weight, $email);
 			header("Location: $webimroot/operator/group.php?gid=$groupid&stored");
 			exit;
 		}
@@ -111,6 +117,7 @@ if (isset($_POST['name'])) {
 		$page['formdescription'] = topage($description);
 		$page['formcommonname'] = topage($commonname);
 		$page['formcommondescription'] = topage($commondescription);
+		$page['formweight'] = topage($weight);
 		$page['formemail'] = topage($email);
 		$page['grid'] = topage($groupid);
 	}
@@ -127,6 +134,7 @@ if (isset($_POST['name'])) {
 		$page['formdescription'] = topage($group['vclocaldescription']);
 		$page['formcommonname'] = topage($group['vccommonname']);
 		$page['formcommondescription'] = topage($group['vccommondescription']);
+		$page['formweight'] = topage($group['iweight']);
 		$page['formemail'] = topage($group['vcemail']);
 		$page['grid'] = topage($group['groupid']);
 	}
diff --git a/src/messenger/webim/view/group.php b/src/messenger/webim/view/group.php
index bc9ba628..7831c2b4 100644
--- a/src/messenger/webim/view/group.php
+++ b/src/messenger/webim/view/group.php
@@ -85,7 +85,16 @@ require_once('inc_errors.php');
 			<div class="fdescr"> &mdash; <?php echo getlocal('form.field.groupcommondesc.description') ?></div>
 			<br clear="all"/>
 		</div>
-		
+
+		<div class="field">
+			<div class="flabel"><?php echo getlocal('form.field.groupweight') ?></div>
+			<div class="fvalue">
+				<input type="text" name="weight" size="40" value="<?php echo form_value('weight') ?>" class="formauth"/>
+			</div>
+			<div class="fdescr"> &mdash; <?php echo getlocal('form.field.groupweight.description') ?></div>
+			<br clear="all"/>
+		</div>
+
 		<div class="field">
 			<div class="flabel"><?php echo getlocal('form.field.mail') ?></div>
 			<div class="fvalue">
diff --git a/src/messenger/webim/view/groups.php b/src/messenger/webim/view/groups.php
index 38483e5c..e62159f1 100644
--- a/src/messenger/webim/view/groups.php
+++ b/src/messenger/webim/view/groups.php
@@ -60,6 +60,8 @@ require_once('inc_errors.php');
 	<?php echo getlocal("page_agents.status") ?>
 </th><th>
 	<?php echo getlocal("page.group.membersnum") ?>
+</th><th>
+	<?php echo getlocal("page.groups.weight") ?>
 <?php if($page['canmodify']) { ?>
 </th><th>
 <?php } ?>
@@ -93,6 +95,9 @@ if(count($page['groups']) > 0) {
 	   		<?php echo htmlspecialchars(topage($grp['inumofagents'])) ?>
    		</a>
 	</td>
+	<td>
+		<?php echo $grp['iweight'] ?>
+	</td>
 <?php if($page['canmodify']) { ?>
 	<td>
 		<a href="<?php echo $webimroot ?>/operator/groups.php?act=del&amp;gid=<?php echo $grp['groupid'] ?>" id="i<?php echo $grp['groupid'] ?>" class="removelink">
@@ -106,7 +111,7 @@ if(count($page['groups']) > 0) {
 } else {
 ?>
 	<tr>
-	<td colspan="4">
+	<td colspan="5">
 		<?php echo getlocal("tag.pagination.no_items.elements") ?>
 	</td>
 	</tr>