clientDetailsForm.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * components/clientDetailsForm.php
  4. *
  5. * Client selection dropdown with auto-fill functionality.
  6. * Replaces modX [[!clientDetailsFORM]] snippet.
  7. */
  8. // TODO: Replace with proper session-based user authentication
  9. $userId = $_SESSION['user_id'] ?? 1; // Temporary fallback
  10. try {
  11. // Database connection - TODO: Move to centralized config
  12. $pdo = new PDO(
  13. 'mysql:host=localhost;dbname=cropmonitor;charset=utf8',
  14. 'cropmonitor',
  15. 'brvnCcaEYxlPCS3',
  16. [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
  17. );
  18. // Prepare and execute query with parameterized statement
  19. $stmt = $pdo->prepare("SELECT id, client, company, email, address FROM client_records WHERE modx_user_id = ? ORDER BY client ASC");
  20. $stmt->execute([$userId]);
  21. $clients = $stmt->fetchAll(PDO::FETCH_ASSOC);
  22. } catch (PDOException $e) {
  23. // TODO: Proper error logging and user-friendly error display
  24. error_log("Database error in clientDetailsForm: " . $e->getMessage());
  25. $clients = [];
  26. }
  27. ?>
  28. <div class="form-group">
  29. <label for="selectUsers" class="form-label">Select Client</label>
  30. <select id="selectUsers" name="client_id" class="form-control form-control-sm">
  31. <option value="">Select current client or add new client below...</option>
  32. <?php foreach ($clients as $client): ?>
  33. <option value="<?= htmlspecialchars($client['id'], ENT_QUOTES, 'UTF-8') ?>"
  34. data-client="<?= htmlspecialchars($client['client'], ENT_QUOTES, 'UTF-8') ?>"
  35. data-company="<?= htmlspecialchars($client['company'], ENT_QUOTES, 'UTF-8') ?>"
  36. data-email="<?= htmlspecialchars($client['email'], ENT_QUOTES, 'UTF-8') ?>"
  37. data-address="<?= htmlspecialchars($client['address'], ENT_QUOTES, 'UTF-8') ?>">
  38. <?= htmlspecialchars($client['id'] . " - " . $client['client'] . " - " . $client['company'], ENT_QUOTES, 'UTF-8') ?>
  39. </option>
  40. <?php endforeach; ?>
  41. <option value="new" class="text-primary fw-bold">➕ Add New Client...</option>
  42. </select>
  43. <div class="form-text">Choose an existing client to auto-fill the form below.</div>
  44. </div>
  45. <script type="text/javascript">
  46. document.getElementById('selectUsers').addEventListener('change', function() {
  47. const selectedOption = this.options[this.selectedIndex];
  48. if (!selectedOption.value) {
  49. // Clear fields if "Select..." is chosen
  50. clearClientFields();
  51. return;
  52. }
  53. if (selectedOption.value === 'new') {
  54. // This will be handled by the modal's change event listener
  55. return;
  56. }
  57. const client = selectedOption.getAttribute('data-client') || '';
  58. const company = selectedOption.getAttribute('data-company') || '';
  59. const email = selectedOption.getAttribute('data-email') || '';
  60. const fullAddress = selectedOption.getAttribute('data-address') || '';
  61. // Parse address components (assuming format: "street, town, state, postcode")
  62. const addressParts = fullAddress.split(', ');
  63. const siteAddress = addressParts[0] || '';
  64. const town = addressParts.slice(1, -2).join(', ') || '';
  65. const state = addressParts[addressParts.length - 2] || '';
  66. const postcode = addressParts[addressParts.length - 1] || '';
  67. // Fill form fields
  68. const nameField = document.getElementById('name');
  69. const companyField = document.getElementById('company');
  70. const emailField = document.getElementById('email');
  71. const siteAddressField = document.getElementById('site_address');
  72. const postalAddressField = document.getElementById('postal_address');
  73. if (nameField) nameField.value = client;
  74. if (companyField) companyField.value = company;
  75. if (emailField) emailField.value = email;
  76. if (siteAddressField) siteAddressField.value = siteAddress;
  77. if (postalAddressField) postalAddressField.value = town + (town && (state || postcode) ? ', ' : '') + state + (state && postcode ? ', ' : '') + postcode;
  78. });
  79. // Function to add new client to dropdown
  80. function addClientToDropdown(clientData) {
  81. const select = document.getElementById('selectUsers');
  82. const option = document.createElement('option');
  83. option.value = clientData.id;
  84. option.setAttribute('data-client', clientData.name);
  85. option.setAttribute('data-company', clientData.company);
  86. option.setAttribute('data-email', clientData.email);
  87. option.setAttribute('data-address', clientData.address);
  88. option.textContent = clientData.id + ' - ' + clientData.name + ' - ' + clientData.company;
  89. // Insert before the "Add New Client" option
  90. const addNewOption = select.querySelector('option[value="new"]');
  91. select.insertBefore(option, addNewOption);
  92. // Select the new client
  93. select.value = clientData.id;
  94. select.dispatchEvent(new Event('change'));
  95. }
  96. // Make function globally available for the modal
  97. window.addClientToDropdown = addClientToDropdown;
  98. function clearClientFields() {
  99. const fields = ['name', 'company', 'email', 'site_address', 'postal_address'];
  100. fields.forEach(fieldId => {
  101. const field = document.getElementById(fieldId);
  102. if (field) field.value = '';
  103. });
  104. }
  105. </script>