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