241 lines
7.0 KiB
PHP
241 lines
7.0 KiB
PHP
<?php
|
|
/**
|
|
* Agent Groups management class
|
|
*/
|
|
class TWP_Agent_Groups {
|
|
|
|
/**
|
|
* Create a new agent group
|
|
*/
|
|
public static function create_group($data) {
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'twp_agent_groups';
|
|
|
|
$result = $wpdb->insert(
|
|
$table_name,
|
|
array(
|
|
'group_name' => sanitize_text_field($data['group_name']),
|
|
'description' => sanitize_textarea_field($data['description']),
|
|
'ring_strategy' => sanitize_text_field($data['ring_strategy'] ?? 'simultaneous'),
|
|
'timeout_seconds' => intval($data['timeout_seconds'] ?? 30)
|
|
),
|
|
array('%s', '%s', '%s', '%d')
|
|
);
|
|
|
|
return $result !== false ? $wpdb->insert_id : false;
|
|
}
|
|
|
|
/**
|
|
* Update an agent group
|
|
*/
|
|
public static function update_group($group_id, $data) {
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'twp_agent_groups';
|
|
|
|
$update_data = array();
|
|
$update_format = array();
|
|
|
|
if (isset($data['group_name'])) {
|
|
$update_data['group_name'] = sanitize_text_field($data['group_name']);
|
|
$update_format[] = '%s';
|
|
}
|
|
|
|
if (isset($data['description'])) {
|
|
$update_data['description'] = sanitize_textarea_field($data['description']);
|
|
$update_format[] = '%s';
|
|
}
|
|
|
|
if (isset($data['ring_strategy'])) {
|
|
$update_data['ring_strategy'] = sanitize_text_field($data['ring_strategy']);
|
|
$update_format[] = '%s';
|
|
}
|
|
|
|
if (isset($data['timeout_seconds'])) {
|
|
$update_data['timeout_seconds'] = intval($data['timeout_seconds']);
|
|
$update_format[] = '%d';
|
|
}
|
|
|
|
return $wpdb->update(
|
|
$table_name,
|
|
$update_data,
|
|
array('id' => $group_id),
|
|
$update_format,
|
|
array('%d')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Delete an agent group
|
|
*/
|
|
public static function delete_group($group_id) {
|
|
global $wpdb;
|
|
$groups_table = $wpdb->prefix . 'twp_agent_groups';
|
|
$members_table = $wpdb->prefix . 'twp_group_members';
|
|
|
|
// Delete all members first
|
|
$wpdb->delete($members_table, array('group_id' => $group_id), array('%d'));
|
|
|
|
// Delete the group
|
|
return $wpdb->delete($groups_table, array('id' => $group_id), array('%d'));
|
|
}
|
|
|
|
/**
|
|
* Get all groups
|
|
*/
|
|
public static function get_all_groups() {
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'twp_agent_groups';
|
|
|
|
return $wpdb->get_results("SELECT * FROM $table_name ORDER BY group_name ASC");
|
|
}
|
|
|
|
/**
|
|
* Get a specific group
|
|
*/
|
|
public static function get_group($group_id) {
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'twp_agent_groups';
|
|
|
|
return $wpdb->get_row($wpdb->prepare(
|
|
"SELECT * FROM $table_name WHERE id = %d",
|
|
$group_id
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Add user to group
|
|
*/
|
|
public static function add_member($group_id, $user_id, $priority = 0) {
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'twp_group_members';
|
|
|
|
return $wpdb->insert(
|
|
$table_name,
|
|
array(
|
|
'group_id' => intval($group_id),
|
|
'user_id' => intval($user_id),
|
|
'priority' => intval($priority),
|
|
'is_active' => 1
|
|
),
|
|
array('%d', '%d', '%d', '%d')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Remove user from group
|
|
*/
|
|
public static function remove_member($group_id, $user_id) {
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'twp_group_members';
|
|
|
|
return $wpdb->delete(
|
|
$table_name,
|
|
array(
|
|
'group_id' => $group_id,
|
|
'user_id' => $user_id
|
|
),
|
|
array('%d', '%d')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get group members
|
|
*/
|
|
public static function get_group_members($group_id) {
|
|
global $wpdb;
|
|
$members_table = $wpdb->prefix . 'twp_group_members';
|
|
$users_table = $wpdb->prefix . 'users';
|
|
$usermeta_table = $wpdb->prefix . 'usermeta';
|
|
|
|
$query = $wpdb->prepare("
|
|
SELECT
|
|
gm.*,
|
|
u.user_login,
|
|
u.display_name,
|
|
u.user_email,
|
|
um.meta_value as phone_number
|
|
FROM $members_table gm
|
|
JOIN $users_table u ON gm.user_id = u.ID
|
|
LEFT JOIN $usermeta_table um ON u.ID = um.user_id AND um.meta_key = 'twp_phone_number'
|
|
WHERE gm.group_id = %d
|
|
ORDER BY gm.priority ASC, u.display_name ASC
|
|
", $group_id);
|
|
|
|
return $wpdb->get_results($query);
|
|
}
|
|
|
|
/**
|
|
* Get all members' phone numbers for a group
|
|
*/
|
|
public static function get_group_phone_numbers($group_id) {
|
|
$members = self::get_group_members($group_id);
|
|
$numbers = array();
|
|
|
|
foreach ($members as $member) {
|
|
if ($member->phone_number && $member->is_active) {
|
|
$numbers[] = array(
|
|
'user_id' => $member->user_id,
|
|
'phone_number' => $member->phone_number,
|
|
'display_name' => $member->display_name,
|
|
'priority' => $member->priority
|
|
);
|
|
}
|
|
}
|
|
|
|
return $numbers;
|
|
}
|
|
|
|
/**
|
|
* Get groups for a user
|
|
*/
|
|
public static function get_user_groups($user_id) {
|
|
global $wpdb;
|
|
$groups_table = $wpdb->prefix . 'twp_agent_groups';
|
|
$members_table = $wpdb->prefix . 'twp_group_members';
|
|
|
|
$query = $wpdb->prepare("
|
|
SELECT g.*
|
|
FROM $groups_table g
|
|
JOIN $members_table gm ON g.id = gm.group_id
|
|
WHERE gm.user_id = %d AND gm.is_active = 1
|
|
ORDER BY g.group_name ASC
|
|
", $user_id);
|
|
|
|
return $wpdb->get_results($query);
|
|
}
|
|
|
|
/**
|
|
* Check if user is in group
|
|
*/
|
|
public static function is_user_in_group($user_id, $group_id) {
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'twp_group_members';
|
|
|
|
$count = $wpdb->get_var($wpdb->prepare(
|
|
"SELECT COUNT(*) FROM $table_name WHERE group_id = %d AND user_id = %d",
|
|
$group_id,
|
|
$user_id
|
|
));
|
|
|
|
return $count > 0;
|
|
}
|
|
|
|
/**
|
|
* Update member status
|
|
*/
|
|
public static function update_member_status($group_id, $user_id, $is_active) {
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'twp_group_members';
|
|
|
|
return $wpdb->update(
|
|
$table_name,
|
|
array('is_active' => $is_active ? 1 : 0),
|
|
array(
|
|
'group_id' => $group_id,
|
|
'user_id' => $user_id
|
|
),
|
|
array('%d'),
|
|
array('%d', '%d')
|
|
);
|
|
}
|
|
} |