To create a PHP custom function for retrieving all users based on a specific role in Vtiger CRM 7, you will use the Vtlib library, which is the framework for creating and managing custom modules and extensions in Vtiger.
Steps to Create a Custom PHP Function to Get All Users by Role in Vtiger 7
1. Understanding the Basics
In Vtiger, roles are stored in the vtiger_role
table, and users are stored in the vtiger_users
table. The relationship between users and their roles is maintained in the vtiger_user2role
table. To get users based on a specific role, you’ll need to query these tables accordingly.
2. Writing the Custom PHP Function
Here’s how you can write a custom PHP function using Vtlib to retrieve all users associated with a particular role.
function getUsersByRole($roleId) {
global $adb; // Vtiger's global database connection object
// Sanitize input to prevent SQL injection
$roleId = $adb->sql_escape_string($roleId);
// Query to find users associated with the specific role
$query = "SELECT vtiger_users.id, vtiger_users.user_name, vtiger_users.first_name, vtiger_users.last_name
FROM vtiger_users
INNER JOIN vtiger_user2role ON vtiger_user2role.userid = vtiger_users.id
WHERE vtiger_user2role.roleid = ? AND vtiger_users.status = 'Active'";
// Execute the query using Vtiger's database API
$result = $adb->pquery($query, array($roleId));
// Initialize an array to hold user data
$users = array();
// Fetch results
if ($adb->num_rows($result) > 0) {
while ($row = $adb->fetch_array($result)) {
$users[] = array(
'id' => $row['id'],
'user_name' => $row['user_name'],
'first_name' => $row['first_name'],
'last_name' => $row['last_name']
);
}
}
return $users; // Return the list of users
}
3. Explanation of the Code
- Database Connection: The
global $adb;
line provides access to the database connection object used in Vtiger. - SQL Query: The query retrieves all active users (
status = 'Active'
) associated with a specific role (roleid
) by joining thevtiger_users
andvtiger_user2role
tables. - Execute the Query: The
pquery
method executes the prepared query with the role ID passed as a parameter to prevent SQL injection. - Fetch Results: The
fetch_array
method is used to loop through the results and store each user’s details in the$users
array.
4. How to Use the Function
To use this function, simply call it with the specific role ID you want to retrieve users for:
$roleId = 'H2'; // Example role ID
$users = getUsersByRole($roleId);
// Display user details
foreach ($users as $user) {
echo "User ID: " . $user['id'] . " - Name: " . $user['first_name'] . " " . $user['last_name'] . " (" . $user['user_name'] . ")\n";
}
5. Where to Place This Function
- Custom PHP Scripts: You can place this function in a custom script file that you execute on demand or include it in a module’s file where it’s needed.
- Vtiger Module File: If you are developing a custom module, place this function in the appropriate file (like
helpers
,models
, oractions
folder) to be called when required.
6. Adding Error Handling and Logging
To make your function more robust, you might want to add error handling and logging:
function getUsersByRole($roleId) {
global $adb;
$users = array(); // Initialize the array to return
try {
$roleId = $adb->sql_escape_string($roleId);
$query = "SELECT vtiger_users.id, vtiger_users.user_name, vtiger_users.first_name, vtiger_users.last_name
FROM vtiger_users
INNER JOIN vtiger_user2role ON vtiger_user2role.userid = vtiger_users.id
WHERE vtiger_user2role.roleid = ? AND vtiger_users.status = 'Active'";
$result = $adb->pquery($query, array($roleId));
if ($adb->num_rows($result) > 0) {
while ($row = $adb->fetch_array($result)) {
$users[] = array(
'id' => $row['id'],
'user_name' => $row['user_name'],
'first_name' => $row['first_name'],
'last_name' => $row['last_name']
);
}
}
} catch (Exception $e) {
error_log("Error retrieving users by role: " . $e->getMessage());
}
return $users;
}
Conclusion
To simply create any specific PHP custom function for repossessing all operators as per a particular role in the case of Vtiger CRM 7, you will utilize the Vtlib library, which is the type of framework for developing and handling all custom modules along with extensions in Vtiger. For doing this seamlessly, make sure that you have the best Vtiger hosting solutions.