1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Images;
13
14 use ICanBoogie\ActiveRecord;
15 use ICanBoogie\ActiveRecord\RecordNotFound;
16
17 class Model extends \Icybee\Modules\Files\Model
18 {
19 static protected $accept = [ '.gif', '.png', '.jpg', '.jpeg' ];
20
21 public function save(array $properties, $key=null, array $options=[])
22 {
23 if (isset($properties[Image::HTTP_FILE]))
24 {
25 $file = $properties[Image::HTTP_FILE];
26
27 list($w, $h) = getimagesize($file->pathname);
28
29 $properties[Image::WIDTH] = $w;
30 $properties[Image::HEIGHT] = $h;
31 }
32
33 return parent::save($properties, $key, $options + [
34
35 self::ACCEPT => self::$accept
36
37 ]);
38 }
39
40 41 42 43 44 45 46 47 48
49 public function including_assigned_image(array $records)
50 {
51 $keys = array();
52
53 foreach ($records as $record)
54 {
55 $keys[] = $record->nid;
56 }
57
58 if (!$keys)
59 {
60 return $records;
61 }
62
63 $pairs = ActiveRecord\get_model('registry/node')
64 ->select('targetid, value')
65 ->filter_by_name_and_targetid('image_id', $keys)
66 ->pairs;
67
68 if (!$pairs)
69 {
70 return $records;
71 }
72
73 try
74 {
75 $images = $this->find($pairs);
76 }
77 catch (RecordNotFound $e)
78 {
79 $images = $e->records;
80 }
81
82 foreach ($records as $record)
83 {
84 $nid = $record->nid;
85 $image_key = $pairs[$nid];
86
87 if (empty($images[$image_key]))
88 {
89 continue;
90 }
91
92 $record->image = $images[$image_key];
93 }
94
95 return $records;
96 }
97 }