Permalink
Browse files

updating example8 with major functionality, working up a chat applica…

…tion
  • Loading branch information...
1 parent 8df596e commit 3018f342620341f0a1e5cdd8af11160e3a2d898f @enygma committed May 7, 2012
@@ -40,6 +40,7 @@ public function init()
->addActionContext('example5update', array('xml', 'json'))
->addActionContext('example6submit', array('xml', 'json'))
->addActionContext('example8submit', array('xml', 'json'))
+ ->addActionContext('example8read', array('xml', 'json'))
->setAutoJsonSerialization(true)
->initContext();
}
@@ -230,19 +231,18 @@ public function example8Action()
// chat application
}
- public function example8submitAction()
+ public function example8readAction()
{
- // gets the submit of the chat message
- error_log(__FUNCTION__);
-
- error_log($this->getRequest()->getParam('username'));
+ $request = json_decode($this->getRequest()->getRawBody());
+ $msg = new Application_Model_Chatmessage();
- $users = new Application_Model_Chatuser();
- //$db = $users::getDefaultAdapter();
- //$users->
+ if (isset($request->message)) {
+ // add the message!
+ $msg->add($request->message);
+ }
- $this->view->success = true;
- $this->view->message = 'okay!';
+ // get the latest messages from the list
+ $this->view->messages = $msg->getLatest(20);
}
private function _checkDbLogin($values)
@@ -30,6 +30,10 @@ class Application_Model_Chatmessage extends Zend_Db_Table_Abstract
*/
protected $_name = 'chat_messages';
+ protected $_dependentTables = array('Application_Model_Chatuser');
+
+ //private $user = null;
+
/**
* Initialize the model object
*
@@ -39,5 +43,44 @@ public function init()
{
//nothing to see, move along
}
+
+ /**
+ * Get the latest # of messages from the queue
+ *
+ * @param int $limit Limit the number of values returned
+ */
+ public function getLatest($limit=10)
+ {
+ $results = $this->fetchAll(
+ $this->select()
+ ->order('date_posted DESC')
+ ->limit($limit)
+ );
+
+ $messages = array();
+ foreach($results as $message) {
+
+ $related = $message->findDependentRowset('Application_Model_Chatuser');
+ $messages[] = array(
+ 'message' => $message->message,
+ 'username'=> $related->current()->username,
+ 'userId' => $message->chatuser_id,
+ 'id' => $message->ID,
+ 'ts' => (int)$message->date_posted,
+ 'posted' => date('m.d.y H:i',$message->date_posted)
+ );
+ }
+
+ return $messages;
+ }
+ public function add($message)
+ {
+ $data = array(
+ 'message' => $message,
+ 'chatuser_id' => 1,
+ 'date_posted' => time()
+ );
+ $this->insert($data);
+ }
}
@@ -30,6 +30,14 @@ class Application_Model_Chatuser extends Zend_Db_Table_Abstract
*/
protected $_name = 'chat_user';
+ protected $_referenceMap = array(
+ 'User' => array(
+ 'columns' => 'ID',
+ 'refTableClass' => 'Application_Model_Chatmessage',
+ 'refColumns' => 'chatuser_id'
+ )
+ );
+
/**
* Initialize the model object
*
@@ -11,10 +11,16 @@ Ext.application({
console.log('Launch application "example8"');
Ext.create('Ext.container.Viewport', {
- layout: 'fit',
+ layout: 'border',
items: [
{
- xtype : 'userwin'
+ xtype : 'userlistgrid',
+ region : 'west',
+ width : 150
+ },
+ {
+ xtype : 'chatwin',
+ region : 'center'
}
]
});
@@ -3,19 +3,50 @@ Ext.define('example8.controller.Index', {
extend: 'Ext.app.Controller',
models: [
- 'User'
+ 'User',
+ 'Userlist',
+ 'Message'
],
views: [
- 'index.Userwin'
+ 'index.Userwin',
+ 'index.Userlist',
+ 'index.Chatwin',
+ 'index.Messagelist',
+ 'index.Chatinput'
],
stores: [
- 'Currentuser'
+ 'Currentuser',
+ 'Userlist',
+ 'Messagelist'
],
init: function() {
console.log('init Index controller');
indexController = this;
+
+ this.control({
+ 'textfield': {
+ specialkey: function(field,e) {
+ if(e.getKey() == e.ENTER) {
+ this.updateMessages();
+ }
+ }
+ }
+ });
+ },
+
+ updateMessages: function() {
+ var formValues = Ext.getCmp('chatForm').getValues();
+ var messageStore = indexController.getStore('Messagelist');
+
+ messageStore.add({
+ message: formValues.chatMessage,
+ ts: Math.round((new Date()).getTime()/1000)
+ });
+ messageStore.sort('ts','DESC');
+
+ Ext.getCmp('chatMessage').setValue('');
}
});
@@ -0,0 +1,12 @@
+Ext.define('example8.model.Message', {
+
+ extend: 'Ext.data.Model',
+ fields: [
+ { name: 'message', type: 'string' },
+ { name: 'posted', type: 'string' },
+ { name: 'username', type: 'string' },
+ { name: 'ts', type: 'int' },
+ { name: 'id', type: 'int' }
+ ]
+
+});
@@ -0,0 +1,6 @@
+Ext.define('example8.model.Userlist', {
+
+ extend: 'Ext.data.Model',
+ fields: ['username','id']
+
+});
@@ -0,0 +1,17 @@
+Ext.define('example8.store.Messagelist', {
+
+ extend: 'Ext.data.Store',
+ model: 'example8.model.Message',
+
+ proxy: {
+ type: 'ajax',
+ url: '/example/example8read/format/json',
+ reader: {
+ type: 'json',
+ root: 'messages'
+ }
+ },
+ autoLoad: true,
+ autoSync: true
+
+});
@@ -0,0 +1,6 @@
+Ext.define('example8.store.Userlist', {
+
+ extend: 'Ext.data.Store',
+ model: 'example8.model.Userlist'
+
+});
@@ -0,0 +1,30 @@
+Ext.define('example8.view.index.Chatinput',{
+
+ extend: 'Ext.panel.Panel',
+ alias: 'widget.chatinput',
+ border: false,
+ height: 30,
+ items: [
+ {
+ xtype: 'form',
+ id: 'chatForm',
+ url: '/example/example8submit/format/json',
+ layout: 'column',
+ border: false,
+ bodyStyle: 'padding: 3px',
+ items: [
+ {
+ xtype: 'textfield',
+ name: 'chatMessage',
+ id: 'chatMessage',
+ columnWidth: .90,
+ required: true
+ },
+ {
+ xtype: 'button',
+ text: 'Send'
+ }
+ ]
+ }
+ ]
+});
@@ -0,0 +1,18 @@
+Ext.define('example8.view.index.Chatwin', {
+
+ extend: 'Ext.panel.Panel',
+ alias: 'widget.chatwin',
+ title: 'Latest Chat Messages',
+ layout: 'border',
+ items: [
+ {
+ xtype: 'messagelist',
+ region: 'center'
+ },
+ {
+ xtype: 'chatinput',
+ region: 'south'
+ }
+ ]
+
+});
@@ -0,0 +1,23 @@
+Ext.define('example8.view.index.Messagelist', {
+
+ extend: 'Ext.grid.Panel',
+ alias: 'widget.messagelist',
+
+ //title : 'Sample Grid',
+ store : 'Messagelist',
+
+ hideHeaders: true,
+
+ initComponent: function() {
+
+ // column definition for the grid
+ this.columns = [
+ {header:'Date',dataIndex:'posted',width:100},
+ {header:'User',dataIndex:'username',width:90},
+ {header:'Message',dataIndex:'message',flex:1}
+ ];
+
+ this.callParent(arguments);
+ }
+
+});
@@ -0,0 +1,27 @@
+Ext.define('example8.view.index.Userlist', {
+
+ /** Extend the default grid */
+ extend : 'Ext.grid.Panel',
+
+ /** Assign an alias to the widget */
+ alias : 'widget.userlistgrid',
+
+ /** Give the grid a title */
+ title : 'Current Users',
+
+ /** The store to pull data from */
+ store : 'Userlist',
+
+ /** Initialize the grid */
+ initComponent: function() {
+ console.log('init sample grid');
+
+ // column definition for the grid
+ this.columns = [
+ {header:'Username',dataIndex:'username',flex:1}
+ ];
+
+ this.callParent(arguments);
+ }
+
+});
@@ -44,6 +44,9 @@ Ext.define('example8.view.index.Userwin', {
success: function(form,action) {
console.log('Form submit success! Response: '+action.result.message);
+ // get the user's ID from the response
+
+
var formValues = form.getValues();
console.log(formValues);

0 comments on commit 3018f34

Please sign in to comment.