liberty
[ class tree: liberty ] [ index: liberty ] [ all elements ]

Source for file attachment_uploader.php

Documentation is available at attachment_uploader.php

  1. <?php
  2. /**
  3.  * @version $Header$
  4.  * @package liberty
  5.  * @subpackage functions
  6.  */
  7.  
  8. /**
  9.  * required setup
  10.  */
  11. require_once( '../kernel/setup_inc.php' );
  12. global $gBitSmarty, $gContent;
  13.  
  14. $error = NULL;
  15. if ( !isset($_FILES['upload'] ) ) {
  16.     $error = tra( "No upload submitted." );
  17. }elseif( !empty( $_REQUEST['liberty_attachments']['content_id'] )) {
  18. // if we have a content id then we just load up that
  19.     if( !($gContent = LibertyBase::getLibertyObject( $_REQUEST['liberty_attachments']['content_id'] )) ) {
  20.         // if there is something wrong with the content id spit back an error
  21.         $error = tra( "You are attempting to upload a file to a content item that does not exist." );
  22.     }
  23. }elseif( isset ( $_REQUEST['liberty_attachments']['content_type_guid'] ) ){
  24. /* if we don't have a content id then we assume this is new content and we need to create a draft.
  25.  * we'll pass a new content_id back to the edit form so it can make the right association later on save.
  26.  */
  27.     // if we are creating new content the status must be enforced, so status recognition must be enabled
  28.     if( !$gBitSystem->isFeatureActive( "liberty_display_status" ) ){
  29.         $error = tra( "You must save the content to upload an attachment." );
  30.     }elseif( !isset( $gLibertySystem->mContentTypes[$_REQUEST['liberty_attachments']['content_type_guid']] ) ){
  31.         $error = tra( "You are attempting to upload a file to an invalid content type" );
  32.     }else{
  33.         // load up the requested content type handler class
  34.         $contentType = $_REQUEST['liberty_attachments']['content_type_guid'];
  35.         $contentTypeHash = $gLibertySystem->mContentTypes[$contentType];
  36.         $class =  $contentTypeHash['handler_class'];
  37.         $classFile =  $contentTypeHash['handler_file'];
  38.         $package = $contentTypeHash['handler_package'];
  39.         $pathVar = strtoupper($package).'_PKG_PATH';
  40.  
  41.         if( !defined( $pathVar ) ) {
  42.             $error = tra( "Undefined handler package path" );
  43.         }else{
  44.             require_once( constant( $pathVar ).$classFile );
  45.             $gContent = new $class();
  46.         }
  47.     }
  48. }else{
  49. // if we don't have a valid content_id or content_type_guid we can't do nothing for you
  50.     $error = "You have not specified a content item or content type to associate this upload with";
  51. }
  52.  
  53. if( isset( $gContent ) ){
  54.     $storeHash = $_REQUEST['liberty_attachments'];
  55.     // Keep a copy in the right place as well so primary works.
  56.     $storeHash['liberty_attachments'] = $_REQUEST['liberty_attachments'];
  57.  
  58.     if ( !$gContent->isValid() ){
  59.         // if we dont have a content object for this attachment yet, lets create a draft.
  60.         $storeHash['content_status_id'] = -5;
  61.     }else{
  62.         // else we'll skip storing the content
  63.         $storeHash['skip_content_store'] = true;
  64.     }
  65.  
  66.     // store the attachment.
  67.     if ( !$gContent->store( $storeHash ) ) {
  68.         $error = $gContent->mErrors;
  69.     }else{
  70.         // Load up the new attachment.
  71.         if ( !empty($storeHash['upload_store'] ) ) {
  72.             foreach ( $storeHash['upload_store'] as $id => $file ) {
  73.                 if ( $id != 'existing' ) {
  74.                     foreach ( $file as $key => $data ) {
  75.                         $gContent->mStorage[$data['attachment_id']] = $gContent->getAttachment( $data['attachment_id'] );
  76.                     }
  77.                 }
  78.             }
  79.         }
  80.     }
  81. }
  82.  
  83. if ( !is_null( $error ) ){
  84.     if ( is_array( $error ) ){
  85.         $error = implode("\n", $error);
  86.     }
  87.     $gBitSmarty->assign('errors', $error);
  88. }else{
  89.     // @Todo is this stuff necessary?
  90.     $gContent->load();
  91.     // Make them come out in the right order
  92.     if( !empty( $gContent->mStorage ) ) {
  93.         ksort( $gContent->mStorage );
  94.     }
  95. }
  96.  
  97. $gBitSmarty->assign( 'gContent', $gContent );
  98. $gBitSmarty->assign( 'libertyUploader', TRUE );
  99. $gBitSmarty->assign( 'uploadTab', TRUE );
  100.  
  101. if( !empty( $_REQUEST['liberty_attachments']['primary_label'] ) ) {
  102.     $gBitSmarty->assign('primary_label', $_REQUEST['liberty_attachments']['primary_label']);
  103. }
  104.  
  105. if( isset( $_REQUEST['liberty_attachments']['form_id'] ) ){
  106.     $gBitSmarty->assign( 'form_id', $_REQUEST['liberty_attachments']['form_id'] );
  107. }
  108.  
  109. echo $gBitSystem->display( 'bitpackage:liberty/attachment_uploader.tpl', NULL, array( 'format'=>'none', 'display_mode' => 'display' ));
  110. ?>

Documentation generated on Wed, 29 Jul 2015 13:55:35 +0000 by phpDocumentor 1.5.0-lsces