Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#31 2013-10-14 12:14:52

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 638
Website

Re: Overwrite the thumbnail size with a URL parameter

New code:
  1. Changed the PHP to only modify the pref if there is a thumbsize parameter, not like before where I added an else statement to go back to the default size.
  2. Changed the JS:
    • Now working with “abl_droploader” plugin too (base on section, as it’s a single button).
    • Changed the way to set the size in the pref, now it’s using AJAX to do that, and not anymore modify the “bot_image_upload” iframe source.
      $.post('?event=image','thumbsize='+thumbsize);
    • It’s now easy to add more plugins to work with it as I set two variables name “gts_by_sections_button_elm” and “gts_by_fields_button_elm” which is where you can add more button elements of other plugins.
      currently are:
      • By fields ID: $gts_by_fields_button_elm = '.bot_add_image';
      • By sections: $gts_by_sections_button_elm = '.bot_add_image, #abl-droploader-open';

Do you find any other issues that I should fix please?
really thank you for all the time you spends on supporting on this.

Have a great week :)

class gil_thumb_size {
	/**
	 * Constructor
	 */
	public function __construct(){
		if(@txpinterface == 'admin'){
			add_privs('gil_thumb_size', '1');
			register_tab('extensions', 'gil_thumb_size', 'Thumbnail Size');
			register_callback(array($this, 'ui'), 'gil_thumb_size');
			register_callback(array($this, 'js'), 'article');
			register_callback(array($this, 'set'), 'image');
		}
	}

	/**
	 * Creates the ui and steps
	 *
	 * Set database table and list size items
	 */
	public function ui(){
		$step = gps('step');
		$steps = array('save' => true, 'delete' => true);
		$title = 'gil_thumb_size Preferences';
		$gts_table = safe_pfx('gil_thumb_size');

		// Steps and message
		if($step && bouncer($step, $steps)){
			$message = $this->$step();
			$ptop = pagetop($title, $message);
		}else{
			$ptop = pagetop($title);
		}
		echo $ptop;

		// Create DB table
		if(!numrows(safe_query("SHOW TABLES LIKE $gts_table"))==1){
			$creatTable = safe_query("CREATE TABLE `".PFX."$gts_table` (".
				"`name` VARCHAR(64) NOT NULL,".
				"`width` INT(4) NOT NULL,".
				"`height` INT(4) NOT NULL,".
				"`section` TINYINT(1) NOT NULL default '0',".
				"UNIQUE KEY `name` (`name`)".
				");");
		}

		// Create H1 and table head
		echo n.hed(gTxt('Thumbnail Sizes'), 1).
			n.n.startTable('list','','txp-list').
			n.'<thead>'.tr(
				n.hCell('Field ID or Section name').
				n.hCell(gTxt('width')).
				n.hCell().
				n.hCell(gTxt('height')).
				n.hCell(gTxt('section')).
				n.hCell(gTxt('delete'))
			).'</thead>';

		// Building the saved sizes as table rows
		$rs = safe_rows_start('*', 'gil_thumb_size', '1=1 ORDER BY `name`');
		$out = '';
		if($rs){
			while($a = nextRow($rs)){ 
				$isChecked = ($a['section'] == 1) ? 'yes' : 'no';
				$out .= n.tr(
					td( htmlspecialchars($a['name'])).
					td( htmlspecialchars($a['width'])).
					td( htmlspecialchars(' X ')).
					td( htmlspecialchars($a['height'])).
					td( $isChecked).
					td( dLink('gil_thumb_size', 'delete', 'name', $a['name']))
				);
			}
		}
		echo form($out);

		// Create a form row for creating new sizes
		$gtb_save = $this->save();
		echo n.tr(
			form(
				td( fInput('text', 'name', '', 'edit','','',20) ).
				td( fInput('text', 'width', '', 'edit','','',5) ).
				td( htmlspecialchars(' X ')).
				td( fInput('text', 'height', '', 'edit','','',5) ).
				td( '<input name="gts_section" class="checkbox" type="checkbox">').
				td( fInput('submit', 'add', gTxt('add'), 'smallerbox') ).
				n.eInput('gil_thumb_size').
				n.sInput('save')
			)
		);
		echo n.endTable();
	}

	/**
	 * Gets the saved sizes
	 */
	public function get($section = '0'){
		$rs = safe_rows_start('*', 'gil_thumb_size', 'section='.$section.' ORDER BY `name`');
		$out = '';
		if($rs){
			while($a = nextRow($rs)){
				$width	= $a['width'];
				$height	= $a['height'];
				$name 	= escape_js($a['name']);
				$out .= "case '".$name."' : thumbsize = '".$width."x".$height."'; break;".n;
			}
		}

		return $out;
	}

