Browse Source

some fixes

euromark 13 years ago
parent
commit
80cde3a688

+ 16 - 20
Lib/MyModel.php

@@ -35,7 +35,7 @@ class MyModel extends Model {
 		}
 		}
 
 
 		# avoiding AppModel instances instead of real Models - testing - 2011-04-03 ms
 		# avoiding AppModel instances instead of real Models - testing - 2011-04-03 ms
-		if (defined('HTTP_HOST') && HTTP_HOST && !is_a($this, $this->name) && $this->displayField !== 'id' && !Configure::read('Core.disableModelInstanceNotice')) {
+		if (defined('HTTP_HOST') && HTTP_HOST && !is_a($this, $this->name) && $this->displayField !== 'id' && $this->useDbConfig != 'test' && !Configure::read('Core.disableModelInstanceNotice')) {
 			trigger_error('AppModel instance! Expected: ' . $this->name);
 			trigger_error('AppModel instance! Expected: ' . $this->name);
 		}
 		}
 	}
 	}
@@ -406,31 +406,27 @@ class MyModel extends Model {
 	 * Makes a subquery
 	 * Makes a subquery
 	 * @link http://bakery.cakephp.org/articles/lucaswxp/2011/02/11/easy_and_simple_subquery_cakephp
 	 * @link http://bakery.cakephp.org/articles/lucaswxp/2011/02/11/easy_and_simple_subquery_cakephp
 	 *
 	 *
-	 * @param string|array $type The type o the query (only available 'count') or the $options
-	 * @param string|array $options The options array or $alias in case of $type be a array
+	 * @param string $type The type o the query ('count'/'all'/'first' - first only works with some mysql versions)
+	 * @param array $options The options array
 	 * @param string $alias You can use this intead of $options['alias'] if you want
 	 * @param string $alias You can use this intead of $options['alias'] if you want
 	 * @return string $result sql snippet of the query to run
 	 * @return string $result sql snippet of the query to run
+	 * @modified Mark Scherer (cake2.x ready and improvements)
 	 * 2011-07-05 ms
 	 * 2011-07-05 ms
 	 */
 	 */
-	public function subquery($type, $options = null, $alias = null) {
-		$fields = array();
-		if (is_string($type)) {
-			$isString = true;
-		} else {
-			$alias = $options;
-			$options = $type;
-		}
-
+	public function subquery($type, $options = array(), $alias = null) {
 		if ($alias === null) {
 		if ($alias === null) {
 			$alias = 'Sub' . $this->alias . '';
 			$alias = 'Sub' . $this->alias . '';
 		}
 		}
 
 
-		if (isset($isString)) {
-			switch ($type) {
-				case 'count':
-					$fields = array('COUNT(*)');
-					break;
-			}
+		$fields = array();
+		$limit = null;
+		switch ($type) {
+			case 'count':
+				$fields = array('COUNT(*)');
+				break;
+			case 'first':
+				$limit = 1;
+				break;
 		}
 		}
 
 
 		$dbo = $this->getDataSource();
 		$dbo = $this->getDataSource();
@@ -439,7 +435,7 @@ class MyModel extends Model {
 			'fields' => $fields,
 			'fields' => $fields,
 			'table' => $dbo->fullTableName($this),
 			'table' => $dbo->fullTableName($this),
 			'alias' => $alias,
 			'alias' => $alias,
-			'limit' => null,
+			'limit' => $limit,
 			'offset' => null,
 			'offset' => null,
 			'joins' => array(),
 			'joins' => array(),
 			'conditions' => array(),
 			'conditions' => array(),
@@ -447,7 +443,7 @@ class MyModel extends Model {
 			'group' => null
 			'group' => null
 		);
 		);
 		$params = array_merge($default, $options);
 		$params = array_merge($default, $options);
-		$subQuery = $dbo->buildStatement($params, $this);
+		$subQuery = '(' . $dbo->buildStatement($params, $this) . ')';
 		return $subQuery;
 		return $subQuery;
 	}
 	}
 
 

+ 51 - 22
Test/Case/View/Helper/FormatHelperTest.php

@@ -75,7 +75,30 @@ class FormatHelperTest extends MyCakeTestCase {
 		}
 		}
 	}
 	}
 
 
