2013-12-18 13 views
1

我想在yii中實現一個簡單的收件箱。它從數據庫表讀取消息並顯示它。在yii中實現一個簡單的收件箱中的通知

但我不知道如何顯示閱讀和未讀消息以不同的樣式以及如何實現新消息的通知。

我搜索了很多,但只發現了一些擴展名,我不想使用它們。

它是如此重要的是找到我怎麼能以不同的方式

任何最初的想法會幫助我 的郵箱擴展代碼的一部分顯示未讀郵件:

public function actionInbox($ajax=null) 
{ 
    $this->module->registerConfig($this->getAction()->getId()); 
    $cs =& $this->module->getClientScript(); 
    $cs->registerScriptFile($this->module->getAssetsUrl().'/js/mailbox.js',CClientScript::POS_END); 
    //$js = '$("#mailbox-list").yiiMailboxList('.$this->module->getOptions().');console.log(1)'; 

    //$cs->registerScript('mailbox-js',$js,CClientScript::POS_READY); 


    if(isset($_POST['convs'])) 
    { 
     $this->buttonAction('inbox'); 
    } 
    $dataProvider = new CActiveDataProvider(Mailbox::model()->inbox($this->module->getUserId())); 
    if(isset($ajax)) 
     $this->renderPartial('_mailbox',array('dataProvider'=>$dataProvider)); 
    else{ 
     if(!isset($_GET['Mailbox_sort'])) 
      $_GET['Mailbox_sort'] = 'modified.desc'; 

     $this->render('mailbox',array('dataProvider'=>$dataProvider)); 
    } 
} 
+0

你好看嗎?你的數據庫是怎樣的?如果你的數據庫知道消息何時被讀取,你能不能簡單地在視圖中做一個簡單的檢查,如if($ model-> read){//改變顏色} else {//不改變顏色}或者類似的東西? – Jeroen

+0

我的數據庫有messages.in這個表我存儲發件人和接收者ID,標題,消息文本和一個字段的讀取/未讀,當消息被讀取時爲1。我如何以不同的方式顯示未讀消息以及未讀消息如何在控制器中成爲讀取消息?我還沒有任何視圖 – user3019375

+0

已添加答案。對於原始問題和「未讀消息如何在控制器中讀取消息?」。但是我不這樣做在控制器中。要在控制器(數據庫中的意思是?)中執行此操作,只需將讀取更新爲1,同時從數據庫中獲取消息。 – Jeroen

回答

0

首先所有的腳本事情應該在視圖中。對於你的問題,我會做類似

在控制器

$mailbox = Mailbox::model()->inbox($this->module->getUserId()); //I assume this returns the mailbox from that user? 

$this->renderPartial('_mailbox',compact('mailbox ')); //compact is the same as array('mailbox'=>$mailbox) so use whatever you prefer. 

在視圖中我只會做這樣的事情

<?php foreach($mailbox->messages as $message): 
    $class = ''; //order unread if you want to give both a different class name 
    if($message->read): //if this is true 
      $class = 'read'; 
    endif; ?> 
    <div id='<?= $message->id ?>'class='message $class'> <!-- insert whatever info from the message --></div> 
<?php endforeach; ?> 

因此,現在將增加閱讀的每一個消息類已閱讀。然後在CSS中,你可以簡單地改變它的風格。我希望這是足夠的信息?我使用foreach():endforeach; if():endif;在視圖文件中,但你可以使用foreach(){},但我更喜歡foreach,因爲它看起來更好地結合HTML。

編輯關於你的第二個問題,他們如何閱讀。你可以用JQUERY做這件事。例。

$(".message").on("click", function() { 
    var id = $(this).attr('id'); 
    $.ajax { 
     type:"POST", 
     url: "controller/action/"+id; //the controller action that fetches the message, the Id is the action variable (ex: public function actionGetMessage($id) {}) 
     completed: function(data) { 
      //data = the message information, you might do type: 'JSON' instead. Use it however you want it. 
      if(!$(this).hasClass("read")) 
       $(this).addClass("read"); //give it the class read if it does not have it already 
     } 
    } 
}); 

這只是給讀取的類的div,它應該看起來像讀取類的其他項目。