浏览代码

also make cIcon templateable

euromark 11 年之前
父节点
当前提交
4962a4d438
共有 2 个文件被更改,包括 131 次插入35 次删除
  1. 60 0
      Test/Case/View/Helper/FormatHelperTest.php
  2. 71 35
      View/Helper/FormatHelper.php

+ 60 - 0
Test/Case/View/Helper/FormatHelperTest.php

@@ -63,6 +63,17 @@ class FormatHelperTest extends MyCakeTestCase {
 	}
 
 	/**
+	 * FormatHelperTest::testCIcon()
+	 *
+	 * @return void
+	 */
+	public function testCIcon() {
+		$result = $this->Format->cIcon('edit.png');
+		$expected = '<img src="/img/icons/edit.png" title="' . __('Edit') . '" alt="[' . __('Edit') . ']" class="icon" />';
+		$this->assertEquals($expected, $result);
+	}
+
+	/**
 	 * FormatHelperTest::testIconWithFontIcon()
 	 *
 	 * @return void
@@ -75,6 +86,18 @@ class FormatHelperTest extends MyCakeTestCase {
 	}
 
 	/**
+	 * FormatHelperTest::testCIconWithFontIcon()
+	 *
+	 * @return void
+	 */
+	public function testCIconWithFontIcon() {
+		$this->Format->settings['fontIcons'] = array('edit' => 'fa fa-pencil');
+		$result = $this->Format->cIcon('edit.png');
+		$expected = '<i class="fa fa-pencil edit" title="' . __('Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
+		$this->assertEquals($expected, $result);
+	}
+
+	/**
 	 * FormatHelperTest::testSpeedOfIcons()
 	 *
 	 * @return void
@@ -121,6 +144,7 @@ class FormatHelperTest extends MyCakeTestCase {
 	}
 
 	/**
+	 * @return void
 	 */
 	public function testOk() {
 		$content = 'xyz';
@@ -135,6 +159,42 @@ class FormatHelperTest extends MyCakeTestCase {
 		}
 	}
 
+	/**
+	 * FormatHelperTest::testConfigure()
+	 *
+	 * @return void
+	 */
+	public function testNeighbors() {
+		if (!defined('ICON_PREV')) {
+			define('ICON_PREV', 'prev');
+		}
+		if (!defined('ICON_NEXT')) {
+			define('ICON_NEXT', 'next');
+		}
+
+		$neighbors = array(
+			'prev' => array('ModelName' => array('id' => 1, 'foo' => 'bar')),
+			'next' => array('ModelName' => array('id' => 2, 'foo' => 'y')),
+		);
+		$result = $this->Format->neighbors($neighbors, 'foo');
+		$expected = '<div class="next-prev-navi nextPrevNavi"><a href="/index/1" title="bar"><img src="/img/icons/prev" alt="[]" class="icon" />&nbsp;prevRecord</a>&nbsp;&nbsp;<a href="/index/2" title="y"><img src="/img/icons/next" alt="[]" class="icon" />&nbsp;nextRecord</a></div>';
+
+		$this->assertEquals($expected, $result);
+
+
+		$this->Format->settings['fontIcons'] = array(
+			'prev' => 'fa fa-prev',
+			'next' => 'fa fa-next');
+		$result = $this->Format->neighbors($neighbors, 'foo');
+		$expected = '<div class="next-prev-navi nextPrevNavi"><a href="/index/1" title="bar"><i class="fa fa-prev prev" title="" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;prevRecord</a>&nbsp;&nbsp;<a href="/index/2" title="y"><i class="fa fa-next next" title="" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;nextRecord</a></div>';
+		$this->assertEquals($expected, $result);
+	}
+
+	/**
+	 * FormatHelperTest::testPriorityIcon()
+	 *
+	 * @return void
+	 */
 	public function testPriorityIcon() {
 		$values = array(
 			array(1, array(), '<div class="prio-low" title="prioLow">&nbsp;</div>'),

+ 71 - 35
View/Helper/FormatHelper.php

@@ -19,6 +19,8 @@ class FormatHelper extends TextHelper {
 	 */
 	public $helpers = array('Html', 'Tools.Numeric');
 
+	public $template;
+
 	protected $_defaultConfig = array(
 		'fontIcons' => false
 	);
@@ -30,6 +32,13 @@ class FormatHelper extends TextHelper {
 			$config['fontIcons'] = (array)Configure::read('Format.fontIcons');
 		}
 
+		$templates = array(
+			'icon' => '<i class="{{class}}" title="{{title}}" data-placement="bottom" data-toggle="tooltip"></i>',
+		) + (array)Configure::read('Format.templates');
+		if (!isset($this->template)) {
+			$this->template = new StringTemplate($templates);
+		}
+
 		parent::__construct($View, $config);
 	}
 
@@ -40,8 +49,8 @@ class FormatHelper extends TextHelper {
 	 */
 	public function thumbs($id, $inactive = false, $inactiveTitle = null) {
 		$class = 'Active';
-		$upTitle = __('Zustimmen');
-		$downTitle = __('Dagegen');
+		$upTitle = __('consentThis');
+		$downTitle = __('dissentThis');
 		if ($inactive === true) {
 			$class = 'Inactive';
 			$upTitle = $downTitle = !empty($inactiveTitle) ? $inactiveTitle : __('alreadyVoted');
@@ -114,7 +123,7 @@ class FormatHelper extends TextHelper {
 			$titleField = h($titleField);
 		}
 
-		$ret = '<div class="nextPrevNavi">';
+		$ret = '<div class="next-prev-navi nextPrevNavi">';
 		if (!empty($neighbors['prev'])) {
 			$url = array($neighbors['prev'][$alias]['id'], $prevSlug);
 			if (!empty($options['url'])) {
@@ -124,7 +133,7 @@ class FormatHelper extends TextHelper {
 			$ret .= $this->Html->link($this->cIcon(ICON_PREV, false) . '&nbsp;' . __('prev' . $name), $url, array('escape' => false, 'title' => $neighbors['prev'][$titleAlias][$titleField]));
 		} else {
 			$ret .= $this->cIcon(ICON_PREV_DISABLED, __('noPrev' . $name)) . '&nbsp;' . __('prev' . $name);
-}
+		}
 		$ret .= '&nbsp;&nbsp;';
 		if (!empty($neighbors['next'])) {
 			$url = array($neighbors['next'][$alias]['id'], $prevSlug);
@@ -373,29 +382,9 @@ class FormatHelper extends TextHelper {
 			return $this->Html->image('icons/' . $pic, $newOptions);
 		}
 
-		$iconType = $this->settings['fontIcons'][$type];
-
-		$templates = array(
-			'icon' => '<i class="{{class}}" title="{{title}}" data-placement="bottom" data-toggle="tooltip"></i>',
-		);
-		if (!isset($this->template)) {
-			$this->template = new StringTemplate($templates);
-		}
-
-		if (!$title) {
-			$title = ucfirst($type);
-			if ($translate !== false) {
-				$title = __($title);
-			}
-		}
-
-		$defaults = array(
-			'title' => $title,
-			'class' => $iconType . ' ' . $type
-		);
-
-		$options += $defaults;
-		return $this->template->format('icon', $options);
+		$options['title'] = $title;
+		$options['translate'] = $translate;
+		return $this->_fontIcon($type, $options);
 	}
 
 	/**
@@ -417,8 +406,8 @@ class FormatHelper extends TextHelper {
 			$t = isset($t['title']) ? $t['title'] : null; // deprecated
 		}
 
-		$title = (isset($t) ? $t : ucfirst(extractPathInfo('filename', $icon)));
-		//$alt = (isset($a)?$a:preg_replace('/[^a-zA-Z0-9]/', '', $title));
+		$type = extractPathInfo('filename', $icon);
+		$title = isset($t) ? $t : ucfirst($type);
 		$alt = (isset($a) ? $a : Inflector::slug($title, '-'));
 		if ($translate !== false) {
 			$title = __($title);
@@ -426,12 +415,43 @@ class FormatHelper extends TextHelper {
 		}
 		$alt = '[' . $alt . ']';
 
-		$defaultOptions = array('title' => $title, 'alt' => $alt, 'class' => 'icon');
-		$options = array_merge($defaultOptions, $options);
-		if (substr($icon, 0, 1) !== '/') {
-			$icon = 'icons/' . $icon;
+		if (!$this->settings['fontIcons'] || !isset($this->settings['fontIcons'][$type])) {
+			$defaults = array('title' => $title, 'alt' => $alt, 'class' => 'icon');
+			$options += $defaults;
+			if (substr($icon, 0, 1) !== '/') {
+				$icon = 'icons/' . $icon;
+			}
+			return $this->Html->image($icon, $options);
 		}
-		return $this->Html->image($icon, $options);
+
+		$options['title'] = $title;
+		$options['translate'] = $translate;
+		return $this->_fontIcon($type, $options);
+	}
+
+	/**
+	 * FormatHelper::_fontIcon()
+	 *
+	 * @param string $type
+	 * @param array $options
+	 * @return string
+	 */
+	protected function _fontIcon($type, $options) {
+		$iconType = $this->settings['fontIcons'][$type];
+
+		$defaults = array(
+			'class' => $iconType . ' ' . $type
+		);
+		$options += $defaults;
+
+		if (!isset($options['title'])) {
+			$options['title'] = ucfirst($options['title']);
+			if ($options['translate'] !== false) {
+				$options['title'] = __($options['title']);
+			}
+		}
+
+		return $this->template->format('icon', $options);
 	}
 
 	/**
@@ -852,15 +872,29 @@ class FormatHelper extends TextHelper {
 		$htmlOptions = array_merge($htmlDefaults, $htmlOptions);
 		//return $this->Html->image('/files/progress_bar/index.php'.$params, $htmlOptions);
 
-		// bug in Html::webroot() ??? ommits ?...
 		return '<img src="' . $this->Html->url('/files') . '/progress_bar/index.php' . $params . '" title="' . $htmlOptions['title'] . '" class="' .
 			$htmlOptions['class'] . '" alt="' . $htmlOptions['title'] . '" />';
 	}
 
+	/**
+	 * FormatHelper::tip()
+	 *
+	 * @param mixed $type
+	 * @param mixed $file
+	 * @param mixed $title
+	 * @param mixed $icon
+	 * @return string
+	 */
 	public function tip($type, $file, $title, $icon) {
 		return $this->cIcon($icon, $title, null, null, array('class' => 'tip' . ucfirst($type) . ' hand', 'rel' => $file));
 	}
 
+	/**
+	 * FormatHelper::tipHelp()
+	 *
+	 * @param mixed $file
+	 * @return string
+	 */
 	public function tipHelp($file) {
 		return $this->tip('help', $file, 'Hilfe', ICON_HELP);
 	}
@@ -892,6 +926,8 @@ class FormatHelper extends TextHelper {
 
 	/**
 	 * Display traffic light for status etc
+	 *
+	 * @return void
 	 */
 	public function statusLight($color = null, $title = null, $alt = null, $options = array()) {
 		$icons = array(