| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | if (!defined('DEBUG_MODE')) { die(); } |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | class Hm_Handler_process_add_contact_from_message extends Hm_Handler_Module { |
| 15: | public function process() { |
| 16: | list($success, $form) = $this->process_form(array('contact_source', 'contact_value')); |
| 17: | if (!$success) { |
| 18: | return; |
| 19: | } |
| 20: | list($type, $source) = explode(':', $form['contact_source']); |
| 21: | if ($type == 'local' && $source == 'local') { |
| 22: | $addresses = Hm_Address_Field::parse($form['contact_value']); |
| 23: | if (!empty($addresses)) { |
| 24: | $contacts = $this->get('contact_store'); |
| 25: | foreach ($addresses as $vals) { |
| 26: | $contacts->add_contact(array('source' => 'local', 'email_address' => $vals['email'], 'display_name' => $vals['name'], 'group' => isset($vals['contact_group']) ? $vals['contact_group'] : 'Personal Addresses')); |
| 27: | } |
| 28: | Hm_Msgs::add('Contact Added'); |
| 29: | } |
| 30: | } |
| 31: | } |
| 32: | } |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | class Hm_Handler_process_delete_contact extends Hm_Handler_Module { |
| 38: | public function process() { |
| 39: | $contacts = $this->get('contact_store'); |
| 40: | list($success, $form) = $this->process_form(array('contact_type', 'contact_source', 'contact_id')); |
| 41: | if ($success && $form['contact_type'] == 'local' && $form['contact_source'] == 'local') { |
| 42: | if ($contacts->delete($form['contact_id'])) { |
| 43: | $this->out('contact_deleted', 1); |
| 44: | Hm_Msgs::add('Contact Deleted'); |
| 45: | } |
| 46: | } |
| 47: | } |
| 48: | } |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | class Hm_Handler_process_add_contact extends Hm_Handler_Module { |
| 54: | public function process() { |
| 55: | $contacts = $this->get('contact_store'); |
| 56: | list($success, $form) = $this->process_form(array('contact_source', 'contact_email', 'contact_name', 'add_contact')); |
| 57: | if ($success && $form['contact_source'] == 'local') { |
| 58: | $details = array('source' => 'local', 'email_address' => $form['contact_email'], 'display_name' => $form['contact_name']); |
| 59: | if (array_key_exists('contact_phone', $this->request->post) && $this->request->post['contact_phone']) { |
| 60: | $details['phone_number'] = $this->request->post['contact_phone']; |
| 61: | } |
| 62: | if (array_key_exists('contact_group', $this->request->post) && $this->request->post['contact_group']) { |
| 63: | $details['group'] = $this->request->post['contact_group']; |
| 64: | } |
| 65: | else { |
| 66: | $details['group'] = 'Personal Addresses'; |
| 67: | } |
| 68: | $contacts->add_contact($details); |
| 69: | Hm_Msgs::add('Contact Added'); |
| 70: | } |
| 71: | } |
| 72: | } |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | class Hm_Handler_process_import_contact extends Hm_Handler_Module { |
| 78: | public function process() { |
| 79: | list($success, $form) = $this->process_form(array('contact_source', 'import_contact')); |
| 80: | if ($success && $form['contact_source'] == 'csv') { |
| 81: | $file = $this->request->files['contact_csv']; |
| 82: | $csv = fopen($file['tmp_name'], 'r'); |
| 83: | if ($csv) { |
| 84: | $contacts = $this->get('contact_store'); |
| 85: | $header = fgetcsv($csv); |
| 86: | $expectedHeader = array('display_name', 'email_address', 'phone_number'); |
| 87: | |
| 88: | if ($header !== $expectedHeader) { |
| 89: | fclose($csv); |
| 90: | Hm_Msgs::add('Invalid CSV file, please use a valid header: '.implode(', ', $expectedHeader), 'danger'); |
| 91: | return; |
| 92: | } |
| 93: | |
| 94: | $contact_list = $contacts->getAll(); |
| 95: | $message = ''; |
| 96: | $update_count = 0; |
| 97: | $create_count = 0; |
| 98: | $invalid_mail_count = 0; |
| 99: | $import_result = []; |
| 100: | |
| 101: | |
| 102: | while (($data = fgetcsv($csv)) !== FALSE) { |
| 103: | $single_contact = [ |
| 104: | 'display_name' => $data[0], |
| 105: | 'email_address' => $data[1], |
| 106: | 'phone_number' => $data[2] ?? '' |
| 107: | ]; |
| 108: | $email = $data[1]; |
| 109: | if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { |
| 110: | $single_contact['status'] = 'invalid email'; |
| 111: | array_push($import_result, $single_contact); |
| 112: | $invalid_mail_count++; |
| 113: | continue; |
| 114: | } |
| 115: | |
| 116: | $details = array('source' => 'local', 'display_name' => $data[0], 'email_address' => $email); |
| 117: | if (array_key_exists(2, $data) && $data[2]) { |
| 118: | $details['phone_number'] = $data[2]; |
| 119: | } |
| 120: | |
| 121: | $contactUpdated = false; |
| 122: | foreach ($contact_list as $key => $contact) { |
| 123: | if ($contact->value('email_address') == $email) { |
| 124: | $contacts->update_contact($key, $details); |
| 125: | $single_contact['status'] = 'update'; |
| 126: | array_push($import_result, $single_contact); |
| 127: | $update_count++; |
| 128: | $contactUpdated = true; |
| 129: | continue 2; |
| 130: | } |
| 131: | } |
| 132: | |
| 133: | if (!$contactUpdated) { |
| 134: | $contacts->add_contact($details); |
| 135: | $single_contact['status'] = 'new'; |
| 136: | array_push($import_result, $single_contact); |
| 137: | $create_count++; |
| 138: | } |
| 139: | } |
| 140: | fclose($csv); |
| 141: | $contacts->save(); |
| 142: | $this->session->record_unsaved('Contact Created'); |
| 143: | $type = 'danger'; |
| 144: | if (isset($import_result) && (!$create_count && !$update_count)) { |
| 145: | $message = $create_count.' contacts created, '.$update_count.' contacts updated, '.$invalid_mail_count.' Invalid email address'; |
| 146: | $type = 'warning'; |
| 147: | } elseif (isset($import_result) && ($create_count || $update_count)) { |
| 148: | $message = $create_count.' contacts created, '.$update_count.' contacts updated, '.$invalid_mail_count.' Invalid email address'; |
| 149: | $type = 'success'; |
| 150: | } else { |
| 151: | $message = 'An error occured'; |
| 152: | } |
| 153: | |
| 154: | $this->session->set('imported_contact', $import_result); |
| 155: | Hm_Msgs::add($message, $type); |
| 156: | } |
| 157: | } |
| 158: | } |
| 159: | } |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | class Hm_Handler_process_edit_contact extends Hm_Handler_Module { |
| 165: | public function process() { |
| 166: | $contacts = $this->get('contact_store'); |
| 167: | list($success, $form) = $this->process_form(array('contact_source', 'contact_id', 'contact_email', 'contact_name', 'edit_contact')); |
| 168: | if ($success && $form['contact_source'] == 'local') { |
| 169: | $details = array('email_address' => $form['contact_email'], 'display_name' => $form['contact_name']); |
| 170: | if (array_key_exists('contact_phone', $this->request->post)) { |
| 171: | $details['phone_number'] = $this->request->post['contact_phone']; |
| 172: | } |
| 173: | if (array_key_exists('contact_group', $this->request->post)) { |
| 174: | $details['group'] = $this->request->post['contact_group']; |
| 175: | } |
| 176: | else { |
| 177: | $details['group'] = 'Personal Addresses'; |
| 178: | } |
| 179: | if ($contacts->update_contact($form['contact_id'], $details)) { |
| 180: | Hm_Msgs::add('Contact Updated'); |
| 181: | } |
| 182: | } |
| 183: | } |
| 184: | } |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | class Hm_Handler_load_edit_contact extends Hm_Handler_Module { |
| 190: | public function process() { |
| 191: | if (array_key_exists('contact_source', $this->request->get) && $this->request->get['contact_source'] == 'local' |
| 192: | && array_key_exists('contact_type', $this->request->get) && $this->request->get['contact_type'] == 'local' && |
| 193: | array_key_exists('contact_id', $this->request->get)) { |
| 194: | $contacts = $this->get('contact_store'); |
| 195: | $contact = $contacts->get($this->request->get['contact_id']); |
| 196: | if (is_object($contact)) { |
| 197: | $current = $contact->export(); |
| 198: | $current['id'] = $this->request->get['contact_id']; |
| 199: | $this->out('current_contact', $current); |
| 200: | } |
| 201: | } |
| 202: | } |
| 203: | } |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | class Hm_Handler_load_local_contacts extends Hm_Handler_Module { |
| 209: | public function process() { |
| 210: | $this->append('contact_sources', 'local'); |
| 211: | $this->append('contact_edit', 'local:local'); |
| 212: | } |
| 213: | } |
| 214: | |
| 215: | |
| 216: | |
| 217: | |
| 218: | class Hm_Output_contacts_form extends Hm_Output_Module { |
| 219: | protected function output() { |
| 220: | |
| 221: | $email = ''; |
| 222: | $name = ''; |
| 223: | $phone = ''; |
| 224: | $form_class = 'contact_form'; |
| 225: | $button = '<input class="btn btn-primary add_contact_submit" type="submit" name="add_contact" value="'.$this->trans('Add').'" />'; |
| 226: | $title = $this->trans('Add Local'); |
| 227: | $current = $this->get('current_contact', array()); |
| 228: | if (!empty($current)) { |
| 229: | if (array_key_exists('email_address', $current)) { |
| 230: | $email = $current['email_address']; |
| 231: | } |
| 232: | if (array_key_exists('display_name', $current)) { |
| 233: | $name = $current['display_name']; |
| 234: | } |
| 235: | if (array_key_exists('phone_number', $current)) { |
| 236: | $phone = $current['phone_number']; |
| 237: | } |
| 238: | if (array_key_exists('group', $current)) { |
| 239: | $group = $current['group']; |
| 240: | } |
| 241: | $form_class = 'contact_update_form mt-3'; |
| 242: | $title = $this->trans('Update Local'); |
| 243: | $button = '<input type="hidden" name="contact_id" value="'.$this->html_safe($current['id']).'" />'. |
| 244: | '<input class="btn btn-primary edit_contact_submit" type="submit" name="edit_contact" value="'.$this->trans('Update').'" />'; |
| 245: | } |
| 246: | return '<div class="add_contact_responsive"><form class="add_contact_form" method="POST">'. |
| 247: | '<button class="server_title mt-2 btn btn-light"><i class="bi bi-person-add me-2"></i>'.$title.'</button>'. |
| 248: | '<div class="'.$form_class.'">'. |
| 249: | '<input type="hidden" name="contact_source" value="local" />'. |
| 250: | '<input type="hidden" name="hm_page_key" value="'.$this->html_safe(Hm_Request_Key::generate()).'" />'. |
| 251: | '<label class="form-label" for="contact_email">'.$this->trans('E-mail Address').' *</label>'. |
| 252: | '<input class="form-control" required placeholder="'.$this->trans('E-mail Address').'" id="contact_email" type="email" name="contact_email" '. |
| 253: | 'value="'.$this->html_safe($email).'" /><br />'. |
| 254: | '<label class="form-label" for="contact_name">'.$this->trans('Full Name').' *</label>'. |
| 255: | '<input class="form-control" required placeholder="'.$this->trans('Full Name').'" id="contact_name" type="text" name="contact_name" '. |
| 256: | 'value="'.$this->html_safe($name).'" /><br />'. |
| 257: | '<label class="form-label" for="contact_phone">'.$this->trans('Telephone Number').'</label>'. |
| 258: | '<input class="form-control" placeholder="'.$this->trans('Telephone Number').'" id="contact_phone" type="text" name="contact_phone" '. |
| 259: | 'value="'.$this->html_safe($phone).'" /><br />'. |
| 260: | '<label class="screen_reader" for="contact_group">'.$this->trans('Contact Group').'</label>'. |
| 261: | '<select class="form-select" id="contact_group" name="contact_group">'. |
| 262: | '<option value="'.$this->trans('Personal Addresses').'"'.(isset($group) && $this->html_safe($group) == $this->trans('Personal Addresses') ? ' selected' : '').'>'.$this->trans('Personal Addresses').'</option>'. |
| 263: | '<option value="'.$this->trans('Trusted Senders').'"'.(isset($group) && $this->html_safe($group) == $this->trans('Trusted Senders') ? ' selected' : '').'>'.$this->trans('Trusted Senders').'</option>'. |
| 264: | '<option value="'.$this->trans('Collected Recipients').'"'.(isset($group) && $this->html_safe($group) == $this->trans('Collected Recipients') ? ' selected' : '').'>'.$this->trans('Collected Recipients').'</option>' . |
| 265: | '</select><br />'. |
| 266: | $button.' <input type="button" class="btn btn-secondary reset_contact" value="'. |
| 267: | $this->trans('Cancel').'" /></div></form></div>'; |
| 268: | } |
| 269: | } |
| 270: | |
| 271: | |
| 272: | |
| 273: | |
| 274: | class Hm_Output_import_contacts_form extends Hm_Output_Module { |
| 275: | protected function output() { |
| 276: | $form_class = 'contact_form'; |
| 277: | $button = '<input class="btn btn-primary add_contact_submit" type="submit" name="import_contact" id="import_contact" value="'.$this->trans('Add').'" />'; |
| 278: | $notice = 'Please ensure your CSV header file follows the format: display_name,email_address,phone_number'; |
| 279: | $title = $this->trans('Import from CSV file'); |
| 280: | $csv_sample_path = WEB_ROOT.'modules/local_contacts/assets/data/contact_sample.csv'; |
| 281: | |
| 282: | return '<div class="add_contact_responsive"><form class="add_contact_form" method="POST" enctype="multipart/form-data">'. |
| 283: | '<button class="server_title mt-2 btn btn-light" title="'.$notice.'"><i class="bi bi-person-add me-2"></i>'.$title.'</button>'. |
| 284: | '<div class="'.$form_class.'">'. |
| 285: | '<div><a href="'.$csv_sample_path.'" data-external="true">'.$this->trans('download a sample csv file').'</a></div><br />'. |
| 286: | '<input type="hidden" name="contact_source" value="csv" />'. |
| 287: | '<input type="hidden" name="hm_page_key" value="'.$this->html_safe(Hm_Request_Key::generate()).'" />'. |
| 288: | '<label class="screen_reader" for="contact_csv">'.$this->trans('Csv File').'</label>'. |
| 289: | '<input class="form-control" required id="contact_csv" type="file" name="contact_csv" accept=".csv"/> <br />'.$button.' <input type="button" class="btn btn-secondary reset_contact" value="'. |
| 290: | $this->trans('Cancel').'" /></div></form></div>'; |
| 291: | } |
| 292: | } |
| 293: | |