-
Notifications
You must be signed in to change notification settings - Fork 2
/
crocodoc.module
124 lines (120 loc) · 5.21 KB
/
crocodoc.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
<?php
/**
* @file
* A field formatter for the File field, which renders the file as
* an embedded document, using Crocodoc file viewer.
* At the current time this viewer will reasonably display files of these
* types: Adobe .pdf, Microsoft .doc/.docx, .xls/.xlsx. and .ppt/.pptx.
*
* The field may be resized and/or otherwise modified using the css selector
* ".crocodoc"
*/
/**
* Implements hook_help().
*
* Describe the purpose and usage of this module.
*/
function crocodoc_help($path, $arg) {
$helptext = '';
if ($path == 'admin/help#crocodoc') {
$helptext = '<p>';
$helptext .= t('This module provides a new format for the File field type.' .
' This format presents the file as a fully rendered object within a web page -' .
' i.e. it displays the contents of the file as appropriate to its filetype' .
' (Adobe Acrobat .pdf, Microsoft Word .doc/.docx, Micrososft Excel .xls/.xlsx, Microsoft Powerpoint .ppt/.pptx),' .
' using the Crocodoc embedded rendering engine.</p>' .
'<p>N.B.: Only files that are public may use this formatter -' .
' Crocodoc must be able to access the file in order to ' .
' render and display it. In other words, it won\'t work on' .
' a typical development laptop, or if your server is behind' .
' a firewall where Crocodoc is unable to access it.');
$helptext .= '</p>';
$helptext .= '<p>';
$helptext .= t('To use this field format, add a File field to a new or existing content type (such as Basic Page) on the content type\'s Manage Fields form.' .
' The File field type provides only one widget type - File - so select that. On the content type\'s "Manage Display" form,' .
' there will be a drop-down select list of available display formats for the File field. To display the file within the embedded' .
' crocodoc viewer, choose the \'Crocodoc Embedded Document Viewer\' format.');
$helptext .= '</p>';
$helptext .= '<p>';
$helptext .= t('The document viewer may be styled using the CSS selector \'.crocodoc\'. By default, the viewer\'s width is 100% and its height is 400px, with a 1px black border.');
$helptext .= '</p>';
}
return $helptext;
}
/**
* Implements hook_field_formatter_info_alter().
*
* Add a new formatter to the
* list of formatters available for the File field type.
*/
function crocodoc_field_formatter_info_alter(&$info) {
$new_formatter = array(
'label' => t('Embedded Crocodoc viewer'),
'field types' => array(0 => 'file'),
'settings' => array(),
'module' => 'crocodoc',
);
$info['crocodoc_embedded_doc'] = $new_formatter;
}
/**
* Implements hook_field_formatter_view().
*
* Renders the ouput of an
* 'Embedded crocodoc viewer' formatted field within an iframe that
* pulls in the crocodocs viewer to display the file inline.
*
*/
function crocodoc_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
// This formatter outputs the field within an iframe.
case 'crocodoc_embedded_doc':
foreach ($items as $delta => $item) {
$item_uri = $item['uri'];
if (file_uri_scheme($item_uri) == 'public') {
$url = file_create_url($item_uri);
$encoded_url = urlencode($url);
$encoded_url = "http://web.crocodoc.com/files/test-document.doc";
try {
include_once ('crocodoc.php');
$croco = new Crocodoc();
$ret = $croco->upload($encoded_url);
$ret_inter = json_decode($ret, true);
if(array_key_exists('error', $ret_inter)) {
throw new Exception("Error occured while uploading. Error : ". $ret_inter["error"]);
}
$uuid = $ret_inter["uuid"];
$status = json_decode($croco->getStatus($uuid), true);
if(array_key_exists('error', $status)) {
throw new Exception("Error occured while retrieving status. Error : ". $status["error"]);
}
$viewable = $status[0]["viewable"];
$count = 0;
while((($viewable != true) && ($count<10)) ) {
$status = json_decode($croco->getStatus($uuid), true);
$viewable = $status[0]["viewable"];
$statusStr = $status[0]["status"];
if ($statusStr == "ERROR") {
throw new Exception("Error occured in document conversion");
}
sleep(2);
$count = $count+2;
}
$session = $croco->createSession($uuid);
$sessionStr = json_decode($session, true);
$url = $sessionStr["session"];
drupal_add_css(drupal_get_path('module', 'crocodoc') . '/crocodoc.css');
$element[$delta]['#markup'] = '<iframe class="crocodoc" src="https://crocodoc.com/view/' . $url . '"></iframe>';
}
catch (Exception $e) {
drupal_set_message(t($e->getMessage()), $type = 'error');
}
}
else {
drupal_set_message(t('The file (%file) is not publicly accessable. It must be publicly available in order for the crocodocs viewer to be able to access it.', array('%file' => $item['filename'])), 'error', FALSE);
}
}
break;
}
return $element;
}