initial commit
[namibia] / module / PriceGuide / src / PriceGuide / Entity / Member.php
1 <?php
2 namespace PriceGuide\Entity;
3
4 use Doctrine\ORM\Mapping as ORM;
5
6
7
8 /**
9  * A price guide club member.
10  *
11  * @ORM\Entity
12  * @ORM\HasLifecycleCallbacks
13  * @ORM\Table(name="price_guide_club_member")
14  */
15 class Member
16 {
17
18         /**
19          * Can archive records.
20          */
21         const ARCHIVE = true;
22         /**
23          * Pull Synchronization Strategy for this table.
24          */
25         const PULL_SYNCH_STRATEGY = false;
26         /**
27          * Push Synchronization Strategy for this table.
28          */
29         const PUSH_SYNCH_STRATEGY = false;
30
31
32
33         /* ------------------------------------ Identification ------------------------------------ */
34         /**
35          * @ORM\Id
36          * @ORM\Column(type="integer");
37          * @ORM\GeneratedValue(strategy="AUTO")
38          */
39         protected $id;
40
41
42         /* ------------------------------------ Linkage ------------------------------------ */
43         /**
44          * @ORM\ManyToOne(targetEntity="Club", inversedBy="members")
45          * @ORM\JoinColumn(nullable=false, name="price_guide_club_id")
46          **/
47         protected $club;
48
49         /**
50          * @ORM\OneToMany(targetEntity="MemberStock", mappedBy="member", cascade={"all"}, fetch="EXTRA_LAZY")
51          **/
52         protected $memberItems;
53
54
55         /* ------------------------------------ Customer ------------------------------------ */
56         /**
57          * @ORM\ManyToOne(targetEntity="\Company\Entity\Company")
58          * @ORM\JoinColumn(nullable=true, name="company_id")
59          **/
60         protected $company;
61
62         /**
63          * @ORM\ManyToOne(targetEntity="\User\Entity\Profile")
64          * @ORM\JoinColumn(nullable=true, name="profile_id")
65          **/
66         protected $profile;
67
68         /**
69          * @ORM\ManyToOne(targetEntity="AllowedMember")
70          * @ORM\JoinColumn(nullable=true, name="price_guide_allowed_member_id")
71          **/
72         protected $allowedMember;
73
74
75         /* ------------------------------------ Filtering ------------------------------------ */
76         /**
77          * @ORM\OneToMany(targetEntity="MemberMake", mappedBy="member", cascade={"all"}, fetch="EXTRA_LAZY")
78          **/
79         private $makes;
80
81         /**
82          * @ORM\Column(type="boolean", name="all_makes");
83          */
84         protected $allMakes = true;
85
86         /**
87          * @ORM\ManyToOne(targetEntity="\Stock\Entity\Year")
88          * @ORM\JoinColumn(nullable=true, name="intro_vehicle_year_id")
89          **/
90         protected $fromYear;
91
92         /**
93          * @ORM\ManyToOne(targetEntity="\Stock\Entity\Year")
94          * @ORM\JoinColumn(nullable=true, name="disc_vehicle_year_id")
95          **/
96         protected $toYear;
97
98         /**
99          * @ORM\Column(nullable=true, type="integer", name="from_km");
100          */
101         protected $fromKm;
102
103         /**
104          * @ORM\Column(nullable=true, type="integer", name="to_km");
105          */
106         protected $toKm;
107
108         /**
109          * @ORM\Column(type="boolean", name="email_notification");
110          */
111         protected $emailNotification = true;
112
113         /**
114          * @ORM\Column(type="boolean", name="sms_notification");
115          */
116         protected $smsNotification = false;
117
118
119         /* ------------------------------------ Tracking ------------------------------------ */
120
121         /**
122          * @ORM\Column(type="string", length=25, nullable=true, name="status");
123          */
124         protected $status = 'Invited';
125
126         /**
127          * @ORM\Column(type="datetime");
128          */
129         protected $created;
130
131         /**
132          * @ORM\Column(type="datetime", nullable=true);
133          */
134         protected $updated;
135
136         /**
137          * @ORM\Column(type="boolean");
138          */
139         protected $archived = false;
140
141
142
143         /**
144          * Initialize collections.
145          */
146         public function __construct()
147         {
148                 $this->makes = new \Doctrine\Common\Collections\ArrayCollection();
149         }
150
151         /**
152          * Add a new Make to this Stock Item.
153          * @param \Stock\Entity\Make $make
154          * @return \PriceGuide\Entity\Member
155          */
156         public function addMake(\Stock\Entity\Make $make)
157         {
158                 $this->makes[] = $make;
159                 return $this;
160         }
161
162
163         /**
164          * Magic getter to expose protected properties.
165          *
166          * @param string $property
167          * @return mixed
168          */
169         public function __get($property)
170         {
171                 return $this->$property;
172         }
173
174         /**
175          * Magic setter to save protected properties.
176          *
177          * @param string $property
178          * @param mixed $value
179          */
180         public function __set($property, $value)
181         {
182                 $this->$property = $value;
183         }
184
185         /**
186          * @ORM\PrePersist
187          */
188         public function setCreateTime()
189         {
190                 $this->created = new \DateTime("now");
191         }
192
193         /**
194          * @ORM\PreUpdate
195          */
196         public function setUpdateTime()
197         {
198                 $this->updated = new \DateTime("now");
199         }
200
201         /**
202          * Convert the object to an array.
203          * @param array $expand
204          * @param array $intersect
205          * @param boolean $showIdentifiers
206          * @param integer $expandAll
207          * @return array
208          */
209         public function toArray(
210                         array $expand = array(), array $intersect = array(),
211                         $showIdentifiers = false, $expandAll = 0
212                         )
213         {
214                 $intersect = array_flip($intersect);
215                 $dateTimeFormat = \Utility\Registry::getConfigParam('DateTimeFormat');
216                 $includeAll = empty($intersect);
217                 $data = array();
218                 ($includeAll || isset($intersect['id']))
219                         && $data['id'] = $this->id;
220
221                 ($includeAll || isset($intersect['club']))
222                         && $data['club'] = (in_array('club', $expand) || $expandAll || $showIdentifiers)
223                                                                         && !is_null($this->club)
224                                 ? (!$showIdentifiers ? $this->club->toArray(
225                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
226                                                 ) : $this->club->id)
227                                 : null;
228                 ($includeAll || isset($intersect['company']))
229                         && $data['company'] = (in_array('company', $expand) || $expandAll || $showIdentifiers)
230                                                                         && !is_null($this->company)
231                                 ? (!$showIdentifiers || in_array('company', $expand) ? $this->company->toArray(
232                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
233                                                 ) : $this->company->id)
234                                 : null;
235                 ($includeAll || isset($intersect['profile']))
236                         && $data['profile'] = (in_array('profile', $expand) || $expandAll || $showIdentifiers)
237                                                                         && !is_null($this->profile)
238                                 ? (!$showIdentifiers || in_array('profile', $expand) ? $this->profile->toArray(
239                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
240                                                 ) : $this->profile->id)
241                                 : null;
242                 ($includeAll || isset($intersect['allowedMember']))
243                         && $data['allowedMember'] = (in_array('allowedMember', $expand) || $expandAll || $showIdentifiers)
244                                                                         && !is_null($this->allowedMember)
245                                 ? (!$showIdentifiers ? $this->allowedMember->toArray(
246                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
247                                                 ) : $this->allowedMember->id)
248                                 : null;
249
250                 ($includeAll || isset($intersect['makes']))
251                         && $data['makes'] = (in_array('makes', $expand) || $expandAll || $showIdentifiers)
252                                                                                 && !is_null($this->makes)
253                                 ? $this->makesToArray($showIdentifiers)
254                                 : null;
255                 ($includeAll || isset($intersect['allMakes']))
256                         && $data['allMakes'] = $this->allMakes;
257                 ($includeAll || isset($intersect['fromYear']))
258                         && $data['fromYear'] = (in_array('fromYear', $expand) || $expandAll || $showIdentifiers)
259                                                                         && !is_null($this->fromYear)
260                                 ? (!$showIdentifiers ? $this->fromYear->toArray(
261                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
262                                                 ) : $this->fromYear->id)
263                                 : null;
264                 ($includeAll || isset($intersect['toYear']))
265                         && $data['toYear'] = (in_array('toYear', $expand) || $expandAll || $showIdentifiers)
266                                                                         && !is_null($this->toYear)
267                                 ? (!$showIdentifiers ? $this->toYear->toArray(
268                                                 $expand, $intersect, $showIdentifiers, ($expandAll - 1)
269                                                 ) : $this->toYear->id)
270                                 : null;
271                 ($includeAll || isset($intersect['fromKm']))
272                         && $data['fromKm'] = $this->fromKm;
273                 ($includeAll || isset($intersect['toKm']))
274                         && $data['toKm'] = $this->toKm;
275                 ($includeAll || isset($intersect['emailNotification']))
276                         && $data['emailNotification'] = $this->emailNotification;
277                 ($includeAll || isset($intersect['smsNotification']))
278                         && $data['smsNotification'] = $this->smsNotification;
279
280                 ($includeAll || isset($intersect['status']))
281                         && $data['status'] = $this->status;
282                 ($includeAll || isset($intersect['created']))
283                         && $data['created'] = !is_null($this->created)
284                                 ? $this->created->format($dateTimeFormat)
285                                 : null;
286                 ($includeAll || isset($intersect['updated']))
287                         && $data['updated'] = !is_null($this->updated)
288                                 ? $this->updated->format($dateTimeFormat)
289                                 : null;
290                 return $data;
291         }
292
293         /**
294          * Internal utility to change makes collection into array.
295          * @param boolean $showIdentifiers
296          * @return array
297          */
298         protected function makesToArray($showIdentifiers = false)
299         {
300                 $data = array();
301                 $iterator = $this->makes->getIterator();
302                 if (2 == $showIdentifiers)
303                 {
304                         foreach ($iterator as $make)
305                         {
306                                 $data[] = $make->make->id;
307                         }
308                 }
309                 else
310                 {
311                         foreach ($iterator as $make)
312                         {
313                                 $data[] = $showIdentifiers
314                                         ? array('id' => $make->make->id)
315                                         : $make->toArray();
316                         }
317                 }
318                 return $data;
319         }
320
321         /**
322          * Populate from an array.
323          * @param array $data
324          */
325         public function fromArray($data = array())
326         {
327                 isset($data['id'])
328                         && $this->id = $data['id'];
329                 isset($data['club'])
330                         && $this->club = $data['club'];
331                 isset($data['company'])
332                         && $this->company = $data['company'];
333                 isset($data['profile'])
334                         && $this->profile = $data['profile'];
335                 isset($data['allowedMember'])
336                         && $this->allowedMember = $data['allowedMember'];
337                 isset($data['status'])
338                         && $this->status = $data['status'];
339                 isset($data['allMakes'])
340                         && $this->allMakes = $data['allMakes'];
341                 isset($data['fromYear'])
342                         ? $this->fromYear = $data['fromYear']
343                         : $this->fromYear = null;
344                 isset($data['toYear'])
345                         ? $this->toYear = $data['toYear']
346                         : $this->toYear = null;
347                 isset($data['fromKm'])
348                         ? $this->fromKm = $data['fromKm']
349                         : $this->fromKm = null;
350                 isset($data['toKm'])
351                         ?  $this->toKm = $data['toKm']
352                         : $this->toKm = null;
353                 isset($data['emailNotification'])
354                         && $this->emailNotification = $data['emailNotification'];
355                 isset($data['smsNotification'])
356                         && $this->smsNotification = $data['smsNotification'];
357                 if (isset($data['applyToAll']) && $data['applyToAll'])
358                 {
359                         $em = \Utility\Registry::getEntityManager();
360                         $members = $em->getRepository('\PriceGuide\Entity\Member')
361                                 ->findBy(array('company' => $this->company->id));
362                         foreach ($members as $member)
363                         {
364                                 $iterator = $member->makes->getIterator();
365                                 foreach ($iterator as $mk)
366                                 {
367                                         $em->remove($mk);
368                                 }
369                                 $em->flush();
370                                 if (isset($data['allMakes']) && $data['allMakes'])
371                                 {
372                                         $member->allMakes = true;
373                                 }
374                                 else
375                                 {
376                                         $member->allMakes = false;
377                                         foreach ($data['makes'] as $make)
378                                         {
379                                                 $memberMake         = new MemberMake();
380                                                 $memberMake->make   = $em->getReference('\Stock\Entity\Make', $make['id']);
381                                                 $memberMake->member = $member;
382                                                 $member->makes->add($memberMake);
383                                         }
384                                 }
385                                 isset($data['emailNotification'])
386                                         ? $member->emailNotification = $data['emailNotification']
387                                         : $member->emailNotification = false;
388                                 isset($data['smsNotification'])
389                                         ? $member->smsNotification = $data['smsNotification']
390                                         : $member->smsNotification = false;
391                                 isset($data['fromYear'])
392                                         ? $member->fromYear = $data['fromYear']
393                                         : $member->fromYear = null;
394                                 isset($data['toYear'])
395                                         ? $member->toYear = $data['toYear']
396                                         : $member->toYear = null;
397                                 isset($data['fromKm'])
398                                         ? $member->fromKm = $data['fromKm']
399                                         : $member->fromKm = null;
400                                 isset($data['toKm'])
401                                         ? $member->toKm = $data['toKm']
402                                         : $member->toKm = null;
403                                 $em->flush($member);
404                         }
405                 }
406                 elseif (isset($data['makes']))
407                 {
408                         $em = \Utility\Registry::getEntityManager();
409                         $newMakes = array();
410                         foreach ($data['makes'] as $make)
411                         {
412                                 $newMakes[$make['id']] = $make['id'];
413                         }
414                         $currentMakes = array();
415                         $iterator = $this->makes->getIterator();
416                         foreach ($iterator as $make)
417                         {
418                                 $currentMakes[$make->make->id] = $make->make->id;
419                                 !isset($newMakes[$make->make->id])
420                                         && $em->remove($make);
421                         }
422                         foreach ($data['makes'] as $make)
423                         {
424                                 if (!isset($currentMakes[$make['id']]))
425                                 {
426                                         $memberMake = new MemberMake();
427                                         $memberMake->make = $em->getReference('\Stock\Entity\Make', $make['id']);
428                                         $memberMake->member = $this;
429                                         $this->makes->add($memberMake);
430                                 }
431                         }
432                 }
433         }
434
435
436
437 }
438