1 | <?php |
---|
2 | /** |
---|
3 | * @file |
---|
4 | * Contains the base row style plugin. |
---|
5 | */ |
---|
6 | |
---|
7 | /** |
---|
8 | * The basic 'fields' row plugin |
---|
9 | * |
---|
10 | * This displays fields one after another, giving options for inline |
---|
11 | * or not. |
---|
12 | * |
---|
13 | * @ingroup views_row_plugins |
---|
14 | */ |
---|
15 | class panels_views_plugin_row_fields extends views_plugin_row_fields { |
---|
16 | function option_definition() { |
---|
17 | $options = parent::option_definition(); |
---|
18 | |
---|
19 | $options['layout'] = array('default' => 'twocol'); |
---|
20 | $options['regions'] = array('default' => array()); |
---|
21 | |
---|
22 | return $options; |
---|
23 | } |
---|
24 | |
---|
25 | /** |
---|
26 | * Provide a form for setting options. |
---|
27 | */ |
---|
28 | function options_form(&$form, &$form_state) { |
---|
29 | parent::options_form($form, $form_state); |
---|
30 | |
---|
31 | ctools_include('plugins', 'panels'); |
---|
32 | $layouts = panels_get_layouts(); |
---|
33 | $options = array(); |
---|
34 | foreach ($layouts as $name => $layout) { |
---|
35 | if (empty($layout['builder'])) { |
---|
36 | $options[$name] = $layout['title']; |
---|
37 | } |
---|
38 | if ($name == $this->options['layout']) { |
---|
39 | $current_layout = $layout; |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | $form['layout'] = array( |
---|
44 | '#prefix' => '<div class="container-inline">', |
---|
45 | '#type' => 'select', |
---|
46 | '#options' => $options, |
---|
47 | '#title' => t('Panel layout'), |
---|
48 | '#default_value' => $this->options['layout'], |
---|
49 | ); |
---|
50 | |
---|
51 | $form['change'] = array( |
---|
52 | '#type' => 'submit', |
---|
53 | '#value' => t('Change'), |
---|
54 | '#submit' => array('panels_change_layout_button'), |
---|
55 | '#suffix' => '</div>', |
---|
56 | ); |
---|
57 | |
---|
58 | if (!empty($current_layout)) { |
---|
59 | $fields = $this->display->handler->get_field_labels(); |
---|
60 | $regions = panels_get_regions($current_layout, panels_new_display()); |
---|
61 | foreach ($fields as $id => $title) { |
---|
62 | $form['regions'][$id] = array( |
---|
63 | '#type' => 'select', |
---|
64 | '#title' => $title, |
---|
65 | '#options' => $regions, |
---|
66 | ); |
---|
67 | if (!empty($this->options['regions'][$id]) && !empty($regions[$this->options['regions'][$id]])) { |
---|
68 | $form['regions'][$id]['#default_value'] = $this->options['regions'][$id]; |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * Perform any necessary changes to the form values prior to storage. |
---|
76 | * There is no need for this function to actually store the data. |
---|
77 | */ |
---|
78 | function options_submit(&$form, &$form_state) { |
---|
79 | $form_state['values']['row_options']['inline'] = array_filter($form_state['values']['row_options']['inline']); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Render a row object. This usually passes through to a theme template |
---|
84 | * of some form, but not always. |
---|
85 | */ |
---|
86 | function render($row) { |
---|
87 | ctools_include('plugins', 'panels'); |
---|
88 | $layout = panels_get_layout($this->options['layout']); |
---|
89 | if (!$layout) { |
---|
90 | // Fall back to normal behavior if the layout is somehow invalid. This |
---|
91 | // can happen if the layout was removed, for example. |
---|
92 | return theme($this->theme_functions(), $this->view, $this->options, $row, $this->field_alias); |
---|
93 | } |
---|
94 | |
---|
95 | // Store a backup copy of the array because we're going to be screwing |
---|
96 | // with this a lot. |
---|
97 | $fields = $this->view->field; |
---|
98 | unset($this->view->field); |
---|
99 | |
---|
100 | $meta = 'standard'; |
---|
101 | // This row style gets run many times; only run this code once. |
---|
102 | if (empty($this->region_fields)) { |
---|
103 | $this->region_fields = array(); |
---|
104 | $regions = panels_get_regions($layout, panels_new_display()); |
---|
105 | |
---|
106 | // Ensure each region has an empty array. |
---|
107 | foreach ($regions as $region_id => $name) { |
---|
108 | if (empty($default_region)) { |
---|
109 | $default_region = $region_id; |
---|
110 | } |
---|
111 | |
---|
112 | $this->region_fields[$region_id] = array(); |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | // Go through all our fields and place them in regions according to the |
---|
117 | // settings. |
---|
118 | foreach ($fields as $id => $field) { |
---|
119 | $region_id = ''; // ensure we don't accidentlly use the last field's region. |
---|
120 | if (!empty($this->options['regions'][$id]) && !empty($regions[$this->options['regions'][$id]])) { |
---|
121 | $region_id = $this->options['regions'][$id]; |
---|
122 | } |
---|
123 | else { |
---|
124 | // Fallback to putting unknown fields into the first region. |
---|
125 | $region_id = $default_region; |
---|
126 | } |
---|
127 | |
---|
128 | // Ensure this works in PHP4 by keeping the reference. |
---|
129 | $this->region_fields[$region_id][$id] = &$fields[$id]; |
---|
130 | } |
---|
131 | |
---|
132 | // We don't need to set 'inline' for every record, so we do it inside |
---|
133 | // this loop. We do need to set inline if we are in the live preview |
---|
134 | // so that the CSS will get transmitted via javascript: |
---|
135 | $meta = !empty($this->view->live_preview) ? 'inline' : 'standard'; |
---|
136 | } |
---|
137 | |
---|
138 | // Now that we have distributed our fields, go through the regions and |
---|
139 | // render them into the content array. |
---|
140 | foreach ($this->region_fields as $region_id => $fields) { |
---|
141 | $this->view->field = $fields; |
---|
142 | $content[$region_id] = theme($this->theme_functions(), $this->view, $this->options, $row, $this->field_alias); |
---|
143 | } |
---|
144 | |
---|
145 | // Restore our $fields array. |
---|
146 | $this->view->field = $fields; |
---|
147 | |
---|
148 | // Now that we have a rendered content array, render it. |
---|
149 | return panels_print_layout($layout, $content, $meta); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * Override handler for views_ui_edit_display_form |
---|
155 | */ |
---|
156 | function panels_change_layout_button($form, &$form_state) { |
---|
157 | $display = &$form_state['view']->display[$form_state['display_id']]; |
---|
158 | $display->handler->options_submit($form, $form_state); |
---|
159 | |
---|
160 | views_ui_cache_set($form_state['view']); |
---|
161 | $form_state['rerender'] = TRUE; |
---|
162 | $form_state['rebuild'] = TRUE; |
---|
163 | } |
---|