|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Craft; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Export User Service. |
|
7
|
|
|
* |
|
8
|
|
|
* Handles export users. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Bob Olde Hampsink <[email protected]> |
|
11
|
|
|
* @copyright Copyright (c) 2015, Bob Olde Hampsink |
|
12
|
|
|
* @license MIT |
|
13
|
|
|
* |
|
14
|
|
|
* @link http://github.com/boboldehampsink |
|
15
|
|
|
*/ |
|
16
|
|
|
class Export_UserService extends BaseApplicationComponent implements IExportElementType |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Return export template. |
|
20
|
|
|
* |
|
21
|
|
|
* @return string |
|
22
|
|
|
*/ |
|
23
|
1 |
|
public function getTemplate() |
|
24
|
|
|
{ |
|
25
|
1 |
|
return 'export/sources/_user'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Get user groups. |
|
30
|
|
|
* |
|
31
|
|
|
* @return array|bool |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function getGroups() |
|
34
|
|
|
{ |
|
35
|
2 |
|
$result = false; |
|
36
|
|
|
// Check if usergroups are allowed in this installation |
|
37
|
2 |
|
if (isset(craft()->userGroups)) { |
|
38
|
|
|
|
|
39
|
|
|
// Get usergroups |
|
40
|
2 |
|
$groups = craft()->userGroups->getAllGroups(); |
|
41
|
|
|
|
|
42
|
|
|
// Return when groups found |
|
43
|
2 |
|
$result = count($groups) ? $groups : true; |
|
44
|
2 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Else, dont proceed with the user element |
|
47
|
2 |
|
return $result; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Return user fields. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $settings |
|
54
|
|
|
* @param bool $reset |
|
55
|
|
|
* |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function getFields(array $settings, $reset) |
|
59
|
|
|
{ |
|
60
|
|
|
// Set criteria |
|
61
|
2 |
|
$criteria = new \CDbCriteria(); |
|
62
|
2 |
|
$criteria->condition = 'settings = :settings'; |
|
63
|
2 |
|
$criteria->params = array( |
|
64
|
2 |
|
':settings' => JsonHelper::encode($settings), |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
// Check if we have a map already |
|
68
|
2 |
|
$stored = craft()->export->findMap($criteria); |
|
69
|
|
|
|
|
70
|
2 |
|
if (!count($stored) || $reset) { |
|
71
|
|
|
|
|
72
|
|
|
// Set the static fields for this type |
|
73
|
|
|
$fields = array( |
|
74
|
1 |
|
ExportModel::HandleId => array('name' => Craft::t('ID'), 'checked' => 0), |
|
75
|
1 |
|
ExportModel::HandleUsername => array('name' => Craft::t('Username'), 'checked' => 1), |
|
76
|
1 |
|
ExportModel::HandleFirstName => array('name' => Craft::t('First Name'), 'checked' => 1), |
|
77
|
1 |
|
ExportModel::HandleLastName => array('name' => Craft::t('Last Name'), 'checked' => 1), |
|
78
|
1 |
|
ExportModel::HandleEmail => array('name' => Craft::t('Email'), 'checked' => 1), |
|
79
|
1 |
|
ExportModel::HandlePreferredLocale => array('name' => Craft::t('Preferred Locale'), 'checked' => 0), |
|
80
|
1 |
|
ExportModel::HandleWeekStartDay => array('name' => Craft::t('Week Start Day'), 'checked' => 0), |
|
81
|
1 |
|
ExportModel::HandleStatus => array('name' => Craft::t('Status'), 'checked' => 0), |
|
82
|
1 |
|
ExportModel::HandleLastLoginDate => array('name' => Craft::t('Last Login Date'), 'checked' => 0), |
|
83
|
1 |
|
ExportModel::HandleInvalidLoginCount => array('name' => Craft::t('Invalid Login Count'), 'checked' => 0), |
|
84
|
1 |
|
ExportModel::HandleLastInvalidLoginDate => array('name' => Craft::t('Last Invalid Login Date'), 'checked' => 0), |
|
85
|
1 |
|
); |
|
86
|
|
|
|
|
87
|
|
|
// Set the dynamic fields for this type |
|
88
|
1 |
|
foreach (craft()->fields->getLayoutByType(ElementType::User)->getFields() as $field) { |
|
89
|
1 |
|
$data = $field->getField(); |
|
90
|
1 |
|
$fields[$data->handle] = array('name' => $data->name, 'checked' => 1, 'fieldtype' => $data->type); |
|
91
|
1 |
|
} |
|
92
|
1 |
|
} else { |
|
93
|
|
|
|
|
94
|
|
|
// Get the stored map |
|
95
|
1 |
|
$fields = $stored->map; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// Return fields |
|
99
|
2 |
|
return $fields; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Set user criteria. |
|
104
|
|
|
* |
|
105
|
|
|
* @param array $settings |
|
106
|
|
|
* |
|
107
|
|
|
* @return ElementCriteriaModel |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function setCriteria(array $settings) |
|
110
|
|
|
{ |
|
111
|
|
|
// Get users by criteria |
|
112
|
1 |
|
$criteria = craft()->elements->getCriteria(ElementType::User); |
|
113
|
1 |
|
$criteria->order = 'id '.$settings['sort']; |
|
114
|
1 |
|
$criteria->offset = $settings['offset']; |
|
115
|
1 |
|
$criteria->limit = $settings['limit']; |
|
116
|
1 |
|
$criteria->status = isset($settings['map']['status']) ? $settings['map']['status'] : null; |
|
117
|
|
|
|
|
118
|
|
|
// Get by group |
|
119
|
1 |
|
$criteria->groupId = $settings['elementvars']['groups']; |
|
120
|
|
|
|
|
121
|
1 |
|
return $criteria; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Get user attributes. |
|
126
|
|
|
* |
|
127
|
|
|
* @param array $map |
|
128
|
|
|
* @param BaseElementModel $element |
|
129
|
|
|
* |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
1 |
|
public function getAttributes(array $map, BaseElementModel $element) |
|
133
|
|
|
{ |
|
134
|
1 |
|
$attributes = array(); |
|
135
|
|
|
|
|
136
|
|
|
// Try to parse checked fields through prepValue |
|
137
|
1 |
|
foreach ($map as $handle => $data) { |
|
138
|
1 |
|
if ($data['checked']) { |
|
139
|
|
|
try { |
|
140
|
1 |
|
$attributes[$handle] = $element->$handle; |
|
141
|
1 |
|
} catch (\Exception $e) { |
|
142
|
1 |
|
$attributes[$handle] = null; |
|
143
|
|
|
} |
|
144
|
1 |
|
} |
|
145
|
1 |
|
} |
|
146
|
|
|
|
|
147
|
|
|
// Call hook allowing 3rd-party plugins to modify attributes |
|
148
|
1 |
|
craft()->plugins->call('modifyExportAttributes', array(&$attributes, $element)); |
|
149
|
|
|
|
|
150
|
1 |
|
return $attributes; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|