Skip to content

Commit

Permalink
Merge pull request #31 from jpwalters/james/two-digit-country-code-su…
Browse files Browse the repository at this point in the history
…pport

Two Digit Country Code - BUG Fix
  • Loading branch information
jpwalters committed May 1, 2016
2 parents 568f54f + aeb6c21 commit e41949c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
29 changes: 21 additions & 8 deletions sms-conversations/js/index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 = '<li class="clearfix">';
message += '<div class="message-data align-right">';

Expand All @@ -34,7 +47,7 @@ function getSmsHistory(smsId) {
}

message += '<span class="message-data-time" >' + smsTimeStamp + '</span> &nbsp; &nbsp;';
message += '<span class="message-data-name" >' + v.to + '</span></div>';
message += '<span class="message-data-name" >' + formatePhoneNumber(v.to) + '</span></div>';
message += '<div class="message other-message float-right">';

if(v.call_sid.substr(0,2)==='MM') {
Expand All @@ -52,11 +65,11 @@ function getSmsHistory(smsId) {
message += v.message;
message += '</div></li>';
}
else if(secondTo == '' || secondTo == v.to){
secondTo = v.to;
else if(secondTo == '' || secondTo == to_phone){
secondTo = to_phone;
message = '<li>';
message += '<div class="message-data">';
message += '<span class="message-data-name">' + v.to + '</span>';
message += '<span class="message-data-name">' + formatePhoneNumber(v.to) + '</span>';
message += '<span class="message-data-time">' + smsTimeStamp + '</span>';

if(v.call_sid.substr(0,2)==='MM') {
Expand Down Expand Up @@ -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]);
});
Expand Down
2 changes: 1 addition & 1 deletion sms-conversations/sms-conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit e41949c

Please sign in to comment.