Projet

Général

Profil

Paste
Télécharger (6,13 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_pdf / modules / views_view_field / views_view_field_handler_include_view.inc @ 11b63505

1
<?php
2
/**
3
 * @file
4
 * Plugin for the views include field
5
 */
6

    
7
/**
8
 * This class contains the functionality to add a view as a new field in
9
 * another view.
10
 *
11
 */
12
class views_view_field_handler_include_view extends views_handler_field {
13

    
14
  /**
15
   * Query the view. Deactivated because we do not want to query anything.
16
   */
17
  function query() {
18
    // Override parent::query() and don't alter query.
19
    $this->field_alias = 'view_include_' . $this->position;
20
  }
21

    
22
  /**
23
   * Definitions of the views field options.
24
   */
25
  function option_definition() {
26
    $options = parent::option_definition();
27

    
28
    $options['view'] = array('default' => '');
29
    $options['number_of_args'] = array('default' => 1);
30
    $options['args'] = array('default' => array());
31

    
32
    return $options;
33
  }
34

    
35
  /**
36
   * Settings form
37
   */
38
  function options_form(&$form, &$form_state) {
39
    parent::options_form($form, $form_state);
40

    
41
    $views = views_get_all_views();
42

    
43
    foreach ($views as $key => $view) {
44
      if ($this->view->name != $view->name) {
45
        $view_options[$key] = $view->name;
46
      }
47
    }
48

    
49
    $form['view'] = array(
50
      '#type' => 'select',
51
      '#title' => t('View to include'),
52
      '#options' => $view_options,
53
      '#default_value' => $this->options['view'],
54
      '#description' => t('Select the view to include. The display will be automatically determined.'),
55
    );
56

    
57
    $form['number_of_args'] = array(
58
      '#type' => 'textfield',
59
      '#title' => t('Enter the number of arguments'),
60
      '#default_value' => $this->options['number_of_args'],
61
      '#description' => t('Enter the number of arguments you want to pass to the view.'),
62
    );
63

    
64
    for ($i = 0; $this->options['number_of_args'] > $i; $i++) {
65
      $form['args'][$i] = array(
66
        '#type' => 'textfield',
67
        '#title' => t('Argument #%number', array('%number' => ($i+1))),
68
        '#default_value' => $this->options['args'][$i],
69
        '#description' => t('Enter here the argument to pass to the view. If you want to use a value from a field, then use the replacement patterns in <strong>Rewrite results</strong> below.'),
70
      );
71
    }
72

    
73
    // Get a list of the available fields and arguments for token replacement.
74
    $options = array();
75
    foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
76

    
77
      // We only use fields up to (and excluding) this one.
78
      if ($field == $this->options['id']) {
79
        break;
80
      }
81
      $options[t('Fields')]["[$field]"] = $handler->ui_name();
82
    }
83
    $count = 0; // This lets us prepare the key as we want it printed.
84
    foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
85
      $options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->ui_name()));
86
      $options[t('Arguments')]['!' . $count] = t('@argument input', array('@argument' => $handler->ui_name()));
87
    }
88

    
89
    $this->document_self_tokens($options[t('Fields')]);
90

    
91
    // Default text.
92
    $output = t('<p>You must add some additional fields to this display before using this field. These fields may be marked as <em>Exclude from display</em> if you prefer. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.</p>');
93
    // We have some options, so make a list.
94
    if (!empty($options)) {
95
      $output = t('<p>The following substitution patterns are available for this display. Use the pattern shown on the left to display the value indicated on the right. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.</p>');
96
      foreach (array_keys($options) as $type) {
97
        if (!empty($options[$type])) {
98
          $items = array();
99
          foreach ($options[$type] as $key => $value) {
100
            $items[] = $key . ' == ' . $value;
101
          }
102
          $output .= theme('item_list', array('items' => $items, 'title' => $type));
103
        }
104
      }
105
    }
106

    
107
    // This construct uses 'hidden' and not markup because process doesn't
108
    // run. It also has an extra div because the dependency wants to hide
109
    // the parent in situations like this, so we need a second div to
110
    // make this work.
111
    $form['alter']['help'] = array(
112
      '#type' => 'hidden',
113
      '#id' => 'views-tokens-help',
114
      '#prefix' => '<div><fieldset id="views-tokens-help"><legend>' . t('Replacement patterns') . '</legend>' . $output . '</fieldset></div>',
115
      /* '#process' => array('views_process_dependency'),
116
      '#dependency' => array(
117
        'edit-options-alter-make-link' => array(1),
118
        'edit-options-alter-alter-text' => array(1),
119
      ), */
120
    );
121

    
122
  }
123

    
124
  /**
125
   * Renders the field. For rendering the new views is created an added. For
126
   * PDF displays the two PDF classes where merged.
127
   */
128
  function render($values) {
129

    
130
    if (!empty($this->options['exclude'])) {
131
      return '';
132
    }
133

    
134
    $displayType = $this->view->display_handler->get_style_type();
135
    $currentDisplay = $this->view->current_display;
136

    
137
    $tokens = $this->get_render_tokens('');
138
    $args = array();
139
    foreach ($this->options['args'] as $arg) {
140
      $args[] = str_replace(array_keys($tokens), $tokens, $arg);
141
    }
142

    
143
    $view_name = $this->options['view'];
144

    
145
    $view = views_get_view($view_name);
146

    
147
    // look for a display named pdf_X.
148
    $pdf_display = NULL;
149
    foreach($view->display as $display) {
150
      if (preg_match('/^pdf_/', $display->id)) {
151
        // found a PDF display so break out of loop.
152
        $pdf_display = $display->id;
153
        break;
154
      }
155
    }
156

    
157
    // Set the found PDF display or automatically revert to default if it's
158
    // not found.
159
    $view->set_display($pdf_display);
160

    
161
    $view->pre_execute($args);
162

    
163
    $view->init_style();
164

    
165
    // Important only for pdf views. With this action we assign the
166
    // PDF document to the new view
167
    if (isset($this->view->pdf) && is_object($this->view->pdf)) {
168
      $view->pdf =& $this->view->pdf;
169

    
170
      $output = $view->render($view->display_handler->display->id);
171
    }
172
    else {
173
      $output = $view->display_handler->execute();
174
    }
175

    
176
    return $output;
177
  }
178

    
179
  function allow_advanced_render() {
180
    return FALSE;
181
  }
182
}