{"id":9110,"date":"2024-09-11T07:47:07","date_gmt":"2024-09-11T07:47:07","guid":{"rendered":"https:\/\/www.infinitivehost.com\/knowledge-base\/?p=9110"},"modified":"2024-09-11T11:38:12","modified_gmt":"2024-09-11T11:38:12","slug":"create-php-function-for-users-by-role-in-vtiger-7","status":"publish","type":"post","link":"https:\/\/www.infinitivehost.com\/knowledge-base\/create-php-function-for-users-by-role-in-vtiger-7\/","title":{"rendered":"Create PHP Function for Users by Role in Vtiger 7"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 1,729<\/span><span class='epvc-label'> Views<\/span><\/div>\n<p>To create a PHP custom function for retrieving all users based on a specific role in Vtiger CRM 7, you will use the <strong>Vtlib<\/strong> library, which is the framework for creating and managing custom modules and extensions in Vtiger.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Steps to Create a Custom PHP Function to Get All Users by Role in Vtiger 7<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1. Understanding the Basics<\/strong><\/h4>\n\n\n\n<p>In Vtiger, roles are stored in the <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">vtiger_role<\/mark><\/code> table, and users are stored in the <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color has-black-color\">vtiger_users<\/mark><\/code> table. The relationship between users and their roles is maintained in the <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">vtiger_user2role<\/mark><\/code> table. To get users based on a specific role, you\u2019ll need to query these tables accordingly.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2. Writing the Custom PHP Function<\/strong><\/h4>\n\n\n\n<p>Here\u2019s how you can write a custom PHP function using <strong>Vtlib<\/strong> to retrieve all users associated with a particular role.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">function getUsersByRole($roleId) {\n    global $adb; \/\/ Vtiger's global database connection object\n\n    \/\/ Sanitize input to prevent SQL injection\n    $roleId = $adb-&gt;sql_escape_string($roleId);\n\n    \/\/ Query to find users associated with the specific role\n    $query = \"SELECT vtiger_users.id, vtiger_users.user_name, vtiger_users.first_name, vtiger_users.last_name\n              FROM vtiger_users\n              INNER JOIN vtiger_user2role ON vtiger_user2role.userid = vtiger_users.id\n              WHERE vtiger_user2role.roleid = ? AND vtiger_users.status = 'Active'\";\n\n    \/\/ Execute the query using Vtiger's database API\n    $result = $adb-&gt;pquery($query, array($roleId));\n\n    \/\/ Initialize an array to hold user data\n    $users = array();\n\n    \/\/ Fetch results\n    if ($adb-&gt;num_rows($result) &gt; 0) {\n        while ($row = $adb-&gt;fetch_array($result)) {\n            $users&#91;] = array(\n                'id' =&gt; $row&#91;'id'],\n                'user_name' =&gt; $row&#91;'user_name'],\n                'first_name' =&gt; $row&#91;'first_name'],\n                'last_name' =&gt; $row&#91;'last_name']\n            );\n        }\n    }\n\n    return $users; \/\/ Return the list of users\n}<\/mark><\/code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Explanation of the Code<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Database Connection<\/strong>: The <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">global $adb;<\/mark><\/code> line provides access to the database connection object used in Vtiger.<\/li>\n\n\n\n<li><strong>SQL Query<\/strong>: The query retrieves all active users (<code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">status = 'Active'<\/mark><\/code>) associated with a specific role (<code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">roleid<\/mark><\/code>) by joining the <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">vtiger_users<\/mark><\/code> and <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">vtiger_user2role<\/mark><\/code> tables.<\/li>\n\n\n\n<li><strong>Execute the Query<\/strong>: The <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">pquery<\/mark><\/code> method executes the prepared query with the role ID passed as a parameter to prevent SQL injection.<\/li>\n\n\n\n<li><strong>Fetch Results<\/strong>: The <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">fetch_array<\/mark><\/code> method is used to loop through the results and store each user&#8217;s details in the <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">$users<\/mark><\/code> array.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>4. How to Use the Function<\/strong><\/h4>\n\n\n\n<p>To use this function, simply call it with the specific role ID you want to retrieve users for:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">$roleId = 'H2'; \/\/ Example role ID\n$users = getUsersByRole($roleId);\n\n\/\/ Display user details\nforeach ($users as $user) {\n    echo \"User ID: \" . $user&#91;'id'] . \" - Name: \" . $user&#91;'first_name'] . \" \" . $user&#91;'last_name'] . \" (\" . $user&#91;'user_name'] . \")\\n\";\n}<\/mark><\/code><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>5. Where to Place This Function<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Custom PHP Scripts<\/strong>: You can place this function in a custom script file that you execute on demand or include it in a module\u2019s file where it&#8217;s needed.<\/li>\n\n\n\n<li><strong>Vtiger Module File<\/strong>: If you are developing a custom module, place this function in the appropriate file (like <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">helpers<\/mark><\/code>, <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">models<\/mark><\/code>, or <code><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">actions<\/mark><\/code> folder) to be called when required.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>6. Adding Error Handling and Logging<\/strong><\/h4>\n\n\n\n<p>To make your function more robust, you might want to add error handling and logging:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">function getUsersByRole($roleId) {\n    global $adb;\n    $users = array(); \/\/ Initialize the array to return\n\n    try {\n        $roleId = $adb-&gt;sql_escape_string($roleId);\n        $query = \"SELECT vtiger_users.id, vtiger_users.user_name, vtiger_users.first_name, vtiger_users.last_name\n                  FROM vtiger_users\n                  INNER JOIN vtiger_user2role ON vtiger_user2role.userid = vtiger_users.id\n                  WHERE vtiger_user2role.roleid = ? AND vtiger_users.status = 'Active'\";\n        $result = $adb-&gt;pquery($query, array($roleId));\n\n        if ($adb-&gt;num_rows($result) &gt; 0) {\n            while ($row = $adb-&gt;fetch_array($result)) {\n                $users&#91;] = array(\n                    'id' =&gt; $row&#91;'id'],\n                    'user_name' =&gt; $row&#91;'user_name'],\n                    'first_name' =&gt; $row&#91;'first_name'],\n                    'last_name' =&gt; $row&#91;'last_name']\n                );\n            }\n        }\n    } catch (Exception $e) {\n        error_log(\"Error retrieving users by role: \" . $e-&gt;getMessage());\n    }\n\n    return $users;\n}<\/mark><\/code><\/code><\/pre>\n\n\n\n<p><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>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 <a href=\"https:\/\/www.infinitivehost.com\/managed-vtiger-solutions\"><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\"><strong>best Vtiger hosting solutions<\/strong><\/mark><\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1,729 Views 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[204],"tags":[],"class_list":["post-9110","post","type-post","status-publish","format-standard","hentry","category-vtiger-solutions"],"_links":{"self":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/comments?post=9110"}],"version-history":[{"count":2,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9110\/revisions"}],"predecessor-version":[{"id":9115,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/posts\/9110\/revisions\/9115"}],"wp:attachment":[{"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/media?parent=9110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/categories?post=9110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infinitivehost.com\/knowledge-base\/wp-json\/wp\/v2\/tags?post=9110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}