Skip to content

Commit 4adffd8

Browse files
maxmimaxmi
authored andcommitted
Magento Exception Handling,
Refactor addFieldToFilter method
1 parent a79a9fe commit 4adffd8

File tree

3 files changed

+58
-20
lines changed

3 files changed

+58
-20
lines changed

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,27 @@ protected function _call($endpoint, $params = null, $method = 'GET', $data = nul
9797
if($response->isError()) {
9898
if(is_array($body) && isset($body['error'])) {
9999
if(is_array($body['error']) && isset($body['error']['title'])) {
100-
if (!$silent)
101-
throw new Exception($body['error']['title'], $response->getStatus());
102-
else
100+
if (!$silent) {
101+
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('zendesk')->__($body['error']['title'],$response->getStatus()));
102+
return;
103+
} else {
103104
return $body;
105+
}
104106
} else {
105-
if (!$silent)
106-
throw new Exception($body['error'], $response->getStatus());
107-
else
107+
if (!$silent) {
108+
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('zendesk')->__($body['error'],$response->getStatus()));
109+
return;
110+
} else {
108111
return $body;
112+
}
109113
}
110114
} else {
111-
if (!$silent)
112-
throw new Exception($body, $response->getStatus());
113-
else
115+
if (!$silent) {
116+
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('zendesk')->__($body, $response->getStatus()));
117+
return;
118+
} else {
114119
return $body;
120+
}
115121
}
116122
}
117123

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

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,47 +30,75 @@ public function __construct() {
3030
public function addFieldToFilter($fieldName, $condition = null) {
3131
if(is_string($condition) OR is_array($condition)) {
3232

33+
$searchFields = array();
34+
3335
switch($fieldName) {
3436
case 'subject':
35-
$this->_search->addField( new Zendesk_Zendesk_Model_Search_Field("subject", '"'.$condition.'"') );
37+
$searchFields[] = array(
38+
'field' => 'subject',
39+
'value' => '"'.$condition.'"'
40+
);
3641
break;
3742
case 'requester':
3843
case 'requester_id':
3944
$value = is_numeric($condition) ? $condition : '*' . $condition . '*';
40-
$this->_search->addField( new Zendesk_Zendesk_Model_Search_Field("requester", $value) );
45+
$searchFields[] = array(
46+
'field' => 'requester',
47+
'value' => $value
48+
);
4149
break;
4250
case 'tags':
4351
case 'status':
4452
case 'priority':
4553
case 'status':
4654
case 'group':
4755
case 'assignee':
48-
$this->_search->addField( new Zendesk_Zendesk_Model_Search_Field($fieldName, $condition) );
56+
$searchFields[] = array(
57+
'field' => $fieldName,
58+
'value' => $condition
59+
);
4960
break;
5061
case 'type':
51-
$this->_search->addField( new Zendesk_Zendesk_Model_Search_Field('ticket_type', $condition) );
62+
$searchFields[] = array(
63+
'field' => 'ticket_type',
64+
'value' => $condition
65+
);
5266
break;
5367
case 'id':
54-
$this->_search->addField( new Zendesk_Zendesk_Model_Search_Field('', $condition, '') );
68+
$searchFields[] = array(
69+
'field' => '',
70+
'value' => $condition,
71+
'operator' => ''
72+
);
5573
break;
5674
case 'created_at':
5775
case 'updated_at':
58-
$fields = array();
5976
$fieldName = substr($fieldName, 0, -3);
6077

6178
if( isset($condition['from']) AND Mage::helper('zendesk')->isValidDate($condition['from']) ) {
6279
$value = Mage::helper('zendesk')->getFormatedDataForAPI( $condition['from'] );
63-
$fields[] = new Zendesk_Zendesk_Model_Search_Field($fieldName, $value, '>');
80+
$searchFields[] = array(
81+
'field' => $fieldName,
82+
'value' => $value,
83+
'operator' => '>'
84+
);
6485
}
6586

6687
if( isset($condition['to']) AND Mage::helper('zendesk')->isValidDate($condition['to']) ) {
6788
$value = Mage::helper('zendesk')->getFormatedDataForAPI( $condition['to'] );
68-
$fields[] = new Zendesk_Zendesk_Model_Search_Field($fieldName, $value, '<');
89+
$searchFields[] = array(
90+
'field' => $fieldName,
91+
'value' => $value,
92+
'operator' => '<'
93+
);
6994
}
70-
71-
$this->_search->addFields($fields);
7295
break;
7396
}
97+
foreach ($searchFields as $field) {
98+
$operator = isset($field['operator']) ? $field['operator'] : ":";
99+
$value = isset($field['value']) ? $field['value'] : "none";
100+
$this->_search->addField( new Zendesk_Zendesk_Model_Search_Field($field['field'], $value, $operator));
101+
}
74102
}
75103

76104
return $this;

src/app/code/community/Zendesk/Zendesk/controllers/Adminhtml/ZendeskController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,13 @@ public function authenticateAction()
113113

114114
if( $check ) {
115115
$email = $settings->getUsername();
116+
} else {
117+
throw new Exception;
116118
}
117119
} catch( Exception $exc ) {
118-
//just do nothing
120+
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('zendesk')->__('Could not connect to Zendesk'));
121+
$this->_redirect('adminhtml/dashboard');
122+
return;
119123
}
120124
}
121125

0 commit comments

Comments
 (0)