Commits

christiansalazar committed d70bb5c

FIX ISSUE #2. different controller name

Comments (1)

Files changed (6)

ImageResizer.php

-<?php
-/**
- * ImageResizer
- *	a helper class to handle image resize.
- *
- *	example usage:
-
-		$imgres = new ImageResizer();
-		list($ow, $oh, $mimetype) = getimagesize($image_local_path);
-		$f = fopen($image_local_path,"r");
-		$newImage = $imgres->resize(fread($f,filesize($image_local_path)), 
-			160, 120, 70, $ow, $oh);
-		fclose($f);
-		header('Content-type: '.$mimetype);
-		imagepng($newImage);
-		imagedestroy($newImage);
-
- * 
- * @author Christian Salazar <christiansalazarh@gmail.com> 
- * @license NEW BSD. 
- */
-class ImageResizer {
-	/**
-	 * resize
-	 *	resizes an image making it to fit into a rectangle
-	 * 
-	 * @param mixed $image 	Binary raw image data.
-	 * @param mixed $dw 	destination Width viewport
-	 * @param mixed $dh 	destination Height viewport
-	 * @param mixed $q		quality for jpg or png: 1 to 100.
-	 * @param mixed $ow 	original image width
-	 * @param mixed $oh 	original image height
-	 * @return new image. you can echo it or use GD functions to handle it. 
-	 */
-	public function resize($image, $dw, $dh, $q, $ow, $oh){
-		$im = imagecreatetruecolor($dw, $dh);
-		$im_src = imagecreatefromstring($image);
-		$_w = 0;
-		$_h = 0;
-		$this->_scaleVector($dw, $dh, 0.95, $ow, $oh, $_w, $_h);
-		$dx = ($dw - $_w)/2;
-		$dy = ($dh - $_h)/2;
-		$fillcolor = imagecolorallocate($im,255,255,255);
-		//$xcolor = imagecolorallocate($im, 200,200,200);
-		imagefilledrectangle($im, 0,0,$dw, $dh, $fillcolor);
-		//imagefilledrectangle($im, $dx,$dy, $dx + $_w, $dy + $_h, $xcolor);
-		imagecopyresampled(
-				$im, $im_src, 
-				$dx, $dy, 0, 0, 
-				$_w, $_h, 
-				$ow, $oh
-		);
-		return $im;
-	}
-
-	/***
-	 	creates a new image saved as {$dst}, using coords from src image.
-	 	 */
-	public function crop($src, $src_x, $src_y, $w, $h, $toFileName){
-		$im_src = imagecreatefromstring($src);
-		$im_dst = imagecreatetruecolor($w, $h);
-
-		$fillcolor = imagecolorallocate($im_dst,255,255,255);
-		imagefilledrectangle($im_dst, 0,0,$w, $h, $fillcolor);
-
-		imagecopyresampled(
-			$im_dst, $im_src, 
-			0,0, $src_x, $src_y, 
-			$w, $h, 
-			$w, $h
-		);
-
-		imagedestroy($im_src);
-		
-		@unlink($toFileName);
-		imagejpeg($im_dst, $toFileName, 100);
-
-		imagedestroy($im_dst);
-	}
-
-	/**
-	 * _scaleVector
-	 *	
-	 * 
-	 * @param mixed $dw 		|	destination viewport:
-	 * @param mixed $dh 		|		d = {w, h}
-	 * @param mixed $delta 		|	delta: is a fixture measurement. max 1.
-	 * @param mixed $ow 		|	original viewport to be scaled into "d":
-	 * @param mixed $oh 		|		o = {w, h}
-	 * @param mixed $out_w 		
-	 * @param mixed $out_h 
-	 * @access private
-	 * @author Christian Salazar H. <christiansalazarh@gmail.com>  
-	 * @return void
-	 */
-	private function _scaleVector($dw, $dh, $delta, $ow, $oh, &$out_w, &$out_h){
-		$dR = $dw / $dh;
-		if($dR >= 1){
-			$out_w = $delta * $dw;
-			$out_h = ($out_w * $oh) / $ow;
-		}else{
-			$out_h = $delta * $dh;
-			$out_w = ($out_h * $ow) / $oh;
-		}
-	}
-}
 			}
 		 }
 
