Browse Source

Merge pull request #2965 from cakephp/3.0-session-helper

3.0 Update session helper & clean up HtmlHelper a bit
José Lorenzo Rodríguez 12 years ago
parent
commit
dfc7487775

+ 0 - 34
src/View/Helper/HtmlHelper.php

@@ -1,9 +1,5 @@
 <?php
 /**
- * Html Helper class file.
- *
- * Simplifies the construction of HTML elements.
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -52,29 +48,6 @@ class HtmlHelper extends Helper {
 		'metalink' => '<link href="%s"%s/>',
 		'link' => '<a href="%s"%s>%s</a>',
 		'mailto' => '<a href="mailto:%s" %s>%s</a>',
-		'form' => '<form action="%s"%s>',
-		'formend' => '</form>',
-		'input' => '<input name="%s"%s/>',
-		'textarea' => '<textarea name="%s"%s>%s</textarea>',
-		'hidden' => '<input type="hidden" name="%s"%s/>',
-		'checkbox' => '<input type="checkbox" name="%s" %s/>',
-		'checkboxmultiple' => '<input type="checkbox" name="%s[]"%s />',
-		'radio' => '<input type="radio" name="%s" id="%s"%s />%s',
-		'selectstart' => '<select name="%s"%s>',
-		'selectmultiplestart' => '<select name="%s[]"%s>',
-		'selectempty' => '<option value=""%s>&nbsp;</option>',
-		'selectoption' => '<option value="%s"%s>%s</option>',
-		'selectend' => '</select>',
-		'optiongroup' => '<optgroup label="%s"%s>',
-		'optiongroupend' => '</optgroup>',
-		'checkboxmultiplestart' => '',
-		'checkboxmultipleend' => '',
-		'password' => '<input type="password" name="%s" %s/>',
-		'file' => '<input type="file" name="%s" %s/>',
-		'file_no_model' => '<input type="file" name="%s" %s/>',
-		'submit' => '<input %s/>',
-		'submitimage' => '<input type="image" src="%s" %s/>',
-		'button' => '<button%s>%s</button>',
 		'image' => '<img src="%s" %s/>',
 		'tableheader' => '<th%s>%s</th>',
 		'tableheaderrow' => '<tr%s>%s</tr>',
@@ -83,25 +56,18 @@ class HtmlHelper extends Helper {
 		'block' => '<div%s>%s</div>',
 		'blockstart' => '<div%s>',
 		'blockend' => '</div>',
-		'hiddenblock' => '<div style="display:none;">%s</div>',
 		'tag' => '<%s%s>%s</%s>',
 		'tagstart' => '<%s%s>',
 		'tagend' => '</%s>',
 		'tagselfclosing' => '<%s%s/>',
 		'para' => '<p%s>%s</p>',
 		'parastart' => '<p%s>',
-		'label' => '<label for="%s"%s>%s</label>',
-		'fieldset' => '<fieldset%s>%s</fieldset>',
-		'fieldsetstart' => '<fieldset><legend>%s</legend>',
-		'fieldsetend' => '</fieldset>',
-		'legend' => '<legend>%s</legend>',
 		'css' => '<link rel="%s" type="text/css" href="%s" %s/>',
 		'style' => '<style type="text/css"%s>%s</style>',
 		'charset' => '<meta http-equiv="Content-Type" content="text/html; charset=%s" />',
 		'ul' => '<ul%s>%s</ul>',
 		'ol' => '<ol%s>%s</ol>',
 		'li' => '<li%s>%s</li>',
-		'error' => '<div%s>%s</div>',
 		'javascriptblock' => '<script%s>%s</script>',
 		'javascriptstart' => '<script>',
 		'javascriptlink' => '<script type="text/javascript" src="%s"%s></script>',

+ 0 - 4
src/View/Helper/NumberHelper.php

@@ -1,9 +1,5 @@
 <?php
 /**
- * Number Helper.
- *
- * Methods to make numbers more readable.
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *

+ 58 - 30
src/View/Helper/SessionHelper.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * Session Helper provides access to the Session in the Views.
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -18,6 +16,8 @@ namespace Cake\View\Helper;
 
 use Cake\Network\Session;
 use Cake\View\Helper;
+use Cake\View\Helper\StringTemplateTrait;
+use Cake\View\View;
 
 /**
  * Session Helper.
@@ -28,6 +28,30 @@ use Cake\View\Helper;
  */
 class SessionHelper extends Helper {
 
+	use StringTemplateTrait;
+
+/**
+ * Default templates to use.
+ *
+ * @var array
+ */
+	protected $_defaultTemplates = [
+		'flash' => '<div id="{{key}}Message" class="{{class}}">{{message}}</div>'
+	];
+
+/**
+ * Construct the helper and sets up templates
+ *
+ * @param \Cake\View\View $view The View this helper is being attached to.
+ * @param array $settings Configuration settings for the helper.
+ */
+	public function __construct(View $view, $settings = []) {
+		$settings += ['templates' => null];
+		parent::__construct($view, $settings);
+
+		$this->initStringTemplates($this->_defaultTemplates);
+	}
+
 /**
  * Used to read a session values set in a controller for a key or return values for all keys.
  *
@@ -103,8 +127,8 @@ class SessionHelper extends Helper {
  *
  * {{{
  * echo $this->Session->flash('flash', array(
- *		'element' => 'my_custom_element',
- *		'params' => array('plugin' => 'my_plugin')
+ *   'element' => 'my_custom_element',
+ *   'params' => array('plugin' => 'my_plugin')
  * ));
  * }}}
  *
@@ -114,37 +138,41 @@ class SessionHelper extends Helper {
  * @return string
  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/session.html#SessionHelper::flash
  */
-	public function flash($key = 'flash', $attrs = array()) {
-		$out = false;
+	public function flash($key = 'flash', $attrs = []) {
+		if (!Session::check('Message.' . $key)) {
+			return '';
+		}
 
-		if (Session::check('Message.' . $key)) {
-			$flash = Session::read('Message.' . $key);
-			$message = $flash['message'];
-			unset($flash['message']);
+		$flash = Session::read('Message.' . $key);
+		$message = $flash['message'];
+		unset($flash['message']);
 
-			if (!empty($attrs)) {
-				$flash = array_merge($flash, $attrs);
-			}
+		if (!empty($attrs)) {
+			$flash = array_merge($flash, $attrs);
+		}
 
-			if ($flash['element'] === 'default') {
-				$class = 'message';
-				if (!empty($flash['params']['class'])) {
-					$class = $flash['params']['class'];
-				}
-				$out = '<div id="' . $key . 'Message" class="' . $class . '">' . $message . '</div>';
-			} elseif (!$flash['element']) {
-				$out = $message;
-			} else {
-				$options = array();
-				if (isset($flash['params']['plugin'])) {
-					$options['plugin'] = $flash['params']['plugin'];
-				}
-				$tmpVars = $flash['params'];
-				$tmpVars['message'] = $message;
-				$out = $this->_View->element($flash['element'], $tmpVars, $options);
+		if ($flash['element'] === 'default') {
+			$class = 'message';
+			if (!empty($flash['params']['class'])) {
+				$class = $flash['params']['class'];
+			}
+			$out = $this->formatTemplate('flash', [
+				'class' => $class,
+				'key' => $key,
+				'message' => $message
+			]);
+		} elseif (!$flash['element']) {
+			$out = $message;
+		} else {
+			$options = array();
+			if (isset($flash['params']['plugin'])) {
+				$options['plugin'] = $flash['params']['plugin'];
 			}
-			Session::delete('Message.' . $key);
+			$tmpVars = $flash['params'];
+			$tmpVars['message'] = $message;
+			$out = $this->_View->element($flash['element'], $tmpVars, $options);
 		}
+		Session::delete('Message.' . $key);
 		return $out;
 	}
 

+ 0 - 4
src/View/Helper/TextHelper.php

@@ -1,9 +1,5 @@
 <?php
 /**
- * Text Helper
- *
- * Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *

+ 0 - 2
src/View/Helper/TimeHelper.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * Time Helper class file.
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *

+ 7 - 7
tests/TestCase/View/Helper/HtmlHelperTest.php

@@ -1819,14 +1819,14 @@ class HtmlHelperTest extends TestCase {
 		$result = $this->Html->useTag('unknowntag');
 		$this->assertEquals('', $result);
 
-		$result = $this->Html->useTag('formend');
-		$this->assertTags($result, '/form');
+		$result = $this->Html->useTag('blockend');
+		$this->assertEquals('</div>', $result);
 
-		$result = $this->Html->useTag('form', 'url', ' test');
-		$this->assertEquals('<form action="url" test>', $result);
+		$result = $this->Html->useTag('image', 'url', 'test');
+		$this->assertEquals('<img src="url" test/>', $result);
 
-		$result = $this->Html->useTag('form', 'example.com', array('test' => 'ok'));
-		$this->assertTags($result, array('form' => array('test' => 'ok', 'action' => 'example.com')));
+		$result = $this->Html->useTag('image', 'example.com', array('test' => 'ok'));
+		$this->assertEquals('<img src="example.com"  test="ok"/>', $result);
 	}
 
 /**
@@ -2093,7 +2093,7 @@ class HtmlHelperTest extends TestCase {
 		$tags = $this->Html->getAttribute('_tags');
 		$this->assertEquals('start form', $tags['form']);
 		$this->assertEquals('finish form', $tags['formend']);
-		$this->assertEquals('</select>', $tags['selectend']);
+		$this->assertEquals('</div>', $tags['blockend']);
 
 		$result = $this->Html->loadConfig(array('htmlhelper_minimized.ini', 'ini'), $path);
 		$expected = array(