Skip to content

Commit

Permalink
updating example8 with major functionality, working up a chat applica…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
enygma committed May 7, 2012
1 parent 8df596e commit 3018f34
Show file tree
Hide file tree
Showing 14 changed files with 245 additions and 15 deletions.
20 changes: 10 additions & 10 deletions zfapp/application/controllers/ExampleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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)
Expand Down
43 changes: 43 additions & 0 deletions zfapp/application/models/Chatmessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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);
}
}

8 changes: 8 additions & 0 deletions zfapp/application/models/Chatuser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
10 changes: 8 additions & 2 deletions zfapp/public/js/examples/example8/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
]
});
Expand Down
37 changes: 34 additions & 3 deletions zfapp/public/js/examples/example8/app/controller/Index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
}

});
12 changes: 12 additions & 0 deletions zfapp/public/js/examples/example8/app/model/Message.js
Original file line number Diff line number Diff line change
@@ -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' }
]

});
6 changes: 6 additions & 0 deletions zfapp/public/js/examples/example8/app/model/Userlist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Ext.define('example8.model.Userlist', {

extend: 'Ext.data.Model',
fields: ['username','id']

});
17 changes: 17 additions & 0 deletions zfapp/public/js/examples/example8/app/store/Messagelist.js
Original file line number Diff line number Diff line change
@@ -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

});
6 changes: 6 additions & 0 deletions zfapp/public/js/examples/example8/app/store/Userlist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Ext.define('example8.store.Userlist', {

extend: 'Ext.data.Store',
model: 'example8.model.Userlist'

});
30 changes: 30 additions & 0 deletions zfapp/public/js/examples/example8/app/view/index/Chatinput.js
Original file line number Diff line number Diff line change
@@ -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'
}
]
}
]
});
18 changes: 18 additions & 0 deletions zfapp/public/js/examples/example8/app/view/index/Chatwin.js
Original file line number Diff line number Diff line change
@@ -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'
}
]

});
23 changes: 23 additions & 0 deletions zfapp/public/js/examples/example8/app/view/index/Messagelist.js
Original file line number Diff line number Diff line change
@@ -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);
}

});
27 changes: 27 additions & 0 deletions zfapp/public/js/examples/example8/app/view/index/Userlist.js
Original file line number Diff line number Diff line change
@@ -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);
}

});
3 changes: 3 additions & 0 deletions zfapp/public/js/examples/example8/app/view/index/Userwin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 3018f34

Please sign in to comment.