統計
| ブランチ: | リビジョン:

pictcode / app / Plugin / UploadPack / View / Helper / UploadHelper.php @ f1dc2ebd

履歴 | 表示 | アノテート | ダウンロード (8.087 KB)

1
<?php
2
App::uses('UploadBehavior', 'UploadPack.Model/Behavior');
3
/**
4
 * This file is a part of UploadPack - a plugin that makes file uploads in CakePHP as easy as possible.
5
 *
6
 * UploadHelper
7
 *
8
 * UploadHelper provides fine access to files uploaded with UploadBehavior. It generates url for those files and can display image tags of uploaded images. For more info read UploadPack documentation.
9
 *
10
 * @author Michał Szajbe (michal.szajbe@gmail.com)
11
 * @link http://github.com/szajbus/uploadpack
12
 */
13

    
14
class UploadHelper extends AppHelper {
15

    
16
    public $helpers = array('Html');
17

    
18
    public function uploadImage($data, $path, $options = array(), $htmlOptions = array())
19
    {
20
        $options += array('urlize' => false);
21
        return $this->output($this->Html->image($this->uploadUrl($data, $path, $options), $htmlOptions));
22
    }
23

    
24
    public function uploadLink($title, $data, $field, $urlOptions = array(), $htmlOptions = array())
25
    {
26
        $urlOptions += array('style' => 'original', 'urlize' => true);
27
        return $this->Html->link($title, $this->uploadUrl($data, $field, $urlOptions), $htmlOptions);
28
    }
29

    
30
    public function uploadUrl($data, $field, $options = array())
31
    {
32
        $options += array('style' => 'original', 'urlize' => true);
33
        list($model, $field) = explode('.', $field);
34
        if(is_array($data))
35
        {
36
            if(isset($data[$model]))
37
            {
38
                if(isset($data[$model]['id']))
39
                {
40
                    $id = $data[$model]['id'];
41
                    $filename = $data[$model][$field.'_file_name'];
42
                }
43
            }
44
            elseif(isset($data['id']))
45
            {
46
                $id = $data['id'];
47
                $filename = $data[$field.'_file_name'];
48
            }
49
        }
50

    
51
        if(isset($id) && !empty($filename))
52
        {
53
            $settings = UploadBehavior::interpolate($model, $id, $field, $filename, $options['style'], array('webroot' => ''));
54
            $url = isset($settings['url']) ? $settings['url'] : $settings['path'];
55
        }
56
        else
57
        {
58
            $settings = UploadBehavior::interpolate($model, null, $field, null, $options['style'], array('webroot' => ''));
59
            $url = isset($settings['default_url']) ? $settings['default_url'] : null;
60
        }
61

    
62
        return $options['urlize'] ? $this->Html->url($url) : $url;
63
    }
64

    
65
    /**
66
     * Returns appropriate extension for given mimetype.
67
     *
68
     * @param string $mime Mimetype
69
     * @return void
70
     * @author Bjorn Post
71
     */
72
    public function extension($mimeType = null)
73
    {
74
        $knownMimeTypes = array(
75
            'ai' => 'application/postscript', 'bcpio' => 'application/x-bcpio', 'bin' => 'application/octet-stream',
76
            'ccad' => 'application/clariscad', 'cdf' => 'application/x-netcdf', 'class' => 'application/octet-stream',
77
            'cpio' => 'application/x-cpio', 'cpt' => 'application/mac-compactpro', 'csh' => 'application/x-csh',
78
            'csv' => 'application/csv', 'dcr' => 'application/x-director', 'dir' => 'application/x-director',
79
            'dms' => 'application/octet-stream', 'doc' => 'application/msword', 'drw' => 'application/drafting',
80
            'dvi' => 'application/x-dvi', 'dwg' => 'application/acad', 'dxf' => 'application/dxf', 'dxr' => 'application/x-director',
81
            'eps' => 'application/postscript', 'exe' => 'application/octet-stream', 'ez' => 'application/andrew-inset',
82
            'flv' => 'video/x-flv', 'gtar' => 'application/x-gtar', 'gz' => 'application/x-gzip',
83
            'bz2' => 'application/x-bzip', '7z' => 'application/x-7z-compressed', 'hdf' => 'application/x-hdf',
84
            'hqx' => 'application/mac-binhex40', 'ips' => 'application/x-ipscript', 'ipx' => 'application/x-ipix',
85
            'js' => 'application/x-javascript', 'latex' => 'application/x-latex', 'lha' => 'application/octet-stream',
86
            'lsp' => 'application/x-lisp', 'lzh' => 'application/octet-stream', 'man' => 'application/x-troff-man',
87
            'me' => 'application/x-troff-me', 'mif' => 'application/vnd.mif', 'ms' => 'application/x-troff-ms',
88
            'nc' => 'application/x-netcdf', 'oda' => 'application/oda', 'pdf' => 'application/pdf',
89
            'pgn' => 'application/x-chess-pgn', 'pot' => 'application/mspowerpoint', 'pps' => 'application/mspowerpoint',
90
            'ppt' => 'application/mspowerpoint', 'ppz' => 'application/mspowerpoint', 'pre' => 'application/x-freelance',
91
            'prt' => 'application/pro_eng', 'ps' => 'application/postscript', 'roff' => 'application/x-troff',
92
            'scm' => 'application/x-lotusscreencam', 'set' => 'application/set', 'sh' => 'application/x-sh',
93
            'shar' => 'application/x-shar', 'sit' => 'application/x-stuffit', 'skd' => 'application/x-koan',
94
            'skm' => 'application/x-koan', 'skp' => 'application/x-koan', 'skt' => 'application/x-koan',
95
            'smi' => 'application/smil', 'smil' => 'application/smil', 'sol' => 'application/solids',
96
            'spl' => 'application/x-futuresplash', 'src' => 'application/x-wais-source', 'step' => 'application/STEP',
97
            'stl' => 'application/SLA', 'stp' => 'application/STEP', 'sv4cpio' => 'application/x-sv4cpio',
98
            'sv4crc' => 'application/x-sv4crc', 'svg' => 'image/svg+xml', 'svgz' => 'image/svg+xml',
99
            'swf' => 'application/x-shockwave-flash', 't' => 'application/x-troff',
100
            'tar' => 'application/x-tar', 'tcl' => 'application/x-tcl', 'tex' => 'application/x-tex',
101
            'texi' => 'application/x-texinfo', 'texinfo' => 'application/x-texinfo', 'tr' => 'application/x-troff',
102
            'tsp' => 'application/dsptype', 'unv' => 'application/i-deas', 'ustar' => 'application/x-ustar',
103
            'vcd' => 'application/x-cdlink', 'vda' => 'application/vda', 'xlc' => 'application/vnd.ms-excel',
104
            'xll' => 'application/vnd.ms-excel', 'xlm' => 'application/vnd.ms-excel', 'xls' => 'application/vnd.ms-excel',
105
            'xlw' => 'application/vnd.ms-excel', 'zip' => 'application/zip', 'aif' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff',
106
            'aiff' => 'audio/x-aiff', 'au' => 'audio/basic', 'kar' => 'audio/midi', 'mid' => 'audio/midi',
107
            'midi' => 'audio/midi', 'mp2' => 'audio/mpeg', 'mp3' => 'audio/mpeg', 'mpga' => 'audio/mpeg',
108
            'ra' => 'audio/x-realaudio', 'ram' => 'audio/x-pn-realaudio', 'rm' => 'audio/x-pn-realaudio',
109
            'rpm' => 'audio/x-pn-realaudio-plugin', 'snd' => 'audio/basic', 'tsi' => 'audio/TSP-audio', 'wav' => 'audio/x-wav',
110
            'asc' => 'text/plain', 'c' => 'text/plain', 'cc' => 'text/plain', 'css' => 'text/css', 'etx' => 'text/x-setext',
111
            'f' => 'text/plain', 'f90' => 'text/plain', 'h' => 'text/plain', 'hh' => 'text/plain', 'htm' => 'text/html',
112
            'html' => 'text/html', 'm' => 'text/plain', 'rtf' => 'text/rtf', 'rtx' => 'text/richtext', 'sgm' => 'text/sgml',
113
            'sgml' => 'text/sgml', 'tsv' => 'text/tab-separated-values', 'tpl' => 'text/template', 'txt' => 'text/plain',
114
            'xml' => 'text/xml', 'avi' => 'video/x-msvideo', 'fli' => 'video/x-fli', 'mov' => 'video/quicktime',
115
            'movie' => 'video/x-sgi-movie', 'mpe' => 'video/mpeg', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg',
116
            'qt' => 'video/quicktime', 'viv' => 'video/vnd.vivo', 'vivo' => 'video/vnd.vivo', 'gif' => 'image/gif',
117
            'ief' => 'image/ief', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg',
118
            'pbm' => 'image/x-portable-bitmap', 'pgm' => 'image/x-portable-graymap', 'png' => 'image/png',
119
            'pnm' => 'image/x-portable-anymap', 'ppm' => 'image/x-portable-pixmap', 'ras' => 'image/cmu-raster',
120
            'rgb' => 'image/x-rgb', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'xbm' => 'image/x-xbitmap',
121
            'xpm' => 'image/x-xpixmap', 'xwd' => 'image/x-xwindowdump', 'ice' => 'x-conference/x-cooltalk',
122
            'iges' => 'model/iges', 'igs' => 'model/iges', 'mesh' => 'model/mesh', 'msh' => 'model/mesh',
123
            'silo' => 'model/mesh', 'vrml' => 'model/vrml', 'wrl' => 'model/vrml',
124
            'mime' => 'www/mime', 'pdb' => 'chemical/x-pdb', 'xyz' => 'chemical/x-pdb'
125
        );
126

    
127
        return array_search($mimeType, $knownMimeTypes);
128
    }
129
}