Skip to content

Commit fcb3298

Browse files
committed
Merge pull request #93 from zendesk/jose/MI-277-user-caching
[MI-277] Sideload users
2 parents 8bc5b11 + edc932d commit fcb3298

File tree

8 files changed

+55
-48
lines changed

8 files changed

+55
-48
lines changed

src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/Abstract.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,11 @@ protected function addColumnBasedOnType($index, $title, $filter = false, $sortab
171171
'filter' => $filter,
172172
'index' => $index,
173173
'type' => $this->getColumnType($index),
174+
'users' => $this->getCollection()->users,
174175
);
175176

176177
$renderer = $this->getColumnRenderer($index);
177-
178+
178179
if($renderer !== null) {
179180
$column['renderer'] = $renderer;
180181
}
@@ -241,5 +242,18 @@ public function getGridJavascript()
241242

242243
return $js;
243244
}
244-
245+
246+
/**
247+
* Override the parent method so that $this->_prepareCollection() is called first
248+
*
249+
* @return $this
250+
*/
251+
protected function _prepareGrid()
252+
{
253+
$this->_prepareCollection();
254+
$this->_prepareColumns();
255+
$this->_prepareMassactionBlock();
256+
257+
return $this;
258+
}
245259
}

src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/All.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ protected function _prepareColumns() {
5555
'renderer' => 'zendesk/adminhtml_dashboard_tab_tickets_grid_renderer_email',
5656
'index' => 'requester_id',
5757
'sortable' => false,
58+
'users' => $this->getCollection()->users,
5859
));
5960

