1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Files;
13
14 15 16
17 class ManageBlock extends \Icybee\Modules\Nodes\ManageBlock
18 {
19 static protected function add_assets(\Brickrouge\Document $document)
20 {
21 parent::add_assets($document);
22
23 $document->css->add(DIR . '/public/manage.css');
24 }
25
26 public function __construct(Module $module, array $attributes)
27 {
28 parent::__construct
29 (
30 $module, $attributes + array
31 (
32 self::T_COLUMNS_ORDER => array('title', 'size', 'download', 'is_online', 'uid', 'mime', 'updated_at')
33 )
34 );
35 }
36
37 38 39 40 41 42 43 44 45
46 protected function get_available_columns()
47 {
48 return array_merge(parent::get_available_columns(), array
49 (
50 File::MIME => __CLASS__ . '\MimeColumn',
51 File::SIZE => __CLASS__ . '\SizeColumn',
52 'download' => __CLASS__ . '\DownloadColumn'
53 ));
54 }
55 }
56
57 namespace Icybee\Modules\Files\ManageBlock;
58
59 use ICanBoogie\ActiveRecord\Query;
60
61 use Brickrouge\A;
62
63 use Icybee\ManageBlock\Column;
64 use Icybee\ManageBlock\FilterDecorator;
65
66 67 68
69 class MimeColumn extends Column
70 {
71 public function render_cell($record)
72 {
73 return new FilterDecorator($record, $this->id, $this->manager->is_filtering($this->id));
74 }
75 }
76
77 78 79
80 class SizeColumn extends \Icybee\ManageBlock\SizeColumn
81 {
82 public function __construct(\Icybee\ManageBlock $manager, $id, array $options=array())
83 {
84 parent::__construct
85 (
86 $manager, $id, $options + array
87 (
88 'class' => 'cell-fitted pull-right',
89 'filters' => array
90 (
91 'options' => array
92 (
93 '=l' => 'Large',
94 '=m' => 'Medium',
95 '=s' => 'Small'
96 )
97 )
98 )
99 );
100 }
101
102 103 104
105 public function alter_filters(array $filters, array $modifiers)
106 {
107 $filters = parent::alter_filters($filters, $modifiers);
108
109 if (isset($modifiers['size']))
110 {
111 $value = $modifiers['size'];
112
113 if (in_array($value, array('l', 'm', 's')))
114 {
115 $filters['size'] = $value;
116 }
117 else
118 {
119 unset($filters['size']);
120 }
121 }
122
123 return $filters;
124 }
125
126 127 128
129 public function alter_query_with_filter(Query $query, $filter_value)
130 {
131 if ($filter_value)
132 {
133 list($avg, $max, $min) = $query->model->similar_site->select('AVG(size), MAX(size), MIN(size)')->one(\PDO::FETCH_NUM);
134
135 $bounds = array
136 (
137 $min,
138 round($avg - ($avg - $min) / 3),
139 round($avg),
140 round($avg + ($max - $avg) / 3),
141 $max
142 );
143
144 switch ($filter_value)
145 {
146 case 'l': $query->and('size >= ?', $bounds[3]); break;
147 case 'm': $query->and('size >= ? AND size < ?', $bounds[2], $bounds[3]); break;
148 case 's': $query->and('size < ?', $bounds[2]); break;
149 }
150 }
151
152 return $query;
153 }
154 }
155
156 157 158
159 class DownloadColumn extends \Icybee\ManageBlock\Column
160 {
161 public function __construct(\Icybee\ManageBlock $manager, $id, array $options=array())
162 {
163 parent::__construct
164 (
165 $manager, $id, array
166 (
167 'orderable' => false,
168 'title' => null
169 )
170 );
171 }
172
173 public function render_cell($record)
174 {
175 return new A
176 (
177 '', $record->url('download'), array
178 (
179 'class' => 'download',
180 'title' => $this->manager->t('Download file')
181 )
182 );
183 }
184 }