coco / CocoWidget.php

<?php
/**
	CocoWidget

	This File uploader Tool is a Yii implementation for Valums File Uploader
	(original source: http://github.com/valums/file-uploader)

	@author	Christian Salazar (christiansalazarh@gmail.com) bluyell, @yiienespanol
	@date oct-2012
	@license http://opensource.org/licenses/bsd-license.php
*/
class CocoWidget extends CWidget implements EYuiActionRunnable {

	public $id='cocowidget0';
	public $debug;
	public $htmlOptions;
	public $defaultControllerName='site';
	public $defaultActionName='coco';

	public $buttonText='Find & Upload';
	public $dropFilesText='Drop Files Here !';
	public $allowedExtensions=array();
	public $sizeLimit;
	public $uploadDir = 'assets/';
	public $onCompleted;
	public $onCancelled;
	public $onMessage;

	private $_baseUrl;

	public static function t($text){
		return Yii::t("CocoWidget.coco",$text);
	}

	public function init(){
		parent::init();
		$this->registerCoreScripts();
		if($this->sizeLimit == null)
			$this->sizeLimit = 2 * 1024 * 1024;
	}
	public function run(){

		$id=$this->id;
		$upid = $id.'uploader';
		$logid = $id.'logger';
		$action = CHtml::normalizeUrl(array($this->defaultControllerName.'/'.$this->defaultActionName));

		$htopts = '';
		if(empty($this->htmlOptions)){
			$htopts = "class='cocowidget'";
		}else{
			if(!isset($this->htmlOptions['class']))
				$this->htmlOptions['class'] = 'cocowidget';
			foreach($this->htmlOptions as $key=>$val)
				$htopts .= " {$key}='$val'";
		}

		if($this->onCompleted == null)
			$this->onCompleted = 'function(id,filename,jsoninfo){ }';
		if(!($this->onCompleted instanceof CJavaScriptExpression))
				$this->onCompleted = new CJavaScriptExpression($this->onCompleted);

		if($this->onCancelled == null)
			$this->onCancelled = 'function(id,filename){ }';
		if(!($this->onCancelled instanceof CJavaScriptExpression))
				$this->onCancelled = new CJavaScriptExpression($this->onCancelled);

		if($this->onMessage == null)
			$this->onMessage = 'function(messageText){ }';
		if(!($this->onMessage instanceof CJavaScriptExpression))
				$this->onMessage = new CJavaScriptExpression($this->onMessage);

		$vars = array(
			'allowedExtensions'=>$this->allowedExtensions,
			'sizeLimit'=>$this->sizeLimit,
			'uploadDir'=>$this->uploadDir,
		);

		$options = CJavaScript::encode(
			array(
				'id'=>$id,
				'loggerid'=>$logid,
				'action'=>$action,
				'onCompleted'=>$this->onCompleted,
				'onCancelled'=>$this->onCancelled,
				'onMessage'=>$this->onMessage,
				'buttonText'=>$this->buttonText,
				'dropFilesText'=>$this->dropFilesText,
				'uploaderContainer'=>$upid,
				'data'=>serialize($vars),
			)
		);

echo
"
	<!-- CocoWidget begins -->
	<div id='{$id}' {$htopts}'>
		<div id='{$upid}' class='uploader'></div>
		<div id='{$logid}' class='logger'></div>
	</div>
	<!-- CocoWidget ends -->
";

		Yii::app()->getClientScript()->registerScript(
			$id,
"
	var _cocoUp = new CocoWidget({$options});
	_cocoUp.run();
",
			CClientScript::POS_LOAD
		);
	}

	public function registerCoreScripts(){

		$localAssetsDir = dirname(__FILE__) . '/assets';
		$this->_baseUrl = Yii::app()->getAssetManager()->publish($localAssetsDir);

        $cs = Yii::app()->getClientScript();
        $cs->registerCoreScript('jquery');

		if($this->debug)
			$this->_baseUrl = 'protected/extensions/coco/assets';

		foreach(scandir($localAssetsDir) as $f){
			$_f = strtolower($f);
			if(strstr($_f,".js"))
				$cs->registerScriptFile($this->_baseUrl."/".$_f);
			if(strstr($_f,".css"))
				$cs->registerCssFile($this->_baseUrl."/".$_f);
		}
	}

	// de: EYuiActionRunnable
	public function runAction($action,$data) {
		Yii::log('ACTION CALLED - action is: '.$action,'info');

		$vars = unserialize($data);

		$this->allowedExtensions = $vars['allowedExtensions'];
		$this->sizeLimit = (integer)$vars['sizeLimit'];
		$this->uploadDir = $vars['uploadDir'];

		if(($this->allowedExtensions == null) || ($this->allowedExtensions==''))
			$this->allowedExtensions = array();

		Yii::log('ACTION CALLED - data is: '.CJSON::encode($vars),'info');


		if($action == 'upload'){

			$uploader = new ValumsFileUploader(array(), $this->sizeLimit);
			if($uploader->checkServerSettings() != null){
				Yii::log("CocoWidget. Please increase post_max_size and upload_max_filesize to ".$this->sizeLimit,"error");
				return;
			}

			$result = $uploader->handleUpload($this->uploadDir);
			if(isset($result['success'])){
				if($result['success']==true){
					Yii::log('ACTION CALLED - RESULT=SUCCESS','info');
				}
				else{
					Yii::log('ACTION CALLED - RESULT=ERROR1','info');
				}
			}else
			Yii::log('ACTION CALLED - RESULT=ERROR2','info');
			echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
		}

	}
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.