This commit is contained in:
Devin Smith 2014-11-27 23:19:18 -08:00
parent 1bc88d9fe7
commit ac59b796e3
4 changed files with 42 additions and 13 deletions

View File

@ -4,7 +4,7 @@ class Crunchbutton_Message_Incoming_Sms extends Cana_Model {
public static function route($request) {
$from = Phone::clean($request['From']);
$to =Phone::clean($request['To']);
$to = Phone::clean($request['To']);
$body = trim($request['Body']);
$admin = Admin::getByPhone($from, true);
@ -21,6 +21,8 @@ class Crunchbutton_Message_Incoming_Sms extends Cana_Model {
'sid' => $request['SmsMessageSid']
];
Phone_Log::log($to, $from, 'message', 'incoming');
// routing for drivers and support
if ($admin->id_admin) {
Log::debug([

View File

@ -104,8 +104,8 @@ class Crunchbutton_Message_Sms extends Crunchbutton_Message {
]);
$ret[] = c::twilio()->account->messages->sendMessage($tfrom, $t, $msg);
Phone_Log::log($t, $tfrom, 'message');
Phone_Log::log($t, $tfrom, 'message', 'outgoing');
} catch (Exception $e) {

View File

@ -17,17 +17,35 @@ class Crunchbutton_Phone extends Cana_Table{
// return a number specific from number based on our numbers in the last 30 days
public function from() {
$phone = Phone::q('
select phone.* from phone_log
select phone.*, phone_log.date
from phone_log
left join phone on phone.id_phone=phone_log.id_phone_from
where phone.phone is not null
and phone_log.id_phone_to="'.$this->id_phone.'"
and datediff(now(), date) < 30
order by date asc
where
phone.id_phone is not null
and phone_log.direction="outgoing"
and phone_log.id_phone_to="'.$this->id_phone.'"
and datediff(now(), date) < 30
group by phone.id_phone
union
select phone.*, phone_log.date
from phone_log
left join phone on phone.id_phone=phone_log.id_phone_to
where
phone.id_phone is not null
and phone_log.direction="incoming"
and phone_log.id_phone_from="'.$this->id_phone.'"
and datediff(now(), date) < 30
group by phone.id_phone
order by date desc
');
foreach ($phone as $p) {
if (in_array($p->phone, self::numbers())) {
$use = $p->phone;
break;
}
}
@ -63,7 +81,7 @@ class Crunchbutton_Phone extends Cana_Table{
$use = $logs[0]->phone;
}
return $use;
return $use ? $use : self::clean(c::config()->phone->support);
}
public static function clean($phone) {

View File

@ -10,14 +10,23 @@ class Crunchbutton_Phone_Log extends Cana_Table{
->load($id);
}
public static function log($to, $from, $type = 'message') {
public static function log($to, $from, $type = 'message', $direction = 'outgoing') {
$to = Phone::byPhone($to)->id_phone;
$from = Phone::byPhone($from)->id_phone;
if (!$to || !$from) {
return false;
}
$log = new Phone_Log([
'id_phone_to' => Phone::byPhone($to)->id_phone,
'id_phone_from' => Phone::byPhone($from)->id_phone,
'id_phone_to' => $to,
'id_phone_from' => $from,
'date' => date('Y-m-d H:i:s'),
'type' => $type
'type' => $type,
'direction' => $direction
]);
$log->save();
return $log;
}