summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Palmer <nick@sluggardy.net>2007-09-23 18:17:22 +0000
committerNick Palmer <nick@sluggardy.net>2007-09-23 18:17:22 +0000
commit7c86e056d8231ee4a9777fa59e53d6d4d3b07187 (patch)
treeb2fc3e6c1bccbff36d2cc406dff002181a94b9e1
parent4e6433c37c988f7b445cdc844af8092cb407525f (diff)
downloadliberty-7c86e056d8231ee4a9777fa59e53d6d4d3b07187.tar.gz
liberty-7c86e056d8231ee4a9777fa59e53d6d4d3b07187.tar.bz2
liberty-7c86e056d8231ee4a9777fa59e53d6d4d3b07187.zip
Changes to get ajax attachments working again. You must turn on the new storage.existing plugin to get them to work.
-rw-r--r--LibertyAttachable.php23
-rw-r--r--admin/schema_inc.php2
-rw-r--r--admin/upgrade_inc.php2
-rw-r--r--attachment_uploader.php74
-rw-r--r--plugins/storage.existing.php72
-rw-r--r--templates/attachment_uploader.tpl4
6 files changed, 134 insertions, 43 deletions
diff --git a/LibertyAttachable.php b/LibertyAttachable.php
index 0e120cd..73aa1d6 100644
--- a/LibertyAttachable.php
+++ b/LibertyAttachable.php
@@ -3,7 +3,7 @@
* Management of Liberty Content
*
* @package liberty
- * @version $Header: /cvsroot/bitweaver/_bit_liberty/LibertyAttachable.php,v 1.132 2007/09/21 03:51:52 spiderr Exp $
+ * @version $Header: /cvsroot/bitweaver/_bit_liberty/LibertyAttachable.php,v 1.133 2007/09/23 18:17:22 nickpalmer Exp $
* @author spider <spider@steelsun.com>
*/
// +----------------------------------------------------------------------+
@@ -301,9 +301,9 @@ class LibertyAttachable extends LibertyContent {
// Things to be stored should be shoved in the array $pParamHash['STORAGE']
function store( &$pParamHash ) {
- global $gLibertySystem, $gBitSystem;
+ global $gLibertySystem, $gBitSystem, $gBitUser;
$this->mDb->StartTrans();
- if( LibertyAttachable::verify( $pParamHash ) && LibertyContent::store( $pParamHash )) {
+ if( LibertyAttachable::verify( $pParamHash ) && ( isset($pParamHash['skip_content_store']) || LibertyContent::store( $pParamHash ) ) ) {
if(!empty( $pParamHash['STORAGE'] ) && count( $pParamHash['STORAGE'] ) ) {
foreach( array_keys( $pParamHash['STORAGE'] ) as $guid ) {
$storeRows = &$pParamHash['STORAGE'][$guid]; // short hand variable assignment
@@ -314,16 +314,13 @@ class LibertyAttachable extends LibertyContent {
foreach( $storeRows as $key => $value ) {
$storeRow = &$pParamHash['STORAGE'][$guid][$key];
$storeRow['plugin_guid'] = $guid;
+
if (empty($pParamHash['content_id'])) {
- $storeRow['content_id'] = $gBitUser->mContentId;
+ $storeRow['content_id'] = NULL;
} else {
$storeRow['content_id'] = $pParamHash['content_id']; // copy in content_id
}
- if (!empty($pParamHash['content_id'])) {
- $storeRow['content_id'] = $pParamHash['content_id']; // copy in content_id
- }
-
if (!empty($pParamHash['user_id'])) {
$storeRow['user_id'] = $pParamHash['user_id']; // copy in the user_id
} else {
@@ -336,7 +333,7 @@ class LibertyAttachable extends LibertyContent {
// For backwards compatibility with a single upload.
if( @BitBase::verifyId( $pParamHash['attachment_id'] )) {
$storeRow['upload']['attachment_id'] = $storeRow['attachment_id'] = $pParamHash['attachment_id'];
- } else {
+ } else if ( !isset($storeRow['skip_insert'] ) ) {
if ( defined( 'LINKED_ATTACHMENTS' ) && @BitBase::verifyId( $pParamHash['content_id'] ) ) {
$storeRow['upload']['attachment_id'] = $storeRow['attachment_id'] = $pParamHash['content_id'];
} else {
@@ -346,7 +343,7 @@ class LibertyAttachable extends LibertyContent {
}
// if we have uploaded a file, we can take care of that generically
- if( is_array( $storeRow['upload'] ) && !empty( $storeRow['upload']['size'] ) ) {
+ if( !empty( $storeRow['upload'] ) && is_array( $storeRow['upload'] ) && !empty( $storeRow['upload']['size'] ) ) {
if( empty( $storeRow['upload']['type'] ) ) {
$ext = substr( $storeRow['upload']['name'], strrpos( $storeRow['upload']['name'], '.' ) + 1 );
$storeRow['upload']['type'] = $gBitSystem->lookupMimeType( $ext );
@@ -370,11 +367,7 @@ class LibertyAttachable extends LibertyContent {
$this->mStorage = $storeFunc( $storeRow );
}
- if( @BitBase::verifyId( $pParamHash['attachment_id'] ) ) {
- // we were passed in an attachment, assume an update to an existing row
- $sql = "UPDATE `".BIT_DB_PREFIX."liberty_attachments` SET `content_id`=?, `attachment_plugin_guid`=?, `foreign_id`=?, `user_id`=? WHERE `attachment_id`=?";
- $rs = $this->mDb->query( $sql, array( $storeRow['content_id'], $storeRow['plugin_guid'], (int)$storeRow['foreign_id'], $storeRow['user_id'], $pParamHash['attachment_id'] ) );
- } elseif( @BitBase::verifyId( $storeRow['attachment_id'] ) ) {
+ if( @BitBase::verifyId( $storeRow['attachment_id'] ) && !isset( $storeRow['skip_insert'] ) ) {
$sql = "INSERT INTO `".BIT_DB_PREFIX."liberty_attachments` ( `content_id`, `attachment_id`, `attachment_plugin_guid`, `foreign_id`, `user_id` ) VALUES ( ?, ?, ?, ?, ? )";
$rs = $this->mDb->query( $sql, array( $storeRow['content_id'], $storeRow['attachment_id'], $storeRow['plugin_guid'], (int)$storeRow['foreign_id'], $storeRow['user_id'] ) );
}
diff --git a/admin/schema_inc.php b/admin/schema_inc.php
index 206c9b0..0508e9c 100644
--- a/admin/schema_inc.php
+++ b/admin/schema_inc.php
@@ -123,7 +123,7 @@ $tables = array(
'liberty_attachments' => "
attachment_id I4 PRIMARY,
- content_id I4 NOTNULL,
+ content_id I4,
attachment_plugin_guid C(16) NOTNULL,
foreign_id I4 NOTNULL,
user_id I4 NOTNULL,
diff --git a/admin/upgrade_inc.php b/admin/upgrade_inc.php
index 99db899..17a58f9 100644
--- a/admin/upgrade_inc.php
+++ b/admin/upgrade_inc.php
@@ -662,7 +662,7 @@ array( 'QUERY' =>
"UPDATE `".BIT_DB_PREFIX."liberty_attachments` la INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( lc.`primary_attachment_id` = la.`attachment_id` ) SET `is_primary` = 'y'",
// now we do the generic update
- "UPDATE `".BIT_DB_PREFIX."liberty_attachments` la SET `content_id` = ( SELECT `content_id` FROM `".BIT_DB_PREFIX."liberty_attachments_map` WHERE `attachment_id` = la.`attachment_id` LIMIT 1 ) WHERE la.`content_id` <> NULL;",
+ "UPDATE `".BIT_DB_PREFIX."liberty_attachments` la SET `content_id` = ( SELECT `content_id` FROM `".BIT_DB_PREFIX."liberty_attachments_map` WHERE `attachment_id` = la.`attachment_id` LIMIT 1 ) WHERE la.`content_id` <> NULL",
)),
),
diff --git a/attachment_uploader.php b/attachment_uploader.php
index c700051..f07f8d2 100644
--- a/attachment_uploader.php
+++ b/attachment_uploader.php
@@ -1,6 +1,6 @@
<?php
/**
- * @version $Header: /cvsroot/bitweaver/_bit_liberty/attachment_uploader.php,v 1.7 2007/09/10 19:26:09 squareing Exp $
+ * @version $Header: /cvsroot/bitweaver/_bit_liberty/attachment_uploader.php,v 1.8 2007/09/23 18:17:22 nickpalmer Exp $
* @package liberty
* @subpackage functions
*/
@@ -16,38 +16,64 @@ $gContent = new LibertyAttachable();
// make a copy of $_REQUEST that we can mess with it without interfering with the rest of the page
$storeHash = $_REQUEST;
-if( !empty( $storeHash['liberty_attachments']['content_id'] )) {
- $gContent->mContentId = $storeHash['liberty_attachments']['content_id'];
- $storeHash['content_id'] = $storeHash['liberty_attachments']['content_id'];
-}
-if( isset( $_FILES['upload'] )) {
- if( !$gContent->storeAttachments( $storeHash, FALSE )) {
+// 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);
- } elseif( empty( $gContent->mContentId )) {
- // Fake it for preflight
- $gContent->mStorage[$storeHash['attachment_id']] = $gContent->getAttachment( $storeHash['attachment_id'] );
}
-}
+ 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']);
+ }
+ }
+ }
+ }
+ }
-// load the attachments up
-if( !empty( $gContent->mContentId )) {
- $gContent->load();
-} elseif( !empty( $storeHash['existing_attachment_id'] )) {
- // Fake it for preflight
- foreach( $storeHash['existing_attachment_id'] as $id ) {
- if( !empty( $id )) {
- $gContent->mStorage[$id] = $gContent->getAttachment( $id );
+ 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 );
+ // Make them come out in the right order
+ if( !empty( $gContent->mStorage )) {
+ ksort( $gContent->mStorage );
+ }
+}
+else {
+ $gBitSmarty->assign('errors', tra('There was an unknown error with the upload.'));
}
$gBitSmarty->assign( 'gContent', $gContent );
$gBitSmarty->assign( 'libertyUploader', TRUE );
-echo $gBitSmarty->display( 'bitpackage:liberty/attachment_uploader.tpl' );
+echo $gBitSystem->display( 'bitpackage:liberty/attachment_uploader.tpl', NULL, 'none' );
?>
diff --git a/plugins/storage.existing.php b/plugins/storage.existing.php
new file mode 100644
index 0000000..716b9d8
--- /dev/null
+++ b/plugins/storage.existing.php
@@ -0,0 +1,72 @@
+<?php
+/**
+ * @version $Revision: 1.1 $
+ * @package liberty
+ * @subpackage plugins_storage
+ */
+global $gLibertySystem, $gBitSystem, $gBitSmarty, $gBitThemes;
+
+/**
+ * definitions
+ */
+define( 'PLUGIN_GUID_EXISTING_FILES', 'existing' );
+
+$pluginParams = array (
+ 'store_function' => 'existing_files_store',
+ 'verify_function' => 'existing_files_verify',
+ 'description' => 'Always load for handling ajax attachments',
+ 'edit_label' => 'Ajax Upload Files',
+ 'plugin_type' => STORAGE_PLUGIN,
+ 'auto_activate' => TRUE,
+);
+
+if( isset( $gBitSystem ) ) {
+ $gLibertySystem->registerPlugin( PLUGIN_GUID_EXISTING_FILES, $pluginParams );
+}
+
+function existing_files_verify( &$pStoreRow ) {
+ global $gBitUser, $gContent;
+
+ if ( @LibertyContent::verifyId( $gContent->mContentId ) ) {
+
+ // Pull all the data on the attachment in question
+ $query = "SELECT * FROM `".BIT_DB_PREFIX."liberty_attachments` a ".
+ "WHERE a.`attachment_id`=?";
+
+ if( $result = $gContent->mDb->query( $query, array( $pStoreRow ))) {
+ $pStoreRow = $result->fetchRow();
+ vd($pStoreRow);
+ // Tell LA not to do the insert.
+ $pStoreRow['skip_insert'] = TRUE;
+ $pStoreRow['plugin_guid'] = PLUGIN_GUID_EXISTING_FILES;
+
+ // Verify the user owns this attachment
+ if ( $gBitUser->isAdmin() ||
+ $pStoreRow['user_id'] == $gBitUser->mUserId ) {
+
+ // Verify that it isn't attached already
+ if ( $pStoreRow['content_id'] == NULL ) {
+ return( TRUE );
+ }
+ }
+ }
+ }
+
+ return( FALSE );
+}
+
+function existing_files_store( &$pStoreRow ) {
+ global $gBitSystem, $gContent;
+
+ if ( @LibertyContent::verifyId( $gContent->mContentId ) ) {
+ // Update the attachments content_id
+ $query = "UPDATE `".BIT_DB_PREFIX."liberty_attachments` SET ".
+ "content_id = ? WHERE attachment_id = ?";
+
+ $result = $gContent->mDb->query( $query, array( $gContent->mContentId, $pStoreRow['attachment_id'] ) );
+ }
+
+ return( TRUE );
+}
+
+?>
diff --git a/templates/attachment_uploader.tpl b/templates/attachment_uploader.tpl
index 184182f..abcbe1e 100644
--- a/templates/attachment_uploader.tpl
+++ b/templates/attachment_uploader.tpl
@@ -15,9 +15,9 @@
{include file="bitpackage:liberty/edit_storage_list.tpl"}
{if empty($gContent->mContentId)}
{foreach from=$gContent->mStorage item=storage key=attachmentId}
- <input type="hidden" name="existing_attachment_id[]" value="{$attachmentId}" />
+ <input type="hidden" name="STORAGE[existing][]" value="{$attachmentId}" />
{/foreach}
{/if}
</body>
</html>
-{/strip} \ No newline at end of file
+{/strip}