From aeb6c21bf257845047fe421fadce4a6b02b5a827 Mon Sep 17 00:00:00 2001 From: "James P. Walters" Date: Sun, 1 May 2016 19:32:52 -0400 Subject: [PATCH] Two Digit Country Code - BUG Fix BUG: The plugin previously assumed only a 1 digit country code. FIX: The plugin now supports 1 and 2 digit country codes. Note: Unfortunately some country codes have up to 4 digits and some phone numbers are longer than 10 digits. The plugin currently only supports 1 or 2 digit country codes and 10 digit phone numbers. All other phones are not supported and likely will cause the plugin not function properly. Issue #27 --- sms-conversations/js/index.js | 29 +++++++++++++++++++------- sms-conversations/sms-conversation.php | 2 +- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/sms-conversations/js/index.js b/sms-conversations/js/index.js index 2ded99a..9d3f1b6 100644 --- a/sms-conversations/js/index.js +++ b/sms-conversations/js/index.js @@ -1,6 +1,16 @@ function formatePhoneNumber(phone) { - return phone.substr(0,2) + ' ' + '(' + phone.substr(2,3) + ') ' + phone.substr(5,3) + '-' + phone.substring(8); + var international = (phone.length === 13 ?1:0); + + var formatted_number = ''; + if(phone.length === 10) { + formatted_number = '(' + phone.substr(0,3) + ') ' + phone.substr(3,3) + '-' + phone.substring(6) + } + else { + formatted_number = phone.substr(0,2 + international) + ' ' + '(' + phone.substr(2 + international,3) + ') ' + phone.substr(5 + international,3) + '-' + phone.substring(8 + international) + } + + return formatted_number; } function getSmsHistory(smsId) { @@ -24,8 +34,11 @@ function getSmsHistory(smsId) { smsTimeStamp = date.toLocaleDateString() + ' ' + date.toLocaleTimeString(); } - if(firstTo == '' || firstTo == v.to) { - firstTo = v.to; + //Country code is not alway present. Therefore we'll match by only 10 digits. + var to_phone = v.to.substr(v.to.length-10); + + if(firstTo == '' || firstTo == to_phone) { + firstTo = to_phone; message = '
  • '; message += '
    '; @@ -34,7 +47,7 @@ function getSmsHistory(smsId) { } message += '' + smsTimeStamp + '    '; - message += '' + v.to + '
    '; + message += '' + formatePhoneNumber(v.to) + ''; message += '
    '; if(v.call_sid.substr(0,2)==='MM') { @@ -52,11 +65,11 @@ function getSmsHistory(smsId) { message += v.message; message += '
  • '; } - else if(secondTo == '' || secondTo == v.to){ - secondTo = v.to; + else if(secondTo == '' || secondTo == to_phone){ + secondTo = to_phone; message = '
  • '; message += '
    '; - message += '' + v.to + ''; + message += '' + formatePhoneNumber(v.to) + ''; message += '' + smsTimeStamp + ''; if(v.call_sid.substr(0,2)==='MM') { @@ -208,7 +221,7 @@ $(document).ready(function(){ var txt = $(this).closest('tr').children('td.from').text() $('.chat-with').text('Chat with ' + txt); $('#sms-to-phone').val('+' + caller_id[0]); - $('#sms-from-phone').val($(this).closest('tr').children('td.to').text().substr(3)); + $('#sms-from-phone').val('+' + caller_id[1]); getSmsHistory('+' + caller_id[0] + '-+' + caller_id[1]); }); diff --git a/sms-conversations/sms-conversation.php b/sms-conversations/sms-conversation.php index ed5a050..c41a042 100644 --- a/sms-conversations/sms-conversation.php +++ b/sms-conversations/sms-conversation.php @@ -14,7 +14,7 @@ //first mark all messages are read $this->db->query("update `messages` set `read`=current_timestamp, `status`='read' where status='new' and caller='".$caller_id."' and called='".$called_id."'"); - $sql = "select (select id from `messages` where caller = '". $caller_id ."' and called = '". $called_id ."' order by created desc limit 1) as 'id',data.* from (select created, call_sid, caller as 'to',called as 'from',content_text as 'message' from messages where caller = '". $caller_id ."' and called = '". $called_id ."' union select created, '' as 'call_sid', concat('+1',Replace(Replace(Replace(Replace(SUBSTRING(description,1,14),'(',''),')',''),' ',''),'-','')) as 'to', concat('+1',Replace(Replace(Replace(Replace(SUBSTRING(description,19,14),'(',''),')',''),' ',''),'-','')) as 'from', SUBSTRING(description,35) as 'message' from annotations where annotation_type = 6 and message_id in (select id from messages where caller = '". $caller_id ."' and called = '". $called_id ."')) as data order by created"; + $sql = "select (select id from `messages` where caller = '". $caller_id ."' and called = '". $called_id ."' order by created desc limit 1) as 'id',data.* from (select created, call_sid, caller as 'to',called as 'from',content_text as 'message' from messages where caller = '". $caller_id ."' and called = '". $called_id ."' union select created, '' as 'call_sid', Replace(Replace(Replace(Replace(substring(description, 1, instr(description,'to')-2),'(',''),')',''),' ',''),'-','') as 'to', Replace(Replace(Replace(Replace(substring(description, instr(description,'to')+3, instr(description,':')-instr(description,'to')-3),'(',''),')',''),' ',''),'-','') as 'from', substring(description, instr(description,':')+2) as 'message' from annotations where annotation_type = 6 and message_id in (select id from messages where caller = '". $caller_id ."' and called = '". $called_id ."')) as data order by created"; $query = $this->db->query($sql); echo json_encode($query->result());