Html::fileScript PHP Method

fileScript() static public method

fileScript : file upload script
static public fileScript ( $options = [] ) : nothing
$options array of possible options: - imagePaste boolean image paste with tinyMce - name string field name (default filename) - multiple boolean allow multiple file upload (default false) - onlyimages boolean restrict to image files (default false) - showfilecontainer string DOM ID of the container showing file uploaded: use selector to display - pasteZone string DOM ID of the paste zone - dropZone string DOM ID of the drop zone - rand string already computed rand value
return nothing (print the image paste)
    static function fileScript($options = array())
    {
        global $CFG_GLPI;
        $randupload = mt_rand();
        $p['imagePaste'] = 0;
        $p['name'] = 'filename';
        $p['multiple'] = false;
        $p['onlyimages'] = false;
        $p['showfilecontainer'] = '';
        $p['pasteZone'] = false;
        $p['dropZone'] = 'dropdoc' . $randupload;
        $p['rand'] = $randupload;
        $p['values'] = array();
        if (is_array($options) && count($options)) {
            foreach ($options as $key => $val) {
                $p[$key] = $val;
            }
        }
        $script = "fileindex" . $p['rand'] . " = 0;\n         function uploadFile" . $p['rand'] . "() {\n            \$('#fileupload" . $p['rand'] . "').fileupload({\n               //forceIframeTransport: true,\n               //replaceFileInput: false,\n               dataType: 'json',";
        if ($p['pasteZone'] != false) {
            $script .= "pasteZone : \$('#" . $p['pasteZone'] . "'),";
        } else {
            if (!$p['imagePaste']) {
                $script .= "pasteZone : false,";
            }
        }
        if ($p['dropZone'] != false) {
            $script .= "dropZone : \$('#" . $p['dropZone'] . "'),";
        } else {
            $script .= "dropZone : false,";
        }
        if ($p['onlyimages']) {
            $script .= "acceptFileTypes: '/(\\.|\\/)(gif|jpe?g|png)\$/i',";
        }
        $script .= "   progressall: function (e, data) {\n                        var progress = parseInt(data.loaded / data.total * 100, 10);\n                        \$('#progress" . $p['rand'] . "').show();\n                        \$('#progress" . $p['rand'] . " .uploadbar').css({\n                              'width':progress + '%'\n                        });\n                        \$('#progress" . $p['rand'] . " .uploadbar').text(progress + '%').show().delay(5000).fadeOut('slow');\n                  },\n               send: function (e, data) {\n                  if (1==" . ($p['imagePaste'] ? 1 : 0) . "\n                     && tinyMCE != undefined\n                     && tinyMCE.imagePaste != undefined\n                     && tinyMCE.imagePaste.pasteddata == undefined\n                     && tinyMCE.imagePaste.stockimage == undefined) {\n\n                     if (!tinyMCE.isIE) {\n                        var reader = new FileReader();\n                        reader.readAsDataURL(data.originalFiles[0]);//Convert the blob from clipboard to base64\n                        reader.onloadend = function(e){\n                           \$('#desc_paste_image').html(e.target.result);\n                           tinyMCE.imagePaste.processpaste(\$('#desc_paste_image'),\n                                                           '" . _sx('button', 'Paste image') . "',\n                                                           data.originalFiles[0]);\n                        }\n                     }\n                     return false\n                  }\n               },\n               done: function (e, data) {\n                     var filedata = data;\n                     // Load image tag, and display image uploaded\n                     \$.ajax({\n                        type: 'POST',\n                        url: '" . $CFG_GLPI['root_doc'] . "/ajax/getFileTag.php',\n                        data: {'data':data.result." . $p['name'] . "},\n                        dataType: 'JSON',\n                        success: function(tag){\n                           \$.each(filedata.result." . $p['name'] . ", function (index, file) {\n                              if (file.error == undefined) {\n\n                                 displayUploadedFile" . $p['rand'] . "(file,tag[index]);\n                                 ";
        if ($p['imagePaste']) {
            $script .= "             // Insert tag in textarea\n                                 if (tinyMCE != undefined) {\n\n                                    tinyMCE.activeEditor.execCommand('mceInsertContent', false, '<p>'+tag[index].tag+'</p>');\n\n                                    if (tinyMCE.imagePaste != undefined) {\n                                       tinyMCE.imagePaste.pasteddata = undefined;\n                                       tinyMCE.imagePaste.stockimage = undefined;\n                                    }\n                                 }\n";
        }
        $script .= "                 \$('#progress" . $p['rand'] . " .uploadbar').text('" . __('Upload successful') . "');\n\n                                 \$('#progress" . $p['rand'] . " .uploadbar').css('width', '100%');\n\n                              } else {\n\n                                 \$('#progress" . $p['rand'] . " .uploadbar').text(file.error);\n\n                                 \$('#progress" . $p['rand'] . " .uploadbar').css('width', '100%');\n\n                              }\n                           });\n                        }\n                    });\n\n               }\n            });\n         };\n\n         function displayUploadedFile" . $p['rand'] . "(file, tag){\n            var p = \$('<p/>').attr('id',file.id).html('<b>" . __('File') . " : </b>'+file.display+' <b>" . __('Tag') . " : </b>'+tag.tag+' ').appendTo('#" . $p['showfilecontainer'] . "');\n\n            var p2 = \$('<p/>').attr('id',file.id+'2').css({'display':'none'}).appendTo('#" . $p['showfilecontainer'] . "');\n\n\n            // File\n            \$('<input/>').attr('type', 'hidden').attr('name', '_" . $p['name'] . "['+fileindex" . $p['rand'] . "+']').attr('value',file.name).appendTo(p);\n\n\n            // Tag\n            \$('<input/>').attr('type', 'hidden').attr('name', '_tag_" . $p['name'] . "['+fileindex" . $p['rand'] . "+']').attr('value', tag.name).appendTo(p);\n\n\n            // Coordinates\n            if (tinyMCE != undefined\n                  && tinyMCE.imagePaste != undefined\n                  && (tinyMCE.imagePaste.imageCoordinates != undefined || tinyMCE.imagePaste.imageCoordinates != null)) {\n               \$('<input/>').attr('type', 'hidden').attr('name', '_coordinates['+fileindex" . $p['rand'] . "+']').attr('value', encodeURIComponent(JSON.stringify(tinyMCE.imagePaste.imageCoordinates))).appendTo(p2);\n               tinyMCE.imagePaste.imageCoordinates = null;\n            }\n\n            // Delete button\n            var elementsIdToRemove = {0:file.id, 1:file.id+'2'};\n            \$('<img src=\"" . $CFG_GLPI['root_doc'] . "/pics/delete.png\" class=\"pointer\">').click(function(){\n\n               deleteImagePasted(elementsIdToRemove, tag.tag);\n\n            }).appendTo(p);\n\n            ";
        if ($p['multiple']) {
            $script .= "             fileindex" . $p['rand'] . " = fileindex" . $p['rand'] . "+1;\n";
        }
        $script .= "}\n         function deleteImagePasted(elementsIdToRemove, tagToRemove){\n\n            // Remove file display lines\n            \$.each(elementsIdToRemove, function (index, id) {\n\n                \$('#'+id).remove();\n\n            });\n\n            ";
        if ($p['imagePaste']) {
            $script .= "\n               // TINYMCE : Remove tag from textarea\n               if (tinyMCE != undefined) {\n                  tinyMCE.activeEditor.setContent(tinyMCE.activeEditor.getContent().replace('<p>'+tagToRemove+'</p>', ''));\n\n               }";
        }
        $script .= "\n            // File counter\n            if (fileindex" . $p['rand'] . " > 0) {\n\n               fileindex" . $p['rand'] . "--;\n\n            }\n         };\n";
        if (is_array($p['values']) && isset($p['values']['filename']) && is_array($p['values']['filename']) && count($p['values']['filename'])) {
            foreach ($p['values']['filename'] as $key => $name) {
                if (isset($p['values']['tag'][$key])) {
                    $file = GLPI_TMP_DIR . '/' . $p['values']['filename'][$key];
                    if (file_exists($file)) {
                        $display = sprintf('%1$s %2$s', $p['values']['filename'][$key], Toolbox::getSize(filesize($file)));
                        $script .= "var tag{$key} = {};\n                              tag{$key}.tag = '" . $p['values']['tag'][$key] . "';\n                              tag{$key}.name = '#" . $p['values']['tag'][$key] . "#';\n                              var file{$key}= {};\n                              file{$key}.name = '" . addslashes($p['values']['filename'][$key]) . "'\n                              file{$key}.display = '" . addslashes($display) . "';\n                              file{$key}.id = 'file{$key}';\n                              displayUploadedFile" . $p['rand'] . "(file{$key}, tag{$key});\n                              ";
                    }
                }
            }
        }
        return $script;
    }

