2 namespace PriceGuide\Entity;
4 use Doctrine\ORM\Mapping as ORM;
9 * A price guide club member.
12 * @ORM\HasLifecycleCallbacks
13 * @ORM\Table(name="price_guide_club_member")
19 * Can archive records.
23 * Pull Synchronization Strategy for this table.
25 const PULL_SYNCH_STRATEGY = false;
27 * Push Synchronization Strategy for this table.
29 const PUSH_SYNCH_STRATEGY = false;
33 /* ------------------------------------ Identification ------------------------------------ */
36 * @ORM\Column(type="integer");
37 * @ORM\GeneratedValue(strategy="AUTO")
42 /* ------------------------------------ Linkage ------------------------------------ */
44 * @ORM\ManyToOne(targetEntity="Club", inversedBy="members")
45 * @ORM\JoinColumn(nullable=false, name="price_guide_club_id")
50 * @ORM\OneToMany(targetEntity="MemberStock", mappedBy="member", cascade={"all"}, fetch="EXTRA_LAZY")
52 protected $memberItems;
55 /* ------------------------------------ Customer ------------------------------------ */
57 * @ORM\ManyToOne(targetEntity="\Company\Entity\Company")
58 * @ORM\JoinColumn(nullable=true, name="company_id")
63 * @ORM\ManyToOne(targetEntity="\User\Entity\Profile")
64 * @ORM\JoinColumn(nullable=true, name="profile_id")
69 * @ORM\ManyToOne(targetEntity="AllowedMember")
70 * @ORM\JoinColumn(nullable=true, name="price_guide_allowed_member_id")
72 protected $allowedMember;
75 /* ------------------------------------ Filtering ------------------------------------ */
77 * @ORM\OneToMany(targetEntity="MemberMake", mappedBy="member", cascade={"all"}, fetch="EXTRA_LAZY")
82 * @ORM\Column(type="boolean", name="all_makes");
84 protected $allMakes = true;
87 * @ORM\ManyToOne(targetEntity="\Stock\Entity\Year")
88 * @ORM\JoinColumn(nullable=true, name="intro_vehicle_year_id")
93 * @ORM\ManyToOne(targetEntity="\Stock\Entity\Year")
94 * @ORM\JoinColumn(nullable=true, name="disc_vehicle_year_id")
99 * @ORM\Column(nullable=true, type="integer", name="from_km");
104 * @ORM\Column(nullable=true, type="integer", name="to_km");
109 * @ORM\Column(type="boolean", name="email_notification");
111 protected $emailNotification = true;
114 * @ORM\Column(type="boolean", name="sms_notification");
116 protected $smsNotification = false;
119 /* ------------------------------------ Tracking ------------------------------------ */
122 * @ORM\Column(type="string", length=25, nullable=true, name="status");
124 protected $status = 'Invited';
127 * @ORM\Column(type="datetime");
132 * @ORM\Column(type="datetime", nullable=true);
137 * @ORM\Column(type="boolean");
139 protected $archived = false;
144 * Initialize collections.
146 public function __construct()
148 $this->makes = new \Doctrine\Common\Collections\ArrayCollection();
152 * Add a new Make to this Stock Item.
153 * @param \Stock\Entity\Make $make
154 * @return \PriceGuide\Entity\Member
156 public function addMake(\Stock\Entity\Make $make)
158 $this->makes[] = $make;
164 * Magic getter to expose protected properties.
166 * @param string $property
169 public function __get($property)
171 return $this->$property;
175 * Magic setter to save protected properties.
177 * @param string $property
178 * @param mixed $value
180 public function __set($property, $value)
182 $this->$property = $value;
188 public function setCreateTime()
190 $this->created = new \DateTime("now");
196 public function setUpdateTime()
198 $this->updated = new \DateTime("now");
202 * Convert the object to an array.
203 * @param array $expand
204 * @param array $intersect
205 * @param boolean $showIdentifiers
206 * @param integer $expandAll
209 public function toArray(
210 array $expand = array(), array $intersect = array(),
211 $showIdentifiers = false, $expandAll = 0
214 $intersect = array_flip($intersect);
215 $dateTimeFormat = \Utility\Registry::getConfigParam('DateTimeFormat');
216 $includeAll = empty($intersect);
218 ($includeAll || isset($intersect['id']))
219 && $data['id'] = $this->id;
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)
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)
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)
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)
250 ($includeAll || isset($intersect['makes']))
251 && $data['makes'] = (in_array('makes', $expand) || $expandAll || $showIdentifiers)
252 && !is_null($this->makes)
253 ? $this->makesToArray($showIdentifiers)
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)
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)
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;
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)
286 ($includeAll || isset($intersect['updated']))
287 && $data['updated'] = !is_null($this->updated)
288 ? $this->updated->format($dateTimeFormat)
294 * Internal utility to change makes collection into array.
295 * @param boolean $showIdentifiers
298 protected function makesToArray($showIdentifiers = false)
301 $iterator = $this->makes->getIterator();
302 if (2 == $showIdentifiers)
304 foreach ($iterator as $make)
306 $data[] = $make->make->id;
311 foreach ($iterator as $make)
313 $data[] = $showIdentifiers
314 ? array('id' => $make->make->id)
322 * Populate from an array.
325 public function fromArray($data = array())
328 && $this->id = $data['id'];
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;
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'])
359 $em = \Utility\Registry::getEntityManager();
360 $members = $em->getRepository('\PriceGuide\Entity\Member')
361 ->findBy(array('company' => $this->company->id));
362 foreach ($members as $member)
364 $iterator = $member->makes->getIterator();
365 foreach ($iterator as $mk)
370 if (isset($data['allMakes']) && $data['allMakes'])
372 $member->allMakes = true;
376 $member->allMakes = false;
377 foreach ($data['makes'] as $make)
379 $memberMake = new MemberMake();
380 $memberMake->make = $em->getReference('\Stock\Entity\Make', $make['id']);
381 $memberMake->member = $member;
382 $member->makes->add($memberMake);
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;
401 ? $member->toKm = $data['toKm']
402 : $member->toKm = null;
406 elseif (isset($data['makes']))
408 $em = \Utility\Registry::getEntityManager();
410 foreach ($data['makes'] as $make)
412 $newMakes[$make['id']] = $make['id'];
414 $currentMakes = array();
415 $iterator = $this->makes->getIterator();
416 foreach ($iterator as $make)
418 $currentMakes[$make->make->id] = $make->make->id;
419 !isset($newMakes[$make->make->id])
420 && $em->remove($make);
422 foreach ($data['makes'] as $make)
424 if (!isset($currentMakes[$make['id']]))
426 $memberMake = new MemberMake();
427 $memberMake->make = $em->getReference('\Stock\Entity\Make', $make['id']);
428 $memberMake->member = $this;
429 $this->makes->add($memberMake);