forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserDetail.php
More file actions
260 lines (240 loc) · 6.34 KB
/
Copy pathUserDetail.php
File metadata and controls
260 lines (240 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/**
* Copyright 2010 - 2011, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2011, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('UsersAppModel', 'Users.Model');
/**
* Users Detail Model
*
* @package users
* @subpackage users.models
*/
class UserDetail extends UsersAppModel {
/**
* Name
*
* @var string
*/
public $name = 'UserDetail';
/**
* Displayfield
*
* @var string
*/
public $displayField = 'field';
/**
* Validation rules for the virtual fields for each section
*
* @var array
*/
public $sectionValidation = array();
/**
* Used to declare field types for each section to make validation work on the virtual fields
*
* @var array
*/
public $sectionSchema = array();
/**
* Constructor
*
* @param string $id ID
* @param string $table Table
* @param string $ds Datasource
*/
public function __construct($id = false, $table = null, $ds = null) {
$userClass = Configure::read('App.UserClass');
if (empty($userClass)) {
$userClass = 'Users.User';
}
$this->belongsTo['User'] = array(
'className' => $userClass,
'foreignKey' => 'user_id');
parent::__construct($id, $table, $ds);
}
/**
* Create the default fields for a user
*
* @param string $userId User ID
* @return void
*/
public function createDefaults($userId) {
$entries = array(
array(
'field' => 'user.firstname',
'value' => '',
'input' => 'text',
'data_type' => 'string'),
array(
'field' => 'user.middlename',
'value' => '',
'input' => 'text',
'data_type' => 'string'),
array(
'field' => 'user.lastname',
'value' => '',
'input' => 'text',
'data_type' => 'string'),
array(
'field' => 'user.abbr-country-name',
'value' => '',
'input' => 'text',
'data_type' => 'string'),
array(
'field' => 'user.abbr-region',
'value' => '',
'input' => 'text',
'data_type' => 'string'),
array(
'field' => 'user.country-name',
'value' => '',
'input' => 'text',
'data_type' => 'string'),
array(
'field' => 'user.location',
'value' => '',
'input' => 'text',
'data_type' => 'string'),
array(
'field' => 'user.postal-code',
'value' => '',
'input' => 'text',
'data_type' => 'string'),
array(
'field' => 'user.region',
'value' => '',
'input' => 'text',
'data_type' => 'string'),
array(
'field' => 'user.timeoffset',
'value' => '',
'input' => 'text',
'data_type' => 'string'));
$i = 0;
$data = array();
foreach ($entries as $entry) {
$data[$this->alias] = $entry;
$data[$this->alias]['user_id'] = $userId;
$data[$this->alias]['position'] = $i++;
$this->create();
$this->save($data);
}
}
/**
* Returns details for named section
*
* @var string $userId User ID
* @var string $section Section name
* @return array
*/
public function getSection($userId = null, $section = null) {
$conditions = array(
"{$this->alias}.user_id" => $userId);
if (!is_null($section)) {
$conditions["{$this->alias}.field LIKE"] = $section . '.%';
}
$results = $this->find('all', array(
'recursive' => -1,
'conditions' => $conditions,
'fields' => array("{$this->alias}.field", "{$this->alias}.value")));
if (!empty($results)) {
foreach($results as $result) {
list($prefix, $field) = explode('.', $result[$this->alias]['field']);
$userDetails[$prefix][$field] = $result[$this->alias]['value'];
}
$results = $userDetails;
}
return $results;
}
/**
* Overriding this method to inject the active section schema
*
* @param mixed $field Set to true to reload schema, or a string to return a specific field
* @return array Array of table metadata
*/
public function schema($field = false) {
if (isset($this->activeSectionSchema) && !empty($this->sectionSchema[$this->activeSectionSchema])) {
return $this->sectionSchema[$this->activeSectionSchema];
}
return parent::schema($field);
}
/**
* Save details for named section
*
* @var string $userId User ID
* @var array $data Data
* @var string $section Section name
* @return boolean True on successful validation and saving of the virtual fields
*/
public function saveSection($userId = null, $data = null, $section = null) {
if (!empty($this->sectionSchema[$section])) {
$this->activeSectionSchema = $section;
foreach($data as $model => $userDetails) {
if ($model == $this->alias) {
foreach($userDetails as $key => $value) {
$data[$model][$key] = $this->deconstruct($key, $value);
}
}
}
}
if (!empty($this->sectionValidation[$section])) {
$tmpValidate = $this->validate;
$data = $this->set($data);
$this->validate = $this->sectionValidation[$section];
if (!$this->validates()) {
return false;
}
$this->validate = $tmpValidate;
}
if (isset($this->activeSectionSchema)) {
unset($this->activeSectionSchema);
}
if (!empty($data) && is_array($data)) {
foreach($data as $model => $userDetails) {
if ($model == $this->alias) {
// Save the details
foreach($userDetails as $key => $value) {
$newUserDetail = array();
$field = $section . '.' . $key;
$userDetail = $this->find('first', array(
'recursive' => -1,
'conditions' => array(
'user_id' => $userId,
'field' => $field),
'fields' => array('id', 'field')));
if (empty($userDetail)) {
$this->create();
$newUserDetail[$model]['user_id'] = $userId;
} else {
$newUserDetail[$model]['id'] = $userDetail[$this->alias]['id'];
}
$newUserDetail[$model]['field'] = $field;
$newUserDetail[$model]['value'] = $value;
$newUserDetail[$model]['input'] = '';
$newUserDetail[$model]['data_type'] = '';
$newUserDetail[$model]['label'] = '';
$this->save($newUserDetail, false);
}
} elseif (isset($this->{$model})) {
// Save other model data
$toSave[$model] = $userDetails;
if (!empty($userId)) {
if ($model == 'User') {
$toSave[$model]['id'] = $userId;
} elseif ($this->{$model}->hasField('user_id')) {
$toSave[$model]['user_id'] = $userId;
}
}
$this->{$model}->save($toSave, false);
}
}
}
return true;
}
}
;