Suggestion #6007 » DatetextboxViewHelper.php

Nathan L, 2010-01-13 16:59

 
1
<?php
2

    
3
/*                                                                        *
4
 * This script belongs to the FLOW3 package "Fluid".                      *
5
 *                                                                        *
6
 * It is free software; you can redistribute it and/or modify it under    *
7
 * the terms of the GNU Lesser General Public License as published by the *
8
 * Free Software Foundation, either version 3 of the License, or (at your *
9
 * option) any later version.                                             *
10
 *                                                                        *
11
 * This script is distributed in the hope that it will be useful, but     *
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
13
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
14
 * General Public License for more details.                               *
15
 *                                                                        *
16
 * You should have received a copy of the GNU Lesser General Public       *
17
 * License along with the script.                                         *
18
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
19
 *                                                                        *
20
 * The TYPO3 project - inspiring people to share!                         *
21
 *                                                                        */
22

    
23
/**
24
 * View Helper which creates a simple Text Box for entering
25
 * DateTime values (<input type="text">).
26
 *
27
  * = Examples =
28
 *
29
 * <code title="Example">
30
 * <f:form.datetextbox property="myTextBox" format="m-d-Y" />
31
 * </code>
32
 *
33
 * Output:
34
 * <input type="text" name="myTextBox" value="01-12-2010" />
35
 *
36
 * @version $Id$
37
 * @package Fluid
38
 * @subpackage ViewHelpers\Form
39
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
40
 * @api
41
 * @scope prototype
42
 */
43
class Tx_Fluid_ViewHelpers_Form_DatetextboxViewHelper extends Tx_Fluid_ViewHelpers_Form_TextboxViewHelper {
44

    
45
	
46
	/**
47
	 * Initialize the arguments.
48
	 *
49
	 * @return void
50
	 */
51
	public function initializeArguments() {
52
		parent::initializeArguments();
53
		$this->registerTagAttribute('format', 'string', 'The format to render the date in.');
54
	}
55

    
56
	/**
57
	 * Renders the textbox.
58
	 *
59
	 * @return string
60
	 * @api
61
	 */
62
	public function render() {
63
		$name = $this->getName();
64
		$this->registerFieldNameForFormTokenGeneration($name);
65
		
66
		$this->tag->addAttribute('type', 'text');
67
		$this->tag->addAttribute('name', $name);
68
		
69
		/** @var DateTime */
70
		$value = $this->getValue();		
71
		if ($value instanceof DateTime) {
72
			$stringValue = $value->format($this->getFormat());
73
		} else {
74
			$stringValue = '';
75
		}
76
		
77
		$this->tag->addAttribute('value', $stringValue);
78
		
79
		$this->setErrorClassAttribute();
80
		return $this->tag->render();
81
	}
82
	
83
	/**
84
	 * Get the format that was passed into the tag
85
	 * 
86
	 * @return string Date format compatible with date()
87
	 */
88
	protected function getFormat() {
89
		/**
90
		 * @TODO What happens when a format isn't provided?  Throw an exception or use a default?
91
		 */
92
		return $this->arguments['format'];
93
	}
94

    
95
}
96

    
97
?>
    (1-1/1)