Usage Example

Example #1
0
 /**
  * imagePaste : Show image paste for an item, with TinyMce
  *
  * @since version 0.85
  *
  * @param $options       array of options
  *     - name              string   field name (default filename)
  *     - multiple          boolean  allow multiple file upload (default false
  *     - onlyimages        boolean  restrict to image files (default false)
  *     - showfilecontainer string   DOM ID of the container showing file uploaded:
  *                                  use selector to display
  *     - imagePaste        boolean  image paste with tinyMce
  *     - dropZone          string   DOM ID of the drop zone
  *     - rand              string   already computed rand value
  *     - pasteZone         string   DOM ID of the paste zone
  *
  * @return nothing (print the image paste)
  **/
 static function imagePaste($options = array())
 {
     $rand = mt_rand();
     $p['name'] = 'stock_image';
     $p['multiple'] = true;
     $p['onlyimages'] = true;
     $p['showfilecontainer'] = 'fileupload_info';
     $p['imagePaste'] = 1;
     $p['dropZone'] = 'image_paste';
     $p['rand'] = $rand;
     if (is_array($options) && count($options)) {
         foreach ($options as $key => $val) {
             $p[$key] = $val;
         }
     }
     echo '<script type="text/javascript">';
     echo Html::fileScript($p);
     echo '</script>';
     echo "<div class='fileupload' id='" . $p['dropZone'] . "'></div>\n";
 }
Html