Permalink
Browse files

updating the example8 - it all works now, woo!

  • Loading branch information...
1 parent 0fc584a commit afbe808a46846d2307ae13fe2e8aa40210b56577 @enygma committed May 9, 2012
View
37 zfapp/application/controllers/ExampleController.php
@@ -41,6 +41,7 @@ public function init()
->addActionContext('example6submit', array('xml', 'json'))
->addActionContext('example8submit', array('xml', 'json'))
->addActionContext('example8read', array('xml', 'json'))
+ ->addActionContext('example8users', array('xml', 'json'))
->setAutoJsonSerialization(true)
->initContext();
}
@@ -233,16 +234,44 @@ public function example8Action()
public function example8readAction()
{
- $request = json_decode($this->getRequest()->getRawBody());
- $msg = new Application_Model_Chatmessage();
+ $request = json_decode($this->getRequest()->getRawBody());
+ $filterUserId = $this->getRequest()->getParam('filterBy');
+ $username = $this->getRequest()->getParam('username');
+ $msg = new Application_Model_Chatmessage();
+
+ if (!empty($username)) {
+ // we're changing our username...
+ $user = new Application_Model_Chatuser();
+ $found = $user->findByUsername($username);
+
+ if (count($found)==0) {
+ // new user - add to the table!
+ $insertId = $user->addUser($username);
+ $this->view->insertUserId = $insertId;
+ } else {
+ $this->view->insertUserId = $found[0]->ID;
+ }
+ }
if (isset($request->message)) {
// add the message!
- $msg->add($request->message);
+ $userId = (!empty($request->chatUser)) ? $request->chatUser : null;
+ $msg->add($request->message,$userId);
}
+ // see if we need to filter the list
+ $filterUserId = ($filterUserId !== null && (int)$filterUserId !== 0)
+ ? $filterUserId : null;
+
// get the latest messages from the list
- $this->view->messages = $msg->getLatest(20);
+ $this->view->messages = $msg->getLatest(20,$filterUserId);
+ $this->view->success = true;
+ }
+
+ public function example8usersAction()
+ {
+ $user = new Application_Model_Chatuser();
+ $this->view->users = $user->getCurrent();
}
private function _checkDbLogin($values)
View
27 zfapp/application/models/Chatmessage.php
@@ -32,6 +32,14 @@ class Application_Model_Chatmessage extends Zend_Db_Table_Abstract
protected $_dependentTables = array('Application_Model_Chatuser');
+ protected $_referenceMap = array(
+ 'Message' => array(
+ 'columns' => 'chatuser_id',
+ 'refTableClass' => 'Application_Model_Chatuser',
+ 'refColumns' => 'ID'
+ )
+ );
+
//private $user = null;
/**
@@ -49,17 +57,18 @@ public function init()
*
* @param int $limit Limit the number of values returned
*/
- public function getLatest($limit=10)
+ public function getLatest($limit=10,$filterUserId=null)
{
- $results = $this->fetchAll(
- $this->select()
- ->order('date_posted DESC')
- ->limit($limit)
- );
+ $select = $this->select()->order('date_posted DESC')->limit($limit);
+ if ($filterUserId !== null) {
+ $select = $select->where('chatuser_id = '.$filterUserId);
+ }
+ $results = $this->fetchAll($select);
$messages = array();
foreach($results as $message) {
-
+ //error_log(print_r($message,true));
+
$related = $message->findDependentRowset('Application_Model_Chatuser');
$messages[] = array(
'message' => $message->message,
@@ -73,11 +82,11 @@ public function getLatest($limit=10)
return $messages;
}
- public function add($message)
+ public function add($message,$userId=1)
{
$data = array(
'message' => $message,
- 'chatuser_id' => 1,
+ 'chatuser_id' => $userId,
'date_posted' => time()
);
$this->insert($data);
View
38 zfapp/application/models/Chatuser.php
@@ -30,6 +30,8 @@ class Application_Model_Chatuser extends Zend_Db_Table_Abstract
*/
protected $_name = 'chat_user';
+ protected $_dependentTables = array('Application_Model_Chatmessage');
+
protected $_referenceMap = array(
'User' => array(
'columns' => 'ID',
@@ -47,5 +49,41 @@ public function init()
{
//nothing to see, move along
}
+
+ public function getCurrent()
+ {
+ $results = $this->fetchAll(
+ $this->select()->order('username DESC')
+ );
+ $users = array(
+ array('username'=>'Show All','id'=>0)
+ );
+ foreach ($results as $user) {
+ $users[] = array(
+ 'username' => $user->username,
+ 'id' => $user->ID
+ );
+ }
+
+ return $users;
+ }
+
+ public function findByUsername($username)
+ {
+ $result = $this->fetchAll(
+ $this->select()->where('username = ?',$username)
+ );
+ //error_log(print_r($result,true));
+ //return $result[0];
+ return $result;
+ }
+
+ public function addUser($username)
+ {
+ $data = array(
+ 'username' => $username
+ );
+ return $this->insert($data);
+ }
}
View
90 zfapp/public/js/examples/example8/app/controller/Index.js
@@ -24,29 +24,101 @@ Ext.define('example8.controller.Index', {
console.log('init Index controller');
indexController = this;
+ indexController.messageParams = {};
+ indexController.currentUser = {
+ name: 'enygma',
+ id: 1
+ };
this.control({
- 'textfield': {
+ '#chatMessage': {
specialkey: function(field,e) {
- if(e.getKey() == e.ENTER) {
+ if(e.getKey() == e.ENTER && field.getValue().length > 0) {
this.updateMessages();
+ } else if (e.getKey() == e.ENTER && field.getValue().length > 0) {
+ Ext.Msg.alert('Alert!','You must enter a message to submit!');
+ }
+ }
+ },
+ 'userlistgrid': {
+ itemclick: function(element,record,item,index,evt) {
+ // filter our grid based on the username
+ var filterUser = record.data.id;
+ var messageStore = Ext.getCmp('messagelist').getStore();
+
+ indexController.messageParams = {filterBy: filterUser}
+ messageStore.load({
+ params: indexController.messageParams
+ });
+ }
+ },
+ '#userwinsubmit': {
+ click: function(el) {
+ var form = Ext.getCmp('userform').getForm();
+ var cp = Ext.getCmp('chatpanel');
+
+ if (form.isValid()) {
+ form.submit({
+ success: function(form,action) {
+
+ var formValues = form.getValues();
+
+ indexController.currentUser = {
+ name: formValues.username,
+ id: action.result.insertUserId
+ }
+
+ // set the username in the form
+ Ext.getCmp('chatUser').setValue(action.result.insertUserId);
+
+ Ext.get('postingastxt').update(
+ cp.renderUserLink(formValues.username)
+ );
+
+ Ext.getCmp('userlistgrid').getStore().load();
+
+ Ext.getCmp('userwin').close();
+ },
+
+ failure: function(form,action) {
+ console.log('You fail!');
+ }
+ });
}
}
}
});
+
+ var task = Ext.TaskManager.start({
+ run: function(){
+ Ext.getCmp('messagelist').getStore().load({params: indexController.messageParams});
+ },
+ interval: 3000
+ });
+ },
+
+ loadUserWin: function() {
+ Ext.create('example8.view.index.Userwin');
},
updateMessages: function() {
+
var formValues = Ext.getCmp('chatForm').getValues();
- var messageStore = indexController.getStore('Messagelist');
+ if (formValues.chatMessage.length > 0) {
+
+ var messageStore = indexController.getStore('Messagelist');
- messageStore.add({
- message: formValues.chatMessage,
- ts: Math.round((new Date()).getTime()/1000)
- });
- messageStore.sort('ts','DESC');
+ messageStore.add({
+ message: formValues.chatMessage,
+ chatUser: formValues.chatUser,
+ ts: Math.round((new Date()).getTime()/1000)
+ });
+ messageStore.sort('ts','DESC');
- Ext.getCmp('chatMessage').setValue('');
+ Ext.getCmp('chatMessage').setValue('');
+ } else {
+ Ext.Msg.alert('Alert!','You must enter a message to submit!');
+ }
}
});
View
3 zfapp/public/js/examples/example8/app/model/Message.js
@@ -6,7 +6,8 @@ Ext.define('example8.model.Message', {
{ name: 'posted', type: 'string' },
{ name: 'username', type: 'string' },
{ name: 'ts', type: 'int' },
- { name: 'id', type: 'int' }
+ { name: 'id', type: 'int' },
+ { name: 'chatUser', type: 'int'}
]
});
View
6 zfapp/public/js/examples/example8/app/model/User.js
@@ -1,6 +1,8 @@
Ext.define('example8.model.User', {
extend: 'Ext.data.Model',
- fields: ['name','id']
-
+ fields: [
+ {name:'name',type:'string'},
+ {name:'id', type:'int'}
+ ]
});
View
11 zfapp/public/js/examples/example8/app/model/Userlist.js
@@ -1,6 +1,15 @@
Ext.define('example8.model.Userlist', {
extend: 'Ext.data.Model',
- fields: ['username','id']
+ fields: [
+ {
+ name:'username',
+ type:'string',
+ },
+ {
+ name: 'id',
+ type: 'int'
+ }
+ ],
});
View
6 zfapp/public/js/examples/example8/app/store/Currentuser.js
@@ -1,6 +1,10 @@
Ext.define('example8.store.Currentuser', {
extend: 'Ext.data.Store',
- model: 'example8.model.User'
+ model: 'example8.model.User',
+
+ data: [
+ {name:'enygma', id:1}
+ ]
});
View
12 zfapp/public/js/examples/example8/app/store/Userlist.js
@@ -1,6 +1,16 @@
Ext.define('example8.store.Userlist', {
extend: 'Ext.data.Store',
- model: 'example8.model.Userlist'
+ model: 'example8.model.Userlist',
+ proxy: {
+ type: 'ajax',
+ url: '/example/example8users/format/json',
+ reader: {
+ type: 'json',
+ root: 'users'
+ }
+ },
+ autoLoad: true,
+ autoSync: true
});
View
78 zfapp/public/js/examples/example8/app/view/index/Chatinput.js
@@ -4,27 +4,59 @@ Ext.define('example8.view.index.Chatinput',{
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'
- }
- ]
- }
- ]
+ id: 'chatpanel',
+
+ initComponent: function() {
+ this.currentUser = this.getCurrentUser();
+
+ this.items = [
+ {
+ xtype: 'form',
+ id: 'chatForm',
+ url: '/example/example8submit/format/json',
+ layout: 'column',
+ border: false,
+ bodyStyle: 'padding: 3px',
+ items: [
+ {
+ html: this.renderUserLink(this.currentUser),
+ border: false,
+ bodyStyle: 'padding:4px',
+ id: 'postingastxt'
+ },
+ {
+ xtype: 'hiddenfield',
+ name: 'chatUser',
+ id: 'chatUser',
+ required: true,
+ value: indexController.currentUser.id
+ },
+ {
+ xtype: 'textfield',
+ name: 'chatMessage',
+ id: 'chatMessage',
+ columnWidth: .90,
+ required: true
+ }
+ ]
+ }
+ ];
+
+ this.callParent(arguments);
+ },
+
+ getCurrentUser: function() {
+ //var user = indexController.getStore('Currentuser').getAt(0).data.name;
+ console.log(indexController.currentUser);
+
+ var user = indexController.currentUser.name;
+ return user;
+ },
+
+ renderUserLink: function(username) {
+ var link = 'posting as: <a href="#" onClick="indexController.loadUserWin()">'
+ +'<b>'+username+'</b></a>';
+ return link;
+ }
+
});
View
6 zfapp/public/js/examples/example8/app/view/index/Messagelist.js
@@ -5,9 +5,13 @@ Ext.define('example8.view.index.Messagelist', {
//title : 'Sample Grid',
store : 'Messagelist',
-
+ id: 'messagelist',
hideHeaders: true,
+ viewConfig: {
+ loadMask: false
+ },
+
initComponent: function() {
// column definition for the grid
View
4 zfapp/public/js/examples/example8/app/view/index/Userlist.js
@@ -12,6 +12,10 @@ Ext.define('example8.view.index.Userlist', {
/** The store to pull data from */
store : 'Userlist',
+ id: 'userlistgrid',
+
+ hideHeaders: true,
+
/** Initialize the grid */
initComponent: function() {
console.log('init sample grid');
View
54 zfapp/public/js/examples/example8/app/view/index/Userwin.js
@@ -11,7 +11,7 @@ Ext.define('example8.view.index.Userwin', {
items: [
{
xtype: 'form',
- url: '/example/example8submit/format/json',
+ url: '/example/example8read/format/json',
id: 'userform',
bodyStyle: 'padding: 5px',
items: [
@@ -29,39 +29,37 @@ Ext.define('example8.view.index.Userwin', {
buttons: [
{
text : 'Enter',
+ id: 'userwinsubmit',
handler : function() {
- Ext.getCmp('userwin').submitForm();
+ //Ext.getCmp('userwin').submitForm();
}
}
],
- submitForm: function() {
- var form = Ext.getCmp('userform').getForm();
- var currentWin = Ext.getCmp('userwin');
+ // submitForm: function() {
+ // var form = Ext.getCmp('userform').getForm();
+ // var currentWin = Ext.getCmp('userwin');
- if (form.isValid()) {
- form.submit({
- success: function(form,action) {
- console.log('Form submit success! Response: '+action.result.message);
+ // if (form.isValid()) {
+ // form.submit({
+ // 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);
- var formValues = form.getValues();
- console.log(formValues);
-
- // set the user information into our currentuser store
- var cuser = indexController.getStore('Currentuser');
- console.log(cuser);
- cuser.add({
- name: formValues.username,
- id: 1
- });
- },
- failure: function(form,action) {
- console.log('Form submit failure! Response: '+action.result.message);
- }
- });
- }
- }
+ // // set the user information into our currentuser store
+ // var cuser = indexController.getStore('Currentuser');
+ // console.log(cuser);
+ // cuser.add({
+ // name: formValues.username,
+ // id: 0
+ // });
+ // },
+ // failure: function(form,action) {
+ // console.log('Form submit failure! Response: '+action.result.message);
+ // }
+ // });
+ // }
+ // }
});

0 comments on commit afbe808

Please sign in to comment.