summaryrefslogtreecommitdiff
path: root/attachment_uploader.php
blob: 8a1e3a4b39610230fdc72ae741862a6423b8eaad (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
<?php
/**
 * @version $Header: /cvsroot/bitweaver/_bit_liberty/attachment_uploader.php,v 1.10 2007/10/08 13:03:45 nickpalmer Exp $
 * @package liberty
 * @subpackage functions
 */

/**
 * required setup
 */
require_once( '../bit_setup_inc.php' );
require_once( LIBERTY_PKG_PATH."LibertyAttachable.php" );
global $gBitSmarty, $gContent;

$gContent = new LibertyAttachable();

// Handle delete.
require_once( LIBERTY_PKG_PATH."edit_storage_inc.php" );

// make a copy of $_REQUEST that we can mess with it without interfering with the rest of the page
$storeHash = $_REQUEST;

// Take the one we are going to delete out of existing.
//if( !empty($_REQUEST['deleteAttachment'] ) ) {
//	unset($storeHash['STORAGE']['existing'][$_REQUEST['deleteAttachment']]);
//}

// Do we have an attachment to save?
if( !empty($_FILES['upload']) ) {

	// Do we have a content_id already or is this a preflight?
	if( !empty( $storeHash['liberty_attachments']['content_id'] ) ) {

		// Get the content_id into the right places.
		$gContent->mContentId = $storeHash['content_id'] = $storeHash['liberty_attachments']['content_id'];
	}
	else {
		$storeHash['content_id'] = $gContent->mContentId = NULL;
	}

	$storeHash['skip_content_store'] = true;

	// store the attachment.
	if ( !$gContent->store( $storeHash ) ) {
		$gBitSmarty->assign('errors', $gContent->mErrors);
		// If this is the first set it to be primary
		if ( empty( $storeHash['STORAGE']['existing'] ) ) {
			foreach ( $storeHash['STORAGE'] as $id => $file ) {
				if ( $id != 'existing' ) {
					foreach ( $file as $key => $data ) {
						$gContent->setPrimaryAttachment( $key );
						$done = true;
						break;
					}
				}
				if ( $done ) {
					break;
				}
			}
		}
	}
	else {
		// Load up the new attachment.
		if ( !empty($storeHash['STORAGE'] ) ) {
			foreach ( $storeHash['STORAGE'] as $id => $file ) {
				if ( $id != 'existing' ) {
					foreach ( $file as $key => $data ) {
						$gContent->mStorage[$data['attachment_id']] = $gContent->getAttachment( $data['attachment_id'] );
					}
				}
			}
		}
	}
}
else {
	$gBitSmarty->assign( 'errors', tra( 'There was an unknown error with the upload.' ) );
}

if ( empty($gContent->mContentId) ) {
	// Setup the existing_attachment_id stuff
	if( !empty( $storeHash['STORAGE']['existing'] ) ) {
		// Fake it for preflight
		foreach( $storeHash['STORAGE']['existing'] as $id ) {
			if( !empty( $id ) ) {
				$gContent->mStorage[$id] = $gContent->getAttachment( $id );
			}
		}
	}
}
else {
	$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 );
echo $gBitSystem->display( 'bitpackage:liberty/attachment_uploader.tpl', NULL, 'none' );
?>