latest changes requested to remove ID number and date of birth
[namibia] / module / User / src / User / DataBin / Profile.php
1 <?php
2 namespace User\DataBin;
3
4
5
6 /**
7  * Manage profile data.
8  * @author andre.fourie
9  */
10 class Profile extends \Workspace\Service\DataBin
11 {
12
13         /**
14          * @var array
15          */
16         protected $meta = array(
17                         'Base'  => 'Profile',
18                         'DatasetName' => 'users',
19                         'Entity'      => '\User\Entity\Profile',
20                         'References'  => array(
21                                         'company'       => '\Company\Entity\Company',
22                                         'manager'       => '\User\Entity\Profile',
23                                         'region'        => '\Location\Entity\Region',
24                                         'group'  => '\Company\Entity\Group',
25                                         'groupDivision' => '\Company\Entity\GroupDivision',
26                                         'permissions'   => '\User\Entity\Permissions'
27                         ),
28                         'Dependants'  => array(
29                                         'sessions' => '\User\Entity\Session'
30                         )
31         );
32         /**
33          * @var array
34          */
35         protected $metaList = array(
36                         'Type'    => 'List',
37                         'Contract'      => 'Recurring',
38                         'RequiredInput' => array(),
39                         'OptionalInput' => array(
40                                         'Filter' => array(
41                                                         'created'            => 'Date',
42                                                         'updated'            => 'Date',
43                                                         'email'        => 'Email',
44                                                         'password'          => 'String40',
45                                                         'firstName'        => 'String100',
46                                                         'familyName'      => 'String100',
47                                                         'idNumber'          => 'IdNumber',
48                                                         'dateOfBirth'    => 'Date',
49                                                         'mobile'              => 'Mobile',
50                                                         'permissions'    => 'Id',
51                                                         'company'            => 'Id',
52                                                         'region'              => 'Id',
53                                                         'group'        => 'Id',
54                                                         'groupDivision'       => 'Id',
55                                                         'userType'          => 'UserType',
56                                                         'position'          => 'Position',
57                                                         'subscribeNewsletter' => 'Boolean',
58                                                         'subscribeReminders'  => 'Boolean',
59                                                         'lastLogin'        => 'DateTime',
60                                                         'jobState'          => 'String25',
61                                                         'mainMemberPublicCentre' => 'Boolean'
62                                         )
63                         ),
64                         'Filter' => array(),
65                         'Fields' => array(
66                                 'firstName',
67                                 'familyName',
68                                 'email',
69                                 'mobile',
70                                 'company',
71                                 'userType',
72                                 'position'
73                         ),
74                         'Expand' => array()
75         );
76         /**
77          * @var array
78          */
79         protected $metaSelectList = array(
80                         'Type'    => 'SelectList',
81                         'Contract'      => 'Recurring',
82                         'RequiredInput' => array(),
83                         'OptionalInput' => array(
84                                         'Filter' => array(
85                                                         'created'            => 'Date',
86                                                         'updated'            => 'Date',
87                                                         'email'        => 'Email',
88                                                         'password'          => 'String40',
89                                                         'firstName'        => 'String100',
90                                                         'familyName'      => 'String100',
91                                                         'idNumber'          => 'IdNumber',
92                                                         'dateOfBirth'    => 'Date',
93                                                         'mobile'              => 'Mobile',
94                                                         'permissions'    => 'Id',
95                                                         'company'            => 'Id',
96                                                         'region'              => 'Id',
97                                                         'group'        => 'Id',
98                                                         'groupDivision'       => 'Id',
99                                                         'userType'          => 'UserType',
100                                                         'position'          => 'Position',
101                                                         'subscribeNewsletter' => 'Boolean',
102                                                         'subscribeReminders'  => 'Boolean',
103                                                         'lastLogin'        => 'DateTime',
104                                                         'jobState'          => 'String25',
105                                                         'mainMemberPublicCentre' => 'Boolean'
106                                         )
107                         ),
108                         'Filter'   => array(),
109                         'Label'    => array(
110                                 'Format' => '[firstName] [familyName]',
111                                 'Fields' => array('firstName', 'familyName')
112                         )
113         );
114         /**
115          * @var array
116          */
117         protected $metaManagerList = array(
118                         'Type'    => 'SelectList',
119                         'Contract'      => 'Recurring',
120                         'RequiredInput' => array(),
121                         'OptionalInput' => array(
122                                         'Filter' => array(
123                                                         'company'            => 'Id'
124                                         )
125                         ),
126                         'Filter'        => array(),
127                         'Label'  => array(
128                                 'Format' => '[firstName] [familyName]',
129                                 'Fields' => array('firstName', 'familyName')
130                         )
131         );
132         /**
133          * @var array
134          */
135         protected $metaAdminManagerList = array(
136                         'Type'    => 'SelectList',
137                         'Contract'      => 'Recurring',
138                         'RequiredInput' => array(),
139                         'OptionalInput' => array(
140                                         'Filter' => array(
141                                                         'company'            => 'Id'
142                                         )
143                         ),
144                         'Filter'        => array(),
145                         'Label'  => array(
146                                 'Format' => '[firstName] [familyName]',
147                                 'Fields' => array('firstName', 'familyName')
148                         )
149         );
150         /**
151          * @var array
152          */
153         protected $metaSalesList = array(
154                         'Type'    => 'SelectList',
155                         'Contract'      => 'Recurring',
156                         'RequiredInput' => array(),
157                         'OptionalInput' => array(),
158                         'Filter'        => array(),
159                         'Label'  => array(
160                                 'Format' => '[firstName] [familyName]',
161                                 'Fields' => array('firstName', 'familyName')
162                         )
163         );
164         /**
165          * @var array
166          */
167         protected $metaValuatorList = array(
168                         'Type'    => 'SelectList',
169                         'Contract'      => 'Recurring',
170                         'RequiredInput' => array(),
171                         'OptionalInput' => array(),
172                         'Filter'        => array(),
173                         'Label'  => array(
174                                 'Format' => '[firstName] [familyName]',
175                                 'Fields' => array('firstName', 'familyName')
176                         )
177         );
178         /**
179          * @var array
180         */
181         protected $metaGrid = array(
182                         'Type'    => 'Grid',
183                         'Contract'      => 'Recurring',
184                         'RequiredInput' => array(),
185                         'OptionalInput' => array(
186                                         'Grid' => array(
187                                                         'NumberOfRecords' => 'Integer',
188                                                         'Page'      => 'Integer',
189                                                         'Filter'          => 'Array',
190                                                         'OrderBy'        => 'Array',
191                                         )
192                         ),
193                         'Base'      => 'profile',
194                         'NumberOfRecords' => 10,
195                         'Query'    => 'SELECT [SELECTION]
196                                                                 FROM \User\Entity\Profile profile
197                                                                 JOIN profile.permissions permissions
198                                                                 LEFT JOIN profile.company company
199                                                                 LEFT JOIN profile.manager manager
200                                                                 [WHERE] [ORDER]',
201                         'Selection'       => 'profile, permissions, manager',
202                         'Filter'          => array(
203                                                                         'profile.archived' => false
204                                                                 ),
205                         'OrderBy'        => array(
206                                                                         'profile.firstName'  => 'ASC',
207                                                                         'profile.familyName' => 'ASC'
208                                                                 ),
209                         'Fields'          => array(
210                                                                         'id',
211                                                                         'firstName',
212                                                                         'familyName',
213                                                                         'email',
214                                                                         'mobile',
215                                                                         'created' => 'DateTime',
216                                                                         'jobState',
217                                                                         'manager' => array(
218                                                                                 'firstName',
219                                                                                 'familyName'
220                                                                         ),
221                                                                         'permissions' => array(
222                                                                                 'name'
223                                                                         )
224                                                                 )
225         );
226         /**
227          * @var array
228         */
229         protected $metaAdminGrid = array(
230                         'Type'    => 'Grid',
231                         'Contract'      => 'Recurring',
232                         'Export'        => true,
233                         'Builder'       => '\User\Report\ProfileGrid',
234                         'Writer'        => '\Utility\Export\ExcelReport',
235                         'Output'        => 'Download',
236                         'Options'       => array(
237                                         'Boolean' => array(
238                                                         'ExportToExcel' => false
239                                         )
240                         ),
241                         'RequiredInput' => array(),
242                         'OptionalInput' => array(
243                                         'Grid'  => array(
244                                                         'NumberOfRecords' => 'Integer',
245                                                         'Page'      => 'Integer',
246                                                         'Filter'          => 'Array',
247                                                         'OrderBy'        => 'Array',
248                                         )
249                         ),
250                         'Base'      => 'profile',
251                         'NumberOfRecords' => 10,
252                         'Query'    => 'SELECT [SELECTION]
253                                                                 FROM \User\Entity\Profile profile
254                                                                 JOIN profile.permissions permissions
255                                                                 LEFT JOIN profile.company company
256                                                                 LEFT JOIN company.group companyGroup
257                                                                 LEFT JOIN company.groupDivision groupDivision
258                                                                 LEFT JOIN company.city city
259                                                                 LEFT JOIN company.region region
260                                                                 LEFT JOIN profile.manager manager
261                                                                 [WHERE] [ORDER]',
262                         'Selection'       => 'profile, permissions, company, manager, companyGroup, groupDivision, city, region',
263                         'Filter'          => array(
264                                                                         'profile.archived' => false
265                                                                 ),
266                         'OrderBy'        => array(
267                                                                         'profile.firstName'  => 'ASC',
268                                                                         'profile.familyName' => 'ASC'
269                                                                 ),
270                         'Fields'          => array(
271                                                                         'id',
272                                                                         'firstName',
273                                                                         'familyName',
274                                                                         'email',
275                                                                         'mobile',
276                                                                         'created' => 'DateTime',
277                                                                         'jobState',
278                                                                         'manager' => array(
279                                                                                 'firstName',
280                                                                                 'familyName'
281                                                                         ),
282                                                                         'permissions' => array(
283                                                                                 'name'
284                                                                         ),
285                                                                         'company' => array(
286                                                                                 'name',
287                                                                                 'street',
288                                                                                 'city' => array(
289                                                                                         'region' => array('name')
290                                                                                 ),
291                                                                                 'group' => array('name'),
292                                                                                 'groupDivision' => array('name')
293                                                                         )
294                                                                 )
295         );
296         /**
297          * @var array
298          */
299         protected $metaCreate = array(
300                         'Type'    => 'Create',
301                         'Contract'      => 'Recurring',
302                         'ExecuteAfter'  => array(
303                                         'sendWelcomeNotification'
304                         ),
305                         'RequiredInput' => array(
306                                         'Profile' => array(
307                                                         'email'       => 'Email',
308                                                         'firstName'   => 'String100',
309                                                         'familyName'  => 'String100',
310 //                                                      'idNumber'    => 'IdNumber',
311 //                                                      'dateOfBirth' => 'Date',
312                                                         'mobile'      => 'String20',
313                                                         'permissions' => 'Id'
314                                         )
315                         ),
316                         'OptionalInput' => array(
317                                         'Profile' => array(
318                                                         'manager'            => 'Id',
319                                                         'subscribeNewsletter' => 'Boolean',
320                                                         'subscribeReminders'  => 'Boolean',
321                                                         'mainMemberPublicCentre' => 'Boolean'
322
323                                         )
324                         )
325         );
326         /**
327          * @var array
328          */
329         protected $metaUpdate = array(
330                         'Type'    => 'Update',
331                         'Contract'      => 'UseOnce',
332                         'ExecuteAfter'  => array(
333                                         'updateAuthSession'
334                         ),
335                         'RequiredInput' => array(
336                                         'Profile' => array(
337                                                         'email'       => 'Email',
338                                                         'firstName'   => 'String100',
339                                                         'familyName'  => 'String100',
340                                                         'idNumber'    => 'IdNumber',
341                                                         'dateOfBirth' => 'Date',
342                                                         'mobile'      => 'String20',
343                                                         'permissions' => 'Id'
344                                         )
345                         ),
346                         'OptionalInput' => array(
347                                         'Profile' => array(
348                                                         'company'            => 'Id',
349                                                         'manager'            => 'Id',
350                                                         'subscribeNewsletter' => 'Boolean',
351                                                         'subscribeReminders'  => 'Boolean',
352                                                         'mainMemberPublicCentre' => 'Boolean'
353
354                                         )
355                         )
356         );
357                         
358         /**
359          * @var array
360          */
361         protected $metaUnsubscribe = array(
362                         'Type'                  => 'Update',
363                         'Contract'      => 'UseOnce',
364                         'ExecuteAfter'  => array(),
365                         'RequiredInput' => array(),
366                         'OptionalInput' => array(
367                                         'Profile' => array(
368                                                         'subscribeNewsletter' => 'Boolean',
369                                                         'subscribeReminders'  => 'Boolean'
370
371                                         )
372                         )
373         );
374         /**
375          * @var array
376          */
377         protected $metaChangePassword = array(
378                         'Type'    => 'Update',
379                         'Contract'      => 'UseOnce',
380                         'RequiredInput' => array(
381                                         'Profile' => array(
382                                                         'oldPassword' => 'String40',
383                                                         'newPassword' => 'String40'
384                                         )
385                         ),
386                         'OptionalInput' => array()
387         );
388         /**
389          * @var array
390          */
391         protected $metaAdminCreate = array(
392                         'Type'    => 'Create',
393                         'Contract'      => 'Recurring',
394                         'RequiredInput' => array(
395                                         'Profile' => array(
396                                                         'email'       => 'Email',
397                                                         'password'    => 'String40',
398                                                         'firstName'   => 'String100',
399                                                         'familyName'  => 'String100',
400                                                         'idNumber'    => 'IdNumber',
401                                                         'dateOfBirth' => 'Date',
402                                                         'mobile'      => 'Mobile',
403                                                         'permissions' => 'Id'
404                                         )
405                         ),
406                         'OptionalInput' => array(
407                                         'Profile' => array(
408                                                         'company'            => 'Id',
409                                                         'region'              => 'Id',
410                                                         'group'        => 'Id',
411                                                         'groupDivision'       => 'Id',
412                                                         'userType'          => 'UserType',
413                                                         'position'          => 'Position',
414                                                         'subscribeNewsletter' => 'Boolean',
415                                                         'subscribeReminders'  => 'Boolean',
416                                                         'lastLogin'        => 'DateTime',
417                                                         'mainMemberPublicCentre' => 'Boolean'
418                                         )
419                         )
420         );
421         /**
422          * @var array
423          */
424         protected $metaAdminUpdate = array(
425                         'Type'    => 'Update',
426                         'Contract'      => 'UseOnce',
427                         'RequiredInput' => array(
428                                         'Profile' => array(
429                                                         'email'       => 'Email',
430                                                         'password'    => 'String40',
431                                                         'firstName'   => 'String100',
432                                                         'familyName'  => 'String100',
433                                                         'idNumber'    => 'IdNumber',
434                                                         'dateOfBirth' => 'Date',
435                                                         'mobile'      => 'Mobile',
436                                                         'permissions' => 'Id'
437                                         )
438                         ),
439                         'OptionalInput' => array(
440                                         'Profile' => array(
441                                                         'company'            => 'Id',
442                                                         'region'              => 'Id',
443                                                         'group'        => 'Id',
444                                                         'groupDivision'       => 'Id',
445                                                         'userType'          => 'UserType',
446                                                         'position'          => 'Position',
447                                                         'subscribeNewsletter' => 'Boolean',
448                                                         'subscribeReminders'  => 'Boolean',
449                                                         'mainMemberPublicCentre' => 'Boolean'
450                                         )
451                         )
452         );
453
454
455         /**
456          * Dynamic filtering.
457          */
458         public function __construct()
459         {
460                 if (!\Utility\Registry::isAuthenticated())
461                 {
462                         return;
463                 }
464                 $authData = \Utility\Registry::getAuthData();
465                 $companyFilter = false;
466                 $companyId = 0;
467                 switch (\Utility\Registry::getUserType())
468                 {
469                         case 'B4C User':
470                                 $groupFilter = \Utility\Registry::getSudo('Group', false);
471                                 $groupFilter
472                                         && $this->metaGrid['Filter']['IDENTITY(company.group)'] = $groupFilter;
473                                 $divisionFilter = \Utility\Registry::getSudo('Division', false);
474                                 $divisionFilter
475                                         && $this->metaGrid['Filter']['IDENTITY(company.groupDivision)'] = $divisionFilter;
476                                 $companyFilter = \Utility\Registry::getSudo('Company', false);
477                                 if ($companyFilter)
478                                 {
479                                         $this->metaGrid['Filter']['company.id'] = $companyFilter;
480                                         $this->metaSelectList['Filter']['company'] = \Utility\Registry::getEntityManager()
481                                                         ->getReference(
482                                                                 '\Company\Entity\Company', $companyFilter
483                                                                 );
484                                         $this->metaManagerList['Filter']['company'] = $companyFilter;
485                                         $this->metaSalesList['Filter']['company'] = $companyFilter;
486                                         $this->metaValuatorList['Filter']['company'] = $companyFilter;
487                                         $companyId = $companyFilter;
488                                 }
489                                 else
490                                 {
491                                         $this->metaManagerList['Filter']['company'] = 0;
492                                         $this->metaSalesList['Filter']['company'] = 0;
493                                         $this->metaValuatorList['Filter']['company'] = 0;
494                                 }
495                                 break;
496                         case 'Group User':
497                                 $this->metaGrid['Filter']['IDENTITY(company.group)'] = $authData['company']['group']['id'];
498                                 $divisionFilter = \Utility\Registry::getSudo('Division', false);
499                                 $divisionFilter
500                                         && $this->metaGrid['Filter']['IDENTITY(company.groupDivision)'] = $divisionFilter;
501                                 $companyFilter = \Utility\Registry::getSudo('Company', false);
502                                 if ($companyFilter)
503                                 {
504                                         $this->metaGrid['Filter']['company.id'] = $companyFilter;
505                                         $this->metaSelectList['Filter']['company'] = \Utility\Registry::getEntityManager()
506                                                         ->getReference(
507                                                                 '\Company\Entity\Company', $companyFilter
508                                                                 );
509                                         $this->metaManagerList['Filter']['company'] = $companyFilter;
510                                         $this->metaSalesList['Filter']['company'] = $companyFilter;
511                                         $this->metaValuatorList['Filter']['company'] = $companyFilter;
512                                         $companyId = $companyFilter;
513                                 }
514                                 else
515                                 {
516                                         $this->metaManagerList['Filter']['company'] = 0;
517                                         $this->metaSalesList['Filter']['company'] = 0;
518                                         $this->metaValuatorList['Filter']['company'] = 0;
519                                 }
520                                 break;
521                         case 'Dealer Principle':
522                                 $this->metaGrid['Filter']['IDENTITY(company.groupDivision)'] = $authData['company']['groupDivision']['id'];
523                                 $companyFilter = \Utility\Registry::getSudo('Company', false);
524                                 if ($companyFilter)
525                                 {
526                                         $this->metaGrid['Filter']['company.id'] = $companyFilter;
527                                         $this->metaSelectList['Filter']['company'] = \Utility\Registry::getEntityManager()
528                                                 ->getReference(
529                                                                 '\Company\Entity\Company', $companyFilter
530                                                 );
531                                         $this->metaManagerList['Filter']['company'] = $companyFilter;
532                                         $this->metaSalesList['Filter']['company'] = $companyFilter;
533                                         $this->metaValuatorList['Filter']['company'] = $companyFilter;
534                                         $companyId = $companyFilter;
535                                 }
536                                 else
537                                 {
538                                         $this->metaManagerList['Filter']['company'] = 0;
539                                         $this->metaSalesList['Filter']['company'] = 0;
540                                         $this->metaValuatorList['Filter']['company'] = 0;
541                                 }
542                                 break;
543                         default:
544                                 $this->metaGrid['Filter']['company.id'] = $authData['company']['id'];
545                                 $this->metaSelectList['Filter']['company'] = \Utility\Registry::getEntityManager()
546                                         ->getReference(
547                                                 '\Company\Entity\Company', $authData['company']['id']
548                                                 );
549                                 $this->metaManagerList['Filter']['company'] = $authData['company']['id'];
550                                 $this->metaSalesList['Filter']['company'] = $authData['company']['id'];
551                                 $this->metaValuatorList['Filter']['company'] = $authData['company']['id'];
552                                 $companyId = $authData['company']['id'];
553                                 break;
554                 }
555                 //\Utility\Debug::errorLog('FILTERS', $this->metaGrid['Filter']);
556                 $em = \Utility\Registry::getEntityManager();
557                 $profiles = array();
558                 if (0 == $companyId)
559                 {
560                         return;
561                 }
562                 if (!is_null($companyId) && is_numeric($companyId))
563                 {
564                         $managerNew = $em
565                                 ->getRepository('\User\Entity\Permissions')
566                                 ->findOneBy(array('name' => 'Dealership Manager (New)'))
567                                 ->id;
568                         $managerUsed = $em
569                                 ->getRepository('\User\Entity\Permissions')
570                                 ->findOneBy(array('name' => 'Dealership Manager (Used)'))
571                                 ->id;
572                         $profs = $em->getRepository('\User\Entity\Profile')
573                                 ->findBy(array('company' => $companyId));
574                         foreach ($profs as $prof)
575                         {
576                                 $profiles[] = $prof->id;
577                         }
578                 }
579                 $sales = array(0);
580                 $valuators = array(0);
581                 if (!empty($profiles))
582                 {
583                         $perms = $em->getRepository('\User\Entity\Override')
584                                 ->findBy(array('valuationIsSales' => true, 'profile' => $profiles));
585                         foreach ($perms as $permission)
586                         {
587                                 $sales[] = $permission->id;
588                         }
589                         $perms = $em->getRepository('\User\Entity\Override')
590                                 ->findBy(array('valuationIsValuator' => true, 'profile' => $profiles));
591                         foreach ($perms as $permission)
592                         {
593                                 $valuators[] = $permission->id;
594                         }
595                 }
596
597                 $this->metaAdminManagerList['Filter']['permissions'] = array($managerNew, $managerUsed);
598                 $this->metaManagerList['Filter']['permissions'] = array($managerNew, $managerUsed);
599                 $this->metaSalesList['Filter']['override']      = $sales;
600                 $this->metaValuatorList['Filter']['override']   = $valuators;
601         }
602
603         public function getValuationFilters()
604         {
605                 return $this->metaValuatorList['Filter'];
606         }
607
608 }