Skip to content

Commit 0c1de28

Browse files
committed
Merge pull request #96 from zendesk/jose/allow-multiple-sync
Allow syncing multiple times
2 parents 966529b + 1c6f7e2 commit 0c1de28

File tree

3 files changed

+141
-2
lines changed

3 files changed

+141
-2
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function all()
5959
while($page && $response = $this->_call('users.json?page=' . $page)) {
6060
$users = array_merge($users, $response['users']);
6161
$page = is_null($response['next_page']) ? 0 : $page + 1;
62-
}
62+
}
6363

6464
return $users;
6565
}
@@ -115,4 +115,21 @@ public function createUserField($field)
115115

116116
return $response['user_field'];
117117
}
118+
119+
/**
120+
* Fetch all user fields
121+
*
122+
* @return array $userFields
123+
*/
124+
public function getUserFields()
125+
{
126+
$page = 1;
127+
$userFields = array();
128+
while($page && $response = $this->_call('user_fields.json?page=' . $page)) {
129+
$userFields = array_merge($userFields, $response['user_fields']);
130+
$page = is_null($response['next_page']) ? 0 : $page + 1;
131+
}
132+
133+
return $userFields;
134+
}
118135
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,9 @@ public function syncAction()
534534
$this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
535535
Mage::log('Synchronization started', null, 'zendesk.log');
536536
try {
537-
$user = Mage::getModel('zendesk/api_users')->all();
537+
$userFieldKeys = array_column(Mage::getModel('zendesk/api_users')->getUserFields(), 'key');
538+
$user = Mage::getModel('zendesk/api_users')->me();
539+
538540
if (is_null($user))
539541
throw new Exception("Connection Failed");
540542

@@ -601,7 +603,11 @@ public function syncAction()
601603
);
602604

603605
foreach($data as $field) {
606+
if (in_array($field['user_field']['key'], $userFieldKeys))
607+
continue;
608+
604609
$response = Mage::getModel('zendesk/api_users')->createUserField($field);
610+
605611
if (!isset($response['active']) || $response['active'] === false)
606612
Mage::log('Unable to create User Field with key '.$field['user_field']['key'], null, 'zendesk.log');
607613
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
if (!function_exists('array_column')) {
4+
/**
5+
* This file is part of the array_column library
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @copyright Copyright (c) Ben Ramsey (http://benramsey.com)
11+
* @license http://opensource.org/licenses/MIT MIT
12+
*/
13+
14+
/**
15+
* Returns the values from a single column of the input array, identified by
16+
* the $columnKey.
17+
*
18+
* Optionally, you may provide an $indexKey to index the values in the returned
19+
* array by the values from the $indexKey column in the input array.
20+
*
21+
* @param array $input A multi-dimensional array (record set) from which to pull
22+
* a column of values.
23+
* @param mixed $columnKey The column of values to return. This value may be the
24+
* integer key of the column you wish to retrieve, or it
25+
* may be the string key name for an associative array.
26+
* @param mixed $indexKey (Optional.) The column to use as the index/keys for
27+
* the returned array. This value may be the integer key
28+
* of the column, or it may be the string key name.
29+
* @return array
30+
*/
31+
function array_column($input = null, $columnKey = null, $indexKey = null)
32+
{
33+
// Using func_get_args() in order to check for proper number of
34+
// parameters and trigger errors exactly as the built-in array_column()
35+
// does in PHP 5.5.
36+
$argc = func_num_args();
37+
$params = func_get_args();
38+
39+
if ($argc < 2) {
40+
trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING);
41+
return null;
42+
}
43+
44+
if (!is_array($params[0])) {
45+
trigger_error(
46+
'array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given',
47+
E_USER_WARNING
48+
);
49+
return null;
50+
}
51+
52+
if (!is_int($params[1])
53+
&& !is_float($params[1])
54+
&& !is_string($params[1])
55+
&& $params[1] !== null
56+
&& !(is_object($params[1]) && method_exists($params[1], '__toString'))
57+
) {
58+
trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
59+
return false;
60+
}
61+
62+
if (isset($params[2])
63+
&& !is_int($params[2])
64+
&& !is_float($params[2])
65+
&& !is_string($params[2])
66+
&& !(is_object($params[2]) && method_exists($params[2], '__toString'))
67+
) {
68+
trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
69+
return false;
70+
}
71+
72+
$paramsInput = $params[0];
73+
$paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null;
74+
75+
$paramsIndexKey = null;
76+
if (isset($params[2])) {
77+
if (is_float($params[2]) || is_int($params[2])) {
78+
$paramsIndexKey = (int) $params[2];
79+
} else {
80+
$paramsIndexKey = (string) $params[2];
81+
}
82+
}
83+
84+
$resultArray = array();
85+
86+
foreach ($paramsInput as $row) {
87+
$key = $value = null;
88+
$keySet = $valueSet = false;
89+
90+
if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
91+
$keySet = true;
92+
$key = (string) $row[$paramsIndexKey];
93+
}
94+
95+
if ($paramsColumnKey === null) {
96+
$valueSet = true;
97+
$value = $row;
98+
} elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
99+
$valueSet = true;
100+
$value = $row[$paramsColumnKey];
101+
}
102+
103+
if ($valueSet) {
104+
if ($keySet) {
105+
$resultArray[$key] = $value;
106+
} else {
107+
$resultArray[] = $value;
108+
}
109+
}
110+
111+
}
112+
113+
return $resultArray;
114+
}
115+
116+
}

0 commit comments

Comments
 (0)