aboutsummaryrefslogtreecommitdiff
path: root/plugins/video_customfields/video_customfields.module
blob: 97d88168e1c7f35810b56700ca381dcb661ea79b (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
 * @file
 * Enable addition of custom fileds on video nodes created by video module.
 *
 * @author Fabio Varesano <fvaresano at yahoo dot it>
 * porting to Drupal 6
 * @author Heshan Wanigasooriya <heshan at heidisoft.com><heshanmw@gmail.com>
 * @todo
 */


/**
 * Implementation of hook_help().
 */
function video_customfields_help($path, $arg) {
  switch ($path) {
    case 'admin/settings/modules#description':
      return t('Enable addition of custom fileds on video nodes created by video module.');
  }
}


/**
 * Implementation of hook_perm().
 */
function video_customfields_perm() {
  return array('insert custom fields');
}

/**
 * Implementation of hook_menu().
 */
function video_customfields_menu() {
  $items = array();
/* TODO
   Non menu code that was placed in hook_menu under the '!$may_cache' block
   so that it could be run during initialization, should now be moved to hook_init.
   Previously we called hook_init twice, once early in the bootstrap process, second
   just after the bootstrap has finished. The first instance is now called boot
   instead of init.
   
   In Drupal 6, there are now two hooks that can be used by modules to execute code
   at the beginning of a page request. hook_boot() replaces hook_boot() in Drupal 5
   and runs on each page request, even for cached pages. hook_boot() now only runs
   for non-cached pages and thus can be used for code that was previously placed in
   hook_menu() with $may_cache = FALSE:
   
   Dynamic menu items under a '!$may_cache' block can often be simplified
   to remove references to arg(n) and use of '%<function-name>' to check
   conditions. See http://drupal.org/node/103114.
   
   The title and description arguments should not have strings wrapped in t(),
   because translation of these happen in a later stage in the menu system.
*/
    $may_cache=true;
  if ($may_cache) {
    $items['admin/content/video/customfields'] = array(
        'title' => 'Customfields',
        'description' => 'Administer video_customfields module settings',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('video_customfields_settings_form'),
        'access arguments' => array('administer site configuration'),
        'type' => MENU_NORMAL_ITEM,
      );
  }
  return $items;
}

/**
 * Settings Hook
 *
 * @return
 *   string of form content or error message
 */
function video_customfields_settings_form() {
  //Must have "administer site configuration" and "administer video" privilages.
  if (!user_access('administer video')) {
    drupal_access_denied();
  }

  $form['customfields'] = array(
    '#type' => 'fieldset',
    '#weight' => -1,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Custom display fields'),
    '#description' => t('Creates custom fields. Fields only show up if you give them a name.')
  );
  $form['customfields']['video_customfieldtitle'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom field group title'),
    '#default_value' => variable_get('video_customfieldtitle', ''),
    '#description' => t('Title of the group of all custom fields.'));
  $form['customfields']['video_customfield1'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom field 1 title'),
    '#default_value' => variable_get('video_customfield1', ''));
  $form['customfields']['video_customfield2'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom field 2 title'),
    '#default_value' => variable_get('video_customfield2', ''));
  $form['customfields']['video_customfield3'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom field 3 title'),
    '#default_value' => variable_get('video_customfield3', ''));
  $form['customfields']['video_customfield4'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom field 4 title'),
    '#default_value' => variable_get('video_customfield4', ''));
  $form['customfields']['video_customfield5'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom field 5 title'),
    '#default_value' => variable_get('video_customfield5', ''));
  $form['customfields']['video_customfield6'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom field 6 title'),
    '#default_value' => variable_get('video_customfield6', ''));


  $options = array(1 => 'Yes', 0 => 'No');

  $form['customfields']['video_customgroupcollapsed'] = array(
    '#type' => 'radios',
    '#title' => t('Start group initially collapsed'),
    '#options' => $options,
    '#default_value' => variable_get('video_customgroupcollapsed', 1),
    '#description' => t('Should the custom fields group be initially collapsed when creating and editing video nodes?')
  );
  return system_settings_form($form);
}



/**
 * Implementation of hook_form_alter()
 * We use this to add some custom fields to the video creation form.
 * Fields will be displayed only if field title is set on settings page.
 */
function video_customfields_form_alter(&$form, &$form_state, $form_id) {

  if($form_id == 'video_node_form' && isset($form['video']) && user_access('insert custom fields')) {

    //get node object from form
    $node = $form['#node'];

    $title1 = variable_get('video_customfield1', '');
    $title2 = variable_get('video_customfield2', '');
    $title3 = variable_get('video_customfield3', '');
    $title4 = variable_get('video_customfield4', '');
    $title5 = variable_get('video_customfield5', '');
    $title6 = variable_get('video_customfield6', '');

    //Only display the custom fields group if atleast one field has a title.
    if ($title1 . $title2 . $title3 . $title4 . $title5 . $title6 != '') {
      $form['customfields'] = array('#type' => 'fieldset', '#title' => variable_get('video_customfieldtitle', 'Custom Fields'), '#collapsible' => TRUE, '#collapsed' => variable_get('video_customgroupcollapsed', FALSE), '#weight' => -17);
      //If the custom field title is not blank, then display it.
      if ($title1 != '') {
        $form['customfields']['custom_field_1'] = array(
          '#type' => 'textfield', '#title' => $title1, '#maxlength' => 250, '#default_value' => $node->custom_field_1);
      }
      if ($title2 != '') {
        $form['customfields']['custom_field_2'] = array(
          '#type' => 'textfield', '#title' => $title2, '#maxlength' => 250, '#default_value' => $node->custom_field_2);
      }
      if ($title3 != '') {
        $form['customfields']['custom_field_3'] = array(
          '#type' => 'textfield', '#title' => $title3, '#maxlength' => 250, '#default_value' => $node->custom_field_3);
      }
      if ($title4 != '') {
        $form['customfields']['custom_field_4'] = array(
          '#type' => 'textfield', '#title' => $title4, '#maxlength' => 250, '#default_value' => $node->custom_field_4);
      }
      if ($title5 != '') {
        $form['customfields']['custom_field_5'] = array(
          '#type' => 'textarea', '#title' => $title5, '#rows' => 4, '#default_value' => $node->custom_field_5);
      }
      if ($title6 != '') {
        $form['customfields']['custom_field_6'] = array(
          '#type' => 'textarea', '#title' => $title6, '#rows' => 4, '#default_value' => $node->custom_field_6);
      }
    }
  }
}


/**
 * Implementation of hook_nodeapi()
 */
function video_customfields_nodeapi(&$node, $op, $teaser) {
  if($node->type == 'video') {
    switch ($op) {
      case 'view':
        //If the main node view is being displayed then add the extra video information.
        if ($teaser == FALSE) {

          if (($node->custom_field_1 . $node->custom_field_2 . $node->custom_field_3 . $node->custom_field_4 . $node->custom_field_5 . $node->custom_field_6) != '') { //Make sure there is data to display.
            //Add the HTML formatted output of the custom fields to the bottom.
            $node->body .= theme('video_customfields', $node);
          }
        }
      break;
    }
  }
}


/**
 * Display custom fields on the view page.
 *
 * @param $node
 *   object with node information
 *
 * @return
 *   string of content to display
 */
function theme_video_customfields($node) {
  //Adds the custom fields.
  $group_title = variable_get('video_customfieldtitle', ''); //Title of the custom fields.
  $title1 = variable_get('video_customfield1', '');
  $title2 = variable_get('video_customfield2', '');
  $title3 = variable_get('video_customfield3', '');
  $title4 = variable_get('video_customfield4', '');
  $title5 = variable_get('video_customfield5', '');
  $title6 = variable_get('video_customfield6', '');
  //Run the fields through the input filter set for the node, then remove paragraphs.
  //Removes the <p> and </p> tags from the filter pass return. This allows each field to be on one line.
  //A better system might be to remove only the first and last <p></P> tags.
  $field1 = str_replace(array('<p>', '</p>'), '', check_markup($node->custom_field_1, $node->format, FALSE));
  $field2 = str_replace(array('<p>', '</p>'), '', check_markup($node->custom_field_2, $node->format, FALSE));
  $field3 = str_replace(array('<p>', '</p>'), '', check_markup($node->custom_field_3, $node->format, FALSE));
  $field4 = str_replace(array('<p>', '</p>'), '', check_markup($node->custom_field_4, $node->format, FALSE));
  $field5 = str_replace(array('<p>', '</p>'), '', check_markup($node->custom_field_5, $node->format, FALSE));
  $field6 = str_replace(array('<p>', '</p>'), '', check_markup($node->custom_field_6, $node->format, FALSE));

  $output = '';
  //Make sure all the titles are not blank, if not then display them.
  if (($title1 . $title2 . $title3 . $title4 . $title5 . $title6) != '') {
    $output = '<div class="videofields">'; //Enclose all output in "videofields" div class.
    if ($group_title != '') {
      $output .= '<div class="title"><h2>' . check_plain($group_title) . '</h2></div>' . "\n";
    }
    if ($title1 != '' and $node->custom_field_1 != '') {
      $fields[] = array('title' => $title1, 'body' => $field1);
    }
    if ($title2 != '' and $node->custom_field_2 != '') {
      $fields[] = array('title' => $title2, 'body' => $field2);
    }
    if ($title3 != '' and $node->custom_field_3 != '') {
      $fields[] = array('title' => $title3, 'body' => $field3);
    }
    if ($title4 != '' and $node->custom_field_4 != '') {
      $fields[] = array('title' => $title4, 'body' => $field4);
    }
    if ($title5 != '' and $node->custom_field_5 != '') {
      $fields[] = array('title' => $title5, 'body' => $field5);
    }
    if ($title6 != '' and $node->custom_field_6 != '') {
      $fields[] = array('title' => $title6, 'body' => $field6);
    }
    $output .= theme('video_fields', $fields); //Generate all the fields HTML.

    $output .= '</div><br />'; //Close the "videofields" class div.
  }
  return $output;
}


/**
 * Implementation of hook_theme().
 */
function video_customfields_theme() {
  return array(
    'video_customfields' => array(
      'arguments' => array('node' => NULL),
    ),
  );
}