/[drupal]/contributions/modules/weight/weight.views.inc
ViewVC logotype

Contents of /contributions/modules/weight/weight.views.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.7 - (show annotations) (download) (as text)
Fri Jan 2 01:46:55 2009 UTC (10 months, 3 weeks ago) by nancyw
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +3 -26 lines
File MIME type: text/x-php
#314248 by sheise - Improved Views2 support.
1 <?php
2 // $Id: weight.views.inc,v 1.6 2009/01/02 01:21:55 nancyw Exp $
3 /**
4 * @file
5 * Views2 support for Weight module.
6 */
7 /**
8 * Implementation of hook_views_data()
9 */
10 function weight_views_data() {
11 $data['node_weight'] = array(
12 'table' => array(
13 'group' => t('Weight'),
14 'join' => array(
15 'node' => array(
16 'table' => 'node',
17 'left_field' => 'nid',
18 'field' => 'nid',
19 ),
20 ),
21 ),
22 'weight' => array(
23 'real field' => 'sticky',
24 'title' => t('Weight'), // The item it appears as on the UI,
25 'help' => t('The node weight.'), // The help that appears on the UI,
26 'field' => array(
27 'handler' => 'weight_handler_field_sticky',
28 'click sortable' => TRUE,
29 ),
30 'filter' => array(
31 'handler' => 'views_handler_filter_numeric',
32 'label' => t('Weight'),
33 ),
34 'sort' => array(
35 'handler' => 'weight_handler_sort',
36 ),
37 ),
38 );
39
40 return $data;
41 }
42
43
44 /**
45 * Implementation of hook_views_plugins();
46 */
47 function weight_views_plugins() {
48 return array(
49 'style' => array(
50 'weight' => array(
51 'title' => t('Weight Changer'),
52 'help' => t('Displays rows in a table which allows weight change. Be sure to add the Weight field and Sort by Weight.'),
53 'handler' => 'views_plugin_style_table',
54 'parent' => 'table',
55 'path' => drupal_get_path('module', 'weight'),
56 'theme' => 'weight_view_weight',
57 'uses row plugin' => FALSE,
58 'uses fields' => TRUE,
59 'uses options' => TRUE,
60 'type' => 'normal',
61 ),
62 ),
63 );
64 }
65
66 /**
67 * Implementation of hook_views_handlers().
68 */
69 function weight_views_handlers() {
70 return array(
71 'info' => array(
72 'path' => drupal_get_path('module', 'weight') /*.'/handlers'*/,
73 ),
74 'handlers' => array(
75 'weight_handler_field_sticky' => array(
76 'parent' => 'views_handler_field_numeric',
77 ),
78 'weight_handler_sort' => array(
79 'parent' => 'views_handler_sort',
80 ),
81 ),
82 );
83 }
84
85 /**
86 * Display a view as a weight changing table.
87 */
88 function theme_weight_view_weight($view, $rows, $type) {
89 $result = $view->result;
90 $rows = array();
91
92 $options = $view->style_plugin->options;
93 $handler = $view->style_plugin;
94
95 $fields = &$view->field;
96 $columns = $handler->sanitize_columns($options['columns'], $fields);
97
98 $active = !empty($handler->active) ? $handler->active : '';
99 $order = !empty($handler->order) ? $handler->order : 'asc';
100
101 $query = tablesort_get_querystring();
102 if ($query) {
103 $query = '&'. $query;
104 }
105 foreach ($columns as $field => $column) {
106 // render the header labels
107 if ($field == $column && empty($fields[$field]->options['exclude'])) {
108 $label = check_plain(!empty($fields[$field]) ? $fields[$field]->label() : '');
109 if (empty($options['info'][$field]['sortable'])) {
110 $header[$field] = $label;
111 }
112 else {
113 // @todo -- make this a setting
114 $initial = 'asc';
115
116 if ($active == $field && $order == 'asc') {
117 $initial = 'desc';
118 }
119
120 $image = theme('tablesort_indicator', $initial);
121 $title = t('sort by @s', array('@s' => $label));
122 $link_options = array(
123 'html' => TRUE,
124 'attributes' => array('title' => $title),
125 'query' => 'order='. urlencode($field) .'&sort='. $initial . $query,
126 );
127 $header[$field] = l($label . $image, $_GET['q'], $link_options);
128 }
129 }
130
131 // Create a second variable so we can easily find what fields we have and what the
132 // CSS classes should be.
133 $weight_fields[$field] = views_css_safe($field);
134 if ($active == $field) {
135 $weight_fields[$field] .= ' active';
136 }
137
138 // Render each field into its appropriate column.
139 foreach ($result as $num => $row) {
140 if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {
141 $field_output = $fields[$field]->theme($row);
142
143 // Don't bother with separators and stuff if the field does not show up.
144 if (!isset($field_output) && isset($rows[$num][$column])) {
145 continue;
146 }
147 // Place the field into the column, along with an optional separator.
148 if (isset($rows[$num][$column])) {
149 if (!empty($options['info'][$column]['separator'])) {
150 $rows[$num][$column] .= filter_xss_admin($options['info'][$column]['separator']);
151 }
152 }
153 else {
154 $rows[$num][$column] = '';
155 }
156
157 $rows[$num][$column] .= $field_output;
158 }
159 }
160 }
161
162 $class = 'views-table-weight';
163 if (!empty($options['sticky'])) {
164 drupal_add_js('misc/tableheader.js');
165 $class .= " sticky-enabled";
166 }
167
168 // Pass along the nid.
169
170 foreach ($view->result as $count => $item) {
171 $rows[$count]['nid_hidden'] = $item->nid;
172 }
173 $id = $view->name .'_drag';
174 return drupal_get_form('weight_view_weight_form', $header, $rows, $weight_fields, $class, $id);
175 }
176
177 /**
178 * Display a view as a weight changing table.
179 */
180 function weight_view_weight_form(&$form_state, $header, $rows, $fields, $class, $id) {
181 // Make this form draggable
182 drupal_add_tabledrag($id, 'order', 'sibling', 'weight_dragger');
183
184 $form = array('#tree' => TRUE);
185 $form['#variables'] = array(
186 'header' => $header,
187 'class' => $class,
188 'fields' => $fields,
189 'id' => $id,
190 );
191 foreach ($rows as $count => $row) {
192 if (is_numeric($count)) {
193 foreach ($row as $field => $content) {
194 $nid = $row['nid_hidden'];
195 if (substr($field, 0, 6) == 'weight') {
196 $form['rows'][$count][$field] = array(
197 '#default_value' => $content,
198 '#type' => 'weight',
199 '#delta' => variable_get('weight_range', 20),
200 );
201 }
202 elseif ($field != 'nid_hidden') {
203 $form['rows'][$count][$field] = array(
204 '#value' => $content,
205 );
206 }
207 else {
208 $form['rows'][$count][$field] = array(
209 '#type' => 'hidden',
210 '#value' => $content,
211 );
212 }
213 }
214 }
215 }
216 $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
217 return $form;
218 }
219
220 /**
221 * Save the changed weights.
222 */
223 function weight_view_weight_form_submit($form, &$form_state) {
224 foreach ($form_state['values']['rows'] as $count => $value) {
225 $weight = $value['weight'];
226 $nid = $value['nid_hidden'];
227 $node = node_load($nid);
228 if ($node->sticky) {
229 $node->sticky = (-1 * $weight) + 100;
230 }
231 // Unweighted non-sticky nodes will have a value of -100.
232 else {
233 $node->sticky = (-1 * $weight) - 100;
234 }
235 db_query("UPDATE {node} SET sticky = %d WHERE nid = %d", $node->sticky, $nid);
236 }
237 drupal_set_message('Your weight changes have been saved.');
238 }

  ViewVC Help
Powered by ViewVC 1.1.2