	/**
	 * Save new sizes
	 */
	public function save(){
		extract(doSlash(psa(array('name','width','height','gts_section'))));

		if($name && $width && $height){ 
			$gts_section = ($gts_section == "on") ? 1 : 0;
			$rs = @safe_insert('gil_thumb_size', "
				`name`	 = '$name',
				`width`	 = '$width',
				`height` = '$height',
				`section`  = '$gts_section'");

			return ($rs) ?
				'New thumb size created' : 
				'<b>Error:</b> Duplicate Name';
		}else{
			return '<b>Width</b> and <b>Height</b> must be numeric values';
		}

		return FALSE;
	}

	/**
	 * Delete sizes
	 */
	public function delete(){
		$name = ps('name');
		return (safe_delete('gil_thumb_size', "`name` = '".doSlash($name)."'")) ? 
			"<b>Deleted</b> $name" : 
			"<b>Error</b> Unable to delete $name"; 
	}

	/**
	 * Write tab javascript
	 */
	public function js(){
		$gts_by_fields = $this->get(0);
		$gts_by_sections = $this->get(1);
		$gts_by_fields_button_elm = '.bot_add_image';
		$gts_by_sections_button_elm = '.bot_add_image, #abl-droploader-open';

		echo script_js("
				$(document).ready(function(){

					// Set the 'thumbsize' variable
					var thumbsize = '';

					// Set thumbnail size base on plugins button field ID
					$('$gts_by_fields_button_elm').each(function(){
						switch($(this).parents('p').find('input').attr('id')){
$gts_by_fields
						}
						$(this).parents('p').find('$gts_by_fields_button_elm').attr('data-thumbsize',thumbsize);
					});

					// Set thumbnail size base on section name
					// This overwrites the previous settings which were base on field ID
					$('#section').change(function(){
						switch($(this).val()){
$gts_by_sections
						}
						$('$gts_by_sections_button_elm').attr('data-thumbsize',thumbsize);
					}).change();

					// Set the thumbnail size in the pref on click using AJAX
					$('$gts_by_sections_button_elm').click(function(){
						thumbsize = $(this).attr('data-thumbsize');
						$.post('?event=image','thumbsize='+thumbsize);
					});

				});
		");
	}

	/**
	 * Images tab PHP
	 *
	 * Gets the "thumbsize" parameter from the URL and set it values in the pref.
	 */
	public function set(){
		global $prefs;
		$currentSize = $prefs['thumb_w'].'x'.$prefs['thumb_h'];
		$newSize = gps('thumbsize');

		if(strpos($newSize,'x') && $newSize != $currentSize){
			$newSize = explode('x',$newSize);
			set_pref('thumb_w', (int) $newSize[0], 'image');
			set_pref('thumb_h', (int) $newSize[1], 'image');
		}
	}

}

new gil_thumb_size;

Offline

#32 2013-10-14 21:42:07

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 638
Website

Re: Overwrite the thumbnail size with a URL parameter

Just tested the plugin on a fresh TXP install and the production status was on “testing”, I received an error, so I changed it to debugging and this is what it says:

Internal error “You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘gil_thumb_size’ at line 1”
in C:\wamp\www\textpattern-4.5.5\textpattern\lib\txplib_db.php at line 95.

adminErrorHandler()
C:\wamp\www\textpattern-4.5.5\textpattern\lib\txplib_db.php:95 trigger_error()
C:\wamp\www\textpattern-4.5.5\textpattern\lib\txplib_misc.php(812) : eval()’d code:36 safe_query()
gil_thumb_size->ui()
C:\wamp\www\textpattern-4.5.5\textpattern\lib\txplib_misc.php:854 call_user_func_array()

I’m sorry. I’m afraid I can’t do that; gil_thumb_size is an unsafe operation.

Any help with this will be much appreciated please.
It has to do with this part, what’s wrong with it?
how I can simply check if the table exist and if not to create it, in a safe way that will not throw an error like above?

		// Create DB table
		if(!numrows(safe_query("SHOW TABLES LIKE $gts_table"))==1){
			$creatTable = safe_query("CREATE TABLE `".PFX."$gts_table` (".
				"`name` VARCHAR(64) NOT NULL,".
				"`width` INT(4) NOT NULL,".
				"`height` INT(4) NOT NULL,".
				"`section` TINYINT(1) NOT NULL default '0',".
				"UNIQUE KEY `name` (`name`)".
				");");
		}

Offline

Board footer

Powered by FluxBB