~ubuntu-drupal-devs/ubuntu-drupal-header/6.x

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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
// $Id$

/*
 * @file
 * Drupal Module: Ubuntu Drupal Header
 * Ubuntu Drupal Header displays a customizable block to be used
 * as a header. It cad be set to display specific headers for
 * different node types.
 */

/**
 * Implementation of hook_perm - Defines user permissions.
 * The suggested naming convention for permissions is "action_verb modulename".
 *
 * @return An array of permissions strings.
 *
 * @ingroup base
 */

function udheader_perm() {
  return array('administer udheader', 'view udheader');
}

/**
 * Implementation of hook_help - Provides online user help.
 *
 * @param $path A Drupal menu router path the help is being requested for
 * @param $arg
 * @return A localized string containing the help text.
 *
 * @ingroup base
 */

function udheader_help($path, $arg) {
  switch ($path) {
    case 'admin/help#udheader':
      $output = '';

      return $output;
  }
}

function udheader_menu() {

  $items = array();

  $items['admin/settings/udheader'] = array(
    'title' => 'UD Header Settings',
    'description' => 'Set the various options for the Ubuntu Drupal Header module.',
    'weight' => '0',
    'page callback' => 'udheader_admin_settings',
    'access arguments' => array('access administration pages'),
    'type' => MENU_NORMAL_ITEM,
   );

  $items['admin/settings/udheader/settings'] = array(
    'title' => 'Settings',
    'description' => 'Set the various options for the Ubuntu Drupal Header module.',
    'weight' => '0',
    'page callback' => 'udheader_admin_settings',
    'access arguments' => array('access administration pages'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
   );
  
  return $items;
}

function udheader_admin_settings() {
  $tid = intval(arg(3)); // @deprecated arg()
  if ($tid > 0) {
    $edit = db_fetch_array(db_query('SELECT * FROM {udheader} WHERE tid = %d', $tid));
    $output .= drupal_get_form('udheader_linkform', $edit, true);
  }
  else {
    $output .= drupal_get_form('udheader_linkform', $edit, false);
    $output .= '<h2>Header Maps</h2>';
    $output .= udheader_view_list();
  }
  return $output;
}

/**
 * Build form to add/edit location links
 */

function udheader_linkform(&$form_state, $edit = array(), $individual = false) {

  // Get available node types
  $node_types = node_get_types('names');
  $node_types['udheader_default'] .= 'Default';

  // Get left images
  $dir =  drupal_get_path('module', 'udheader') . "/images/left/";
  $handle = opendir($dir);
  while (false !== ($file = readdir($handle))) {
    if ($file != '.' && $file != '..') {
      $images['left'][$file] = $file;
    }
  }
  closedir($handle);

  // Get center images
  $dir =  drupal_get_path('module', 'udheader') . "/images/center/";
  $handle = opendir($dir);
  while (false !== ($file = readdir($handle))) {
    if ($file != '.' && $file != '..') {
      $images['center'][$file] = $file;
    }
  }
  closedir($handle);

  // Get right images
  $dir =  drupal_get_path('module', 'udheader') . "/images/right/";
  $handle = opendir($dir);
  while (false !== ($file = readdir($handle))) {
    if ($file != '.' && $file != '..') {
      $images['right'][$file] = $file;
    }
  }
  closedir($handle);

  $form = array();
  
  $c = $edit['tid'] > 0 ? FALSE : TRUE;
  $t = $edit['tid'] > 0 ? 'Edit' : 'Add';
  $form['udheader'] = array(
    '#type' => 'fieldset',
    '#title' => t($t . ' Header'),
    '#collapsible' => $c,
    '#collapsed' => $c,
    '#tree' => FALSE,
  );
  $form['udheader']['tid'] = array(
    '#type' => 'hidden',
    '#value' => $edit['tid']
  );
  $form['udheader']['node'] = array(
    '#type' => 'select',
    '#title' => t('Node Type'),
    '#options' => $node_types,
    '#default_value' => $edit['node'],
    '#description' => t('Select the node type which the header will be applied for.'),
  );
  $form['udheader']['left'] = array(
    '#type' => 'select',
    '#title' => t('Left Image'),
    '#options' => $images['left'],
    '#default_value' => $edit['leftimg'],
    '#description' => t('Select the image to be used on the left side of the header.'),
  );
  $form['udheader']['center'] = array(
    '#type' => 'select',
    '#title' => t('Center Image'),
    '#options' => $images['center'],
    '#default_value' => $edit['centerimg'],
    '#description' => t('Select the image to be used in the center of the header.'),
  );
  $form['udheader']['centertext'] = array(
    '#type' => 'textarea',
    '#title' => t('Text to be displayed in the center of the masthead'),
    '#default_value' => $edit['centertext'],
  );
  $form['udheader']['right'] = array(
    '#type' => 'select',
    '#title' => t('Right Image'),
    '#options' => $images['right'],
    '#default_value' => $edit['rightimg'],
    '#description' => t('Select the image to be used on the right side of the header.'),
  );
  $form['udheader']['righttext'] = array(
    '#type' => 'textarea',
    '#title' => t('Text to be displayed in the right part of the masthead'),
    '#default_value' => $edit['righttext'],
  );
  $form['udheader']['submit'] = array(
    '#type' => 'submit',
    '#value' => $edit['tid'] > 0 ? 'Update' : 'Add'
  );

  if ($individual) {
    $form['udheader']['delete'] = array(
      '#type' => 'submit',
      '#value' => 'Delete',
    );
  }
  
  return $form;
}

/**
 * Build list of existing linked locations
 */

function udheader_view_list() {
  $imgmaps = db_query('SELECT * FROM {udheader}');
  $rows = array();
  $headers = array('Node Type', 'Left Image', 'Center Image', 'Right Image', 'Edit');
  
  while ($imgmap = db_fetch_object($imgmaps)) {
    $tid = strval($imgmap->tid);
    array_push($rows, array(
      $imgmap->node,
      $imgmap->leftimg,
      $imgmap->centerimg,
      $imgmap->rightimg,
      l('edit', 'admin/settings/udheader/'. $tid),
    )
    );
  }
  return theme('table', $headers, $rows);                                                                                                                                                       
}

/**
 * Add/Delete/Update location link to database
 */

function udheader_linkform_submit($form, &$form_state) {
  
  $form_state['redirect'] = 'admin/settings/udheader';
  
  $op  = $form_state['clicked_button']['#post']['op'];
  $tid = $form_state['clicked_button']['#post']['tid'];
  $node = $form_state['clicked_button']['#post']['node'];
  $left = $form_state['clicked_button']['#post']['left'];
  $center = $form_state['clicked_button']['#post']['center'];
  $centertext = $form_state['clicked_button']['#post']['centertext'];
  $right = $form_state['clicked_button']['#post']['right'];
  $righttext = $form_state['clicked_button']['#post']['righttext'];

  // Update
  if ($op == 'Update') {
    $rslt = db_query('UPDATE {udheader} SET node="%s", leftimg="%s", centerimg="%s", centertext="%s", rightimg="%s", righttext="%s" WHERE tid=%d', $node, $left, $center, $centertext, $right, $righttext, $tid);
  }

  // Delete
  if ($op == 'Delete') {
    $rslt = db_query('DELETE FROM {udheader} WHERE tid=%d', $tid);
  }

  // Add
  if ($op == 'Add') {
    $rslt = db_query('INSERT INTO {udheader} (node, leftimg, centerimg, centertext, rightimg, righttext) VALUES ("%s", "%s", "%s", "%s", "%s", "%s")', $node, $left, $center, $centertext, $right, $righttext);
  }

  // Check Result
  if ('1' == $rslt) {
    drupal_set_message('Record modification made.');
    return TRUE;
  } else {
    trigger_error('Record modification failed.');
    return FALSE;
  }
}

/**
 * Implementation of hook_block
 * @param string $op one of "list", "view", "save" and "configure"
 * @param integer $delta code to identify the block
 * @param array $edit only for "save" operation
 **/

function udheader_block($op = 'list', $delta = 0, $edit = array()) {
  $block = array();

  if ($op == "list") {
    $block[0]["info"] = t('Ubuntu Drupal Header');
  } elseif ($op == 'view') {
    $block['subject'] = null;
    $block['content'] = udheader_build_block();
  }
  return $block;
}

/**
 * Build header content
 */
function udheader_build_block() {
  drupal_add_css(drupal_get_path('module', 'udheader') .'/udheader-style.css');

  $node = node_load(arg(1));
  if ($node->type) {
    $rslt = db_fetch_array(db_query('SELECT leftimg, centerimg, centertext, rightimg, righttext FROM {udheader} WHERE node = "%s" LIMIT 1', $node->type));
    if (!$rslt) {
      $rslt = db_fetch_array(db_query('SELECT leftimg, centerimg, centertext, rightimg, righttext FROM {udheader} WHERE node = "%s" LIMIT 1', 'udheader_default'));
    }
  } else {
    $rslt = db_fetch_array(db_query('SELECT leftimg, centerimg, centertext, rightimg, righttext FROM {udheader} WHERE node = "%s" LIMIT 1', 'udheader_default'));
  }
  $image['left'] = base_path() . drupal_get_path('module', 'udheader') . "/images/left/" . $rslt['leftimg'];
  $image['center'] = base_path() . drupal_get_path('module', 'udheader') . "/images/center/" . $rslt['centerimg'];
  $image['right'] = base_path() . drupal_get_path('module', 'udheader') . "/images/right/" . $rslt['rightimg'];

  $h1 = udheader_block_height($rslt['centerimg'], 1);
  $h2 = udheader_block_height($rslt['centerimg'], 10, 'sub');

  $header  = '<div id="udheader-block" style="height: ' . $h1 . ';">';
  $header .= '  <div id="udheader-left">';
  $header .= '    <img src="' . $image['left'] . '" />';
  $header .= '  </div>';
  $header .= '  <div id="udheader-right" style="background-image: url(' . $image['right'] . ');">';
  $header .= '    <span id="udheader-righttext" style="height: ' . $h2 . ';">' . $rslt['righttext']. '</span>';
  $header .= '  </div>';
  $header .= '  <div id="udheader-center" style="background-image: url(' . $image['center'] . ');">';
  $header .= '    <span id="udheader-centertext" style="height: ' . $h2 . ';">' . $rslt['centertext']. '</span>';
  $header .= '  </div>';
  $header .= '</div>';

  return $header;
}

/**
 * Find height based on center image selection
 */

function udheader_block_height($img, $val = '0', $oper = 'add') {
  // Masthead height will be based on the center content
  $center = array(
    'center.png' => '97',
    'center-tall.png' => '173',
  );
  
  switch ($oper) {
     case 'add':
       $calc = ( $center[$img] + $val );
       break;
     case 'sub':
       $calc = ( $center[$img] - $val );
       break;
   }
  
  $height = $calc . 'px';
  
  return $height;
}