PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/sbweb/sbweb_logica/lib/symfony/plugins/sfPropelPlugin/lib/widget/sfWidgetFormPropelChoice.class.php

http://opac-sbweb.googlecode.com/
PHP | 108 lines | 53 code | 12 blank | 43 comment | 4 complexity | 828dd6ff8f35f1f9e1d9d10f3ff1d588 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfWidgetFormPropelChoice represents a choice widget for a model.
  11. *
  12. * @package symfony
  13. * @subpackage widget
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfWidgetFormPropelChoice.class.php 12803 2008-11-09 07:26:18Z fabien $
  16. */
  17. class sfWidgetFormPropelChoice extends sfWidgetFormChoice
  18. {
  19. /**
  20. * @see sfWidget
  21. */
  22. public function __construct($options = array(), $attributes = array())
  23. {
  24. $options['choices'] = new sfCallable(array($this, 'getChoices'));
  25. parent::__construct($options, $attributes);
  26. }
  27. /**
  28. * Constructor.
  29. *
  30. * Available options:
  31. *
  32. * * model: The model class (required)
  33. * * add_empty: Whether to add a first empty value or not (false by default)
  34. * If the option is not a Boolean, the value will be used as the text value
  35. * * method: The method to use to display object values (__toString by default)
  36. * * key_method: The method to use to display the object keys (getPrimaryKey by default)
  37. * * order_by: An array composed of two fields:
  38. * * The column to order by the results (must be in the PhpName format)
  39. * * asc or desc
  40. * * criteria: A criteria to use when retrieving objects
  41. * * connection: The Propel connection to use (null by default)
  42. * * multiple: true if the select tag must allow multiple selections
  43. * * peer_method: The peer method to use to fetch objects
  44. *
  45. * @see sfWidgetFormSelect
  46. */
  47. protected function configure($options = array(), $attributes = array())
  48. {
  49. $this->addRequiredOption('model');
  50. $this->addOption('add_empty', false);
  51. $this->addOption('method', '__toString');
  52. $this->addOption('key_method', 'getPrimaryKey');
  53. $this->addOption('order_by', null);
  54. $this->addOption('criteria', null);
  55. $this->addOption('connection', null);
  56. $this->addOption('multiple', false);
  57. $this->addOption('peer_method', 'doSelect');
  58. parent::configure($options, $attributes);
  59. }
  60. /**
  61. * Returns the choices associated to the model.
  62. *
  63. * @return array An array of choices
  64. */
  65. public function getChoices()
  66. {
  67. $choices = array();
  68. if (false !== $this->getOption('add_empty'))
  69. {
  70. $choices[''] = true === $this->getOption('add_empty') ? '' : $this->getOption('add_empty');
  71. }
  72. $class = constant($this->getOption('model').'::PEER');
  73. $criteria = is_null($this->getOption('criteria')) ? new Criteria() : clone $this->getOption('criteria');
  74. if ($order = $this->getOption('order_by'))
  75. {
  76. $method = sprintf('add%sOrderByColumn', 0 === strpos(strtoupper($order[1]), 'ASC') ? 'Ascending' : 'Descending');
  77. $criteria->$method(call_user_func(array($class, 'translateFieldName'), $order[0], BasePeer::TYPE_PHPNAME, BasePeer::TYPE_COLNAME));
  78. }
  79. $objects = call_user_func(array($class, $this->getOption('peer_method')), $criteria, $this->getOption('connection'));
  80. $methodKey = $this->getOption('key_method');
  81. if (!method_exists($this->getOption('model'), $methodKey))
  82. {
  83. throw new RuntimeException(sprintf('Class "%s" must implement a "%s" method to be rendered in a "%s" widget', $this->getOption('model'), $methodKey, __CLASS__));
  84. }
  85. $methodValue = $this->getOption('method');
  86. if (!method_exists($this->getOption('model'), $methodValue))
  87. {
  88. throw new RuntimeException(sprintf('Class "%s" must implement a "%s" method to be rendered in a "%s" widget', $this->getOption('model'), $methodValue, __CLASS__));
  89. }
  90. foreach ($objects as $object)
  91. {
  92. $choices[$object->$methodKey()] = $object->$methodValue();
  93. }
  94. return $choices;
  95. }
  96. }