| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfWidgetFormPropelChoice extends sfWidgetFormChoice |
|---|
| 20 |
{ |
|---|
| 21 |
|
|---|
| 22 |
* @see sfWidget |
|---|
| 23 |
*/ |
|---|
| 24 |
public function __construct($options = array(), $attributes = array()) |
|---|
| 25 |
{ |
|---|
| 26 |
$options['choices'] = array(); |
|---|
| 27 |
|
|---|
| 28 |
parent::__construct($options, $attributes); |
|---|
| 29 |
} |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
* Constructor. |
|---|
| 33 |
* |
|---|
| 34 |
* Available options: |
|---|
| 35 |
* |
|---|
| 36 |
* * model: The model class (required) |
|---|
| 37 |
* * add_empty: Whether to add a first empty value or not (false by default) |
|---|
| 38 |
* If the option is not a Boolean, the value will be used as the text value |
|---|
| 39 |
* * method: The method to use to display object values (__toString by default) |
|---|
| 40 |
* * key_method: The method to use to display the object keys (getPrimaryKey by default) |
|---|
| 41 |
* * order_by: An array composed of two fields: |
|---|
| 42 |
* * The column to order by the results (must be in the PhpName format) |
|---|
| 43 |
* * asc or desc |
|---|
| 44 |
* * criteria: A criteria to use when retrieving objects |
|---|
| 45 |
* * connection: The Propel connection to use (null by default) |
|---|
| 46 |
* * multiple: true if the select tag must allow multiple selections |
|---|
| 47 |
* * peer_method: The peer method to use to fetch objects |
|---|
| 48 |
* |
|---|
| 49 |
* @see sfWidgetFormSelect |
|---|
| 50 |
*/ |
|---|
| 51 |
protected function configure($options = array(), $attributes = array()) |
|---|
| 52 |
{ |
|---|
| 53 |
$this->addRequiredOption('model'); |
|---|
| 54 |
$this->addOption('add_empty', false); |
|---|
| 55 |
$this->addOption('method', '__toString'); |
|---|
| 56 |
$this->addOption('key_method', 'getPrimaryKey'); |
|---|
| 57 |
$this->addOption('order_by', null); |
|---|
| 58 |
$this->addOption('criteria', null); |
|---|
| 59 |
$this->addOption('connection', null); |
|---|
| 60 |
$this->addOption('multiple', false); |
|---|
| 61 |
$this->addOption('peer_method', 'doSelect'); |
|---|
| 62 |
|
|---|
| 63 |
parent::configure($options, $attributes); |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
* Returns the choices associated to the model. |
|---|
| 68 |
* |
|---|
| 69 |
* @return array An array of choices |
|---|
| 70 |
*/ |
|---|
| 71 |
public function getChoices() |
|---|
| 72 |
{ |
|---|
| 73 |
$choices = array(); |
|---|
| 74 |
if (false !== $this->getOption('add_empty')) |
|---|
| 75 |
{ |
|---|
| 76 |
$choices[''] = true === $this->getOption('add_empty') ? '' : $this->translate($this->getOption('add_empty')); |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
$class = constant($this->getOption('model').'::PEER'); |
|---|
| 80 |
|
|---|
| 81 |
$criteria = null === $this->getOption('criteria') ? new Criteria() : clone $this->getOption('criteria'); |
|---|
| 82 |
if ($order = $this->getOption('order_by')) |
|---|
| 83 |
{ |
|---|
| 84 |
$method = sprintf('add%sOrderByColumn', 0 === strpos(strtoupper($order[1]), 'ASC') ? 'Ascending' : 'Descending'); |
|---|
| 85 |
$criteria->$method(call_user_func(array($class, 'translateFieldName'), $order[0], BasePeer::TYPE_PHPNAME, BasePeer::TYPE_COLNAME)); |
|---|
| 86 |
} |
|---|
| 87 |
$objects = call_user_func(array($class, $this->getOption('peer_method')), $criteria, $this->getOption('connection')); |
|---|
| 88 |
|
|---|
| 89 |
$methodKey = $this->getOption('key_method'); |
|---|
| 90 |
if (!method_exists($this->getOption('model'), $methodKey)) |
|---|
| 91 |
{ |
|---|
| 92 |
throw new RuntimeException(sprintf('Class "%s" must implement a "%s" method to be rendered in a "%s" widget', $this->getOption('model'), $methodKey, __CLASS__)); |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
$methodValue = $this->getOption('method'); |
|---|
| 96 |
if (!method_exists($this->getOption('model'), $methodValue)) |
|---|
| 97 |
{ |
|---|
| 98 |
throw new RuntimeException(sprintf('Class "%s" must implement a "%s" method to be rendered in a "%s" widget', $this->getOption('model'), $methodValue, __CLASS__)); |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
foreach ($objects as $object) |
|---|
| 102 |
{ |
|---|
| 103 |
$choices[$object->$methodKey()] = $object->$methodValue(); |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
return $choices; |
|---|
| 107 |
} |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|