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

pictcode / app / Plugin / UploadPack / View / Helper / UploadHelper.php @ 0b1b8047

履歴 | 表示 | アノテート | ダウンロード (8.086 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
class UploadHelper extends AppHelper {
14

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

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

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

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

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

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

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

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