Jump to content
Larry Ullman's Book Forums

Coding A Message Inbox


Edward
 Share

Recommended Posts

I started to code my message inbox, i have an inbox table and i first tried generating the links for the inbox when rendering it but this seems like a huge process. So the other idea would be to create the links and save them to the inbox database table with the html code. This process would seem to be less exhaustive. So do you think saving links in the database with html code is okay?

Link to comment
Share on other sites

Here is the array i will use to render the inbox in the views, this is getting ugly, which is why i think saving html data in the database may be more appropriate. Please excuse my poor Yii coding, i am just hacking away here.

 

public function getInboxMessages($id)
	{
		// Make an array for inbox messages
		$message = array();
		
		$criteria = new CDbCriteria();
		$criteria->condition = 'user_id=:user_id';
		$criteria->params = array(':user_id'=>$id);

		$modelInbox = $this->model()->findAll($criteria);
		
		if($modelInbox===null)
		{
			throw new CHttpException(404,'The requested page does not exist.');		
		} 
		else 
		{
			foreach($modelInbox as $message) 
			
			{
				if($message->type=='offer')
				{
					$modelSender = User::model()->findByPk($message->sender_id);
					$sender_username = $modelSender->username;
					$from = CHtml::link($sender_username, Yii::app()->createUrl('$sender_username'));
					
					$icon = '<i class="icon-hand-right"></i>';
					
					$modelOffer = Offer::model()->with('action')->findByPk($message->type_id);
					$modelAction = $modelOffer->action;
					
					$thread_id = $modelAction->thread_id;
					
					$modelThread = Thread::model()->findByPk($thread_id);
					$item_id = $modelThread->item_id;
					
					$modelItem = Item::model()->findByPk($item_id);
					$item_title = $modelItem->title;
					$link = CHtml::link($item_title, Yii::app()->createUrl('$item_title'));
						
						
    				$messages[] = array("from" => $from, "icon" => $icon, "link" => $link, "date" => $message->date);  
				}
			}
		}
		return $messages;	
	}

Here was the view rendering part

 

<?php
/* @var $this InboxController */
/* @var $dataProvider CActiveDataProvider */

$this->breadcrumbs=array(
	'Inbox',
);
?>

<h1>Message Inbox</h1>

<table class="table table-bordered">
	<tr class="info">
		<td><strong>From</strong></td>
        <td><i></i></td>
		<td><strong>Subject</strong></td>
		<td><strong>Date Received</strong></td>
	</tr>  
<?php

$messages = Inbox::model()->getInboxMessages(4);

foreach($messages as $message)
{
	echo '<tr class="warning">';
	echo "<td>".$message['from']."</td>";
	echo "<td>".$message['icon']."</td>";
	echo "<td>".$message['link']."</td>";
	echo "<td>".$message['date']."</td>";
	echo '</tr>';
}

?>
</table>
Link to comment
Share on other sites

I totally agree with saving these links somehow, instead of generating them dynamically. I gather they cannot be just hardcoded into a view file? If not, then saving them to a database is okay, but saving them as a cached fragment would be better.

  • Upvote 1
Link to comment
Share on other sites

There could be between 30 - 50 of these dynamic inbox links, so that code above would be repeated for each particular case with a case statement. Hardcoding into the view is not really possible as its all dynamic info and then the view would be doing logic, i did try to push the render return array funnction into the model then load that up in the view but it seems like too much processing. Okay ill find a way to generate the links as particular actions are taken. I was thinking of creating a widget like dropdownlist for the views, because i need to make the links bold for unread messages and regular font for read.

Link to comment
Share on other sites

I've not done something like this in YII, but doesn't it make more sense to do this in the view file? Both the user links and the PM links can be done that way. Considering the class is static, this makes more sense to me. The views looks a bit uglier, but they also make more sense.

 

Not to insult you, but you would of course need to change the $messages array bindings for this to work. Just an easy miss...

<?php foreach($messages as $message) : ?>
    <tr class="warning">
       <td><?=CHtml::link($sender_username, Yii::app()->createUrl('$sender_username'));?></td>
       <td><i class="<?=$message['icon'];?>"></i></td>
       <td><?=CHtml::link($item_title, Yii::app()->createUrl('$item_title'));?></td>
       <td><?=$message['date'];?></td>
    </tr>
<?php endforeach; ?>
 

Same with the icon. Place the HTML where it should be. The class name is enough to change the icon if you should ever need it. If not, do that fully in HTML.

Link to comment
Share on other sites

I found another way to handle this, better not to render individual links but give the link its message id, then when it is clicked on that particular link is generated on the fly. That saves having to generate possibly up to 1000 dynamic links at a time.

Link to comment
Share on other sites

 Share

×
×
  • Create New...