~joebordes/chive/chive_lib_upgrade

« back to all changes in this revision

Viewing changes to yii/web/widgets/CMultiFileUpload.php

  • Committer: Joe Bordes
  • Date: 2011-08-22 18:24:26 UTC
  • Revision ID: joe@tsolucio.com-20110822182426-zrvpiuyvm20ybjki
Update yii framework 1.1.8 and associated libraries.
Update About page to reflect changes.
Basic testing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * CMultiFileUpload class file.
 
4
 *
 
5
 * @author Qiang Xue <qiang.xue@gmail.com>
 
6
 * @link http://www.yiiframework.com/
 
7
 * @copyright Copyright &copy; 2008-2011 Yii Software LLC
 
8
 * @license http://www.yiiframework.com/license/
 
9
 */
 
10
 
 
11
/**
 
12
 * CMultiFileUpload generates a file input that can allow uploading multiple files at a time.
 
13
 *
 
14
 * This is based on the {@link http://www.fyneworks.com/jquery/multiple-file-upload/ jQuery Multi File Upload plugin}.
 
15
 * The uploaded file information can be accessed via $_FILES[widget-name], which gives an array of the uploaded
 
16
 * files. Note, you have to set the enclosing form's 'enctype' attribute to be 'multipart/form-data'.
 
17
 *
 
18
 * Example:
 
19
 * <pre>
 
20
 * <?php
 
21
 *   $this->widget('CMultiFileUpload', array(
 
22
 *      'model'=>$model,
 
23
 *      'attribute'=>'files',
 
24
 *      'accept'=>'jpg|gif',
 
25
 *      'options'=>array(
 
26
 *         'onFileSelect'=>'function(e, v, m){ alert("onFileSelect - "+v) }',
 
27
 *         'afterFileSelect'=>'function(e, v, m){ alert("afterFileSelect - "+v) }',
 
28
 *         'onFileAppend'=>'function(e, v, m){ alert("onFileAppend - "+v) }',
 
29
 *         'afterFileAppend'=>'function(e, v, m){ alert("afterFileAppend - "+v) }',
 
30
 *         'onFileRemove'=>'function(e, v, m){ alert("onFileRemove - "+v) }',
 
31
 *         'afterFileRemove'=>'function(e, v, m){ alert("afterFileRemove - "+v) }',
 
32
 *      ),
 
33
 *   ));
 
34
 * ?>
 
35
 * </pre>
 
36
 *
 
37
 * @author Qiang Xue <qiang.xue@gmail.com>
 
38
 * @version $Id: CMultiFileUpload.php 3221 2011-05-13 12:00:40Z mdomba $
 
39
 * @package system.web.widgets
 
40
 * @since 1.0
 
41
 */
 
42
class CMultiFileUpload extends CInputWidget
 
43
{
 
44
        /**
 
45
         * @var string the file types that are allowed (eg "gif|jpg").
 
46
         * Note, the server side still needs to check if the uploaded files have allowed types.
 
47
         */
 
48
        public $accept;
 
49
        /**
 
50
         * @var integer the maximum number of files that can be uploaded. If -1, it means no limits. Defaults to -1.
 
51
         */
 
52
        public $max=-1;
 
53
        /**
 
54
         * @var string the label for the remove button. Defaults to "Remove".
 
55
         */
 
56
        public $remove;
 
57
        /**
 
58
         * @var string message that is displayed when a file type is not allowed.
 
59
         */
 
60
        public $denied;
 
61
        /**
 
62
         * @var string message that is displayed when a file is selected.
 
63
         */
 
64
        public $selected;
 
65
        /**
 
66
         * @var string message that is displayed when a file appears twice.
 
67
         */
 
68
        public $duplicate;
 
69
        /**
 
70
         * @var string the message template for displaying the uploaded file name
 
71
         * @since 1.1.3
 
72
         */
 
73
        public $file;
 
74
        /**
 
75
         * @var array additional options that can be passed to the constructor of the multifile js object.
 
76
         * @since 1.1.7
 
77
         */
 
78
        public $options=array();
 
79
 
 
80
 
 
81
        /**
 
82
         * Runs the widget.
 
83
         * This method registers all needed client scripts and renders
 
84
         * the multiple file uploader.
 
85
         */
 
86
        public function run()
 
87
        {
 
88
                list($name,$id)=$this->resolveNameID();
 
89
                if(substr($name,-2)!=='[]')
 
90
                        $name.='[]';
 
91
                if(isset($this->htmlOptions['id']))
 
92
                        $id=$this->htmlOptions['id'];
 
93
                else
 
94
                        $this->htmlOptions['id']=$id;
 
95
                $this->registerClientScript();
 
96
                echo CHtml::fileField($name,'',$this->htmlOptions);
 
97
        }
 
98
 
 
99
        /**
 
100
         * Registers the needed CSS and JavaScript.
 
101
         * @since 1.0.1
 
102
         */
 
103
        public function registerClientScript()
 
104
        {
 
105
                $id=$this->htmlOptions['id'];
 
106
 
 
107
                $options=$this->getClientOptions();
 
108
                $options=$options===array()? '' : CJavaScript::encode($options);
 
109
 
 
110
                $cs=Yii::app()->getClientScript();
 
111
                $cs->registerCoreScript('multifile');
 
112
                $cs->registerScript('Yii.CMultiFileUpload#'.$id,"jQuery(\"#{$id}\").MultiFile({$options});");
 
113
        }
 
114
 
 
115
        /**
 
116
         * @return array the javascript options
 
117
         */
 
118
        protected function getClientOptions()
 
119
        {
 
120
                $options=$this->options;
 
121
                foreach(array('onFileRemove','afterFileRemove','onFileAppend','afterFileAppend','onFileSelect','afterFileSelect') as $event)
 
122
                {
 
123
                        if(isset($options[$event]) && strpos($options[$event],'js:')!==0)
 
124
                                $options[$event]='js:'.$options[$event];
 
125
                }
 
126
 
 
127
                if($this->accept!==null)
 
128
                        $options['accept']=$this->accept;
 
129
                if($this->max>0)
 
130
                        $options['max']=$this->max;
 
131
 
 
132
                $messages=array();
 
133
                foreach(array('remove','denied','selected','duplicate','file') as $messageName)
 
134
                {
 
135
                        if($this->$messageName!==null)
 
136
                                $messages[$messageName]=$this->$messageName;
 
137
                }
 
138
                if($messages!==array())
 
139
                        $options['STRING']=$messages;
 
140
 
 
141
                return $options;
 
142
        }
 
143
}