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 += '';
- 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());