PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/propel_15/vendor/propel/contrib/pat/patForms/Rule.php

http://github.com/eventhorizonpl/forked-php-orm-benchmark
PHP | 340 lines | 97 code | 34 blank | 209 comment | 12 complexity | 0967ae1caf87e7fd590e76c36a6a5168 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * patForms rule base class
  4. *
  5. * $Id: Rule.php 1347 2009-12-03 21:06:36Z francois $
  6. *
  7. * @access protected
  8. * @package patForms
  9. * @subpackage Rules
  10. */
  11. /**
  12. * patForms rule base class
  13. *
  14. * @access protected
  15. * @package patForms
  16. * @subpackage Rules
  17. * @author Stephan Schmidt <schst@php-tools.net>
  18. * @license LGPL, see license.txt for details
  19. * @link http://www.php-tools.net
  20. * @todo implement javascript helper methods (set a javascript property plus an
  21. * array of keys that will be replaced by the properties of the rule)
  22. */
  23. class patForms_Rule
  24. {
  25. /**
  26. * time when the rule should be applied
  27. *
  28. * Possible values are:
  29. * -PATFORMS_RULE_BEFORE_VALIDATION
  30. * -PATFORMS_RULE_AFTER_VALIDATION
  31. * -PATFORMS_RULE_BOTH
  32. *
  33. * @access private
  34. * @var integer
  35. */
  36. var $_time = PATFORMS_RULE_AFTER_VALIDATION;
  37. /**
  38. * script that will be displayed only once
  39. *
  40. * @access private
  41. * @var array
  42. */
  43. var $globalScript = array();
  44. /**
  45. * script that will be displayed once per instance
  46. *
  47. * @access private
  48. * @var array
  49. */
  50. var $instanceScript = array();
  51. /**
  52. * properties that have to be replaced in the instance script.
  53. *
  54. * @access private
  55. * @var array
  56. */
  57. var $scriptPlaceholders = array();
  58. /**
  59. * store the container of the rule
  60. *
  61. * @access private
  62. * @var object
  63. */
  64. var $container;
  65. /**
  66. * define error codes an messages for each form element
  67. *
  68. * @abstract
  69. * @access private
  70. * @var array
  71. */
  72. var $validatorErrorCodes = array();
  73. /**
  74. * error code offset for the rule
  75. *
  76. * @abstract
  77. * @access private
  78. */
  79. var $errorOffset;
  80. /**
  81. * format of the rule
  82. *
  83. * @abstract
  84. * @access private
  85. */
  86. var $format = 'html';
  87. /**
  88. * name of the rule
  89. *
  90. * @abstract
  91. * @access private
  92. */
  93. var $ruleName = '';
  94. /**
  95. * Get the time when the rule should be applied.
  96. *
  97. * This has to be defined in the _time property of the rule.
  98. *
  99. * @access public
  100. * @return integer
  101. */
  102. function getTime()
  103. {
  104. return $this->_time;
  105. }
  106. /**
  107. * create a new rule object
  108. *
  109. * @access public
  110. * @param string id
  111. */
  112. function patForms_Rule( $id = null )
  113. {
  114. if ( $id === null )
  115. {
  116. $id = uniqid( '' );
  117. }
  118. $this->_id = $id;
  119. }
  120. /**
  121. * set the id for the rule
  122. *
  123. * @access public
  124. * @param string id
  125. */
  126. function setId( $id )
  127. {
  128. $this->_id = $id;
  129. }
  130. /**
  131. * set the locale, this is needed to update the rule
  132. * translations, that have been passed to the container
  133. * element
  134. *
  135. * @access public
  136. * @param string new locale
  137. * @return boolean
  138. */
  139. function setLocale( $locale )
  140. {
  141. // rules do not store locale information
  142. if (!patForms::isCustomLocale($locale)) {
  143. return true;
  144. }
  145. $errorMessages = patForms::getCustomLocale($locale, 'Rule::' . $this->getRuleName());
  146. if (is_array($errorMessages)) {
  147. $this->validatorErrorCodes[$locale] = $errorMessages;
  148. }
  149. $this->container->addValidatorErrorCodes( $this->validatorErrorCodes, $this->errorOffset );
  150. return true;
  151. }
  152. /**
  153. * prepare the rule
  154. *
  155. * This method is used to initialize the rule.
  156. * By default it adds it validatorErrorCodes
  157. * to the container and stores a reference to the
  158. * container.
  159. *
  160. * You may extend it in your custom rules, but should always be calling
  161. * this method using:
  162. *
  163. * <code>
  164. * patForms_Rule::prepareRule( $container );
  165. * </code>
  166. *
  167. * @access public
  168. * @param object Either a patForms or patForms_Element object
  169. */
  170. function prepareRule( &$container )
  171. {
  172. $this->format = $container->getFormat();
  173. $this->container = &$container;
  174. $this->errorOffset = $container->getErrorOffset();
  175. $container->addValidatorErrorCodes( $this->validatorErrorCodes, $this->errorOffset );
  176. return true;
  177. }
  178. /**
  179. * method called by patForms or any patForms_Element to validate the
  180. * element or the form.
  181. *
  182. * @abstract
  183. * @access public
  184. * @param object Either a patForms or patForms_Element object
  185. * @return boolean true, if rule has been applied succesfully, false otherwise
  186. */
  187. function applyRule( &$container, $type = PATFORMS_RULE_BEFORE_VALIDATION )
  188. {
  189. // your code
  190. }
  191. /**
  192. * addValidationError
  193. *
  194. * @access private
  195. * @param integer $code
  196. * @param array $vars fill named placeholder with values
  197. * @return boolean $result true on success
  198. */
  199. function addValidationError( $code, $vars = array() )
  200. {
  201. $code= $this->errorOffset + $code;
  202. return $this->container->addValidationError( $code, $vars );
  203. }
  204. /**
  205. * get the name of the rule
  206. *
  207. * By default just return the classname, this is sufficient.
  208. *
  209. * @access public
  210. * @return string
  211. */
  212. function getRuleName()
  213. {
  214. if (!empty($this->ruleName)) {
  215. return $this->ruleName;
  216. }
  217. return get_class( $this );
  218. }
  219. /**
  220. * get the global javascript of the rule
  221. *
  222. * @access public
  223. * @return string
  224. * @todo Rules need to know the output format
  225. */
  226. /*
  227. function getGlobalJavascript()
  228. {
  229. if ( isset( $this->globalScript['html'] ) )
  230. {
  231. return $this->globalScript['html'];
  232. }
  233. return '';
  234. }
  235. */
  236. /**
  237. * get the instance javascript of the rule
  238. *
  239. * @access public
  240. * @return string
  241. */
  242. /*
  243. function getInstanceJavascript()
  244. {
  245. if ( !isset( $this->instanceScript[$this->format] ) )
  246. {
  247. return false;
  248. }
  249. // get the script for the current format
  250. $script = $this->instanceScript[$this->format];
  251. // always replace the id
  252. $script = str_replace( '[RULE::ID]', $this->_id, $script );
  253. if ( method_exists( $this->container, 'getId' ) )
  254. {
  255. $script = str_replace( '[CONTAINER::ID]', $this->container->getId(), $script );
  256. }
  257. if ( method_exists( $this->container, 'getName' ) )
  258. {
  259. $script = str_replace( '[CONTAINER::NAME]', $this->container->getName(), $script );
  260. }
  261. foreach ( $this->scriptPlaceholders as $placeholder => $property )
  262. {
  263. if ( isset( $this->$property ) )
  264. $script = str_replace( '['.$placeholder.']', $this->$property, $script );
  265. else
  266. $script = str_replace( '['.$placeholder.']', '', $script );
  267. }
  268. return $script;
  269. }
  270. */
  271. function registerJavascripts(&$form) {
  272. if ($script = $this->getGlobalJavascript()) {
  273. $form->registerGlobalJavascript($this->getRuleName(), $script);
  274. }
  275. if ($script = $this->getInstanceJavascript()) {
  276. $form->registerInstanceJavascript($script);
  277. }
  278. }
  279. function getGlobalJavascript() {
  280. if (isset($this->globalScript[$this->format])) {
  281. return $this->globalScript[$this->format];
  282. }
  283. }
  284. function getInstanceJavascript(){
  285. if (isset($this->instanceScript[$this->format])) {
  286. $script = $this->instanceScript[$this->format];
  287. $script = str_replace('[RULE::ID]', $this->_id, $script);
  288. if (method_exists($this->container, 'getId')) {
  289. $script = str_replace('[CONTAINER::ID]', $this->container->getId(), $script);
  290. }
  291. if (method_exists($this->container, 'getName')) {
  292. $script = str_replace('[CONTAINER::NAME]', $this->container->getName(), $script);
  293. }
  294. foreach ($this->scriptPlaceholders as $placeholder => $property) {
  295. if (isset($this->$property)) {
  296. $script = str_replace('['.$placeholder.']', $this->$property, $script);
  297. } else {
  298. $script = str_replace('['.$placeholder.']', '', $script);
  299. }
  300. }
  301. return $script;
  302. }
  303. }
  304. }