OSDN Git Service

mediaPlugin1.3:Versionファイルの個別出力・削除対応
[trpgtools-onweb/cake-frame.git] / app / plugins / media / views / helpers / upfile.php
1 <?php 
2 /**
3  * mediaプラグインアップロードファイル出力用ヘルパー
4  * Mediaヘルパー拡張
5  */
6
7 App::import('Vendor', 'Media.Media');
8
9 class UpfileHelper extends MediaHelper {
10         var $helpers = array('Html', 'Media');
11
12 /**
13  * Huck of Media::file()
14  * Resolves partial path
15  * Generates Version File if it dose not exist.
16  * Touches the Version File if it exists.
17  * $size: s, m, l...
18  * $arg[1]: Data of type Attachment.0
19  * $arg[2]: options
20  */
21         function file($previewVersion)
22         {
23                 if (!isset($previewVersion)) {
24                         return null;
25                 }
26                 $args = func_get_args();
27
28                 // ファイル指定がない場合
29                 if (empty($args[1])) {
30                         if (isset($args[2]['nodata']) && !empty($args[2]['nodata'])) {
31                                 $data = array(
32                                         'id' => 0,
33                                         'model' => NULL,
34                                         'foreign_key' => NULL,
35                                         'dirname' => '',
36                                         'basename' => '',
37                                         'checksum' => '',
38                                         'group' => '',
39                                         'alternative' => '',
40                                 );
41
42                                 if ($args[2]['nodata'] == 'image') {
43                                         $data['dirname'] = 'img';
44                                         $data['basename'] = 'noimage.png';
45                                         $data['alternative'] = 'No Image';
46                                 } else {
47                                         return NULL;
48                                 }
49                         } else {
50                                 return NULL;
51                         }
52                 } else {
53                         $data = $args[1];
54                 }
55
56                 $path = $previewVersion. DS. $data['dirname']. DS. $data['basename']; 
57
58                 // ファイルがある場合、ファイルパスを返す
59                 $file = parent::file($path);
60                 if (is_file($file) && is_readable($file)) {
61                         return $file;
62                 // ファイルがない場合、作成
63                 } else {
64                         require_once(APP.'plugins'.DS.'media'.DS.'libs'.DS.'upfile.php');
65 //                      $make_result = Upfile::make_version($path, $data, $args[2]);
66                         $maked_version = Upfile::make_version($previewVersion, $path, $args[2]);
67
68                         // cacheを削除して$file読み込み
69                         if (!empty($maked_version)) {
70                                 parent::__destruct();
71                                 return self::file($previewVersion, $args[1], $args[2]);
72                         }
73                 }
74
75                 return null;
76         }
77
78
79 /**
80  * Extend method of Media::embed()
81  * Generates markup to render a file inline
82  * Link to $options['linkTo'].
83  * HtmlAttributes can be userd for the link as $options['htmlAttributes'].
84  */
85         function embed($path, $options = array()) {
86                 if (!isset($path)) {
87                         return null;
88                 }
89
90                 if (isset($options['linkTo']) && !empty($options['linkTo'])) {
91                         unset($options['url']);
92                 }
93
94                 $out = $this->Media->embed($path, $options);
95
96                 if (!isset($options['linkTo']) || empty($options['linkTo'])) {
97                         return $out;
98                 }
99
100                 if (!isset($options['htmlAttributes'])) {
101                         $options['htmlAttributes'] = array();
102                 }
103                 $options['htmlAttributes'] = array_merge(array('escape' => false), $options['htmlAttributes']);
104                 return $this->Html->link($out, $options['linkTo'], $options['htmlAttributes'], false);
105         }
106
107
108         // orig Versionファイルにリンクする
109         function embed_linkto_orig($file, $item, $model, $options = array()) {
110                 $orig_size_filepath = self::file(
111                         'filter/orig', 
112                         $item, 
113                         array(
114                                 'model_name' => $model, 
115                                 'mime_type' => 'original',
116                         )
117                 );
118                 $options['linkTo'] = parent::url($orig_size_filepath, true);
119
120                 return self::embed($file, $options);
121         }
122
123 /**
124  * Generates file information
125  */
126         function fileInfo($file, $item)
127         {
128                 if (empty($file)) {
129                         return null;
130                 }
131                 $Media = Media::factory($file);
132                 $size = $item['size'];
133
134                 if (isset($number)) {
135                         $size = $number->toReadableSize($size);
136                 } else {
137                         $size .= ' Bytes';
138                 }
139
140                 return sprintf('<div class="fileOptions"><em>%s</em>&nbsp;(%s/%s)</div>',
141                         $item['alternative'],
142 //                      $url ? $html->link($item['basename'], $url) : $item['basename'],
143                         strtolower($Media->name), 
144                         $size
145                 );
146
147         }
148
149         /* $attachments情報からファイルパスを取得 */
150         function get_charaimage_version_path4attachment($attachment, $options = array())
151         {
152                 return self::file(
153                         $options['previewVersion'], 
154                         $attachment,
155                         $options
156                 );
157         }
158
159         /* basenameからファイルパスを取得 */
160         function get_cet_charaimage_version4basename($basename, $options = array())
161         {
162                 $data = array();
163                 if (!empty($basename)) {
164                         $alternative = null;
165                         if (isset($options['alternative'])) {
166                                 $alternative = $options['alternative'];
167                                 unset($options['alternative']);
168                         }
169                         $data = array(
170                                 'dirname' => 'img',
171                                 'basename' => $basename,
172                                 'alternative' => $alternative,
173                         );
174                 }
175                 return self::get_charaimage_version_path4attachment($data, $options);
176         }
177
178         function setting_charaimage_version_path($options = array())
179         {
180                 $default_options = array(
181                         'previewVersion' => 'l',
182                         'class' => '',
183                         'url' => '',
184                         'alternative' => '',
185                         'model_name' => 'CharacterPicture',
186                         'mime_type' => 'original',
187                         'nodata' => 'image',
188                         'htmlAttributes' => array(),
189                 );
190
191                 return array_merge($default_options, $options);
192         }
193 }