1: <?php
2: /*****************************************************************************************
3: * X2Engine Open Source Edition is a customer relationship management program developed by
4: * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
5: *
6: * This program is free software; you can redistribute it and/or modify it under
7: * the terms of the GNU Affero General Public License version 3 as published by the
8: * Free Software Foundation with the addition of the following permission added
9: * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10: * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
11: * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12: *
13: * This program is distributed in the hope that it will be useful, but WITHOUT
14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15: * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16: * details.
17: *
18: * You should have received a copy of the GNU Affero General Public License along with
19: * this program; if not, see http://www.gnu.org/licenses or write to the Free
20: * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21: * 02110-1301 USA.
22: *
23: * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
24: * California 95067, USA. or at email address contact@x2engine.com.
25: *
26: * The interactive user interfaces in modified source and object code versions
27: * of this program must display Appropriate Legal Notices, as required under
28: * Section 5 of the GNU Affero General Public License version 3.
29: *
30: * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31: * these Appropriate Legal Notices must retain the display of the "Powered by
32: * X2Engine" logo. If the display of the logo is not reasonably feasible for
33: * technical reasons, the Appropriate Legal Notices must display the words
34: * "Powered by X2Engine".
35: *****************************************************************************************/
36:
37: /**
38: * Widget to upload files via AJAX.
39: * @package application.components
40: * @author Alex Rowe <alex@x2engine.com>
41: *
42: * Examples Usage:
43: *
44: * widget('FileUploader', array(
45: *
46: * 'id' => 'myFileUploader',
47: *
48: * 'mediaParams' => array (
49: * 'associationType' => 'Contacts',
50: * 'associationId' => 23
51: * ),
52: *
53: * 'viewParams' => array (
54: * 'showButton' => false
55: * ),
56: *
57: * 'events' => array (
58: * 'success' => 'console.log("success")'
59: * )
60: *
61: * 'acceptedFiles' => 'image/*'
62: *
63: * ));
64: *
65: * How to access in Javascript:
66: * x2.FileUploader.list['myFileUploader']
67: *
68: * See js/FileUploader.js
69: * for Javascript Examples
70: *
71: */
72: class FileUploader extends X2Widget {
73:
74: /**
75: * Static counter for number of instances
76: * @var integer
77: */
78: public static $instanceCount = 0;
79:
80: /**
81: * Config array of extra options to be sent to viewFile
82: * @var array
83: */
84: public static $defaultViewParams = array (
85: 'class' => '',
86: 'noPadding' => false,
87: 'showButton' => true,
88: 'open' => false,
89: 'closeButton' => true,
90: 'buttonText' => null,
91: );
92:
93: /**
94: * @see X2Widget::$JSClass
95: */
96: public $JSClass = 'FileUploader';
97:
98: /**
99: * @see X2Widget::$viewFile
100: */
101: public $viewFile = 'fileUploader';
102:
103: /**
104: * Id / Namespace of this instance. Used to create a unique
105: * ID, and to reference
106: * @var string
107: */
108: public $id;
109:
110: /**
111: * Url to upload media to
112: */
113: public $url = '/site/upload';
114:
115: /**
116: * Wether to allow Google Drive
117: * @var array
118: */
119: public $googleDrive = true;
120:
121: /**
122: * Array of model attributes to set to uploaded files
123: * @var array
124: */
125: public $mediaParams = array();
126:
127: /**
128: * Array of model attributes to set to uploaded files
129: * @var array
130: */
131: public $viewParams = array();
132:
133: /**
134: * Array of Javascript snippets
135: * @var array
136: */
137: public $events = array(
138: // 'success' => 'console.log(this)'
139: );
140:
141: public $acceptedFiles = '';
142:
143: public function init() {
144: // Increment instance count
145: self::$instanceCount++;
146:
147: // Create a unique ID if one is not set
148: if (empty($this->id)) {
149: $this->id = 'attachments-'.self::$instanceCount;
150: }
151:
152: // Create a name space to register mutiple scripts
153: $this->namespace = 'attachments'.self::$instanceCount;
154:
155: // Set up default view Params
156: $this->viewParams = array_merge (self::$defaultViewParams, $this->viewParams);
157: if(is_null($this->viewParams['buttonText'])){
158: $this->viewParams['buttonText'] = Yii::t('media','Upload File');
159: }
160:
161: $this->googleDrive &= Yii::app()->params->profile->mediaWidgetDrive &&
162: Yii::app()->settings->googleIntegration;
163:
164:
165: $this->registerJSEvents ($this->events);
166: }
167:
168: public function run () {
169: $this->registerPackages ();
170: $this->instantiateJSClass ();
171: $this->render ($this->viewFile, $this->viewParams);
172: }
173:
174: public function getPackages () {
175: return array_merge (parent::getPackages (), array (
176: 'FileUploaderJS' => array(
177: 'baseUrl' => Yii::app()->baseUrl,
178: 'js' => array(
179: 'js/FileUploader.js'
180: ),
181: 'depends' => array('Dropzone', 'auxlib')
182: ),
183: 'FileUploaderCSS' => array(
184: 'baseUrl' => Yii::app()->theme->baseUrl,
185: 'css' => array(
186: 'css/components/FileUploader.css'
187: ),
188: ),
189: ));
190: return $this->_packages;
191: }
192:
193: public function getJSClassParams () {
194: if (!isset ($this->_JSClassParams)) {
195: $this->_JSClassParams = array_merge(
196: parent::getJSClassParams(), array(
197: 'url' => $this->url,
198: 'id' => $this->id,
199: 'mediaParams' => $this->mediaParams,
200: 'viewParams' => $this->viewParams,
201: 'acceptedFiles' => $this->acceptedFiles,
202: 'maxFileSize' =>
203: floor (AppFileUtil::sizeToMb (ini_get('upload_max_filesize'))),
204: )
205: );
206: }
207: return $this->_JSClassParams;
208: }
209:
210:
211: public function registerJSEvents ($events) {
212: $js = '';
213: foreach ($events as $event => $snippet) {
214: $js .= "x2.FileUploader.on('$this->id', '$event', function(){".$snippet.";});";
215: }
216: Yii::app()->clientScript->registerScript ("FileUploaderEvents-$this->id", $js);
217: }
218: }
219:
220: ?>
221: