From edc932d332e83e10956013528929307f92910968 Mon Sep 17 00:00:00 2001 From: Jose Consador Date: Tue, 8 Sep 2015 16:49:33 +0800 Subject: [PATCH] sideload users --- .../Dashboard/Tab/Tickets/Grid/Abstract.php | 18 +++++++- .../Dashboard/Tab/Tickets/Grid/All.php | 10 +---- .../Tab/Tickets/Grid/Renderer/Email.php | 2 +- .../Tab/Tickets/Grid/Renderer/User.php | 10 ++--- .../community/Zendesk/Zendesk/Helper/Data.php | 10 ----- .../Zendesk/Zendesk/Model/Api/Tickets.php | 7 +-- .../Zendesk/Zendesk/Model/Api/Views.php | 1 + .../Model/Resource/Tickets/Collection.php | 45 +++++++++++-------- 8 files changed, 55 insertions(+), 48 deletions(-) diff --git a/src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/Abstract.php b/src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/Abstract.php index 8b8dda19..8871be9f 100644 --- a/src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/Abstract.php +++ b/src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/Abstract.php @@ -171,10 +171,11 @@ protected function addColumnBasedOnType($index, $title, $filter = false, $sortab 'filter' => $filter, 'index' => $index, 'type' => $this->getColumnType($index), + 'users' => $this->getCollection()->users, ); $renderer = $this->getColumnRenderer($index); - + if($renderer !== null) { $column['renderer'] = $renderer; } @@ -241,5 +242,18 @@ public function getGridJavascript() return $js; } - + + /** + * Override the parent method so that $this->_prepareCollection() is called first + * + * @return $this + */ + protected function _prepareGrid() + { + $this->_prepareCollection(); + $this->_prepareColumns(); + $this->_prepareMassactionBlock(); + + return $this; + } } diff --git a/src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/All.php b/src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/All.php index d2a4b95a..1b98dcdc 100644 --- a/src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/All.php +++ b/src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/All.php @@ -55,6 +55,7 @@ protected function _prepareColumns() { 'renderer' => 'zendesk/adminhtml_dashboard_tab_tickets_grid_renderer_email', 'index' => 'requester_id', 'sortable' => false, + 'users' => $this->getCollection()->users, )); $this->addColumn('type', array( @@ -75,15 +76,6 @@ protected function _prepareColumns() { 'options' => Mage::helper('zendesk')->getStatusMap(), )); - $this->addColumn('priority', array( - 'header' => Mage::helper('zendesk')->__('Priority'), - 'sortable' => true, - 'width' => '100px', - 'index' => 'priority', - 'type' => 'options', - 'options' => Mage::helper('zendesk')->getPriorityMap(), - )); - $this->addColumn('created_at', array( 'header' => Mage::helper('zendesk')->__('Requested'), 'sortable' => true, diff --git a/src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/Renderer/Email.php b/src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/Renderer/Email.php index d4b8be60..b339415e 100644 --- a/src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/Renderer/Email.php +++ b/src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/Renderer/Email.php @@ -19,7 +19,7 @@ class Zendesk_Zendesk_Block_Adminhtml_Dashboard_Tab_Tickets_Grid_Renderer_Email extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract { public function render(Varien_Object $row) { - $users = Mage::registry('zendesk_users'); + $users = $this->getColumn()->users; $value = (int) $row->getData($this->getColumn()->getIndex()); if ($users) { diff --git a/src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/Renderer/User.php b/src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/Renderer/User.php index 51d4e5c1..630fdf71 100644 --- a/src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/Renderer/User.php +++ b/src/app/code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Tab/Tickets/Grid/Renderer/User.php @@ -19,19 +19,19 @@ class Zendesk_Zendesk_Block_Adminhtml_Dashboard_Tab_Tickets_Grid_Renderer_User extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract { public function render(Varien_Object $row) { - $users = Mage::registry('zendesk_users') ? Mage::registry('zendesk_users') : array(); + $users = $this->getColumn()->users; $value = (int) $row->getData($this->getColumn()->getIndex()); - + $found = array_filter($users, function($user) use($value) { return (int) $user['id'] === $value; }); - + if( count($found) ) { $user = array_shift($found); - + return $user['name']; } - + return ''; } diff --git a/src/app/code/community/Zendesk/Zendesk/Helper/Data.php b/src/app/code/community/Zendesk/Zendesk/Helper/Data.php index b9f0b450..7dc7eb38 100644 --- a/src/app/code/community/Zendesk/Zendesk/Helper/Data.php +++ b/src/app/code/community/Zendesk/Zendesk/Helper/Data.php @@ -395,16 +395,6 @@ public function getConnectionStatus() { public function storeDependenciesInCachedRegistry() { $cache = Mage::app()->getCache(); - if (null == Mage::registry('zendesk_users')) { - if( $cache->load('zendesk_users') === false) { - $users = serialize( Mage::getModel('zendesk/api_users')->all() ); - $cache->save($users, 'zendesk_users', array('zendesk', 'zendesk_users'), 300); - } - - $users = unserialize( $cache->load('zendesk_users') ); - Mage::register('zendesk_users', $users); - } - if (null == Mage::registry('zendesk_groups')) { if( $cache->load('zendesk_groups') === false) { $groups = serialize( Mage::getModel('zendesk/api_groups')->all() ); diff --git a/src/app/code/community/Zendesk/Zendesk/Model/Api/Tickets.php b/src/app/code/community/Zendesk/Zendesk/Model/Api/Tickets.php index e1778c94..96a24af1 100644 --- a/src/app/code/community/Zendesk/Zendesk/Model/Api/Tickets.php +++ b/src/app/code/community/Zendesk/Zendesk/Model/Api/Tickets.php @@ -82,20 +82,21 @@ public function get($id, $sideload = false) public function recent() { - $response = $this->_call('tickets/recent.json'); + $response = $this->_call('tickets/recent.json', array('include' => 'users,groups')); return $response['tickets']; } public function all() { - $response = $this->_call('tickets.json'); + $response = $this->_call('tickets.json', array('include' => 'users,groups')); return $response['tickets']; } public function search($data) { - return $this->_call('search.json', $data); + $data['include'] = 'users,groups'; + return $this->_call('search/incremental', $data); } public function forOrder($orderIncrementId) diff --git a/src/app/code/community/Zendesk/Zendesk/Model/Api/Views.php b/src/app/code/community/Zendesk/Zendesk/Model/Api/Views.php index df0c7c9f..9ba478ec 100644 --- a/src/app/code/community/Zendesk/Zendesk/Model/Api/Views.php +++ b/src/app/code/community/Zendesk/Zendesk/Model/Api/Views.php @@ -39,6 +39,7 @@ public function execute($id, array $params = array()) throw new InvalidArgumentException('View ID not provided'); } + $params['include'] = 'users,groups'; $paramsString = count($params) ? '?' . http_build_query($params) : ''; $response = $this->_call('views/' . $id . '/execute.json' . $paramsString); diff --git a/src/app/code/community/Zendesk/Zendesk/Model/Resource/Tickets/Collection.php b/src/app/code/community/Zendesk/Zendesk/Model/Resource/Tickets/Collection.php index 8572b07b..9dfe43ea 100644 --- a/src/app/code/community/Zendesk/Zendesk/Model/Resource/Tickets/Collection.php +++ b/src/app/code/community/Zendesk/Zendesk/Model/Resource/Tickets/Collection.php @@ -23,15 +23,17 @@ class Zendesk_Zendesk_Model_Resource_Tickets_Collection extends Varien_Data_Coll protected $_viewColumns = array(); protected static $_excludedColumns = array('score'); + public $users = array(); + public function __construct() { $this->_search = new Zendesk_Zendesk_Model_Search( Zendesk_Zendesk_Model_Search::TYPE_TICKET ); } public function addFieldToFilter($fieldName, $condition = null) { if(is_string($condition) OR is_array($condition)) { - + $searchFields = array(); - + switch($fieldName) { case 'subject': $searchFields[] = array( @@ -82,7 +84,7 @@ public function addFieldToFilter($fieldName, $condition = null) { case 'created_at': case 'updated_at': $fieldName = substr($fieldName, 0, -3); - + if( isset($condition['from']) AND Mage::helper('zendesk')->isValidDate($condition['from']) ) { $value = Mage::helper('zendesk')->getFormatedDataForAPI( $condition['from'] ); $searchFields[] = array( @@ -91,7 +93,7 @@ public function addFieldToFilter($fieldName, $condition = null) { 'operator' => '>' ); } - + if( isset($condition['to']) AND Mage::helper('zendesk')->isValidDate($condition['to']) ) { $value = Mage::helper('zendesk')->getFormatedDataForAPI( $condition['to'] ); $searchFields[] = array( @@ -111,14 +113,14 @@ public function addFieldToFilter($fieldName, $condition = null) { return $this; } - + public function getCollection(array $params = array()) { $searchQuery = array( 'query' => $this->_search->getString(), ); - + $params = array_merge($searchQuery, $params); - + $all = Mage::getModel('zendesk/api_tickets')->search($params); foreach ($all['results'] as $ticket) { @@ -127,54 +129,61 @@ public function getCollection(array $params = array()) { $this->addItem($obj); } + // Set the users for this collection + $this->users = $all['users']; + $this->setPageSize($params['per_page']); $this->setCurPage($params['page']); $this->setOrder($params['sort_by'], $params['sort_order']); $this->_count = $all['count']; - + Mage::unregister('zendesk_tickets_all'); Mage::register('zendesk_tickets_all', $all['count']); - + return $this; } - + public function getCollectionFromView($viewId, array $params = array()) { $view = Mage::getModel('zendesk/api_views')->execute($viewId, $params); + if (is_array($view['rows'])) { foreach ($view['rows'] as $row) { $ticket = array_merge($row, $row['ticket']); - + $this->appendParamsWithoutIdPostfix($ticket, array('requester', 'assignee', 'group')); - + $obj = new Varien_Object(); $obj->setData($ticket); $this->addItem($obj); } } - + + // Set the users for this collection + $this->users = (isset($view['users'])) ? $view['users'] : array(); + $this->_viewColumns = $view['columns'] ? $view['columns'] : array(); $this->setPageSize($params['per_page']); $this->setCurPage($params['page']); $this->setOrder($params['sort_by'], $params['sort_order']); $this->_count = $view['count']; - + Mage::unregister('zendesk_tickets_view_'.$viewId); Mage::register('zendesk_tickets_view_'.$viewId, $view['count']); - + return $this; } - + protected function appendParamsWithoutIdPostfix(& $item, array $params = array()) { foreach($params as $param) { $name = $param . '_id'; - + if(isset($item[$name])) { $item[$param] = $item[$name]; } } } - + public function getColumnsForView() { $excludedColumns = static::$_excludedColumns;