1: <?php
2:
3: namespace Mapbender\CoreBundle\Entity;
4:
5: use Doctrine\ORM\Mapping as ORM;
6: use Doctrine\Common\Collections\ArrayCollection;
7: use Symfony\Component\Validator\Constraints as Assert;
8: use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
9:
10: /**
11: * Applicaton entity
12: *
13: * @author Christian Wygoda <christian.wygoda@wheregroup.com>
14: *
15: * @ORM\Entity
16: * @UniqueEntity("title")
17: * @UniqueEntity("slug")
18: * @ORM\Table(name="mb_core_application")
19: * @ORM\HasLifecycleCallbacks
20: */
21: class Application {
22:
23: const SOURCE_YAML = 1;
24: const SOURCE_DB = 2;
25:
26: private $preparedElements;
27: private $screenshotPath;
28:
29: /**
30: * @var integer $source
31: */
32: protected $source;
33:
34: /**
35: * @ORM\Id
36: * @ORM\Column(type="integer")
37: * @ORM\GeneratedValue
38: */
39: protected $id;
40:
41: /**
42: * @ORM\Column(type="string", length=128, unique=true)
43: * @Assert\NotBlank()
44: */
45: protected $title;
46:
47: /**
48: * @ORM\Column(type="string", length=255, unique=true)
49: * @Assert\Regex(
50: * pattern="/^[0-9\-\_a-zA-Z]+$/",
51: * message="The slug value is wrong."
52: * )
53: * @Assert\NotBlank()
54: */
55: protected $slug;
56:
57: /**
58: * @ORM\Column(type="text", nullable=true)
59: */
60: protected $description;
61:
62: /**
63: * @ORM\Column(length=1024)
64: */
65: protected $template;
66:
67: /**
68: * @ORM\OneToMany(targetEntity="Element", mappedBy="application", cascade={"persist", "remove"})
69: * @ORM\OrderBy({"weight" = "asc"})
70: */
71: protected $elements;
72:
73: /**
74: * @ORM\OneToMany(targetEntity="Layerset", mappedBy="application", cascade={"persist", "remove"})
75: */
76: protected $layersets;
77:
78: /**
79: * @ORM\ManyToOne(targetEntity="FOM\UserBundle\Entity\User", cascade={"persist"})
80: */
81: protected $owner;
82:
83: /**
84: * @ORM\Column(type="boolean")
85: */
86: protected $published;
87:
88: /**
89: * @ORM\Column(type="string", length=256, nullable=true)
90: */
91: protected $screenshot;
92:
93: /**
94: * @ORM\Column(type="array", nullable=true)
95: */
96: protected $extra_assets;
97:
98: /**
99: * @Assert\File(maxSize="102400")
100: */
101: protected $screenshotFile;
102:
103: /**
104: * @ORM\Column(type="datetime")
105: */
106: protected $updated;
107:
108: public function __construct() {
109: $this->elements = new ArrayCollection();
110: $this->layersets = new ArrayCollection();
111: }
112:
113: /**
114: * Get entity source type
115: *
116: * @param int $source
117: */
118: public function setSource($source) {
119: $this->source = $source;
120: return $this;
121: }
122:
123: /**
124: * Get type
125: *
126: * @return type
127: */
128: public function getSource() {
129: return $this->source;
130: }
131:
132: /**
133: * Get id
134: *
135: * @return integer
136: */
137: public function getId() {
138: return $this->id;
139: }
140:
141: /**
142: * Set title
143: *
144: * @param string $title
145: */
146: public function setTitle($title) {
147: $this->title = $title;
148: return $this;
149: }
150:
151: /**
152: * Get title
153: *
154: * @return string
155: */
156: public function getTitle() {
157: return $this->title;
158: }
159:
160: /**
161: * Set slug
162: *
163: * @param string $slug
164: */
165: public function setSlug($slug) {
166: $this->slug = $slug;
167: return $this;
168: }
169:
170: /**
171: * Get slug
172: *
173: * @return string
174: */
175: public function getSlug() {
176: return $this->slug;
177: }
178:
179: /**
180: * Set description
181: *
182: * @param text $description
183: */
184:
185: public function setDescription($description) {
186: $this->description = $description;
187: return $this;
188: }
189:
190: /**
191: * Get description
192: *
193: * @return text
194: */
195: public function getDescription() {
196: return $this->description;
197: }
198:
199: /**
200: * Set template
201: *
202: * @param string $template
203: */
204: public function setTemplate($template)
205: {
206: $this->template = $template;
207: return $this;
208: }
209:
210: /**
211: * Get template
212: *
213: * @return string
214: */
215: public function getTemplate()
216: {
217: return $this->template;
218: }
219:
220: /**
221: * Add elements
222: *
223: * @param Mapbender\CoreBundle\Entity\Element $elements
224: */
225: public function addElements(\Mapbender\CoreBundle\Entity\Element $elements) {
226: $this->elements[] = $elements;
227: }
228:
229: /**
230: * Get elements
231: *
232: * @return Doctrine\Common\Collections\Collection
233: */
234: public function getElements() {
235: return $this->elements;
236: }
237:
238: /**
239: * Add layersets
240: *
241: * @param Layerset $layerset
242: */
243: public function addLayerset(Layerset $layerset) {
244: $this->layersets[] = $layerset;
245: }
246:
247: /**
248: * Get layersets
249: *
250: * @return Doctrine\Common\Collections\Collection
251: */
252: public function getLayersets() {
253: return $this->layersets;
254: }
255:
256: /**
257: * Set screenshot
258: *
259: * @param string $screenshot
260: */
261: public function setScreenshot($screenshot) {
262: $this->screenshot = $screenshot;
263: }
264:
265: /**
266: * Get screenshot
267: *
268: * @return string
269: */
270: public function getScreenshot() {
271: return $this->screenshot;
272: }
273:
274: /**
275: * Set owner
276: *
277: * @param User $owner
278: */
279: public function setOwner($owner) {
280: $this->owner = $owner;
281: return $this;
282: }
283:
284: /**
285: * Get owner
286: *
287: * @return User
288: */
289: public function getOwner() {
290: return $this->owner;
291: }
292:
293: /**
294: * Set extra assets
295: *
296: * @param array $extra_assets
297: */
298: public function setExtraAssets(array $extra_assets)
299: {
300: $this->extra_assets = $extra_assets;
301: return $this;
302: }
303:
304: /**
305: * Get extra assets
306: *
307: * @return array
308: */
309: public function getExtraAssets()
310: {
311: return $this->extra_assets;
312: }
313:
314: /**
315: * Set published
316: *
317: * @param boolean $published
318: */
319: public function setPublished($published)
320: {
321: $this->published = $published;
322: return $this;
323: }
324:
325: /**
326: * Is published?
327: *
328: * @return boolean
329: */
330: public function isPublished()
331: {
332: return $this->published;
333: }
334:
335: /**
336: * Set updated
337: *
338: * @param DateTime $updated
339: */
340: public function setUpdated(\DateTime $updated)
341: {
342: $this->updated = $updated;
343: return $this;
344: }
345:
346: /**
347: * Get updated
348: *
349: * @return DateTime
350: */
351: public function getUpdated()
352: {
353: return $this->updated;
354: }
355:
356: public function getElementsByRegion($region = null) {
357: if($this->preparedElements === null) {
358: $this->preparedElements = array();
359:
360: foreach($this->getElements() as $element) {
361: $elementRegion = $element->getRegion();
362: if(!array_key_exists($elementRegion, $this->preparedElements)) {
363: $this->preparedElements[$elementRegion] = array();
364: }
365: $this->preparedElements[$elementRegion][] = $element;
366: }
367:
368: foreach($this->preparedElements as $elementRegion => $elements) {
369: usort($elements, function($a, $b) {
370: return $a->getWeight() - $b->getWeight();
371: });
372: }
373: }
374:
375: if($this->preparedElements !== null) {
376: if(array_key_exists($region, $this->preparedElements)) {
377: return $this->preparedElements[$region];
378: } else {
379: return null;
380: }
381: } else {
382: return $this->preparedElements;
383: }
384: }
385:
386: public function __toString(){
387: return (string) $this->getId();
388: }
389: }
390:
391: