]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Added upload support to file embed.
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Tue, 6 Jul 2010 21:06:15 +0000 (21:06 +0000)
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Tue, 6 Jul 2010 21:06:15 +0000 (21:06 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@6646 36083f99-b078-4883-b0ff-0f9b5a30f544

mod/file/actions/upload.php
mod/file/start.php
mod/file/views/default/file/embed_upload.php [new file with mode: 0644]

index 5a22b12317b173e91c4f41fcece2d6efac8cae0c..1f12dad9a11362773fa5aa7c6c579a24b7c16666 100644 (file)
@@ -17,6 +17,8 @@
        $desc = get_input("description");
        $access_id = (int) get_input("access_id");
        $container_guid = (int) get_input('container_guid', 0);
+       $ajax = get_input('ajax', FALSE);
+       
        if ($container_guid == 0) {
                $container_guid = get_loggedin_userid();
        }
                        $_SESSION['uploadtags'] = $tags;
                        $_SESSION['uploadaccessid'] = $access_id;
                        
-                       register_error(elgg_echo('file:nofile'));
-                       forward($_SERVER['HTTP_REFERER']);
+                       $error = elgg_echo('file:nofile');
+                       
+                       if ($ajax) {
+                               echo json_encode(array(
+                                       'status' => 'error',
+                                       'message' => $error
+                               ));
+                               exit;
+                       } else {
+                               register_error($error);
+                               forward($_SERVER['HTTP_REFERER']);
+                       }
                }
                
                $file = new FilePluginFile();
        unset($_SESSION['uploadaccessid']);
        
        // handle results differently for new files and file updates
+       // ajax is only for new files from embed right now.
        if ($new_file) {
                if ($guid) {
-                       system_message(elgg_echo("file:saved"));
-                       add_to_river('river/object/file/create', 'create', get_loggedin_userid(), $file->guid);
+                       $message = elgg_echo("file:saved");
+                       if ($ajax) {
+                               echo json_encode(array(
+                                       'status' => 'success',
+                                       'message' => $message
+                               ));
+                               exit;
+                               
+                       } else {
+                               system_message($message);
+                               add_to_river('river/object/file/create', 'create', get_loggedin_userid(), $file->guid);
+                       }
                } else {
                        // failed to save file object - nothing we can do about this
-                       register_error(elgg_echo("file:uploadfailed"));
+                       $error = elgg_echo("file:uploadfailed");
+                       
+                       if ($ajax) {
+                               echo json_encode(array(
+                                       'status' => 'error',
+                                       'message' => $error
+                               ));
+                               exit;
+                               
+                       } else {
+                               register_error($error);
+                       }
                }
        
-               $container_user = get_entity($container_guid);
-               forward($CONFIG->wwwroot . "pg/file/" . $container_user->username);
+               if (!$ajax) {
+                       $container_user = get_entity($container_guid);
+                       forward($CONFIG->wwwroot . "pg/file/" . $container_user->username);
+               }
        
        } else {
                if ($guid) {
index f3da33caf53993c9d81ae36c18156b8c8c411b47..a2785386f8619cb8c9c9346f31210991916c229b 100644 (file)
@@ -65,6 +65,8 @@
                // embed support
                register_plugin_hook('embed_get_sections', 'all', 'file_embed_get_sections');
                register_plugin_hook('embed_get_items', 'file', 'file_embed_get_items');
+               register_plugin_hook('embed_get_upload_sections', 'all', 'file_embed_get_upload_sections');
+               
        }
        
        /**
         */
        function file_embed_get_sections($hook, $type, $value, $params) {
                $value['file'] = array(
-                       'name' => elgg_echo('file:files'),
+                       'name' => elgg_echo('file'),
                        'layout' => 'list',
-                       'icon_size' => 'medium',
+                       'icon_size' => 'small',
                );
        
                return $value;
                return $value;
        }
        
+       /**
+        * Register file as an embed type.
+        *
+        * @param unknown_type $hook
+        * @param unknown_type $type
+        * @param unknown_type $value
+        * @param unknown_type $params
+        */
+       function file_embed_get_upload_sections($hook, $type, $value, $params) {
+               $value['file'] = array(
+                       'name' => elgg_echo('file'),
+                       'view' => 'file/embed_upload'
+               );
+       
+               return $value;
+       }
+       
+       
        /**
         * Populates the ->getUrl() method for file objects
         *
diff --git a/mod/file/views/default/file/embed_upload.php b/mod/file/views/default/file/embed_upload.php
new file mode 100644 (file)
index 0000000..0460eb4
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+/**
+ * Files upload form for embed
+ */
+
+$access_id = get_default_access(get_loggedin_user());
+if ($categories = elgg_view('categories', $vars)) {
+       $categories = "<p>$categories</p>";
+}
+
+// recycling the upload action so some of these options are a bit weird.
+$form_body = '<p>' . elgg_view('input/file', array('internalname' => 'upload')) . '</p>';
+$form_body .= '<p>' . elgg_echo('file:title') . ": " . elgg_view("input/text", array('internalname' => 'title')) . '</p>';
+$form_body .= '<p>' . elgg_echo('file:desc') . ": " . elgg_view("input/text",array('internalname' => 'description')) . '</p>';
+$form_body .= '<p>' . elgg_echo('file:tags') . ": " . elgg_view("input/tags", array('internalname' => 'tags')) . '</p>';
+$form_body .= '<p>' . elgg_echo('access') . ": " . elgg_view('input/access', array('internalname' => 'access_id', 'value' => $access_id)) . '</p>';
+$form_body .= $categories;
+$form_body .= elgg_view('input/hidden', array('internalname' => 'ajax', 'value' => TRUE));
+$form_body .= '<p>' . elgg_view('input/submit', array('value' => elgg_echo('upload'))) . '</p>';
+$form_body .= '</div>';
+
+echo elgg_view('input/form', array(
+       'body' => $form_body,
+       'internalid' => 'file_embed_upload',
+       'action' => $vars['url'] . 'action/file/upload',
+));
+
+?>
+
+<script type="text/javascript">
+$(document).ready(function() {
+       // fire off the ajax upload
+       $('#file_embed_upload').submit(function() {
+               var options = {
+                       success: function(data) {
+                               var info = jQuery.parseJSON(data);
+
+                               if (info.status == 'success') {
+                                       $('.popup .content').load('<?php echo $vars['url'] . 'pg/embed/embed'; ?>?active_section=file');
+                               } else {
+                                       $('.popup .content').find('form').prepend('<p>' + info.message + '</p>');
+                               }
+                       }
+               };
+               $(this).ajaxSubmit(options);
+               return false;
+       });
+});
+</script>