6061
$this->addColumn('type', array(
@@ -75,15 +76,6 @@ protected function _prepareColumns() {
7576
'options' => Mage::helper('zendesk')->getStatusMap(),
7677
));
7778

78-
$this->addColumn('priority', array(
79-
'header' => Mage::helper('zendesk')->__('Priority'),
80-
'sortable' => true,
81-
'width' => '100px',
82-
'index' => 'priority',
83-
'type' => 'options',
84-
'options' => Mage::helper('zendesk')->getPriorityMap(),
85-
));
86-
8779
$this->addColumn('created_at', array(
8880
'header' => Mage::helper('zendesk')->__('Requested'),
8981
'sortable' => true,

src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/Renderer/Email.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class Zendesk_Zendesk_Block_Adminhtml_Dashboard_Tab_Tickets_Grid_Renderer_Email extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
2020

2121
public function render(Varien_Object $row) {
22-
$users = Mage::registry('zendesk_users');
22+
$users = $this->getColumn()->users;
2323
$value = (int) $row->getData($this->getColumn()->getIndex());
2424

2525
if ($users) {

src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/Renderer/User.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@
1919
class Zendesk_Zendesk_Block_Adminhtml_Dashboard_Tab_Tickets_Grid_Renderer_User extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
2020

2121
public function render(Varien_Object $row) {
22-
$users = Mage::registry('zendesk_users') ? Mage::registry('zendesk_users') : array();
22+
$users = $this->getColumn()->users;
2323
$value = (int) $row->getData($this->getColumn()->getIndex());
24-
24+
2525
$found = array_filter($users, function($user) use($value) {
2626
return (int) $user['id'] === $value;
2727
});
28-
28+
2929
if( count($found) ) {
3030
$user = array_shift($found);
31-
31+
3232
return $user['name'];
3333
}
34-
34+
3535
return '';
3636
}
3737

src/app/code/community/Zendesk/Zendesk/Helper/Data.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -395,16 +395,6 @@ public function getConnectionStatus() {
395395
public function storeDependenciesInCachedRegistry() {
396396
$cache = Mage::app()->getCache();
397397

398-
if (null == Mage::registry('zendesk_users')) {
399-
if( $cache->load('zendesk_users') === false) {
400-
$users = serialize( Mage::getModel('zendesk/api_users')->all() );
401-
$cache->save($users, 'zendesk_users', array('zendesk', 'zendesk_users'), 300);
402-
}
403-
404-
$users = unserialize( $cache->load('zendesk_users') );
405-
Mage::register('zendesk_users', $users);
406-
}
407-
408398
if (null == Mage::registry('zendesk_groups')) {
409399
if( $cache->load('zendesk_groups') === false) {
410400
$groups = serialize( Mage::getModel('zendesk/api_groups')->all() );

src/app/code/community/Zendesk/Zendesk/Model/Api/Tickets.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,21 @@ public function get($id, $sideload = false)
8282

8383
public function recent()
8484
{
85-
$response = $this->_call('tickets/recent.json');
85+
$response = $this->_call('tickets/recent.json', array('include' => 'users,groups'));
8686

8787
return $response['tickets'];
8888
}
8989

9090
public function all()
9191
{
92-
$response = $this->_call('tickets.json');
92+
$response = $this->_call('tickets.json', array('include' => 'users,groups'));
9393
return $response['tickets'];
9494
}
9595

9696
public function search($data)
9797
{
98-
return $this->_call('search.json', $data);
98+
$data['include'] = 'users,groups';
99+
return $this->_call('search/incremental', $data);
99100
}
100101

101102
public function forOrder($orderIncrementId)

src/app/code/community/Zendesk/Zendesk/Model/Api/Views.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function execute($id, array $params = array())
3939
throw new InvalidArgumentException('View ID not provided');
4040
}
4141

42+
$params['include'] = 'users,groups';
4243
$paramsString = count($params) ? '?' . http_build_query($params) : '';
4344

4445
$response = $this->_call('views/' . $id . '/execute.json' . $paramsString);

src/app/code/community/Zendesk/Zendesk/Model/Resource/Tickets/Collection.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@ class Zendesk_Zendesk_Model_Resource_Tickets_Collection extends Varien_Data_Coll
2323
protected $_viewColumns = array();
2424
protected static $_excludedColumns = array('score');
2525

26+
public $users = array();
27+
2628
public function __construct() {
2729
$this->_search = new Zendesk_Zendesk_Model_Search( Zendesk_Zendesk_Model_Search::TYPE_TICKET );
2830
}
2931

3032
public function addFieldToFilter($fieldName, $condition = null) {
3133
if(is_string($condition) OR is_array($condition)) {
32-
34+
3335
$searchFields = array();
34-
36+
3537
switch($fieldName) {
3638
case 'subject':
3739
$searchFields[] = array(
@@ -82,7 +84,7 @@ public function addFieldToFilter($fieldName, $condition = null) {
8284
case 'created_at':
8385
case 'updated_at':
8486
$fieldName = substr($fieldName, 0, -3);
85-
87+
8688
if( isset($condition['from']) AND Mage::helper('zendesk')->isValidDate($condition['from']) ) {
8789
$value = Mage::helper('zendesk')->getFormatedDataForAPI( $condition['from'] );
8890
$searchFields[] = array(
@@ -91,7 +93,7 @@ public function addFieldToFilter($fieldName, $condition = null) {
9193
'operator' => '>'
9294
);
9395
}
94-
96+
9597
if( isset($condition['to']) AND Mage::helper('zendesk')->isValidDate($condition['to']) ) {
9698
$value = Mage::helper('zendesk')->getFormatedDataForAPI( $condition['to'] );
9799
$searchFields[] = array(
@@ -111,14 +113,14 @@ public function addFieldToFilter($fieldName, $condition = null) {
111113

112114
return $this;
113115
}
114-
116+
115117
public function getCollection(array $params = array()) {
116118
$searchQuery = array(
117119
'query' => $this->_search->getString(),
118120
);
119-
121+
120122
$params = array_merge($searchQuery, $params);
121-
123+
122124
$all = Mage::getModel('zendesk/api_tickets')->search($params);
123125

124126
foreach ($all['results'] as $ticket) {
@@ -127,54 +129,61 @@ public function getCollection(array $params = array()) {
127129
$this->addItem($obj);
128130
}
129131

132+
// Set the users for this collection
133+
$this->users = $all['users'];
134+
130135
$this->setPageSize($params['per_page']);
131136
$this->setCurPage($params['page']);
132137
$this->setOrder($params['sort_by'], $params['sort_order']);
133138
$this->_count = $all['count'];
134-
139+
135140
Mage::unregister('zendesk_tickets_all');
136141
Mage::register('zendesk_tickets_all', $all['count']);
137-
142+
138143
return $this;
139144
}
140-
145+
141146
public function getCollectionFromView($viewId, array $params = array()) {
142147
$view = Mage::getModel('zendesk/api_views')->execute($viewId, $params);
148+
143149
if (is_array($view['rows'])) {
144150
foreach ($view['rows'] as $row) {
145151
$ticket = array_merge($row, $row['ticket']);
146-
152+
147153
$this->appendParamsWithoutIdPostfix($ticket, array('requester', 'assignee', 'group'));
148-
154+
149155
$obj = new Varien_Object();
150156
$obj->setData($ticket);
151157
$this->addItem($obj);
152158
}
153159
}
154-
160+
161+
// Set the users for this collection
162+
$this->users = (isset($view['users'])) ? $view['users'] : array();
163+
155164
$this->_viewColumns = $view['columns'] ? $view['columns'] : array();
156165

157166
$this->setPageSize($params['per_page']);
158167
$this->setCurPage($params['page']);
159168
$this->setOrder($params['sort_by'], $params['sort_order']);
160169
$this->_count = $view['count'];
161-
170+
162171
Mage::unregister('zendesk_tickets_view_'.$viewId);
163172
Mage::register('zendesk_tickets_view_'.$viewId, $view['count']);
164-
173+
165174
return $this;
166175
}
167-
176+
168177
protected function appendParamsWithoutIdPostfix(& $item, array $params = array()) {
169178
foreach($params as $param) {
170179
$name = $param . '_id';
171-
180+
172181
if(isset($item[$name])) {
173182
$item[$param] = $item[$name];
174183
}
175184
}
176185
}
177-
186+
178187
public function getColumnsForView() {
179188
$excludedColumns = static::$_excludedColumns;
180189

0 commit comments

Comments
 (0)