initial commit
[namibia] / module / Auction / src / Auction / Entity / OpenDay.php
1 <?php
2 namespace Auction\Entity;
3
4 use Doctrine\ORM\Mapping as ORM;
5
6
7
8 /**
9  * @ORM\Entity
10  * @ORM\HasLifecycleCallbacks
11  * @ORM\Table(name="auction_open_day")
12  */
13 class OpenDay
14 {
15
16         /**
17          * Can archive records.
18          */
19         const ARCHIVE = false;
20         /**
21          * Pull Synchronization Strategy for this table.
22          */
23         const PULL_SYNCH_STRATEGY = false;
24         /**
25          * Push Synchronization Strategy for this table.
26          */
27         const PUSH_SYNCH_STRATEGY = false;
28
29         /**
30          * @ORM\Id
31          * @ORM\Column(type="integer");
32          * @ORM\GeneratedValue(strategy="AUTO")
33          */
34         protected $id;
35
36         /**
37          * @ORM\Column(type="date", name="open_date");
38          */
39         protected $openDate;
40
41         /**
42          * @ORM\OneToMany(targetEntity="OpenDayGroup", mappedBy="openDay", cascade={"all"}, fetch="EXTRA_LAZY")
43          **/
44         protected $groups;
45
46         /**
47          * @ORM\Column(type="datetime");
48          */
49         protected $created;
50
51         /**
52          * @ORM\Column(type="datetime", nullable=true);
53          */
54         protected $updated;
55
56
57
58         /**
59          * Initialize collections.
60          */
61         public function __construct()
62         {
63                 $this->groups = new \Doctrine\Common\Collections\ArrayCollection();
64         }
65
66         /**
67          * Magic getter to expose protected properties.
68          * @param string $property
69          * @return mixed
70          */
71         public function __get($property)
72         {
73                 return $this->$property;
74         }
75
76         /**
77          * Magic setter to save protected properties.
78          * @param string $property
79          * @param mixed $value
80          */
81         public function __set($property, $value)
82         {
83                 $this->$property = $value;
84         }
85
86         /**
87          * @ORM\PrePersist
88          */
89         public function setCreateTime()
90         {
91                 $this->created = new \DateTime("now");
92         }
93
94         /**
95          * @ORM\PreUpdate
96          */
97         public function setUpdateTime()
98         {
99                 $this->updated = new \DateTime("now");
100         }
101
102         /**
103          * Convert the object to an array.
104          * @param array $expand
105          * @param array $intersect
106          * @return array
107          */
108         public function toArray(
109                         array $expand = array('attachment'), array $intersect = array(),
110                         $showIdentifiers = false, $expandAll = 0
111                         )
112         {
113                 $dateTimeFormat = \Utility\Registry::getConfigParam('DateTimeFormat');
114                 $dateFormat     = \Utility\Registry::getConfigParam('DateFormat');
115                 $includeAll     = empty($intersect);
116                 $data = array();
117                 ($includeAll || isset($intersect['id']))
118                         && $data['id'] = $this->id;
119                 ($includeAll || isset($intersect['openDate']))
120                         && $data['openDate'] = !is_null($this->openDate)
121                                 ? $this->openDate->format($dateFormat)
122                                 : null;
123                 ($includeAll || isset($intersect['groups']))
124                         && $data['groups'] = (in_array('groups', $expand) || $expandAll || $showIdentifiers)
125                                                                                 && !is_null($this->groups)
126                                 ? $this->groupsToArray($showIdentifiers)
127                                 : null;
128                 ($includeAll || isset($intersect['created']))
129                         && $data['created'] = !is_null($this->created)
130                                 ? $this->created->format($dateTimeFormat)
131                                 : null;
132                 ($includeAll || isset($intersect['updated']))
133                         && $data['updated'] = !is_null($this->updated)
134                                 ? $this->updated->format($dateTimeFormat)
135                                 : null;
136                 return $data;
137         }
138
139         /**
140          * Internal utility to change auction groups collection into array.
141          * @param boolean $showIdentifiers
142          * @return array
143          */
144         protected function groupsToArray($showIdentifiers = false)
145         {
146                 $data = array();
147                 $iterator = $this->groups->getIterator();
148                 foreach ($iterator as $group)
149                 {
150                         $data[] = array('id' => $group->companyGroup->id);
151                 }
152                 return $data;
153         }
154
155         /**
156          * Populate from an array.
157          * @param array $data
158          */
159         public function fromArray($data = array())
160         {
161                 isset($data['id'])
162                         && $this->id = $data['id'];
163                 isset($data['openDate'])
164                         && $this->openDate = is_object($data['openDate'])
165                         ? $data['openDate']
166                         : new \DateTime($data['openDate']);
167                 if (isset($data['groups']))
168                 {
169                         $em = \Utility\Registry::getEntityManager();
170                         $newGrps = array();
171                         foreach ($data['groups'] as $grp)
172                         {
173                                 $newGrps[$grp['id']] = $grp['id'];
174                         }
175                         $currentGrps = array();
176                         $iterator = $this->groups->getIterator();
177                         foreach ($iterator as $grp)
178                         {
179                                 $currentGrps[$grp->companyGroup->id] = $grp->companyGroup->id;
180                                 !isset($newGrps[$grp->companyGroup->id])
181                                         && $em->remove($grp);
182                         }
183                         foreach ($data['groups'] as $grp)
184                         {
185                                 if (!isset($currentGrps[$grp['id']]))
186                                 {
187                                         $openDayGrp = new \Auction\Entity\OpenDayGroup();
188                                         $openDayGrp->companyGroup = $em->getReference('\Company\Entity\Group', $grp['id']);
189                                         $openDayGrp->openDay = $this;
190                                         $this->groups->add($openDayGrp);
191                                 }
192                         }
193                 }
194         }
195
196 }