Want more features on Pastebin? Sign Up, it's FREE!

Yii YiiBooster TbFileUpload added callbacks

By: sz_dudziak on Aug 16th, 2013  |  syntax: Diff  |  size: 6.90 KB  |  views: 569  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # This patch file was generated by NetBeans IDE
  2. # It uses platform neutral UTF-8 encoding and \n newlines.
  3. --- <html>TbFileUpload.php (<b>2013-07-14 13:10:34</b>)</html>
  4. +++ <html><b>Current File</b></html>
  5. @@ -22,6 +22,8 @@
  6.   */
  7.  class TbFileUpload extends CJuiInputWidget {
  8.  
  9. +       private static $_callbackFnPrefix = 'fileupload';
  10. +
  11.      /**
  12.       * the url to the upload handler
  13.       * @var string
  14. @@ -57,6 +59,35 @@
  15.      public $imageProcessing = true;
  16.  
  17.      /**
  18. +        * Stores callback functions JS code, reffering do jQuery-File-Upload documentation
  19. +        * Use these values for array keys:
  20. +        *      - add,
  21. +        *  - submit,
  22. +        *  - send,
  23. +        *  - done,
  24. +        *  - fail,
  25. +        *  - always,
  26. +        *  - progress,
  27. +        *  - progressall,
  28. +        *  - start,
  29. +        *  - stop
  30. +        *      - change,
  31. +        *  - pase,
  32. +        *  - drop,
  33. +        *  - dragover,
  34. +        *  - chunksend,
  35. +        *  - chunkdone,
  36. +        *  - chunkfail,
  37. +        *  - chunkalways
  38. +        *  Another part of callback function name will be added from code.
  39. +        *
  40. +        * See link below for more details:
  41. +        * @link https://github.com/blueimp/jQuery-File-Upload/wiki/Options#callback-options Callback options
  42. +        * @var string[]
  43. +        */
  44. +       public $callbacks = array( );
  45. +
  46. +       /**
  47.       * @var string name of the form view to be rendered
  48.       */
  49.      public $formView = 'bootstrap.views.fileupload.form';
  50. @@ -80,15 +111,15 @@
  51.       * Widget initialization
  52.       */
  53.      public function init() {
  54. -       if ($this->uploadTemplate === null) {
  55. +               if ( $this->uploadTemplate === null ) {
  56.             $this->uploadTemplate = "#template-upload";
  57.         }
  58.  
  59. -       if ($this->downloadTemplate === null) {
  60. +               if ( $this->downloadTemplate === null ) {
  61.             $this->downloadTemplate = "#template-download";
  62.         }
  63.  
  64. -       if (!isset($this->htmlOptions['enctype'])) {
  65. +               if ( !isset($this->htmlOptions['enctype']) ) {
  66.             $this->htmlOptions['enctype'] = 'multipart/form-data';
  67.         }
  68.  
  69. @@ -107,33 +138,35 @@
  70.         $this->options['url'] = $this->url;
  71.  
  72.         // if acceptFileTypes is not set as option, try getting it from models rules
  73. -       if (!isset($this->options['acceptFileTypes'])) {
  74. +               if ( !isset($this->options['acceptFileTypes']) ) {
  75.             $fileTypes = $this->getFileValidatorProperty($this->model, $this->attribute, 'types');
  76. -           if (isset($fileTypes)) {
  77. -               $fileTypes = (preg_match(':jpg:', $fileTypes) && !preg_match(':jpe:', $fileTypes) ? preg_replace(
  78. +                       if ( isset($fileTypes) ) {
  79. +                               $fileTypes                                                       = (preg_match(':jpg:', $fileTypes) && !preg_match(':jpe:',
  80. +                                               $fileTypes) ? preg_replace(
  81.                                         ':jpg:', 'jpe?g', $fileTypes
  82.                                 ) : $fileTypes);
  83. -               $this->options['acceptFileTypes'] = 'js:/(\.)(' . preg_replace(':,:', '|', $fileTypes) . ')$/i';
  84. +                               $this->options['acceptFileTypes']        = 'js:/(\.)(' . preg_replace(':,:', '|', $fileTypes) . ')$/i';
  85.             }
  86.         }
  87.  
  88.         // if maxFileSize is not set as option, try getting it from models rules
  89. -       if (!isset($this->options['maxFileSize'])) {
  90. +               if ( !isset($this->options['maxFileSize']) ) {
  91.             $fileSize = $this->getFileValidatorProperty($this->model, $this->attribute, 'maxSize');
  92. -           if (isset($fileSize)) {
  93. +                       if ( isset($fileSize) ) {
  94.                 $this->options['maxFileSize'] = $fileSize;
  95.             }
  96.         }
  97.  
  98. -       if ($this->multiple) {
  99. +               if ( $this->multiple ) {
  100.             $this->htmlOptions["multiple"] = true;
  101.         }
  102.  
  103.         $this->render($this->uploadView);
  104.         $this->render($this->downloadView);
  105. -       $this->render($this->formView, array('name' => $name, 'htmlOptions' => $this->htmlOptions));
  106. +               $this->render($this->formView,
  107. +                       array( 'name' => $name, 'htmlOptions' => $this->htmlOptions ));
  108.  
  109. -       if ($this->previewImages || $this->imageProcessing) {
  110. +               if ( $this->previewImages || $this->imageProcessing ) {
  111.             $this->render($this->previewImagesView);
  112.         }
  113.  
  114. @@ -155,22 +188,25 @@
  115.         //The Templates plugin is included to render the upload/download listings
  116.         Yii::app()->bootstrap->registerAssetJs("fileupload/tmpl.min.js", CClientScript::POS_END);
  117.  
  118. -       if ($this->previewImages || $this->imageProcessing) {
  119. -           Yii::app()->bootstrap->registerAssetJs("fileupload/load-image.min.js", CClientScript::POS_END);
  120. -           Yii::app()->bootstrap->registerAssetJs("fileupload/canvas-to-blob.min.js", CClientScript::POS_END);
  121. +               if ( $this->previewImages || $this->imageProcessing ) {
  122. +                       Yii::app()->bootstrap->registerAssetJs("fileupload/load-image.min.js",
  123. +                               CClientScript::POS_END);
  124. +                       Yii::app()->bootstrap->registerAssetJs("fileupload/canvas-to-blob.min.js",
  125. +                               CClientScript::POS_END);
  126.             // gallery :)
  127.             Yii::app()->bootstrap->registerAssetCss("bootstrap-image-gallery.min.css");
  128. -           Yii::app()->bootstrap->registerAssetJs("bootstrap-image-gallery.min.js", CClientScript::POS_END);
  129. +                       Yii::app()->bootstrap->registerAssetJs("bootstrap-image-gallery.min.js",
  130. +                               CClientScript::POS_END);
  131.         }
  132.         //The Iframe Transport is required for browsers without support for XHR file uploads
  133.         Yii::app()->bootstrap->registerAssetJs('fileupload/jquery.iframe-transport.js');
  134.         Yii::app()->bootstrap->registerAssetJs('fileupload/jquery.fileupload.js');
  135.         // The File Upload image processing plugin
  136. -       if ($this->imageProcessing) {
  137. +               if ( $this->imageProcessing ) {
  138.             Yii::app()->bootstrap->registerAssetJs('fileupload/jquery.fileupload-ip.js');
  139.         }
  140.         // The File Upload file processing plugin
  141. -       if ($this->previewImages) {
  142. +               if ( $this->previewImages ) {
  143.             Yii::app()->bootstrap->registerAssetJs('fileupload/jquery.fileupload-fp.js');
  144.         }
  145.         // locale
  146. @@ -179,7 +215,11 @@
  147.         Yii::app()->bootstrap->registerAssetJs('fileupload/jquery.fileupload-ui.js');
  148.  
  149.         $options = CJavaScript::encode($this->options);
  150. -       Yii::app()->clientScript->registerScript(__CLASS__ . '#' . $id, "jQuery('#{$id}').fileupload({$options});");
  151. +               Yii::app()->clientScript->registerScript(__CLASS__ . '#' . $id,
  152. +                       "jQuery('#{$id}')
  153. +                               .fileupload({$options})
  154. +                               {$this->generateCallbackBindJSString()};"
  155. +               );
  156.      }
  157.  
  158.      /**
  159. @@ -191,17 +231,30 @@
  160.       *
  161.       * @return string property's value or null
  162.       */
  163. -    private function getFileValidatorProperty($model = null, $attribute = null, $property = null) {
  164. -       if (!isset($model, $attribute, $property)) {
  165. +       private function getFileValidatorProperty($model = null, $attribute = null,
  166. +               $property = null) {
  167. +               if ( !isset($model, $attribute, $property) ) {
  168.             return null;
  169.         }
  170.  
  171. -       foreach ($model->getValidators($attribute) as $validator) {
  172. -           if ($validator instanceof CFileValidator) {
  173. +               foreach ( $model->getValidators($attribute) as $validator ) {
  174. +                       if ( $validator instanceof CFileValidator ) {
  175.                 $ret = $validator->$property;
  176.             }
  177.         }
  178.         return isset($ret) ? $ret : null;
  179.      }
  180.  
  181. +       private function generateCallbackBindJSString() {
  182. +               if ( count($this->callbacks) > 0 ) {
  183. +                       $output = '';
  184. +                       foreach ( $this->callbacks as $callbackName => $fnCode ) {
  185. +                               $output .= '.bind("' . self::$_callbackFnPrefix
  186. +                                       . $callbackName . '", ' . $fnCode . ')';
  187.  }
  188. +                       return $output;
  189. +               }
  190. +               return '';
  191. +       }
  192. +
  193. +}
clone this paste RAW Paste Data