aboutsummaryrefslogtreecommitdiff
path: root/plugins/video_image/video_image.module
blob: 850781a9947486e2fe1def268246594eb1ff5c67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
// $Id$

/**
 * @file
 * Enable image support for video module.
 *
 * @author Fabio Varesano <fvaresano at yahoo dot it>
 */


/**
 * Implementation of hook_help().
 */
function video_image_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Enable thumbnails support for video module.');
  }
}

/**
 * Implementation of hook_menu()
 */
function video_image_menu($may_cache) {
  $items = array();
  if ($may_cache) {
      $items[] = array(
      'path' => 'admin/content/video/image',
      'title' => t('Video image'),
      'description' => t('Administer video_image module settings'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('video_image_admin_settings'),
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}


/**
 * Settings form
 */
function video_image_admin_settings() {
  if (module_exists('video_upload')) {
    if (variable_get('video_image_auto_thumbnail', 0)) {
      $upload_weight = db_result(db_query("SELECT weight FROM {system} WHERE name='video_upload'"));
      db_query("UPDATE {system} SET weight=".($upload_weight+1)." WHERE name='video_image'");
    }
  }
  $form = array();
  $form['video_image_publish_thumbnail'] = array(
      '#type' => 'checkbox',
      '#title' => t('Publish the video thumbnails'),
      '#description' => t('Checking this value will cause the video thumbnail image nodes to be published and therefore could show up in blocks.  Usually, this is not what you want because then you could end up with both the thumbnail node and the video node showing up and since there is no way to link the image node to the video node, this is not desirable.  However, with this unchecked, the administrator will end up with a lot of unpublished nodes.'),
      '#default_value' => _video_image_publish_thumbnails(),
    );
  $form['video_image_promote_thumbnail'] = array(
      '#type' => 'checkbox',
      '#title' => t('Promote the thumbnails to the front page'),
      '#default_value' => _video_image_promote_thumbnails(),
    );
    
  return system_settings_form($form);
}

/**
 * Implementation of hook_form_alter()
 */
function video_image_form_alter($form_id, &$form) {

  if($form_id == 'video_node_form') {

    $node = $form['#node'];
    $value = ($node->new_image) ? '#value' : '#default_value';
    $form['iid'] = array('#type' => 'hidden', $value => $node->iid);

    if (function_exists('_image_check_settings')) {
      _image_check_settings();
      $form['#attributes'] = array("enctype" => "multipart/form-data");

     
      $form['image'] = array('#type' => 'fieldset', '#title' => t('Image thumbnails'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#weight' => -17, '#description' => t('Use this form to upload an image.'));
      
      $form['image']['image'] = array('#type' => 'file', '#title' => t('Image'));
      $form['image']['image_title'] = array('#type' => 'textfield', '#title' => t('Image title'), '#default_value' => $node->image->image_title);
    }
  }

}


/**
 * Implementation of hook_nodeapi()
 */
function video_image_nodeapi(&$node, $op, $teaser) {
  if($node->type == 'video') {
    switch ($op) {
      case 'load': 
        $output['iid'] = $node->serial_data['iid'];
        return $output;
      case 'submit':
        if (variable_get('video_image_auto_thumbnail_debug', false) && variable_get('video_image_auto_thumbnail', false)) {
          drupal_set_message(t('video_image_nodeapi: prepare: ready to thumbnail image'));
        }
        $field_name = file_check_upload('image');
        $image->uid = $node->uid;
        $image->name = $node->name;
        $image->created = $node->created;
        $image->type = 'image';
        //$image->status = _video_image_publish_thumbnails();
        //$image->promote = _video_image_promote_thumbnails();
        if (!$field_name && module_exists('video_ffmpeg_helper') && variable_get('video_ffmpeg_helper_auto_thumbnail', false)) {
          $image->title = $_SESSION['video_upload_file']->filename;
          $field_name = _video_ffmpeg_helper_auto_thumbnail($node);
        }
        else {
          $image->title = $_POST['edit']['image_title'];
          $field_name = 'image';
        }
        image_prepare($image, $field_name);
        if ($image->images) {
          node_validate($image);
          if (!form_get_errors()) {
            $image = node_submit($image);
            node_save($image);
            
            // needed to set the correct status and promote values if the user does not have enought permissions. Is there a better solution???
            db_query('UPDATE {node} SET status = %d, promote = %d WHERE nid = %d AND vid = %d', _video_image_publish_thumbnails(), _video_image_promote_thumbnails(), $image->nid, $image->vid); 
            
            $node->iid = $node->serial_data['iid'] = $image->nid;
            $_SESSION['video_upload_file']->iid = $image->nid;
            $node->new_image = TRUE;
          }
        }
        else if (isset($_SESSION['video_upload_file']->iid)) {
          $node->iid = $_SESSION['video_upload_file']->iid;
        }
      
        $node->serial_data['iid'] = $node->iid;
      break;
      case 'prepare':
        ;
      break;

      case 'view':
        if ($node->iid) {
          if($teaser) {
            $node->content['video_image_thumbnail'] = array('#value' => theme('video_image_teaser', $node));
          }
          else {
            $node->content['video_image_thumbnail'] = array('#value' => theme('video_image_body', $node));
          }
        }
        break;
      case 'delete':
        node_delete(array('nid' => $node->iid));
        break;
    }
  }
}


/**
 * Render the output for the node teaser.
 *
 * @param $node
 *   object with node information
 *
 * @return
 *   string of content to display
 */
function theme_video_image_teaser($node) {
  if($node->serial_data['iid']) {
    $image = node_load($node->serial_data['iid']);
    $image = image_display($image, 'thumbnail', array('class' => 'video_image_teaser'));
  }
  else { // only for backward compatibility
    $image = theme('image', $node->serial_data['image_teaser'], $node->title, $node->title, array('class' => 'video_image_teaser'), FALSE);
  }
  $output .= l($image, "node/$node->nid", array(), NULL, NULL, FALSE, TRUE); //Create a link with an image in it.
  $output .= '<br class="video_image_clear" />';
  return $output;
}


/**
 * Generates the image HTML displayed in the Node body.
 *
 * @param $node
 *   object with node information
 *
 * @return
 *   string of content to display
 */
function theme_video_image_body($node) {
  if($node->serial_data['iid']) {
    $image = node_load($node->serial_data['iid']);
    $image = image_display($image, 'thumbnail');
  }
  else { // only for backward compatibility
    $image = theme('image', $node->serial_data['image_view'], $node->title, $node->title, array('class' => 'video_image_view'), FALSE); //Create image HTML
  }
  $output = l($image, "node/$node->nid/play", array('title' => t('play') . ' ' . $node->title), NULL, NULL, FALSE, TRUE); //Create link HTML with image in it.

  return $output;
}


/* If the user has set a promote preference, use that, otherwise return
 * if 'promote' is set in the drupal content type settings
 *
 * @return
 *   Returns whether we should promote thumbnails or not
 */
function _video_image_promote_thumbnails() {
  $settings_override = variable_get('video_image_promote_thumbnail', NULL);
  if ($settings_override === NULL) {
    $node_options = variable_get('node_options_image', array('status', 'promote'));
    return in_array('promote', $node_options);
  }
  return $settings_override;
}

/* If the user has set a publish preference, use that, otherwise return
 * if 'status' is set in the drupal content type settings
 *
 * @return
 *   Returns whether we should publish thumbnails or not
 */
function _video_image_publish_thumbnails() {
  $settings_override = variable_get('video_image_publish_thumbnail', NULL);
  if ($settings_override === NULL) {
    $node_options = variable_get('node_options_image', array('status', 'promote'));
    return in_array('status', $node_options);
  }
  return $settings_override;
}
?>