Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Code samples from http://www.commonplaces.com/blog/embedding-documents-using-html5
- #
- <?php
- require 'lib/box-view-api.php';
- require 'lib/box-view-document.php';
- $box = new Box_View_API('YOUR_API_KEY');
- $doc = new Box_View_Document();
- $doc->name = 'My Awesome Document';
- $doc->file_url = 'http://my-public-url';
- $doc->file_path = '/path/to/file.docx';
- try {
- $box->upload($doc);
- }
- catch(Exception $e) {
- // Log this error.
- }
- $box->view($doc); // Generates a viewing session.
- ?>
- <iframe src="<?= $doc->session->url ?>"></iframe>
- <?php
- $box->getOriginal($doc); // Will fetch the contents of the original file.
- $box->delete($doc); // Delete the document.
- function CUSTOM_box_view_node_presave($node) { ... }
- function CUSTOM_box_view_node_presave($node) {
- if ($fields = field_get_items('node', $node, 'field_document')) {
- // There is a document attached.
- $field = $fields[0];
- if ($file = file_load($field['fid'])) {
- // Upload file if it isn't already uploaded.
- $file_path = drupal_realpath($file->uri);
- // Check to see if this document is already in the system.
- $doc = box_view_get_document(array('path' => $file_path))->document;
- if (!$doc->id && $file_path) {
- // This document doesn't exist yet, need to upload.
- $doc = box_view_document(array(
- 'file_path' => $file_path,
- ));
- // Upload this document.
- if (box_view_api_upload($doc)) {
- drupal_set_message('Document is being rendered, and will be available to preview shortly.', 'status');
- }
- }
- }
- }
- }
- function CUSTOM_preprocess_node(&$vars) {
- if ($fields = field_get_items('node', $vars['node'], 'field_document')) {
- $field = $fields[0];
- $file = file_load($field->fid);
- $doc = box_view_get_document(array('path' => drupal_realpath($file->uri)));
- box_view_api_view($doc); // Retrieves a cached viewing session, or creates a new one if need be.
- // Add the document to the page template.
- $vars['box_view_doc'] = $doc;
- // Session URL has been cached, and set. We can now do:
- $vars['box_view_doc_preview'] = '<iframe src ="' . $doc->session->url . '"></iframe >';
- }
- }
- ?>
- <?php print $box_view_doc_preview ?>
- <?php print l(t('View'), 'box_view/view/' . $doc->id); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement