/lib/extension/sfMondongoExtensionForms.php

https://github.com/pycmam/sfMondongoPlugin · PHP · 304 lines · 182 code · 42 blank · 80 comment · 9 complexity · 3d7274e58c8845d78c5b247d60e4ef2a MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright 2010 Pablo Díez Pascual <pablodip@gmail.com>
  4. *
  5. * This file is part of sfMondongoPlugin.
  6. *
  7. * sfMondongoPlugin is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * sfMondongoPlugin is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with sfMondongoPlugin. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. use Mondongo\Inflector;
  21. use Mondongo\Mondator\Definition\Definition;
  22. use Mondongo\Mondator\Definition\Method;
  23. use Mondongo\Mondator\Extension;
  24. use Mondongo\Mondator\Output\Output;
  25. /**
  26. * sfMondongoExtensionForms.
  27. *
  28. * @package sfMondongoPlugin
  29. * @author Pablo Díez Pascual <pablodip@gmail.com>
  30. */
  31. class sfMondongoExtensionForms extends Extension
  32. {
  33. protected function setUp()
  34. {
  35. $this->addRequiredOption('output');
  36. }
  37. protected function doProcess()
  38. {
  39. if ($this->configClass['is_embedded'])
  40. {
  41. return;
  42. }
  43. /*
  44. * Definitions.
  45. */
  46. $this->definitions['form'] = $definition = new Definition($this->class.'Form');
  47. $definition->setParentClass('Base'.$this->class.'Form');
  48. $definition->setDocComment(<<<EOF
  49. /**
  50. * {$this->class} Form.
  51. */
  52. EOF
  53. );
  54. $this->definitions['form_base'] = $definition = new Definition('Base'.$this->class.'Form');
  55. $definition->setParentClass('BaseFormMondongo');
  56. $definition->setDocComment(<<<EOF
  57. /**
  58. * {$this->class} Base Form.
  59. */
  60. EOF
  61. );
  62. /*
  63. * Outputs.
  64. */
  65. $this->outputs['form'] = new Output($this->getOption('output'));
  66. $this->outputs['form_base'] = new Output($this->getOption('output').'/Base', true);
  67. /*
  68. * Process.
  69. */
  70. $this->processSetupMethod();
  71. $this->processGetModelNameMethod();
  72. /*
  73. * Plugins.
  74. */
  75. if (isset($this->configClass['plugin_name']) && isset($this->configClass['plugin_dir']))
  76. {
  77. // definitions
  78. $this->definitions['form']->setParentClass('Plugin'.$this->class.'Form');
  79. $this->definitions['form_plugin'] = $definition = new Definition('Plugin'.$this->class.'Form');
  80. $definition->setParentClass('Base'.$this->class.'Form');
  81. $definition->setDocComment(<<<EOF
  82. /**
  83. * {$this->class} Plugin Form.
  84. */
  85. EOF
  86. );
  87. // outputs
  88. $this->outputs['form']->setDir($this->outputs['form']->getDir().'/'.$this->configClass['plugin_name']);
  89. $this->outputs['form_base']->setDir($this->outputs['form']->getDir().'/Base');
  90. $this->outputs['form_plugin'] = new Output($this->configClass['plugin_dir'].'/lib/model/form');
  91. }
  92. }
  93. /*
  94. * "setup" method.
  95. */
  96. protected function processSetupMethod()
  97. {
  98. /*
  99. * Widgets.
  100. */
  101. $widgets = array();
  102. // fields
  103. foreach ($this->configClass['fields'] as $name => $field)
  104. {
  105. $widgets[$name] = array(
  106. 'class' => $this->getWidgetClassForType($field['type']),
  107. 'options' => $this->getWidgetOptionsForType($field['type']),
  108. );
  109. }
  110. // references
  111. foreach ($this->configClass['references'] as $name => $reference)
  112. {
  113. $options = array('model' => $reference['class'], 'multiple' => 'one' == $reference['type'] ? false : true);
  114. $widgets[$reference['field']] = array(
  115. 'class' => 'sfWidgetFormMondongoChoice',
  116. 'options' => var_export($options, true),
  117. );
  118. }
  119. // code
  120. $widgetsCode = '';
  121. foreach ($widgets as $name => $widget)
  122. {
  123. $widgetsCode .= <<<EOF
  124. '$name' => new {$widget['class']}({$widget['options']}),
  125. EOF;
  126. }
  127. /*
  128. * Validators.
  129. */
  130. $validators = array();
  131. // fields
  132. foreach ($this->configClass['fields'] as $name => $field)
  133. {
  134. $validators[$name] = array(
  135. 'class' => $this->getValidatorClassForType($field['type']),
  136. 'options' => $this->getValidatorOptionsForType($field['type'], $field),
  137. );
  138. }
  139. // references
  140. foreach ($this->configClass['references'] as $name => $validator)
  141. {
  142. $options = array('model' => $reference['class'], 'multiple' => 'one' == $reference['type'] ? false : true);
  143. $validators[$reference['field']] = array(
  144. 'class' => 'sfValidatorMondongoChoice',
  145. 'options' => var_export($options, true),
  146. );
  147. }
  148. // code
  149. $validatorsCode = '';
  150. foreach ($validators as $name => $validator)
  151. {
  152. $validatorsCode .= <<<EOF
  153. '$name' => new {$validator['class']}({$validator['options']}),
  154. EOF;
  155. }
  156. /*
  157. * nameFormat
  158. */
  159. $nameFormat = Inflector::underscore($this->class);
  160. $code = <<<EOF
  161. \$this->setWidgets(array(
  162. $widgetsCode
  163. ));
  164. \$this->setValidators(array(
  165. $validatorsCode
  166. ));
  167. \$this->widgetSchema->setNameFormat('{$nameFormat}[%s]');
  168. EOF
  169. ;
  170. $method = new Method('public', 'setup', '', $code);
  171. $method->setDocComment(<<<EOF
  172. /**
  173. * @see sfForm
  174. */
  175. EOF
  176. );
  177. $this->definitions['form_base']->addMethod($method);
  178. }
  179. /*
  180. * "getModelName" method.
  181. */
  182. protected function processGetModelNameMethod()
  183. {
  184. $code = <<<EOF
  185. return '$this->class';
  186. EOF;
  187. $method = new Method('public', 'getModelName', '', $code);
  188. $method->setDocComment(<<<EOF
  189. /**
  190. * @see sfMondongoForm
  191. */
  192. EOF
  193. );
  194. $this->definitions['form_base']->addMethod($method);
  195. }
  196. /*
  197. * Widgets.
  198. */
  199. protected function getWidgetClassForType($type)
  200. {
  201. $class = 'sfWidgetFormInputText';
  202. switch ($type)
  203. {
  204. case 'bin_data':
  205. $class = 'sfWidgetFormInputFile';
  206. break;
  207. case 'boolean':
  208. $class = 'sfWidgetFormInputCheckbox';
  209. break;
  210. case 'date':
  211. $class = 'sfWidgetFormDateTime';
  212. break;
  213. }
  214. return $class;
  215. }
  216. protected function getWidgetOptionsForType($type)
  217. {
  218. $options = array();
  219. $attributes = array();
  220. $options = count($options) ? sprintf('array(%s)', implode(', ', $options)) : 'array()';
  221. $attributes = count($attributes) ? sprintf('array(%s)', implode(', ', $attributes)) : 'array()';
  222. return sprintf('%s, %s', $options, $attributes);
  223. }
  224. /**
  225. * Validators.
  226. */
  227. protected function getValidatorClassForType($type)
  228. {
  229. $class = 'sfValidatorString';
  230. switch ($type)
  231. {
  232. case 'bin_data':
  233. $class = 'sfValidatorFile';
  234. break;
  235. case 'boolean':
  236. $class = 'sfValidatorBoolean';
  237. break;
  238. case 'date':
  239. $class = 'sfValidatorDateTime';
  240. break;
  241. case 'float':
  242. $class = 'sfValidatorNumber';
  243. break;
  244. case 'integer':
  245. $class = 'sfValidatorInteger';
  246. break;
  247. }
  248. return $class;
  249. }
  250. protected function getValidatorOptionsForType($type, array $field)
  251. {
  252. $options = array();
  253. $attributes = array();
  254. if (isset($field['required']) && !$field['required'])
  255. {
  256. $options['required'] = false;
  257. }
  258. $options = count($options) ? sprintf('array(%s)', implode(', ', $options)) : 'array()';
  259. $attributes = count($attributes) ? sprintf('array(%s)', implode(', ', $attributes)) : 'array()';
  260. return sprintf('%s, %s', $options, $attributes);
  261. }
  262. }