1
0
mirror of https://github.com/nerzhul/ocsms.git synced 2025-06-07 16:06:15 +00:00

contrainer for message count (need tests)

This commit is contained in:
Loïc Blot (@U-Exp) 2014-10-13 19:39:55 +02:00
parent 4420139285
commit 00d157ea2c
3 changed files with 49 additions and 25 deletions

View File

@ -115,31 +115,26 @@ class SmsController extends Controller {
$messages = array(); $messages = array();
$phoneNumbers = array(); $phoneNumbers = array();
$msgCount = 0;
$iContacts = $this->app->getInvertedContacts();
// Contact resolved // Contact resolved
if ($contactName != "") { if ($contactName != "" && isset($iContacts[$contactName])) {
$iContacts = $this->app->getInvertedContacts(); $ctPn = count($iContacts[$contactName]);
$messages = array();
// If there is iContacts (this must be) // We merge each message list into global messagelist
if (isset($iContacts[$contactName])) { for ($i=0; $i < $ctPn; $i++) {
$ctPn = count($iContacts[$contactName]); $messages = $messages +
$this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $iContacts[$contactName][$i], $lastDate);
$phoneNumbers[] = $iContacts[$contactName][$i];
// We merge each message list into global messagelist $msgCount += $this->smsMapper->countMessagesForPhoneNumber($this->userId, $iContacts[$contactName][$i]);
for ($i=0; $i < $ctPn; $i++) {
$messages = $messages +
$this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $iContacts[$contactName][$i], $lastDate);
$phoneNumbers[] = $iContacts[$contactName][$i];
}
}
// This case mustn't be reached, but add it.
else {
$messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $phoneNumber, $lastDate);
$phoneNumbers[] = $phoneNumber;
} }
} }
else { else {
$messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $phoneNumber, $lastDate); $messages = $this->smsMapper->getAllMessagesForPhoneNumber($this->userId, $phoneNumber, $lastDate);
$msgCount = $this->smsMapper->countMessagesForPhoneNumber($this->userId, $phoneNumber);
$phoneNumbers[] = $phoneNumber; $phoneNumbers[] = $phoneNumber;
} }
@ -147,7 +142,7 @@ class SmsController extends Controller {
ksort($messages); ksort($messages);
// @ TODO: filter correctly // @ TODO: filter correctly
return new JSONResponse(array("conversation" => $messages, "contactName" => $contactName, "phoneNumbers" => $phoneNumbers)); return new JSONResponse(array("conversation" => $messages, "contactName" => $contactName, "phoneNumbers" => $phoneNumbers, "msgCount" => $msgCount));
} }
/** /**

View File

@ -91,7 +91,7 @@ class SmsMapper extends Mapper {
$result = $query->execute(array($userId, $phoneNumber, 0, 1, $minDate)); $result = $query->execute(array($userId, $phoneNumber, 0, 1, $minDate));
$messageList = array(); $messageList = array();
while($row = $result->fetchRow()) { while ($row = $result->fetchRow()) {
$messageList[$row["sms_date"]] = array( $messageList[$row["sms_date"]] = array(
"msg" => $row["sms_msg"], "msg" => $row["sms_msg"],
"type" => $row["sms_type"] "type" => $row["sms_type"]
@ -100,6 +100,17 @@ class SmsMapper extends Mapper {
return $messageList; return $messageList;
} }
public function countMessagesForPhoneNumber ($userId, $phoneNumber) {
$query = \OCP\DB::prepare('SELECT count(smsdate) as ct FROM ' .
'*PREFIX*ocsms_smsdatas WHERE user_id = ? AND sms_address = ? ' .
'AND sms_mailbox IN (?,?)');
$result = $query->execute(array($userId, $phoneNumber, 0, 1));
if ($row = $result->fetchRow()) {
return $row["ct"];
}
}
public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) { public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) {
\OCP\DB::beginTransaction(); \OCP\DB::beginTransaction();

View File

@ -54,6 +54,8 @@ var refreshConversation = function() {
} }
} }
setMessageCountInfo(jsondata);
if ($('#app-content-header').is(':hidden')) { if ($('#app-content-header').is(':hidden')) {
$('#app-content-header').show(); $('#app-content-header').show();
} }
@ -61,6 +63,20 @@ var refreshConversation = function() {
); );
}; };
function setMessageCountInfo(jsondata) {
if (typeof jsondata['msgCount'] != 'undefined') {
if (jsondata['msgCount'] == 1) {
$('#ocsms-phone-msg-nb').html(jsondata['msgCount'] + ' message');
}
else {
$('#ocsms-phone-msg-nb').html(jsondata['msgCount'] + ' messages');
}
}
else {
$('#ocsms-phone-msg-nb').html('');
}
}
function fetchConversation(phoneNumber) { function fetchConversation(phoneNumber) {
$.getJSON(OC.generateUrl('/apps/ocsms/get/conversation'), $.getJSON(OC.generateUrl('/apps/ocsms/get/conversation'),
{ {
@ -97,6 +113,8 @@ function fetchConversation(phoneNumber) {
$('#ocsms-phone-opt-number').html(phoneNumberLabel); $('#ocsms-phone-opt-number').html(phoneNumberLabel);
} }
setMessageCountInfo(jsondata);
if ($('#app-content-header').is(':hidden')) { if ($('#app-content-header').is(':hidden')) {
$('#app-content-header').show(); $('#app-content-header').show();
} }