summaryrefslogtreecommitdiffstats
path: root/gallery_block.inc
blob: a62dbe951e2e7a921c7882dfa36f3cfdb0a0a773 (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
<?php

/**
 * gallery.module : gallery_block.inc
 * Block functions
 */
 
/**
 * Implementation of hook_block().
 */
function _gallery_block($op = 'list', $delta = 0, $edit = array()) {
  $type_map = $param_map = array();
  _gallery_block_options($type_map, $param_map);
  
  list($type, $delta) = explode('-', $delta);
  switch ($op) {
    case 'list':
      $blocks['navigation']['info'] = t('Gallery Navigation');
      $imageblock_num = variable_get('gallery_block_image_num', 1);
      for ($i=0; $i<$imageblock_num; $i++) {
        $id = variable_get('gallery_block_image_'. $i .'_blockid', '');
        $blocks['image-'. $i]['info'] = t('Gallery Image Block @id',
          array('@id' => ($imageblock_num > 1) ? ('['. ($id ? $id : $i+1) .']') : ''));
      }
      $gridblock_num = variable_get('gallery_block_grid_num', 1);
      for ($i=0; $i<$gridblock_num; $i++) {
        $id = variable_get('gallery_block_grid_'. $i .'_blockid', '');
        $blocks['grid-'. $i]['info'] = t('Gallery Grid Block @id',
          array('@id' => ($gridblock_num > 1) ? ('['. ($id ? $id : $i+1) .']') : ''));
      }
      return $blocks;
    case 'view':
      if (!user_access('access gallery') || !_gallery_init(TRUE) || $_GET['q'] == 'admin/settings/gallery/install') {
        return;
      }
      switch ($type) {
        case 'navigation': // Navigation Block
          if ((arg(0) == 'gallery') && !empty($GLOBALS['gallery_sidebar'])) {
            $block['subject'] = t('Gallery Navigation');
            $block['content'] = '<div id="gsSidebar" class="gcBorder1">'. implode('', $GLOBALS['gallery_sidebar']) .'</div>';
          }
          break;
        case 'image': // Image Block
          $block = _gallery_block_image_block($delta, $type_map);
          break;
        case 'grid': // Image Grid Block
          $block = _gallery_block_grid_block($delta, $type_map);
          break;
      }
      GalleryEmbed::done();
      return $block;
    case 'configure':
      require_once(drupal_get_path('module', 'gallery') .'/gallery_settings.inc');
      switch ($type) {
        case 'image': // Image Block
          return _gallery_settings_block_image($delta);
        case 'grid': // Image Grid Block
          return _gallery_settings_block_grid($delta);
      }
      break;
    case 'save':
      require_once(drupal_get_path('module', 'gallery') .'/gallery_settings.inc');
      _gallery_settings_block_save($delta, $edit);
      break;
  }
}

/**
 * Function _gallery_block_options().
 */
function _gallery_block_options(&$type_map, &$param_map) {
  $type_map = array(
    'randomImage'   => t('Random image'),
    'recentImage'   => t('Recent image'),
    'viewedImage'   => t('Viewed image'),
    'randomAlbum'   => t('Random album'),
    'recentAlbum'   => t('Recent album'),
    'viewedAlbum'   => t('Viewed album'),
    'dailyImage'    => t('Daily image'),
    'weeklyImage'   => t('Weekly image'),
    'monthlyImage'  => t('Monthly image'),
    'dailyAlbum'    => t('Daily album'),
    'weeklyAlbum'   => t('Weekly album'),
    'monthlyAlbum'  => t('Monthly album')
  );
  $param_map = array(
    'title'   => t('Title'),
    'date'    => t('Date'),
    'views'   => t('View Count'),
    'owner'   => t('Item owner'),
    'heading' => t('Heading')
  );
}

/**
 * Function _gallery_block_image_block().
 */
function _gallery_block_image_block($delta, $type_map) {
  $param_blocks_array = array_filter(variable_get('gallery_block_image_'. $delta .'_block_block', array('randomImage')));
  $params['blocks'] = is_array($param_blocks_array) ? implode('|', $param_blocks_array) : ''; 
  $params['itemId'] = variable_get('gallery_block_image_'. $delta .'_item_id', '');
  $param_show_array = variable_get('gallery_block_image_'. $delta .'_block_show', array());
  $params['show'] = is_array($param_show_array) ? implode('|', $param_show_array) : '';
  if (variable_get('gallery_block_image_'. $delta .'_size_method', GALLERY_IMAGEBLOCK_SIZE_METHOD_DEFAULT) == 'maxsize') {
    $params['maxSize'] = variable_get('gallery_block_image_'. $delta .'_size', GALLERY_IMAGEBLOCK_SIZE_DEFAULT);
  } 
  else {
    $params['exactSize'] = variable_get('gallery_block_image_'. $delta .'_size', GALLERY_IMAGEBLOCK_SIZE_DEFAULT);
  }
  $params['albumFrame'] = variable_get('gallery_block_image_'. $delta .'_album_frame', 'none');
  $params['itemFrame'] = variable_get('gallery_block_image_'. $delta .'_item_frame', 'none');
  $params['linkTarget'] = variable_get('gallery_block_image_'. $delta .'_link_target', '');
  $params['link'] = variable_get('gallery_block_image_'. $delta .'_link', '');
  
  $block = _gallery_block_get_block($params);
  $block['subject'] = (count($param_blocks_array) > 1) ? t('Gallery') : $type_map[$params['blocks']];
  
  return $block;
}

/**
 * Function _gallery_block_grid_block().
 */
function _gallery_block_grid_block($delta, $type_map) {
  $num_cols = variable_get('gallery_block_grid_'. $delta .'_num_cols', 2);
  $num_rows = variable_get('gallery_block_grid_'. $delta .'_num_rows', 2);
  $num_images = $num_cols * $num_rows; 
  
  $param_blocks_array = array_fill(0, $num_images, variable_get('gallery_block_grid_'. $delta .'_block_block', 'randomImage'));
  $params['blocks'] = is_array($param_blocks_array) ? implode('|', $param_blocks_array) : '';
  $params['itemId'] = variable_get('gallery_block_grid_'. $delta .'_item_id', '');
  $param_show_array = variable_get('gallery_block_grid_'. $delta .'_block_show', array());
  $params['show'] = is_array($param_show_array) ? implode('|', $param_show_array) : '';
  if (variable_get('gallery_block_grid_'. $delta .'_size_method', GALLERY_GRID_SIZE_METHOD_DEFAULT) == 'maxsize') {
    $params['maxSize'] = variable_get('gallery_block_grid_'. $delta .'_size', GALLERY_GRID_SIZE_DEFAULT);
  } 
  else {
    $params['exactSize'] = variable_get('gallery_block_grid_'. $delta .'_size', GALLERY_GRID_SIZE_DEFAULT);
  }
  $params['albumFrame'] =  variable_get('gallery_block_grid_'. $delta .'_album_frame', 'none');
  $params['itemFrame'] =  variable_get('gallery_block_grid_'. $delta .'_item_frame', 'none');
  $params['linkTarget'] =  variable_get('gallery_block_grid_'. $delta .'_link_target', '');
  $params['link'] = variable_get('gallery_block_grid_'. $delta .'_link', '');
  
  return _gallery_block_get_block($params, array('num_cols' => $num_cols));
}

/**
 * Function _gallery_block_get_block().
 */
function _gallery_block_get_block($params, $extra = array()) {
  // Handle useralbum feature
  if (preg_match('/user(:([\d]+))?/i', $params['itemId'], $param_uid)) {
    require_once(drupal_get_path('module', 'gallery') .'/gallery_user.inc');
    $params['itemId'] = gallery_user_useralbum(isset($param_uid[2]) ? $param_uid[2] : NULL, FALSE);
  }
  if (empty($params['itemId']) || $params['itemId'] === FALSE) {
    unset($params['itemId']);
  }
  gallery_debug($params, t('Block parameters'));
  // Get the image(s) from G2
  list($ret, $content, $head) = GalleryEmbed::getImageBlock($params);
  if ($ret) {
    gallery_error(t('Error trying to get image block.'), $ret);
    return array();
  }
  // Format the block content
  $block = array();
  if ($content) {
    $block['subject'] = t('Gallery');
    if (isset($extra['num_cols'])) {
      $class = isset($extra['class']) ? $extra['class'] : 'gallery-grid-block';
      $block['content'] = theme('gallery_block_grid_block', $content, $extra['num_cols'], $class);
    }
    else {
      $class = isset($extra['class']) ? $extra['class'] : 'g2image_centered';
      $block['content'] = theme('gallery_block_image_block', $content, $class);
    }
    // Add css/js to the page
    if ($head) {
      gallery_set_head($head);
    }
  }

  return $block;
}

/**
 * Function _gallery_block_split_imageblock().
 * (split an image block result into individual images)
 */
function _gallery_block_split_imageblock($html) {
  // From http://uk.php.net/manual/en/function.preg-split.php
  // Split the html from image block into <...> parts
  $pattern = '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/';
  $html_array = preg_split($pattern, trim($html), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  
  $ndx = 0;
  $images = array();
  $image_html = '';
  // Iterate through this array and combine again, but on a per-image basis
  foreach ($html_array as $value) {
    $value = trim($value);
    $image_html .= $value; 
    if (!strcmp($value, '<div class="one-image">')) {
      // Found the opening <div> for the image
      $open_divs = 0;
    }
    elseif (!strncmp($value, '<div', 4)) {
      // Found a <div> but not the main image one (eg a frame)
      $open_divs++;
    }
    elseif (!strcmp($value, '</div>')) {
      // Found a </div> but check if it's for the main image or a subcomponent (eg frame)
      if ($open_divs > 0) {
        $open_divs--;
      }
      else {
        // This must be the closing div for "one-image" so move to next image
        $images[] = $image_html;
        $image_html = '';
      }
    }
  }
  
  return $images;  
}

/**
 * Theme function : theme_gallery_block_image_block().
 */
function theme_gallery_block_image_block($content, $class = 'g2image_centered') {
  return '<div class="'. $class .'">'. $content .'</div>';
}

/**
 * Theme function : theme_gallery_block_grid_block().
 */
function theme_gallery_block_grid_block($content, $num_cols, $class = 'gallery-grid-block') {
  $images = _gallery_block_split_imageblock($content);
  $images = array_chunk($images, $num_cols);
  
  $rows = array();
  foreach ($images as $image_row) {
    $row = array();
    foreach ($image_row as $image) {
      $row[] = array('data' => $image, 'style' => 'text-align:center;');
    }
    $rows[] = $row;
  }

  $html  = '<div class="'. $class .'"><div class="image-grid">';
  $html .= theme('table', array(), $rows);
  $html .= '</div></div>';

  return $html;
}