+	please note: if you're not using the default siteController as default for your static action
+	then provide the controller name (and/or action name) in your class: yiifileman_data(), see below.
+
+
 - copy the provided client class 
 
 		'protected/extensions/yiifilemanagerfilepicker/demo-component/MyYiiFileManViewer.php'  (full documented)
 					'allow_rename_files'=>true,
 					'allow_delete_files'=>true,
 					'allow_file_uploads'=>true,
+					// optionals:
+					// 'controller'=>'site',
+					// 'action'=>'yiifilemanagerfilepicker' 
 				);	
 			}
 

YiiFileManagerFilePicker.php

  * @license http://opensource.org/licenses/bsd-license.php
  */
 include_once ("IYiiFileManagerFilePicker.php");
-include_once ("ImageResizer.php");
+include_once ("YiiFilemanImageResizer.php");
 abstract class YiiFileManagerFilePicker extends CWidget 
 	/* depends on interface: IYiiFileManagerFilePicker */ {
 
 
 	// the controller name and action name holding the 
 	//	static action: YiiFileManagerFilePickerAction
-	public $action="yiifilemanagerfilepicker";
-	public $controller="site";
+	private $_default_action="yiifilemanagerfilepicker";
+	private $_default_controller="site";
 
 	public $onBeforeLaunch;		// launched before to start showing the viewer
 	public $onBeforeAction;		// launched before the select or delete actions are to be taken
 	public $onClientUploaderProgress;
 
 	private $_baseUrl;			// assets resource path
-	private $_action_path;
 
 	public function __construct(){
-		$this->_action_path = "/".trim($this->controller,"/")."/".$this->action;
 
 		if($this->onBeforeLaunch == null)
 			$this->onBeforeLaunch = 'function(_dialog){ return true; }';
 		parent::init();
 	}
 
+	private function _getActionPath(){
+		$data=$this->yiifileman_data();
+		$controller=$this->_default_controller;
+		$action=$this->_default_action;
+		if(isset($data['controller']))
+			$controller = $data['controller'];
+		if(isset($data['action']))
+			$action = $data['action'];
+		return rtrim($controller,'/')."/".ltrim($action);
+	}
+
 	public function run(){
 		$this->_prepareAssets();
 		extract($this->yiifileman_data());
 
 		$options = CJavaScript::encode(array(
-			'ajax_handler'=>CHtml::normalizeUrl(array($this->_action_path,
+			'ajax_handler'=>CHtml::normalizeUrl(array($this->_getActionPath(),
 				"class"=>$this->yiifileman_classname(),
 					"method"=>"ajax_query")),
-			'ajax_file_uploader_handler'=>CHtml::normalizeUrl(array($this->_action_path,
+			'ajax_file_uploader_handler'=>CHtml::normalizeUrl(array($this->_getActionPath(),
 				"class"=>$this->yiifileman_classname(),
 					"method"=>"ajax_file_upload")),
 			'dialog_mode' => ($this->launch_selector != ''),
 		$image_local_path = $this->yiifileman_get_image_substitution(
 				$file_info, $local_path, $mimetype);
 		if($output_size != null){
-			$imgres = new ImageResizer();
+			$imgres = new YiiFilemanImageResizer();
 			list($ow, $oh) = getimagesize($image_local_path);
 			$f = fopen($image_local_path,"r");
 			$newImage = $imgres->resize(fread($f,filesize($image_local_path)), 
 			if($giveme=='list_files')
 				return $this->ajax_list_files($post);
 		}
-		return array('nothing');
+		return array();
 	}
 	
 	public function build_file_viewer_url($file_id){
-		return CHtml::normalizeUrl(array($this->_action_path,
+		return CHtml::normalizeUrl(array($this->_getActionPath(),
 			"class"=>$this->yiifileman_classname(),
 			"method"=>"viewer",
 			"size"=>"gallery",

YiiFilemanImageResizer.php

+<?php
+/**
+ * YiiFilemanImageResizer
+ *	a helper class to handle image resize.
+ *
+ *	example usage:
+
+		$imgres = new YiiFilemanImageResizer();
+		list($ow, $oh, $mimetype) = getimagesize($image_local_path);
+		$f = fopen($image_local_path,"r");
+		$newImage = $imgres->resize(fread($f,filesize($image_local_path)), 
+			160, 120, 70, $ow, $oh);
+		fclose($f);
+		header('Content-type: '.$mimetype);
+		imagepng($newImage);
+		imagedestroy($newImage);
+
+ * 
+ * @author Christian Salazar <christiansalazarh@gmail.com> 
+ * @license NEW BSD. 
+ */
+class YiiFilemanImageResizer {
+	/**
+	 * resize
+	 *	resizes an image making it to fit into a rectangle
+	 * 
+	 * @param mixed $image 	Binary raw image data.
+	 * @param mixed $dw 	destination Width viewport
+	 * @param mixed $dh 	destination Height viewport
+	 * @param mixed $q		quality for jpg or png: 1 to 100.
+	 * @param mixed $ow 	original image width
+	 * @param mixed $oh 	original image height
+	 * @return new image. you can echo it or use GD functions to handle it. 
+	 */
+	public function resize($image, $dw, $dh, $q, $ow, $oh){
+		$im = imagecreatetruecolor($dw, $dh);
+		$im_src = imagecreatefromstring($image);
+		$_w = 0;
+		$_h = 0;
+		$this->_scaleVector($dw, $dh, 0.95, $ow, $oh, $_w, $_h);
+		$dx = ($dw - $_w)/2;
+		$dy = ($dh - $_h)/2;
+		$fillcolor = imagecolorallocate($im,255,255,255);
+		//$xcolor = imagecolorallocate($im, 200,200,200);
+		imagefilledrectangle($im, 0,0,$dw, $dh, $fillcolor);
+		//imagefilledrectangle($im, $dx,$dy, $dx + $_w, $dy + $_h, $xcolor);
+		imagecopyresampled(
+				$im, $im_src, 
+				$dx, $dy, 0, 0, 
+				$_w, $_h, 
+				$ow, $oh
+		);
+		return $im;
+	}
+
+	/***
+	 	creates a new image saved as {$dst}, using coords from src image.
+	 	 */
+	public function crop($src, $src_x, $src_y, $w, $h, $toFileName){
+		$im_src = imagecreatefromstring($src);
+		$im_dst = imagecreatetruecolor($w, $h);
+
+		$fillcolor = imagecolorallocate($im_dst,255,255,255);
+		imagefilledrectangle($im_dst, 0,0,$w, $h, $fillcolor);
+
+		imagecopyresampled(
+			$im_dst, $im_src, 
+			0,0, $src_x, $src_y, 
+			$w, $h, 
+			$w, $h
+		);
+
+		imagedestroy($im_src);
+		
+		@unlink($toFileName);
+		imagejpeg($im_dst, $toFileName, 100);
+
+		imagedestroy($im_dst);
+	}
+
+	/**
+	 * _scaleVector
+	 *	
+	 * 
+	 * @param mixed $dw 		|	destination viewport:
+	 * @param mixed $dh 		|		d = {w, h}
+	 * @param mixed $delta 		|	delta: is a fixture measurement. max 1.
+	 * @param mixed $ow 		|	original viewport to be scaled into "d":
+	 * @param mixed $oh 		|		o = {w, h}
+	 * @param mixed $out_w 		
+	 * @param mixed $out_h 
+	 * @access private
+	 * @author Christian Salazar H. <christiansalazarh@gmail.com>  
+	 * @return void
+	 */
+	private function _scaleVector($dw, $dh, $delta, $ow, $oh, &$out_w, &$out_h){
+		$dR = $dw / $dh;
+		if($dR >= 1){
+			$out_w = $delta * $dw;
+			$out_h = ($out_w * $oh) / $ow;
+		}else{
+			$out_h = $delta * $dh;
+			$out_w = ($out_h * $ow) / $oh;
+		}
+	}
+}

assets/yiifilemanager-fileviewer-widget.js

 		}
 		var list = me.find("ul");
 		_send({ giveme: 'list_files' },function(ok, resp){
+			if(ok==true)
 			$.each(resp, function(file_id, file_data){
 				var can_insert_new = false;
 				if(mode == 'refresh'){

demo-component/MyYiiFileManViewer.php

 			'allow_rename_files'=>true,
 			'allow_delete_files'=>true,
 			'allow_file_uploads'=>true,
+			/*
+			 	optional, only change if you're not using default siteController
+				for holding the required static action (see README).
+
+				'controller'=>'/mymodule/myanothercontroller',
+				'action'=>'myyiifilemanaction',
+			 */
     	);	
 	}