1. <?php
  2. if (!defined("WHMCS"))
  3. die("This file cannot be accessed directly");
  4. use WHMCS\Database\Capsule;
  5. use WHMCS\View\Menu\Item as MenuItem;
  6. function restrict_access_to_add_funds($vars) {
  7. if (filter_var($_GET['action'], FILTER_SANITIZE_STRING) == 'addfunds') {
  8. // Get PDO for the database queries
  9. $pdo = Capsule::connection()->getPdo();
  10. if ($vars['clientsdetails']['customfields']) {
  11. $customfields = $vars['clientsdetails']['customfields'];
  12. foreach ($customfields as $key => $customfield) {
  13. ##### Start Database Query #####
  14. try {
  15. $customfieldsquery = $pdo->query("SELECT fieldname FROM tblcustomfields WHERE id = " . $pdo->quote($customfield['id']));
  16. while($row = $customfieldsquery->fetch(PDO::FETCH_ASSOC)) {
  17. if ($row['fieldname'] == 'Enable Add Funds') {
  18. $addfunds = $customfield['value'];
  19. }
  20. }
  21. } catch(PDOException $e) {
  22. echo 'ERROR: ' . $e->getMessage();
  23. }
  24. }
  25. if ($addfunds !== 'on') {
  26. header("Location: clientarea.php");
  27. exit();
  28. }
  29. }
  30. }
  31. }
  32. function restrict_add_funds_link(MenuItem $primaryNavbar) {
  33. $client = Menu::context('client');
  34. // Get PDO for the database queries
  35. $pdo = Capsule::connection()->getPdo();
  36. try {
  37. $clientquery = $pdo->query("SELECT tblcustomfields.fieldname AS customfieldname, tblcustomfieldsvalues.value AS customfieldvalue FROM tblcustomfields INNER JOIN tblcustomfieldsvalues ON tblcustomfields.id = tblcustomfieldsvalues.fieldid WHERE tblcustomfieldsvalues.relid = " . $pdo->quote($client->id));
  38. while($row = $clientquery->fetch(PDO::FETCH_ASSOC)) {
  39. if (trim($row['customfieldname']) == "Enable Add Funds") {
  40. $addfunds = $row['customfieldvalue'];
  41. }
  42. }
  43. } catch(PDOException $e) {
  44. echo 'ERROR: ' . $e->getMessage();
  45. }
  46. if ($addfunds !== 'on') {
  47. if (!is_null($primaryNavbar->getChild('Billing'))) {
  48. $primaryNavbar->getChild('Billing')->removeChild('Add Funds');
  49. }
  50. }
  51. }
  52. function add_funds_sidebar(MenuItem $primarySidebar) {
  53. $filename = APP::getCurrentFileName();
  54. $client = Menu::context("client");
  55. $clientid = intval( $client->id );
  56. $action = $_GET['action'];
  57. $allowed = array('invoices', 'quotes', 'masspay', 'addfunds');
  58. /* prevents balance display to unauth'd users */
  59. if ($filename!=='clientarea' || $clientid===0 || strpos($_SERVER['REQUEST_URI'], 'verificationId') !== false || is_null($client)) {
  60. return;
  61. }
  62. // Get PDO for the database queries
  63. $pdo = Capsule::connection()->getPdo();
  64. try {
  65. $clientquery = $pdo->query("SELECT tblcustomfields.fieldname AS customfieldname, tblcustomfieldsvalues.value AS customfieldvalue FROM tblcustomfields INNER JOIN tblcustomfieldsvalues ON tblcustomfields.id = tblcustomfieldsvalues.fieldid WHERE tblcustomfieldsvalues.relid = " . $pdo->quote($client->id));
  66. while($row = $clientquery->fetch(PDO::FETCH_ASSOC)) {
  67. if (trim($row['customfieldname']) == "Enable Add Funds") {
  68. $addfunds = $row['customfieldvalue'];
  69. }
  70. }
  71. } catch(PDOException $e) {
  72. echo 'ERROR: ' . $e->getMessage();
  73. }
  74. /* uncomment this to hide the sidebar if the client has no balance */
  75. if ($client->credit <= 0.00 && $addfunds !== 'on' && filter_var($_GET['action'], FILTER_SANITIZE_STRING) !== 'addfunds') { return; }
  76. $primarySidebar->addChild('Client-Balance', array(
  77. 'label' => Lang::trans('availcreditbal'),
  78. 'uri' => '#',
  79. 'order' => '1',
  80. 'icon' => 'fa fa-credit-card'
  81. ));
  82. # Get Currency
  83. $getCurrency = getCurrency($clientid);
  84. $balanceDisplay = formatCurrency($client->credit, $getCurrency);
  85. # Retrieve the panel we just created.
  86. $balancePanel = $primarySidebar->getChild('Client-Balance');
  87. // Move the panel to the end of the sorting order so it's always displayed
  88. // as the last panel in the sidebar.
  89. $balancePanel->moveToBack();
  90. $balancePanel->setOrder(0);
  91. # Add Balance.
  92. if ($addfunds == 'on' && filter_var($_GET['action'], FILTER_SANITIZE_STRING) !== 'addfunds') {
  93. $balancePanel->addChild('balance-amount', array(
  94. 'uri' => 'clientarea.php?action=addfunds',
  95. 'label' => '<h4 style="text-align:center;">'.$balanceDisplay.'</h4>',
  96. 'order' => 1
  97. ));
  98. $balancePanel->setFooterHtml(
  99. '<a href="clientarea.php?action=addfunds" class="btn btn-success btn-sm btn-block">
  100. <i class="fa fa-plus"></i> Add Funds </a>'
  101. );
  102. }else{
  103. $balancePanel->addChild('balance-amount', array(
  104. 'label' => '<h4 style="text-align:center;">'.$balanceDisplay.'</h4>',
  105. 'order' => 1
  106. ));
  107. }
  108. }
  109. add_hook("ClientAreaPageAddFunds", 0, "restrict_access_to_add_funds");
  110. add_hook("ClientAreaPrimaryNavbar", 0, "restrict_add_funds_link");
  111. add_hook("ClientAreaSecondarySidebar", 0, "add_funds_sidebar");
  112. ?>