7
7
8
8
use Magento \Customer \Api \AccountManagementInterface ;
9
9
use Magento \Customer \Controller \RegistryConstants ;
10
- use Magento \Customer \Model \AccountManagement ;
11
10
use Magento \Customer \Model \Address \Mapper ;
12
11
use Magento \Framework \Exception \NoSuchEntityException ;
13
12
use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
14
13
15
14
/**
16
- * Adminhtml customer view personal information sales block
15
+ * Adminhtml customer view personal information sales block.
16
+ *
17
17
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
18
18
*/
19
19
class PersonalInfo extends \Magento \Backend \Block \Template
20
20
{
21
21
/**
22
+ * Interval in minutes that shows how long customer will be marked 'Online'
23
+ * since his last activity. Used only if it's impossible to get such setting
24
+ * from configuration.
25
+ */
26
+ const DEFAULT_ONLINE_MINUTES_INTERVAL = 15 ;
27
+
28
+ /**
29
+ * Customer
30
+ *
22
31
* @var \Magento\Customer\Api\Data\CustomerInterface
23
32
*/
24
33
protected $ customer ;
25
34
26
35
/**
36
+ * Customer log
37
+ *
38
+ * @var \Magento\Customer\Model\Log
39
+ */
40
+ protected $ customerLog ;
41
+
42
+ /**
43
+ * Customer logger
44
+ *
45
+ * @var \Magento\Customer\Model\Logger
46
+ */
47
+ protected $ customerLogger ;
48
+
49
+ /**
50
+ * Account management
51
+ *
27
52
* @var AccountManagementInterface
28
53
*/
29
54
protected $ accountManagement ;
30
55
31
56
/**
57
+ * Customer group repository
58
+ *
32
59
* @var \Magento\Customer\Api\GroupRepositoryInterface
33
60
*/
34
61
protected $ groupRepository ;
35
62
36
63
/**
64
+ * Customer data factory
65
+ *
37
66
* @var \Magento\Customer\Api\Data\CustomerInterfaceFactory
38
67
*/
39
68
protected $ customerDataFactory ;
40
69
41
70
/**
71
+ * Address helper
72
+ *
42
73
* @var \Magento\Customer\Helper\Address
43
74
*/
44
75
protected $ addressHelper ;
45
76
46
77
/**
78
+ * Date time
79
+ *
47
80
* @var \Magento\Framework\Stdlib\DateTime
48
81
*/
49
82
protected $ dateTime ;
@@ -56,11 +89,15 @@ class PersonalInfo extends \Magento\Backend\Block\Template
56
89
protected $ coreRegistry ;
57
90
58
91
/**
92
+ * Address mapper
93
+ *
59
94
* @var Mapper
60
95
*/
61
96
protected $ addressMapper ;
62
97
63
98
/**
99
+ * Data object helper
100
+ *
64
101
* @var \Magento\Framework\Api\DataObjectHelper
65
102
*/
66
103
protected $ dataObjectHelper ;
@@ -75,6 +112,7 @@ class PersonalInfo extends \Magento\Backend\Block\Template
75
112
* @param \Magento\Framework\Registry $registry
76
113
* @param Mapper $addressMapper
77
114
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
115
+ * @param \Magento\Customer\Model\Logger $customerLogger
78
116
* @param array $data
79
117
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
80
118
*/
@@ -88,6 +126,7 @@ public function __construct(
88
126
\Magento \Framework \Registry $ registry ,
89
127
Mapper $ addressMapper ,
90
128
\Magento \Framework \Api \DataObjectHelper $ dataObjectHelper ,
129
+ \Magento \Customer \Model \Logger $ customerLogger ,
91
130
array $ data = []
92
131
) {
93
132
$ this ->coreRegistry = $ registry ;
@@ -98,10 +137,14 @@ public function __construct(
98
137
$ this ->dateTime = $ dateTime ;
99
138
$ this ->addressMapper = $ addressMapper ;
100
139
$ this ->dataObjectHelper = $ dataObjectHelper ;
140
+ $ this ->customerLogger = $ customerLogger ;
141
+
101
142
parent ::__construct ($ context , $ data );
102
143
}
103
144
104
145
/**
146
+ * Retrieve customer object
147
+ *
105
148
* @return \Magento\Customer\Api\Data\CustomerInterface
106
149
*/
107
150
public function getCustomer ()
@@ -117,13 +160,31 @@ public function getCustomer()
117
160
}
118
161
119
162
/**
163
+ * Retrieve customer id
164
+ *
120
165
* @return string|null
121
166
*/
122
167
public function getCustomerId ()
123
168
{
124
169
return $ this ->coreRegistry ->registry (RegistryConstants::CURRENT_CUSTOMER_ID );
125
170
}
126
171
172
+ /**
173
+ * Retrieves customer log model
174
+ *
175
+ * @return \Magento\Customer\Model\Log
176
+ */
177
+ protected function getCustomerLog ()
178
+ {
179
+ if (!$ this ->customerLog ) {
180
+ $ this ->customerLog = $ this ->customerLogger ->get (
181
+ $ this ->getCustomer ()->getId ()
182
+ );
183
+ }
184
+
185
+ return $ this ->customerLog ;
186
+ }
187
+
127
188
/**
128
189
* Returns customer's created date in the assigned store
129
190
*
@@ -146,6 +207,8 @@ public function getStoreCreateDate()
146
207
}
147
208
148
209
/**
210
+ * Retrieve store default timezone from configuration
211
+ *
149
212
* @return string
150
213
*/
151
214
public function getStoreCreateDateTimezone ()
@@ -172,6 +235,8 @@ public function getCreateDate()
172
235
}
173
236
174
237
/**
238
+ * Check if account is confirmed
239
+ *
175
240
* @return \Magento\Framework\Phrase
176
241
*/
177
242
public function getIsConfirmedStatus ()
@@ -189,6 +254,8 @@ public function getIsConfirmedStatus()
189
254
}
190
255
191
256
/**
257
+ * Retrieve store
258
+ *
192
259
* @return null|string
193
260
*/
194
261
public function getCreatedInStore ()
@@ -199,6 +266,8 @@ public function getCreatedInStore()
199
266
}
200
267
201
268
/**
269
+ * Retrieve billing address html
270
+ *
202
271
* @return \Magento\Framework\Phrase|string
203
272
*/
204
273
public function getBillingAddressHtml ()
@@ -221,6 +290,8 @@ public function getBillingAddressHtml()
221
290
}
222
291
223
292
/**
293
+ * Retrieve group name
294
+ *
224
295
* @return string|null
225
296
*/
226
297
public function getGroupName ()
@@ -236,6 +307,8 @@ public function getGroupName()
236
307
}
237
308
238
309
/**
310
+ * Retrieve customer group by id
311
+ *
239
312
* @param int $groupId
240
313
* @return \Magento\Customer\Api\Data\GroupInterface|null
241
314
*/
@@ -248,4 +321,104 @@ private function getGroup($groupId)
248
321
}
249
322
return $ group ;
250
323
}
324
+
325
+ /**
326
+ * Returns timezone of the store to which customer assigned.
327
+ *
328
+ * @return string
329
+ */
330
+ public function getStoreLastLoginDateTimezone ()
331
+ {
332
+ return $ this ->_scopeConfig ->getValue (
333
+ $ this ->_localeDate ->getDefaultTimezonePath (),
334
+ \Magento \Store \Model \ScopeInterface::SCOPE_STORE ,
335
+ $ this ->getCustomer ()->getStoreId ()
336
+ );
337
+ }
338
+
339
+ /**
340
+ * Get customer's current status.
341
+ *
342
+ * Customer considered 'Offline' in the next cases:
343
+ *
344
+ * - customer has never been logged in;
345
+ * - customer clicked 'Log Out' link\button;
346
+ * - predefined interval has passed since customer's last activity.
347
+ *
348
+ * In all other cases customer considered 'Online'.
349
+ *
350
+ * @return \Magento\Framework\Phrase
351
+ */
352
+ public function getCurrentStatus ()
353
+ {
354
+ // Customer has never been logged in.
355
+ if (!$ this ->getCustomerLog ()->getLastLoginAt ()) {
356
+ return __ ('Offline ' );
357
+ }
358
+
359
+ // Customer clicked 'Log Out' link\button.
360
+ if ($ this ->getCustomerLog ()->getLastLogoutAt () &&
361
+ strtotime ($ this ->getCustomerLog ()->getLastLogoutAt ()) > strtotime ($ this ->getCustomerLog ()->getLastLoginAt ())
362
+ ) {
363
+ return __ ('Offline ' );
364
+ }
365
+
366
+ // Predefined interval has passed since customer's last activity.
367
+ $ interval = $ this ->getOnlineMinutesInterval ();
368
+
369
+ if ($ this ->getCustomerLog ()->getLastVisitAt () &&
370
+ strtotime ($ this ->dateTime ->now ()) - strtotime ($ this ->getCustomerLog ()->getLastVisitAt ()) > $ interval * 60
371
+ ) {
372
+ return __ ('Offline ' );
373
+ }
374
+
375
+ return __ ('Online ' );
376
+ }
377
+
378
+ /**
379
+ * Get customer last login date.
380
+ *
381
+ * @return \Magento\Framework\Phrase|string
382
+ */
383
+ public function getLastLoginDate ()
384
+ {
385
+ $ date = $ this ->getCustomerLog ()->getLastLoginAt ();
386
+
387
+ if ($ date ) {
388
+ return $ this ->formatDate ($ date , TimezoneInterface::FORMAT_TYPE_MEDIUM , true );
389
+ }
390
+
391
+ return __ ('Never ' );
392
+ }
393
+
394
+ /**
395
+ * Returns customer last login date in store's timezone.
396
+ *
397
+ * @return \Magento\Framework\Phrase|string
398
+ */
399
+ public function getStoreLastLoginDate ()
400
+ {
401
+ $ date = strtotime ($ this ->getCustomerLog ()->getLastLoginAt ());
402
+
403
+ if ($ date ) {
404
+ $ date = $ this ->_localeDate ->scopeDate ($ this ->getCustomer ()->getStoreId (), $ date , true );
405
+ return $ this ->formatDate ($ date , TimezoneInterface::FORMAT_TYPE_MEDIUM , true );
406
+ }
407
+
408
+ return __ ('Never ' );
409
+ }
410
+
411
+ /**
412
+ * Returns interval that shows how long customer will be considered 'Online'.
413
+ *
414
+ * @return int Interval in minutes
415
+ */
416
+ protected function getOnlineMinutesInterval ()
417
+ {
418
+ $ configValue = $ this ->_scopeConfig ->getValue (
419
+ 'customer/online_customers/online_minutes_interval ' ,
420
+ \Magento \Store \Model \ScopeInterface::SCOPE_STORE
421
+ );
422
+ return intval ($ configValue ) > 0 ? intval ($ configValue ) : self ::DEFAULT_ONLINE_MINUTES_INTERVAL ;
423
+ }
251
424
}
0 commit comments