1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Taxonomy\Terms;
13
14 class ManageBlock extends \Icybee\ManageBlock
15 {
16 public function __construct(Module $module, array $attributes=array())
17 {
18 parent::__construct
19 (
20 $module, $attributes += array
21 (
22 self::T_ORDER_BY => array('term', 'asc')
23 )
24 );
25 }
26
27 28 29 30 31 32 33
34 protected function get_available_columns()
35 {
36 return array_merge(parent::get_available_columns(), array
37 (
38 'term' => __CLASS__ . '\TermColumn',
39 'vid' => __CLASS__ . '\VidColumn',
40 'popularity' => __CLASS__ . '\PopularityColumn'
41 ));
42 }
43 }
44
45 namespace Icybee\Modules\Taxonomy\Terms\ManageBlock;
46
47 use ICanBoogie\ActiveRecord\Query;
48
49 use Icybee\ManageBlock\Column;
50 use Icybee\ManageBlock\FilterDecorator;
51 use Icybee\ManageBlock\EditDecorator;
52
53 class TermColumn extends Column
54 {
55 public function __construct(\Icybee\ManageBlock $manager, $id, array $options=array())
56 {
57 parent::__construct
58 (
59 $manager, $id, $options + array
60 (
61 'title' => 'Term'
62 )
63 );
64 }
65
66 public function render_cell($record)
67 {
68 return new EditDecorator($record->term, $record);
69 }
70 }
71
72 73 74
75 class VidColumn extends Column
76 {
77 public function __construct(\Icybee\Modules\Taxonomy\Terms\ManageBlock $manager, $id, array $options=array())
78 {
79 global $core;
80
81 parent::__construct
82 (
83 $manager, $id, $options + array
84 (
85 'title' => 'Vocabulary',
86 'orderable' => true
87 )
88 );
89 }
90
91 92 93 94 95 96
97 protected function get_options()
98 {
99 global $core;
100
101
102
103 $keys = $this->manager->module->model->select('DISTINCT vid')->all(\PDO::FETCH_COLUMN);
104
105 if (count($keys) < 2)
106 {
107 $this->orderable = false;
108
109 return;
110 }
111
112
113
114 return $core->models['taxonomy.vocabulary']
115 ->select('CONCAT("?vid=", vid), vocabulary')
116 ->where(array('vid' => $keys))
117 ->order('vocabulary')
118 ->pairs;
119 }
120
121 122 123
124 public function alter_query_with_filter(Query $query, $filter_value)
125 {
126 if ($filter_value)
127 {
128 $query->filter_by_vid($filter_value);
129 }
130
131 return $query;
132 }
133
134 135 136
137 public function alter_query_with_order(Query $query, $order_direction)
138 {
139 global $core;
140
141 $names = $core->models['taxonomy.vocabulary']->select('vid, vocabulary')->order("vocabulary " . ($order_direction < 0 ? 'DESC' : 'ASC'))->pairs;
142
143 return $query->order('vid', array_keys($names));
144 }
145
146 public function render_cell($record)
147 {
148 return new FilterDecorator($record, $this->id, $this->manager->is_filtering($this->id), $record->vocabulary);
149 }
150 }
151
152 153 154 155 156
157 class PopularityColumn extends Column
158 {
159 160 161 162 163
164 private $values;
165
166 public function __construct(\Icybee\ManageBlock $manager, $id, array $options=array())
167 {
168 parent::__construct
169 (
170 $manager, $id, array
171 (
172 'title' => 'Popularity',
173 'class' => 'pull-right',
174 'orderable' => true
175 )
176 );
177 }
178
179 180 181 182 183
184 public function alter_records(array $records)
185 {
186 $keys = array();
187
188 foreach ($records as $record)
189 {
190 $keys[] = $record->vtid;
191 }
192
193 if ($keys)
194 {
195 $this->values = $this->manager->module->model('nodes')->filter_by_vtid($keys)->count('vtid');
196 }
197
198 return $records;
199 }
200
201 202 203
204 public function alter_query_with_order(Query $query, $order_direction)
205 {
206 return $query->order("(SELECT COUNT(vtid) FROM {self}__nodes WHERE vtid = term.vtid) " . ($order_direction < 0 ? 'DESC' : 'ASC'));
207 }
208
209 public function render_cell($record)
210 {
211 return isset($this->values[$record->vtid]) ? $this->values[$record->vtid] : 0;
212 }
213 }