summaryrefslogtreecommitdiff
path: root/attachment_uploader.php
blob: bcc344693e0373307d9918926f0c4cc07cd23c13 (plain)
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
<?php
/**
 * @version $Header$
 * @package liberty
 * @subpackage functions
 */

/**
 * required setup
 */
use Bitweaver\KernelTools;
require_once '../kernel/includes/setup_inc.php';
global $gBitSmarty, $gContent;

$error = null;
if ( !isset($_FILES['upload'] ) ) {
	$error = KernelTools::tra( "No upload submitted." );
}elseif( !empty( $_REQUEST['liberty_attachments']['content_id'] )) {
// if we have a content id then we just load up that
	if( !($gContent = LibertyBase::getLibertyObject( $_REQUEST['liberty_attachments']['content_id'] )) ) {
		// if there is something wrong with the content id spit back an error
		$error = KernelTools::tra( "You are attempting to upload a file to a content item that does not exist." );
	}
}elseif( isset ( $_REQUEST['liberty_attachments']['content_type_guid'] ) ){
/* if we don't have a content id then we assume this is new content and we need to create a draft.
 * we'll pass a new content_id back to the edit form so it can make the right association later on save.
 */
	// if we are creating new content the status must be enforced, so status recognition must be enabled
	if( !$gBitSystem->isFeatureActive( "liberty_display_status" ) ){
		$error = KernelTools::tra( "You must save the content to upload an attachment." );
	}elseif( !isset( $gLibertySystem->mContentTypes[$_REQUEST['liberty_attachments']['content_type_guid']] ) ){
		$error = KernelTools::tra( "You are attempting to upload a file to an invalid content type" );
	}else{
		// load up the requested content type handler class
		$contentType = $_REQUEST['liberty_attachments']['content_type_guid'];
		$contentTypeHash = $gLibertySystem->mContentTypes[$contentType];
		$class =  $contentTypeHash['handler_class'];
		$classFile =  $contentTypeHash['handler_file'];
		$package = $contentTypeHash['handler_package'];
		$pathVar = strtoupper($package).'_PKG_PATH';

		if( !defined( $pathVar ) ) {
			$error = KernelTools::tra( "Undefined handler package path" );
		}else{
			require_once( constant( $pathVar ).$classFile );
			$gContent = new $class();
		}
	}
}else{
// if we don't have a valid content_id or content_type_guid we can't do nothing for you
	$error = "You have not specified a content item or content type to associate this upload with";
}

if( isset( $gContent ) ){
	$storeHash = $_REQUEST['liberty_attachments'];
	// Keep a copy in the right place as well so primary works.
	$storeHash['liberty_attachments'] = $_REQUEST['liberty_attachments'];

	if ( !$gContent->isValid() ){
		// if we dont have a content object for this attachment yet, lets create a draft.
		$storeHash['content_status_id'] = -5;
	}else{
		// else we'll skip storing the content
		$storeHash['skip_content_store'] = true;
	}

	// store the attachment.
	if ( !$gContent->store( $storeHash ) ) {
		$error = $gContent->mErrors;
	}else{
		// Load up the new attachment.
		if ( !empty($storeHash['upload_store'] ) ) {
			foreach ( $storeHash['upload_store'] as $id => $file ) {
				if ( $id != 'existing' ) {
					foreach ( $file as $key => $data ) {
						$gContent->mStorage[$data['attachment_id']] = $gContent->getAttachment( $data['attachment_id'] );
					}
				}
			}
		}
	}
}

if ( !is_null( $error ) ){
	if ( is_array( $error ) ){
		$error = implode("\n", $error);
	}
	$gBitSmarty->assign('errors', $error);
}else{
	// @Todo is this stuff necessary?
	$gContent->load();
	// Make them come out in the right order
	if( !empty( $gContent->mStorage ) ) {
		ksort( $gContent->mStorage );
	}
}

$gBitSmarty->assign( 'gContent', $gContent );
$gBitSmarty->assign( 'libertyUploader', true );
$gBitSmarty->assign( 'uploadTab', true );

if( !empty( $_REQUEST['liberty_attachments']['primary_label'] ) ) {
	$gBitSmarty->assign('primary_label', $_REQUEST['liberty_attachments']['primary_label']);
}

if( isset( $_REQUEST['liberty_attachments']['form_id'] ) ){
	$gBitSmarty->assign( 'form_id', $_REQUEST['liberty_attachments']['form_id'] );
}

echo $gBitSystem->display( 'bitpackage:liberty/attachment_uploader.tpl', null, [ 'format'=>'none', 'display_mode' => 'display' ]);
?>