summaryrefslogtreecommitdiffstats
path: root/advpoll_field_image.module
blob: c5e085beb21695b47cb8d1d0856f902bffbef837 (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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php

/**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 */
function advpoll_field_image_field_widget_advpoll_write_in_form_alter(&$element, &$form_state, $context) {
  $default_value = advpoll_field_image_image_load($element['choice_id']['#default_value']);

  $element['image'] = array(
    '#type' => 'managed_file',
    '#title' => t('Image'),
    '#default_value' => $default_value ? $default_value : 0,
    // TODO: Do not hardwire image location
    '#upload_location' => 'public://advpoll/advpoll-field-image',
    '#upload_validators' => array(
      'file_validate_is_image' => array(),
      'file_validate_extensions' => array('png gif jpg jpeg'),
    ),
  );
}

/**
 * Implements hook_theme().
 */
function advpoll_field_image_theme($existing, $type, $theme, $path) {
  return array(
    // Theme image output
    'advpoll_field_image_image' => array(
      'variables' => array('fid' => NULL, 'choice_text' => NULL),
    ),
    // Override / Add suggestion for advpoll choice form template
    // TODO: is this the best way?
    'advpoll_choice_form' => array(
      'render element' => 'form',
      'path' => drupal_get_path('module', 'advpoll_field_image') . '/templates',
      'template' => 'advpoll-choice-form',
    ),
    // Override / Add suggestion for advpoll bar template
    // TODO: is this the best way?
    'advpoll_bar' => array(
      'variables' => array(
        'percentage' => 0,
        'votes' => 0,
        'voted' => 0
      ),
      'path' => drupal_get_path('module', 'advpoll_field_image') . '/templates',
      'template' => 'advpoll-bar',
    ),
  );
}

/**
 * Return image html.
 */
function theme_advpoll_field_image_image($variables) {
  $fid = $variables['fid'];
  $choice_text = isset($variables['choice_text']) ? $variables['choice_text'] : '';

  if (!$fid) {
    return '';
  }

  $file = file_load($fid);
  return ' ' . theme('image_style', array(
    'style_name' => variable_get('advpoll_field_image_style', 'thumbnail'),
    'path' => $file->uri,
    'title' => $choice_text,
    'alt' => "$choice_text image",
    'attributes' => array(
      'class' => array('image', 'choice-image'),
    ),
  ));
}

/**
 * Implements hook_node_update().
 */
function advpoll_field_image_node_update($node) {
  if ($node->type == 'advpoll') {
    advpoll_field_image_choices_save($node);
  }
}

/**
 * Implements hook_node_insert().
 */
function advpoll_field_image_node_insert($node) {
  if ($node->type == 'advpoll') {
    advpoll_field_image_choices_save($node);
  }
}

/**
 * Save choices images.
 */
function advpoll_field_image_choices_save($node) {
  $lang = advpoll_field_image_get_choices_lang($node);

  $new_images = array();
  foreach ($node->advpoll_choice[$lang] as $choice) {
    if ($choice['image']) {
      $new_images[$choice['choice_id']] = $choice['image'];
    }
  }

  $old_images = array();
  if (isset($node->original)) {
    foreach ($node->original->advpoll_choice[$lang] as $choice) {
      if ($choice['image']) {
        $old_images[$choice['choice_id']] = $choice['image'];
      }
    }
  }

  // Delete images, that existed in the old node, but do not exist in the new node
  foreach ($old_images as $choice_id => $image) {
    if (!isset($new_images[$choice_id])) {
      advpoll_field_image_image_delete($choice_id);
    }
  }

  foreach ($new_images as $choice_id => $image) {
    // Delete images, that has changed in the new node
    if (isset($old_images[$choice_id]) && $old_images[$choice_id] != $image) {
      advpoll_field_image_image_delete($choice_id);
    }

    // Add images, which were not in the old node
    if (!isset($old_images[$choice_id])) {
      advpoll_field_image_image_add($node->nid, $choice_id, $image);
    }
  }
}

/**
 * Implements hook_node_delete().
 */
function advpoll_field_image_node_delete($node) {
  if ($node->type == 'advpoll') {
    $lang = advpoll_field_image_get_choices_lang($node);
    foreach ($node->advpoll_choice[$lang] as $choice) {
      advpoll_field_image_image_delete($choice['choice_id']);
    }
  }
}

/**
 * Implements hook_field_attach_load().
 */
function advpoll_field_image_field_attach_load($entity_type, $entities, $age, $options) {
  if ($entity_type == 'node') {
    // Find choice ids
    $choice_ids = array();
    foreach ($entities as $node) {
      if ($node->type == 'advpoll') {
        $lang = advpoll_field_image_get_choices_lang($node);
        foreach ($node->advpoll_choice[$lang] as $choice) {
          $choice_ids[] = $choice['choice_id'];
        }
      }
    }

    // Add data to nodes
    if ($choice_ids) {
      $images = advpoll_field_image_image_load_multiple($choice_ids);
      foreach ($entities as &$node) {
        if ($node->type == 'advpoll') {
          $lang = advpoll_field_image_get_choices_lang($node);
          foreach ($node->advpoll_choice[$lang] as &$choice) {
            $choice['image'] = isset($images[$choice['choice_id']]) ? $images[$choice['choice_id']] : 0;
          }
        }
      }
    }
  }
}

/**
 * Return choices language.
 */
function advpoll_field_image_get_choices_lang($node) {
  $lang = $node->language;
  if (!isset($node->advpoll_choice[$lang])) {
    $lang = 'und';
  }
  return $lang;
}

/**
 * Return image fid by choice id.
 */
function advpoll_field_image_image_load($choice_id) {
  return db_select('advpoll_choice_image', 'aci')
    ->fields('aci', array('fid'))
    ->condition('choice_id', $choice_id)
    ->execute()
    ->fetchField();
}

/**
 * Return images by choice ids
 */
function advpoll_field_image_image_load_multiple($choice_ids) {
  return db_select('advpoll_choice_image', 'aci')
    ->fields('aci', array('choice_id', 'fid'))
    ->condition('choice_id', $choice_ids, 'IN')
    ->execute()
    ->fetchAllKeyed();
}

/**
 * Delete image file and relation by choice id.
 */
function advpoll_field_image_image_delete($choice_id) {
  $image = advpoll_field_image_image_load($choice_id);
  if ($image) {
    // Delete file
    $file = file_load($image);
    file_usage_delete($file, 'advpoll_field_image');
    file_delete($file);

    // Delete relation
    db_delete('advpoll_choice_image')
      ->condition('choice_id', $choice_id)
      ->execute();
  }
}

/**
 * Change file status and add relation.
 */
function advpoll_field_image_image_add($nid, $choice_id, $image) {
  if (!$image || !$choice_id) {
    return FALSE;
  }

  db_insert('advpoll_choice_image')
    ->fields(array(
      'choice_id' => $choice_id,
      'fid' => $image,
    ))
    ->execute();

  $file = file_load($image);
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
  file_usage_add($file, 'advpoll_field_image', 'node', $nid);
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function advpoll_field_image_form_advpoll_choice_form_alter(&$form, &$form_state, $form_id) {
  // Add choice images to form, designed to be accessed and themed at the template level.
  // Image keys correspond to choice keys.

  // Get images for this poll
  $node = $form_state['build_info']['args'][0];
  $lang = advpoll_field_image_get_choices_lang($node);
  $images = array();
  foreach ($node->advpoll_choice[$lang] as $choice) {
    if ($choice['image']) {
      $images[$choice['choice']] = $choice['image'];
    }
  }

  // Return if we don't have any images set
  if (empty($images)) {
    return;
  }

  // Alter form
  foreach (element_children($form) as $key) {
    if ($form[$key]['#type'] == 'radios' || $form[$key]['#type'] == 'checkboxes') {
      foreach ($form[$key]['#options'] as $option_value => &$option_text) {
        $form['images'][$option_value]['#markup'] = theme('advpoll_field_image_image', array('fid' => $images[$option_value], 'choice_text' => $option_text));
      }
    }
  }
}

/**
 * Assign the elements of the form to variables so
 * the themer can use those values to control how the
 * form elements are displayed, or alternatively
 * displaying the whole form as constructed above.
 * @see Pro Drupal Development
 */
function template_preprocess_advpoll_choice_form(&$variables) {
  $hidden = array();
  // Provide variables named after form keys so themers can print each element independently.
  foreach (element_children($variables['form']) as $key) {
    $type = isset($variables['form'][$key]['#type']) ? $variables['form'][$key]['#type'] : '';

    // Choices are badly keyed in advpoll choice form... we want to re-key them so they are easy to find.
    // TODO: get these keys fixed in advpoll module
    $new_key = $key;
    if ($type == 'radios' || $type == 'checkboxes') {
      $new_key = 'choices';
    }

    // Put all the hidden stuff together
    if ($type == 'hidden' || $type == 'token') {
      $hidden[] = drupal_render($variables['form'][$key]);
    }

    // Deal with elements that have child elements
    elseif (element_children($variables['form'][$key])) {
      // Render each child individually
      foreach (element_children($variables['form'][$key]) as $child_key) {
        $variables[$new_key][$child_key] = drupal_render($variables['form'][$key][$child_key]);
      }
    }
    else {
      $variables[$new_key] = drupal_render($variables['form'][$key]);
    }
  }

  // Hidden form elements have no value to themers. No need for separation.
  $variables['hidden'] = implode($hidden);
}

/**
 * Preprocess function for advpoll-bar.tpl.php
 */
function advpoll_field_image_preprocess_advpoll_bar(&$vars) {
  // Ideally, theme_advpoll_bar() would be passed $nid and $choice_id, which would
  // allow us to get and pass the proper image directly here, rather than this placeholder hash.
  $vars['image_placeholder'] = '<!-- advpoll-image-' . md5($vars['title']) . ' -->';
}

/**
 * Preprocess function for advpoll-results.tpl.php
 */
function advpoll_field_image_preprocess_advpoll_results(&$vars) {
  // We wouldn't need to implement this function if we could pass the image directly in
  // advpoll_field_image_preprocess_advpoll_bar()
  $node = node_load($vars['nid']);
  $lang = advpoll_field_image_get_choices_lang($node);

  foreach ($node->advpoll_choice[$lang] as $delta => $choice) {
    $vars['bars'] = str_replace(
      '<!-- advpoll-image-' . md5($choice['choice']) . ' -->',
      theme('advpoll_field_image_image', array('fid' => $choice['image'], 'choice_text' => $choice['choice'])),
      $vars['bars']
    );
  }
}