-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagile_iit.module
533 lines (504 loc) · 16.5 KB
/
agile_iit.module
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<?php
/**
* @file
* Module file for agile_iit.
*/
/**
* Implements hook_views_api().
*/
function agile_iit_views_api() {
return array(
'api' => 3.0,
'path' => drupal_get_path('module', 'agile_iit') . '/includes',
);
}
/**
* Implements hook_menu().
*/
function agile_iit_menu() {
return array(
'admin/config/media/iit' => array(
'title' => 'Image Investigation Toolkit',
'description' => "Configure settings associated with IIT.",
'page callback' => 'drupal_get_form',
'page arguments' => array('iit_admin'),
'file' => 'includes/admin.form.inc',
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
),
'agile/iit/crop' => array(
'file' => 'includes/callbacks.inc',
'page callback' => 'agile_iit_crop_callback',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
),
'agile/iit/croptool' => array(
'file' => 'includes/callbacks.inc',
'page callback' => 'agile_iit_crop_tool_callback',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
),
'agile/iit/twoviews' => array(
'file' => 'includes/callbacks.inc',
'page callback' => 'agile_iit_twoviews',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
),
'agile/iit/imagecropper' => array(
'file' => 'includes/callbacks.inc',
'page callback' => 'agile_iit_image_cropper',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
),
'agile/iit/imagederivative' => array(
'file' => 'includes/callbacks.inc',
'page callback' => 'agile_iit_image_derivative',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
),
'agile/iit/savecomparison' => array(
'page callback' => 'drupal_get_form',
'page arguments' => array('agile_iit_save_comparison_form'),
'access callback' => 'user_access',
'access arguments' => array('create iit_detail_comparison content'),
),
'agile/iit/image_dimensions' => array(
'file' => 'includes/callbacks.inc',
'page callback' => 'agile_iit_get_image_dimensions',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
),
'agile/iit/help' => array(
'file' => 'includes/callbacks.inc',
'page callback' => 'agile_iit_get_help_node',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
),
);
}
/**
* Implements hook_block_info().
*/
function agile_iit_block_info() {
$blocks['iit_content'] = array(
'info' => t('IIT Comparison Tool'),
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function agile_iit_block_view($delta = '') {
module_load_include('inc', 'agile_iit', 'includes/utilities');
$has_content = FALSE;
switch ($delta) {
case 'iit_content':
$block['subject'] = t('Image Investigation Workspace');
$block['content'] = theme('agile_iit', array());
$has_content = TRUE;
break;
}
if ($has_content) {
return $block;
}
}
/**
* Implements hook_theme().
*
* Defines the theming capabilities provided by this module.
*/
function agile_iit_theme() {
return array(
'agile_iit' => array(
'file' => 'theme/theme.inc',
'template' => 'theme/agile_iit',
),
'agile_iit_crop' => array(
'file' => 'theme/theme.inc',
'template' => 'theme/agile_iit_crop',
'variables' => array(
'img1_src' => NULL,
'img2_src' => NULL,
),
),
'agile_iit_twoviews' => array(
'file' => 'theme/theme.inc',
'template' => 'theme/agile_iit_twoviews',
'variables' => array(
'img1_src' => NULL,
'img2_src' => NULL,
),
),
'agile_iit_croptool' => array(
'file' => 'theme/theme.inc',
'template' => 'theme/agile_iit_croptool',
'variables' => array(
'n' => NULL,
'image_url' => NULL,
'ratio_width' => NULL,
'ratio_height' => NULL,
),
),
);
}
/*
* Implements hook_theme_registry_alter().
* Adds the custom template in the templates folder.
*/
function agile_iit_theme_registry_alter(&$theme_registry) {
// Defined path to the current module.
$module_path = drupal_get_path('module', 'agile_iit');
// Find all .tpl.php files in this module's folder recursively.
$template_file_objects = drupal_find_theme_templates($theme_registry, '.tpl.php', $module_path . '/templates');
// Iterate through all found template file objects.
foreach ($template_file_objects as $key => $template_file_object) {
// If the template has not already been overridden by a theme.
if (!isset($theme_registry[$key]['theme path']) || !preg_match('#/themes/#', $theme_registry[$key]['theme path'])) {
// Alter the theme path and template elements.
$theme_registry[$key]['theme path'] = $module_path;
$theme_registry[$key] = array_merge($theme_registry[$key], $template_file_object);
$theme_registry[$key]['type'] = 'module';
}
}
}
/**
* Implements hook_image_effect_info().
*/
function agile_iit_image_effect_info() {
$effects = array();
$effects['scale_from_data'] = array(
'label' => t('Scale with field data'),
'help' => t('Resize an image using data in the host entity, making a scaled-to-life representation.'),
'effect callback' => 'agile_iit_resize_effect',
'form callback' => 'agile_iit_resize_form',
'summary theme' => 'agile_iit_resize_summary',
);
return $effects;
}
function agile_iit_resize_form($data) {
module_load_include('inc', 'agile_iit', 'includes/utilities');
$form = array();
$fields = agile_iit_get_number_fields();
if (isset($data['host_entity'])) {
if (array_key_exists($data['host_entity'], field_info_bundles('node'))) {
$fields = agile_iit_get_number_fields($data['host_entity']);
}
}
$types = agile_iit_get_candidates();
$form['host_entity'] = array(
// select, with options from existing content types.
'#type' => 'select',
'#options' => $types,
'#title' => 'Host entity type',
'#description' => t('Only entity types with number fields are shown.'),
'#required' => TRUE,
'#default_value' => isset($data['host_entity']) ? $data['host_entity'] : '',
'#ajax' => array(
'wrapper' => 'width-field',
'callback' => 'agile_iit_refresh_fields_callback',
'method' => 'replace',
'effect' => 'fade',
),
);
$default_width_field = NULL;
$default_height_field = NULL;
$default_diameter_field = NULL;
foreach ($fields as $key => $value) {
$test_string = strtolower($value);
if (strpos($test_string, 'width') !== FALSE) {
$default_width_field = $key;
}
if (strpos($test_string, 'height') !== FALSE) {
$default_height_field = $key;
}
if (strpos($test_string, 'diameter') !== FALSE) {
$default_diameter_field = $key;
}
}
$form['dimensions'] = array(
'#title' => t('Physical dimensions of original object'),
'#type' => 'fieldset',
);
$form['dimensions']['width_field'] = array(
// select, with fields from fields in host_entity.
'#type' => 'select',
'#options' => $fields,
'#title' => t('Width field'),
'#prefix' => '<div id="width-field">',
'#suffix' => '</div>',
'#default_value' => isset($data['width_field']) ? $data['width_field'] : $default_width_field,
);
$form['dimensions']['height_field'] = array(
// select, with fields from fields in host_entity.
'#type' => 'select',
'#options' => $fields,
'#title' => t('Height field'),
'#prefix' => '<div id="height-field">',
'#suffix' => '</div>',
'#default_value' => isset($data['height_field']) ? $data['height_field'] : $default_height_field,
);
$form['dimensions']['diameter_field'] = array(
// select, with fields from fields in host_entity.
'#type' => 'select',
'#options' => $fields,
'#title' => t('Diameter field'),
'#prefix' => '<div id="diameter-field">',
'#suffix' => '</div>',
'#default_value' => isset($data['diameter_field']) ? $data['diameter_field'] : $default_diameter_field,
);
$form['default_width'] = array(
// select, with fields from host_entity.
'#type' => 'textfield',
'#title' => t('Default width'),
'#default_value' => isset($data['default_width']) ? $data['default_width'] : '',
'#description' => 'If the value in the field above is not available, assume the image has this actual width.',
'#field_suffix' => ' ' . t('cm'),
'#required' => TRUE,
'#size' => 10,
'#allow_negative' => FALSE,
);
$form['scale_factor'] = array(
// select, with fields from host_entity.
'#type' => 'textfield',
'#title' => t('Scaling Factor'),
'#default_value' => isset($data['scale_factor']) ? $data['scale_factor'] : '',
'#field_suffix' => ' ' . t('pixels per cm'),
'#required' => TRUE,
'#size' => 10,
'#allow_negative' => FALSE,
);
return $form;
// $data is empty.
}
/**
* Definition of custom resizing effect.
*/
function agile_iit_resize_effect(&$image, $data) {
module_load_include('inc', 'agile_iit', 'includes/utilities');
$file = file_uri_to_object($image->source);
$use_default = TRUE;
$parent_nid = NULL;
if ($file && isset($file->fid)) {
$objects = file_usage_list($file);
if (isset($objects['file']) and isset($objects['file']['node'])) {
foreach ($objects['file']['node'] as $nid => $count) {
if ($parent_nid === NULL) {
if (agile_iit_has_number_type($nid)) {
$parent_nid = $nid;
}
}
elseif ($nid < $parent_nid) {
$parent_nid = $nid;
}
}
}
}
$physical_width = NULL;
$physical_height = NULL;
if ($parent_nid) {
$parent_node = node_load($parent_nid);
if ($parent_node) {
$wrapper = entity_metadata_wrapper('node', $parent_node);
}
if ($wrapper && property_exists($parent_node, $data['dimensions']['width_field'])) {
$test_width = $wrapper->{$data['dimensions']['width_field']}->value();
if ($test_width) {
$physical_width = $test_width;
$use_default = FALSE;
}
}
if ($wrapper && property_exists($parent_node, $data['dimensions']['height_field'])) {
$test_height = $wrapper->{$data['dimensions']['height_field']}->value();
if ($test_height) {
$physical_height = $test_height;
$use_default = FALSE;
}
}
if ($wrapper && $use_default && property_exists($parent_node, $data['dimensions']['diameter_field'])) {
$physical_height = $wrapper->{$data['dimensions']['diameter_field']}->value();
$physical_width = $physical_height;
}
}
if ($physical_width) {
$scaled_width = $physical_width * $data['scale_factor'];
}
if ($physical_height) {
$scaled_height = $physical_height * $data['scale_factor'];
}
// Compute missing dimension, if possible.
if (!$physical_width && $scaled_height) {
$scaled_width = $image->info['width'] / $image->info['height'] * $scaled_height;
}
if (!$physical_height && $scaled_width) {
$scaled_height = $image->info['height'] / $image->info['width'] * $scaled_width;
}
// USe defaults if no dimensions are available.
if (!$physical_height && !$physical_width) {
$scaled_width = $data['dimensions']['default_width'] * $data['scale_factor'];
$scaled_height = $scaled_width * $image->info['height'] / $image->info['width'];
}
if (!image_resize($image, $scaled_width, $scaled_height)) {
watchdog('image', 'Image resize failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit, '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR);
return FALSE;
}
return TRUE;
}
/**
* Returns HTML for a summary of an image resize effect.
*
* @param $variables
* An associative array containing:
* - data: The current configuration for this resize effect.
*
* @ingroup themeable
*/
function theme_agile_iit_resize_summary($variables) {
// This is copied verbatim from the vanilla resize. Fixme when we know what should be here.
$data = $variables['data'];
if ($data['width'] && $data['height']) {
return check_plain($data['width']) . 'x' . check_plain($data['height']);
}
else {
return ($data['width']) ? t('width @width', array('@width' => $data['width'])) : t('height @height', array('@height' => $data['height']));
}
}
/**
* AJAX Callback.
*
* @param array $form
* Drupal form
* @param array $form_state
* Drupal form state
*
* @return string
* Width field
*/
function agile_iit_refresh_fields_callback($form, &$form_state) {
$host_entity_type = !empty($form_state['values']['data']['host_entity']) ? $form_state['values']['data']['host_entity'] : NULL;
$fields_available = agile_iit_get_number_fields($host_entity_type);
$form['data']['width_field']['#options'] = $fields_available;
return $form['data']['width_field'];
}
/**
* Implements hook_preprocess_page().
*/
function agile_iit_preprocess_page(&$vars, $hook) {
if (current_path() == 'selected') {
drupal_add_js(drupal_get_path('module', 'agile_iit') . "/js/jquery.js");
drupal_add_js(drupal_get_path('module', 'agile_iit') . "/js/jquery-ui.js");
drupal_add_js(drupal_get_path('module', 'agile_iit') . "/js/iit-gallery-view.js");
}
}
/**
* Implements hook_node_info().
*/
function agile_iit_node_info() {
return array(
'iit_detail_comparison' => array(
'name' => t('IIT Detail Comparison'),
'base' => 'iit_detail_comparison',
'description' => t('A detail comparison.'),
'has_title' => TRUE,
'title_label' => t('Title'),
),
);
}
/**
* Implement hook_form()
*/
function iit_detail_comparison_form($node, $form_state) {
return node_content_form($node, $form_state);
}
/**
* Form for saving the pair of extracted details
*/
function agile_iit_save_comparison_form($form, &$form_state, $node1 = NULL, $node2 = NULL, $detail1 = NULL, $detail2 = NULL, $section1 = NULL, $section2 = NULL) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => t('Title:'),
'#size' => 60,
);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Comments:'),
'#rows' => 3,
);
$form['node1'] = array(
'#type' => 'value',
'#value' => $node1->nid,
);
$form['node2'] = array(
'#type' => 'value',
'#value' => $node2->nid,
);
$form['detail1'] = array(
'#type' => 'value',
'#value' => $detail1->source,
);
$form['detail2'] = array(
'#type' => 'value',
'#value' => $detail2->source,
);
$form['section1'] = array(
'#type' => 'value',
'#value' => serialize($section1),
);
$form['section2'] = array(
'#type' => 'value',
'#value' => serialize($section2),
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('submit'),
);
$form['actions']['submit']["#submit"][] = "agile_iit_save_comparison_form_submit";
$form['#cache'] = TRUE;
$form_state['cache'] = TRUE;
return $form;
}
function agile_iit_save_comparison_form_submit($form, &$form_state) {
global $user;
$values = array(
'type' => 'iit_detail_comparison',
'uid' => $user->uid,
'status' => 1,
'comment' => 0,
'promote' => 0,
);
$entity = entity_create('node', $values);
$ewrapper = entity_metadata_wrapper('node', $entity);
$ewrapper->title->set($form_state['values']['title']);
$ewrapper->iit_comments->set($form_state['values']['description']);
$ewrapper->iit_nid_1->set(intval($form_state['values']['node1']));
$ewrapper->iit_nid_2->set(intval($form_state['values']['node2']));
// Set images from detail uri
$image1_path = $form_state['values']['detail1'];
$file = file_save_data(file_get_contents($image1_path), $image1_path, FILE_EXISTS_REPLACE);
$ewrapper->iit_detail_1->file->set($file);
$image2_path = $form_state['values']['detail2'];
$file2 = file_save_data(file_get_contents($image2_path), $image2_path, FILE_EXISTS_REPLACE);
$ewrapper->iit_detail_2->file->set($file2);
$ewrapper->iit_crop_data_1->set($form_state['values']['section1']);
$ewrapper->iit_crop_data_2->set($form_state['values']['section2']);
$ewrapper->save();
// Flag this node.
$nid = $ewrapper->getIdentifier();
$flag = flag_get_flag('saved_comparisons');
if ($flag) {
$flag->flag('flag', $nid);
}
drupal_goto('node/' . $nid);
}
/**
* Implements hook_entity_info_alter().
*/
function agile_iit_entity_info_alter(&$entity_info) {
$entity_info['node']['view modes']['iit_crop_info'] = array(
'label' => t('IIT Crop Info'),
'custom settings' => TRUE,
);
$entity_info['node']['view modes']['iit_detail_comparison'] = array(
'label' => t('IIT Detail Comparison'),
'custom settings' => TRUE,
);
}