-
+	public function testPriorityIcon() {
+		$values = array(
+			array(1, array(), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
+			array(2, array(), '<div class="prio-lower" title="prioLower">&nbsp;</div>'),
+			array(3, array(), ''),
+			array(3, array('normal' => true), '<div class="prio-normal" title="prioNormal">&nbsp;</div>'),
+			array(4, array(), '<div class="prio-higher" title="prioHigher">&nbsp;</div>'),
+			array(5, array(), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
+			array(1, array('max' => 3), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
+			array(2, array('max' => 3), ''),
+			array(2, array('max' => 3, 'normal' => true), '<div class="prio-normal" title="prioNormal">&nbsp;</div>'),
+			array(3, array('max' => 3), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
+
+			array(0, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
+			array(1, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), ''),
+			array(2, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
+		);
+		foreach ($values as $key => $value) {
+			$res = $this->Format->priorityIcon($value[0], $value[1]);
+			echo $key;
+			//debug($res, null, false); ob_flush();
+			$this->assertEquals($value[2], $res);
+		}
+	}
 
 
 	/**
 	/**
 	 * 2009-08-30 ms
 	 * 2009-08-30 ms
@@ -101,16 +124,22 @@ class FormatHelperTest extends MyCakeTestCase {
 	 */
 	 */
 	public function testTruncate() {
 	public function testTruncate() {
 		$data = array(
 		$data = array(
-			'dfssdfsdj sdkfj sdkfj ksdfj sdkf ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf',
-			'122 jsdf sjdkf sdfj sdf',
-			'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd',
-			'\';alert(String.fromCharCode(88,83,83))//\';alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83))//\";alert(String.fromCharCode(88,83,83))//--></SCRIPT>">\'><SCRIPT>alert(String.fromCharCode(88,83,83))</SCRIPT>'
+			'dfssdfsdj sdkfj sdkfj ksdfj sdkf ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf' => 'dfssdfsdj sdkfj sdkfj k&#8230;',
+			'122 jsdf sjdkf sdfj sdf' => '122 jsdf sjdkf sdfj sdf',
+			'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd' => 'ddddddddddddddddddddddd&#8230;',
+			'dsfdsf hods hfoéfh oéfhoéfhoéfhoéfhoéfhiu oéfhoéfhdüf s' => 'dsfdsf hods hfoéfh oéfh&#8230;'
 		);
 		);
-		foreach ($data as $key => $value) {
+		foreach ($data as $value => $expected) {
 			$res = $this->Format->truncate($value, 30);
 			$res = $this->Format->truncate($value, 30);
 
 
-			echo '\''.h($value).'\' becomes \''.$res.'\'';
-			$this->assertTrue(!empty($res));
+			debug( '\''.h($value).'\' becomes \''.$res.'\'', null, false); ob_flush();
+
+			$res = $this->Format->truncate($value, 30, array('html' => true));
+
+			debug( '\''.h($value).'\' becomes \''.$res.'\'', null, false); ob_flush();
+
+			//$this->assertTrue(!empty($res));
+			$this->assertEquals($expected, $res);
 		}
 		}
 
 
 	}
 	}
@@ -128,7 +157,7 @@ class FormatHelperTest extends MyCakeTestCase {
 			$res = $this->Format->hideEmail($mail);
 			$res = $this->Format->hideEmail($mail);
 
 
 			echo '\''.$mail.'\' becomes \''.$res.'\' - expected \''.$expected.'\'';
 			echo '\''.$mail.'\' becomes \''.$res.'\' - expected \''.$expected.'\'';
-			$this->assertEquals($res, $expected);
+			$this->assertEquals($expected, $res);
 		}
 		}
 
 
 	}
 	}
@@ -140,17 +169,17 @@ class FormatHelperTest extends MyCakeTestCase {
 	 */
 	 */
 	public function testWordCensor() {
 	public function testWordCensor() {
 		$data = array(
 		$data = array(
-			'dfssdfsdj sdkfj sdkfj ksdfj arsch ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf',
-			'122 jsdf ficken Sjdkf sdfj sdf',
-			'122 jsdf FICKEN sjdkf sdfj sdf',
-			'ddddddddddddddddddddddddd ARSCH dddddddddddddddddddddddddddddddddddddddddddddddddd',
-			'\';alert(String.fromCharCode(88,83,83))//\';alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83))//\";alert(String.fromCharCode(88,83,83))//--></SCRIPT>">\'><SCRIPT>alert(String.fromCharCode(88,83,83))</SCRIPT>'
+			'dfssdfsdj sdkfj sdkfj ksdfj bitch ksdfj' => 'dfssdfsdj sdkfj sdkfj ksdfj ##### ksdfj',
+			'122 jsdf ficken Sjdkf sdfj sdf' => '122 jsdf ###### Sjdkf sdfj sdf',
+			'122 jsdf FICKEN sjdkf sdfjs sdf' => '122 jsdf ###### sjdkf sdfjs sdf',
+			'dddddddddd ARSCH ddddddddddddd' => 'dddddddddd ##### ddddddddddddd',
+			//'\';alert(String.fromCharCode(88,83,83))//\';alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83))//\";alert(String.fromCharCode(88,83,83))//--></SCRIPT>">\'><SCRIPT>alert(String.fromCharCode(88,83,83))</SCRIPT>' => null
 		);
 		);
-		foreach ($data as $key => $value) {
-			$res = $this->Format->wordCensor($value, array('Arsch', 'Ficken'));
+		foreach ($data as $value => $expected) {
+			$res = $this->Format->wordCensor($value, array('Arsch', 'Ficken', 'Bitch'));
 
 
-			echo '\''.h($value).'\' becomes \''.h($res).'\'';
-			$this->assertTrue(!empty($res));
+			//debug('\''.h($value).'\' becomes \''.h($res).'\'', null, false); ob_flush();
+			$this->assertEquals($expected === null ? $value : $expected, $res);
 		}
 		}
 
 
 	}
 	}
@@ -162,16 +191,16 @@ class FormatHelperTest extends MyCakeTestCase {
 /*
 /*
 	public function testReverseAscii() {
 	public function testReverseAscii() {
 		$is = $this->Format->reverseAscii('f&eacute;s');
 		$is = $this->Format->reverseAscii('f&eacute;s');
-		$expected = 'fés';
+		$expected = 'fés';
 		$this->assertEquals($expected, $is);
 		$this->assertEquals($expected, $is);
 
 
 
 
 		$is = entDec('f&eacute;s');
 		$is = entDec('f&eacute;s');
-		$expected = 'fés';
+		$expected = 'fés';
 		$this->assertEquals($expected, $is);
 		$this->assertEquals($expected, $is);
 
 
 		$is = html_entity_decode('f&eacute;s');
 		$is = html_entity_decode('f&eacute;s');
-		$expected = 'fés';
+		$expected = 'fés';
 		$this->assertEquals($expected, $is);
 		$this->assertEquals($expected, $is);
 
 
 		#TODO: correct it + more
 		#TODO: correct it + more
@@ -185,7 +214,7 @@ class FormatHelperTest extends MyCakeTestCase {
 /*
 /*
 	public function testDecodeEntities() {
 	public function testDecodeEntities() {
 		$is = $this->Format->decodeEntities('f&eacute;s');
 		$is = $this->Format->decodeEntities('f&eacute;s');
-		$expected = 'fés';
+		$expected = 'fés';
 		$this->assertEquals($expected, $is);
 		$this->assertEquals($expected, $is);
 
 
 	}
 	}

+ 1 - 1
View/Helper/FormExtHelper.php

@@ -860,7 +860,7 @@ jQuery(\''.$selector.'\').maxlength('.$this->Js->object($settings, array('quoteK
 			$js .= 'jQuery(\''.$selector.'\').charCount('.$this->Js->object($settings, array('quoteKeys'=>false)).');';
 			$js .= 'jQuery(\''.$selector.'\').charCount('.$this->Js->object($settings, array('quoteKeys'=>false)).');';
 		}
 		}
 		$js = $this->documentReady($js);
 		$js = $this->documentReady($js);
-		return $this->Html->scriptBlock($js);
+		return $this->Html->scriptBlock($js, array('inline' => isset($options['inline']) ? $options['inline'] : true));
 	}
 	}
 
 
 
 

+ 73 - 9
View/Helper/FormatHelper.php

@@ -4,10 +4,15 @@ App::uses('TextHelper', 'View/Helper');
 App::import('Helper', 'Text');
 App::import('Helper', 'Text');
 
 
 /**
 /**
- * Format helper
+ * Format helper with basic html snippets
+ *
+ * TODO: make snippets more "css and background image" (instead of inline img links)
+ * TODO: test cases
+ *
  * 2009-12-31 ms
  * 2009-12-31 ms
  */
  */
 class FormatHelper extends TextHelper {
 class FormatHelper extends TextHelper {
+
 	/**
 	/**
 	 * Other helpers used by FormHelper
 	 * Other helpers used by FormHelper
 	 *
 	 *
@@ -16,10 +21,6 @@ class FormatHelper extends TextHelper {
 	 */
 	 */
 	public $helpers = array('Html', 'Form', 'Tools.Common', 'Tools.Gravatar', 'Tools.PhpThumb');
 	public $helpers = array('Html', 'Form', 'Tools.Common', 'Tools.Gravatar', 'Tools.PhpThumb');
 
 
-
-
-
-
 	/**
 	/**
 	 * jqueryAccess: {id}Pro, {id}Contra
 	 * jqueryAccess: {id}Pro, {id}Contra
 	 * 2009-07-24 ms
 	 * 2009-07-24 ms
@@ -233,6 +234,70 @@ class FormatHelper extends TextHelper {
 		return $this->Html->image(IMG_ICONS.$icon.'_'.$image.'.'.$ending);
 		return $this->Html->image(IMG_ICONS.$icon.'_'.$image.'.'.$ending);
 	}
 	}
 
 
+
+	/**
+	 * @param value
+	 * @param array $options
+	 * - max (3/5, defaults to 5)
+	 * - normal: display an icon for normal as well (defaults to false)
+	 * - map: array (manually map values, if you use 1 based values no need for that)
+	 * - title, alt, ...
+	 * @return string $html
+	 * 2012-08-02 ms
+	 */
+	public function priorityIcon($value, $options = array()) {
+		$defaults = array(
+			'max' => 5,
+			'normal' => false,
+			'map' => array(),
+			'css' => true,
+		);
+		$options = am($defaults, $options);
+		extract($options);
+
+		$matching = array(
+			1 => 'low',
+			2 => 'lower',
+			3 => 'normal',
+			4 => 'higher',
+			5 => 'high'
+		);
+
+		if (!empty($map)) {
+			$value = $map[$value];
+		}
+		if (!$normal && $value == ($max+1)/2) {
+			return '';
+		}
+
+		if ($max != 5) {
+			if ($value == 2) {
+				$value = 3;
+			} elseif ($value == 3) {
+				$value = 5;
+			}
+		}
+
+		$attr = array(
+			'class' => 'prio-'.$matching[$value],
+			'title' => __('prio'.ucfirst($matching[$value])),
+		);
+		if (!$css) {
+			$attr['alt'] = $matching[$value];
+		}
+		$attr = am($attr, array_diff_key($options, $defaults));
+
+		if ($css) {
+			$html = $this->Html->tag('div', '&nbsp;', $attr);
+
+		} else {
+			$icon = 'priority_' . $matching[$value] . '.gif';
+			$html = $this->Html->image('icons/'.$icon, $attr);
+		}
+
+		return $html;
+	}
+
 	/**
 	/**
 	 * Quick way of printing default icons (have to be 16px X 16px !!!)
 	 * Quick way of printing default icons (have to be 16px X 16px !!!)
 	 * @param type
 	 * @param type
@@ -1304,11 +1369,10 @@ class FormatHelper extends TextHelper {
 	 * @return	string
 	 * @return	string
 	 * 2009-11-11 ms
 	 * 2009-11-11 ms
 	 */
 	 */
-	public function wordCensor($str, $censored, $replacement = '') {
-		if (!is_array($censored)) {
+	public function wordCensor($str, $censored, $replacement = null) {
+		if (empty($censored)) {
 			return $str;
 			return $str;
 		}
 		}
-
 		$str = ' ' . $str . ' ';
 		$str = ' ' . $str . ' ';
 
 
 		// \w, \b and a few others do not match on a unicode character
 		// \w, \b and a few others do not match on a unicode character
@@ -1318,7 +1382,7 @@ class FormatHelper extends TextHelper {
 		$delim = '[-_\'\"`() {}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
 		$delim = '[-_\'\"`() {}<>\[\]|!?@#%&,.:;^~*+=\/ 0-9\n\r\t]';
 
 
 		foreach ($censored as $badword) {
 		foreach ($censored as $badword) {
-			if ($replacement != '') {
+			if ($replacement !== null) {
 				$str = preg_replace("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/i", "\\1{$replacement}\\3", $str);
 				$str = preg_replace("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/i", "\\1{$replacement}\\3", $str);
 			} else {
 			} else {
 				$str = preg_replace("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/ie", "'\\1'.str_repeat('#', strlen('\\2')).'\\3'",
 				$str = preg_replace("/({$delim})(" . str_replace('\*', '\w*?', preg_quote($badword, '/')) . ")({$delim})/ie", "'\\1'.str_repeat('#', strlen('\\2')).'\\3'",