SHARE
TWEET

CommonPlaces - Box View API Code Snippets

cpdan Feb 6th, 2014 188 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # Code samples from http://www.commonplaces.com/blog/embedding-documents-using-html5
  3. #
  4. <?php
  5.  
  6. require 'lib/box-view-api.php';
  7. require 'lib/box-view-document.php';
  8.  
  9. $box = new Box_View_API('YOUR_API_KEY');
  10.  
  11. $doc = new Box_View_Document();
  12. $doc->name = 'My Awesome Document';
  13. $doc->file_url = 'http://my-public-url';
  14.  
  15. $doc->file_path = '/path/to/file.docx';
  16.  
  17. try {
  18.   $box->upload($doc);
  19. }
  20. catch(Exception $e) {
  21.   // Log this error.
  22. }
  23.  
  24. $box->view($doc); // Generates a viewing session.
  25.  
  26. ?>
  27.  
  28. <iframe src="<?= $doc->session->url ?>"></iframe>
  29.  
  30. <?php
  31.  
  32. $box->getOriginal($doc); // Will fetch the contents of the original file.
  33. $box->delete($doc); // Delete the document.
  34.  
  35. function CUSTOM_box_view_node_presave($node) { ... }
  36.  
  37. function CUSTOM_box_view_node_presave($node) {
  38.   if ($fields = field_get_items('node', $node, 'field_document')) {
  39.     // There is a document attached.
  40.     $field = $fields[0];
  41.     if ($file = file_load($field['fid'])) {
  42.       // Upload file if it isn't already uploaded.
  43.       $file_path = drupal_realpath($file->uri);
  44.       // Check to see if this document is already in the system.
  45.       $doc = box_view_get_document(array('path' => $file_path))->document;
  46.       if (!$doc->id && $file_path) {
  47.         // This document doesn't exist yet, need to upload.
  48.         $doc = box_view_document(array(
  49.           'file_path' => $file_path,
  50.         ));
  51.         // Upload this document.
  52.         if (box_view_api_upload($doc)) {
  53.           drupal_set_message('Document is being rendered, and will be available to preview shortly.', 'status');
  54.         }
  55.       }
  56.     }
  57.   }
  58. }
  59.  
  60. function CUSTOM_preprocess_node(&$vars) {
  61.   if ($fields = field_get_items('node', $vars['node'], 'field_document')) {
  62.     $field = $fields[0];
  63.     $file = file_load($field->fid);
  64.     $doc = box_view_get_document(array('path' => drupal_realpath($file->uri)));
  65.     box_view_api_view($doc); // Retrieves a cached viewing session, or creates a new one if need be.
  66.     // Add the document to the page template.
  67.     $vars['box_view_doc'] = $doc;
  68.     // Session URL has been cached, and set. We can now do:
  69.     $vars['box_view_doc_preview'] = '<iframe src ="' . $doc->session->url . '"></iframe >';
  70.   }
  71. }
  72.  
  73. ?>
  74.  
  75. <?php print $box_view_doc_preview ?>
  76.  
  77. <?php print l(t('View'), 'box_view/view/' . $doc->id); ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top